Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #include <private/dvr/ion_buffer.h> |
| 2 | |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame] | 3 | #include <log/log.h> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 4 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 5 | #include <utils/Trace.h> |
| 6 | |
| 7 | #include <mutex> |
| 8 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 9 | namespace { |
| 10 | |
| 11 | constexpr uint32_t kDefaultGraphicBufferLayerCount = 1; |
| 12 | |
| 13 | } // anonymous namespace |
| 14 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 15 | namespace android { |
| 16 | namespace dvr { |
| 17 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 18 | IonBuffer::IonBuffer() : IonBuffer(nullptr, 0, 0, 0, 0, 0, 0, 0) {} |
| 19 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 20 | IonBuffer::IonBuffer(uint32_t width, uint32_t height, uint32_t format, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 21 | uint64_t usage) |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 22 | : IonBuffer() { |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 23 | Alloc(width, height, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 26 | IonBuffer::IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 27 | uint32_t stride, uint32_t format, uint64_t usage) |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 28 | : IonBuffer(handle, width, height, 1, stride, 0, format, usage) {} |
| 29 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 30 | IonBuffer::IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height, |
| 31 | uint32_t layer_count, uint32_t stride, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 32 | uint32_t layer_stride, uint32_t format, uint64_t usage) |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 33 | : buffer_(nullptr) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 34 | ALOGD_IF(TRACE, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 35 | "IonBuffer::IonBuffer: handle=%p width=%u height=%u layer_count=%u " |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 36 | "stride=%u layer stride=%u format=%u usage=%" PRIx64, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 37 | handle, width, height, layer_count, stride, layer_stride, format, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 38 | usage); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 39 | if (handle != 0) { |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 40 | Import(handle, width, height, stride, format, usage); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 41 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | IonBuffer::~IonBuffer() { |
| 45 | ALOGD_IF(TRACE, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 46 | "IonBuffer::~IonBuffer: handle=%p width=%u height=%u stride=%u " |
| 47 | "format=%u usage=%x", |
| 48 | handle(), width(), height(), stride(), format(), usage()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 49 | FreeHandle(); |
| 50 | } |
| 51 | |
| 52 | IonBuffer::IonBuffer(IonBuffer&& other) : IonBuffer() { |
| 53 | *this = std::move(other); |
| 54 | } |
| 55 | |
| 56 | IonBuffer& IonBuffer::operator=(IonBuffer&& other) { |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 57 | ALOGD_IF(TRACE, "IonBuffer::operator=: handle_=%p other.handle_=%p", handle(), |
| 58 | other.handle()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 59 | |
| 60 | if (this != &other) { |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 61 | buffer_ = other.buffer_; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 62 | other.FreeHandle(); |
| 63 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | void IonBuffer::FreeHandle() { |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 68 | if (buffer_.get()) { |
| 69 | // GraphicBuffer unregisters and cleans up the handle if needed |
| 70 | buffer_ = nullptr; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 71 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 74 | int IonBuffer::Alloc(uint32_t width, uint32_t height, uint32_t format, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 75 | uint64_t usage) { |
| 76 | ALOGD_IF(TRACE, |
| 77 | "IonBuffer::Alloc: width=%u height=%u format=%u usage=%" PRIx64, |
| 78 | width, height, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 79 | |
Chris Forbes | 73feb06 | 2017-04-19 17:16:48 -0700 | [diff] [blame] | 80 | sp<GraphicBuffer> buffer = new GraphicBuffer( |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 81 | width, height, format, kDefaultGraphicBufferLayerCount, usage); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 82 | if (buffer->initCheck() != OK) { |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 83 | ALOGE("IonBuffer::Aloc: Failed to allocate buffer"); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 84 | return -EINVAL; |
| 85 | } else { |
| 86 | buffer_ = buffer; |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 87 | return 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 88 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 91 | void IonBuffer::Reset(buffer_handle_t handle, uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 92 | uint32_t stride, uint32_t format, uint64_t usage) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 93 | ALOGD_IF(TRACE, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 94 | "IonBuffer::Reset: handle=%p width=%u height=%u stride=%u format=%u " |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 95 | "usage=%" PRIx64, |
| 96 | handle, width, height, stride, format, usage); |
| 97 | Import(handle, width, height, stride, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 100 | int IonBuffer::Import(buffer_handle_t handle, uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 101 | uint32_t stride, uint32_t format, uint64_t usage) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 102 | ATRACE_NAME("IonBuffer::Import1"); |
| 103 | ALOGD_IF( |
| 104 | TRACE, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 105 | "IonBuffer::Import: handle=%p width=%u height=%u stride=%u format=%u " |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 106 | "usage=%" PRIx64, |
| 107 | handle, width, height, stride, format, usage); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 108 | FreeHandle(); |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 109 | sp<GraphicBuffer> buffer = new GraphicBuffer( |
| 110 | handle, GraphicBuffer::TAKE_UNREGISTERED_HANDLE, width, height, format, |
| 111 | kDefaultGraphicBufferLayerCount, usage, stride); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 112 | if (buffer->initCheck() != OK) { |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 113 | ALOGE("IonBuffer::Import: Failed to import buffer"); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 114 | return -EINVAL; |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 115 | } else { |
| 116 | buffer_ = buffer; |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 117 | return 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 118 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int IonBuffer::Import(const int* fd_array, int fd_count, const int* int_array, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 122 | int int_count, uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 123 | uint32_t stride, uint32_t format, uint64_t usage) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 124 | ATRACE_NAME("IonBuffer::Import2"); |
| 125 | ALOGD_IF(TRACE, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 126 | "IonBuffer::Import: fd_count=%d int_count=%d width=%u height=%u " |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 127 | "stride=%u format=%u usage=%" PRIx64, |
| 128 | fd_count, int_count, width, height, stride, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 129 | |
| 130 | if (fd_count < 0 || int_count < 0) { |
| 131 | ALOGE("IonBuffer::Import: invalid arguments."); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | native_handle_t* handle = native_handle_create(fd_count, int_count); |
| 136 | if (!handle) { |
| 137 | ALOGE("IonBuffer::Import: failed to create new native handle."); |
| 138 | return -ENOMEM; |
| 139 | } |
| 140 | |
| 141 | // Copy fd_array into the first part of handle->data and int_array right |
| 142 | // after it. |
| 143 | memcpy(handle->data, fd_array, sizeof(int) * fd_count); |
| 144 | memcpy(handle->data + fd_count, int_array, sizeof(int) * int_count); |
| 145 | |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 146 | const int ret = Import(handle, width, height, stride, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 147 | if (ret < 0) { |
| 148 | ALOGE("IonBuffer::Import: failed to import raw native handle: %s", |
| 149 | strerror(-ret)); |
| 150 | native_handle_close(handle); |
| 151 | native_handle_delete(handle); |
| 152 | } |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | int IonBuffer::Duplicate(const IonBuffer* other) { |
| 158 | if (!other->handle()) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | const int fd_count = other->handle()->numFds; |
| 162 | const int int_count = other->handle()->numInts; |
| 163 | |
| 164 | if (fd_count < 0 || int_count < 0) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | native_handle_t* handle = native_handle_create(fd_count, int_count); |
| 168 | if (!handle) { |
| 169 | ALOGE("IonBuffer::Duplicate: Failed to create new native handle."); |
| 170 | return -ENOMEM; |
| 171 | } |
| 172 | |
| 173 | // Duplicate the file descriptors from the other native handle. |
| 174 | for (int i = 0; i < fd_count; i++) |
| 175 | handle->data[i] = dup(other->handle()->data[i]); |
| 176 | |
| 177 | // Copy the ints after the file descriptors. |
| 178 | memcpy(handle->data + fd_count, other->handle()->data + fd_count, |
| 179 | sizeof(int) * int_count); |
| 180 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 181 | const int ret = |
| 182 | Import(handle, other->width(), other->height(), other->stride(), |
Jiwen 'Steve' Cai | 044963e | 2017-05-01 22:43:21 -0700 | [diff] [blame^] | 183 | other->format(), other->usage()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 184 | if (ret < 0) { |
| 185 | ALOGE("IonBuffer::Duplicate: Failed to import duplicate native handle: %s", |
| 186 | strerror(-ret)); |
| 187 | native_handle_close(handle); |
| 188 | native_handle_delete(handle); |
| 189 | } |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 194 | int IonBuffer::Lock(uint32_t usage, int x, int y, int width, int height, |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 195 | void** address) { |
| 196 | ATRACE_NAME("IonBuffer::Lock"); |
| 197 | ALOGD_IF(TRACE, |
| 198 | "IonBuffer::Lock: handle=%p usage=%d x=%d y=%d width=%d height=%d " |
| 199 | "address=%p", |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 200 | handle(), usage, x, y, width, height, address); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 201 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 202 | status_t err = |
| 203 | buffer_->lock(usage, Rect(x, y, x + width, y + height), address); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 204 | if (err != NO_ERROR) |
| 205 | return -EINVAL; |
| 206 | else |
| 207 | return 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 210 | int IonBuffer::LockYUV(uint32_t usage, int x, int y, int width, int height, |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 211 | struct android_ycbcr* yuv) { |
| 212 | ATRACE_NAME("IonBuffer::LockYUV"); |
| 213 | ALOGD_IF(TRACE, |
| 214 | "IonBuffer::Lock: handle=%p usage=%d x=%d y=%d width=%d height=%d", |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 215 | handle(), usage, x, y, width, height); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 216 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 217 | status_t err = |
| 218 | buffer_->lockYCbCr(usage, Rect(x, y, x + width, y + height), yuv); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 219 | if (err != NO_ERROR) |
| 220 | return -EINVAL; |
| 221 | else |
| 222 | return 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int IonBuffer::Unlock() { |
| 226 | ATRACE_NAME("IonBuffer::Unlock"); |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 227 | ALOGD_IF(TRACE, "IonBuffer::Unlock: handle=%p", handle()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 228 | |
Mark Urbanus | 8a71b13 | 2017-03-16 11:06:51 -0700 | [diff] [blame] | 229 | status_t err = buffer_->unlock(); |
| 230 | if (err != NO_ERROR) |
| 231 | return -EINVAL; |
| 232 | else |
| 233 | return 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 234 | } |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 235 | } // namespace dvr |
| 236 | } // namespace android |