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