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