| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 17 | #define LOG_TAG "GraphicBufferMapper" | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 20 |  | 
| Mathias Agopian | fe2f54f | 2017-02-15 19:48:58 -0800 | [diff] [blame] | 21 | #include <ui/GraphicBufferMapper.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 |  | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 23 | // We would eliminate the non-conforming zero-length array, but we can't since | 
|  | 24 | // this is effectively included from the Linux kernel | 
|  | 25 | #pragma clang diagnostic push | 
|  | 26 | #pragma clang diagnostic ignored "-Wzero-length-array" | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 27 | #include <sync/sync.h> | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 28 | #pragma clang diagnostic pop | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 29 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | #include <utils/Log.h> | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 31 | #include <utils/Trace.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 33 | #include <ui/GrallocMapper.h> | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 34 | #include <ui/GraphicBuffer.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 36 | #include <system/graphics.h> | 
| Mathias Agopian | 8b765b7 | 2009-04-10 20:34:46 -0700 | [diff] [blame] | 37 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 38 | namespace android { | 
|  | 39 | // --------------------------------------------------------------------------- | 
|  | 40 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 41 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper ) | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 42 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 43 | GraphicBufferMapper::GraphicBufferMapper() | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 44 | : mMapper(std::make_unique<const Gralloc2::Mapper>()) | 
|  | 45 | { | 
|  | 46 | if (!mMapper->valid()) { | 
|  | 47 | mLoader = std::make_unique<Gralloc1::Loader>(); | 
|  | 48 | mDevice = mLoader->getDevice(); | 
|  | 49 | } | 
|  | 50 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 51 |  | 
|  | 52 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 53 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 54 | status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 55 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 56 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 57 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 58 | gralloc1_error_t error; | 
|  | 59 | if (mMapper->valid()) { | 
|  | 60 | error = static_cast<gralloc1_error_t>(mMapper->retain(handle)); | 
|  | 61 | } else { | 
| Chia-I Wu | 7bd8adb | 2017-02-10 14:53:12 -0800 | [diff] [blame] | 62 | // This always returns GRALLOC1_BAD_HANDLE when handle is from a | 
|  | 63 | // remote process and mDevice is backed by Gralloc1On0Adapter. | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 64 | error = mDevice->retain(handle); | 
| Chia-I Wu | 7bd8adb | 2017-02-10 14:53:12 -0800 | [diff] [blame] | 65 | if (error == GRALLOC1_ERROR_BAD_HANDLE && | 
|  | 66 | mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) { | 
|  | 67 | ALOGE("registerBuffer by handle is not supported with " | 
|  | 68 | "Gralloc1On0Adapter"); | 
|  | 69 | } | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 72 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d", | 
|  | 73 | handle, error); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 74 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 75 | return error; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | status_t GraphicBufferMapper::registerBuffer(const GraphicBuffer* buffer) | 
|  | 79 | { | 
|  | 80 | ATRACE_CALL(); | 
|  | 81 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 82 | gralloc1_error_t error; | 
|  | 83 | if (mMapper->valid()) { | 
|  | 84 | error = static_cast<gralloc1_error_t>( | 
|  | 85 | mMapper->retain(buffer->getNativeBuffer()->handle)); | 
|  | 86 | } else { | 
|  | 87 | error = mDevice->retain(buffer); | 
|  | 88 | } | 
|  | 89 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 90 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d", | 
|  | 91 | buffer->getNativeBuffer()->handle, error); | 
|  | 92 |  | 
|  | 93 | return error; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 96 | status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 97 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 98 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 99 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 100 | gralloc1_error_t error; | 
|  | 101 | if (mMapper->valid()) { | 
|  | 102 | mMapper->release(handle); | 
|  | 103 | error = GRALLOC1_ERROR_NONE; | 
|  | 104 | } else { | 
|  | 105 | error = mDevice->release(handle); | 
|  | 106 | } | 
|  | 107 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 108 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "unregisterBuffer(%p): failed %d", | 
|  | 109 | handle, error); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 110 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 111 | return error; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 114 | static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) { | 
|  | 115 | gralloc1_rect_t outRect{}; | 
|  | 116 | outRect.left = rect.left; | 
|  | 117 | outRect.top = rect.top; | 
|  | 118 | outRect.width = rect.width(); | 
|  | 119 | outRect.height = rect.height(); | 
|  | 120 | return outRect; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | status_t GraphicBufferMapper::getDimensions(buffer_handle_t handle, | 
|  | 125 | uint32_t* outWidth, uint32_t* outHeight) const | 
|  | 126 | { | 
|  | 127 | ATRACE_CALL(); | 
|  | 128 |  | 
|  | 129 | gralloc1_error_t error; | 
|  | 130 | if (mMapper->valid()) { | 
|  | 131 | mMapper->getDimensions(handle, outWidth, outHeight); | 
|  | 132 | error = GRALLOC1_ERROR_NONE; | 
|  | 133 | } else { | 
|  | 134 | error = mDevice->getDimensions(handle, outWidth, outHeight); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getDimensions(%p, ...): failed %d", | 
|  | 138 | handle, error); | 
|  | 139 |  | 
|  | 140 | return error; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | status_t GraphicBufferMapper::getFormat(buffer_handle_t handle, | 
|  | 144 | int32_t* outFormat) const | 
|  | 145 | { | 
|  | 146 | ATRACE_CALL(); | 
|  | 147 |  | 
|  | 148 | gralloc1_error_t error; | 
|  | 149 | if (mMapper->valid()) { | 
|  | 150 | mMapper->getFormat(handle, outFormat); | 
|  | 151 | error = GRALLOC1_ERROR_NONE; | 
|  | 152 | } else { | 
|  | 153 | error = mDevice->getFormat(handle, outFormat); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getFormat(%p, ...): failed %d", | 
|  | 157 | handle, error); | 
|  | 158 |  | 
|  | 159 | return error; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | status_t GraphicBufferMapper::getLayerCount(buffer_handle_t handle, | 
|  | 163 | uint32_t* outLayerCount) const | 
|  | 164 | { | 
|  | 165 | ATRACE_CALL(); | 
|  | 166 |  | 
|  | 167 | gralloc1_error_t error; | 
|  | 168 | if (mMapper->valid()) { | 
|  | 169 | mMapper->getLayerCount(handle, outLayerCount); | 
|  | 170 | error = GRALLOC1_ERROR_NONE; | 
|  | 171 | } else { | 
|  | 172 | error = mDevice->getLayerCount(handle, outLayerCount); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getLayerCount(%p, ...): failed %d", | 
|  | 176 | handle, error); | 
|  | 177 |  | 
|  | 178 | return error; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | status_t GraphicBufferMapper::getProducerUsage(buffer_handle_t handle, | 
|  | 182 | uint64_t* outProducerUsage) const | 
|  | 183 | { | 
|  | 184 | ATRACE_CALL(); | 
|  | 185 |  | 
|  | 186 | gralloc1_error_t error; | 
|  | 187 | if (mMapper->valid()) { | 
|  | 188 | mMapper->getProducerUsage(handle, outProducerUsage); | 
|  | 189 | error = GRALLOC1_ERROR_NONE; | 
|  | 190 | } else { | 
|  | 191 | error = mDevice->getProducerUsage(handle, outProducerUsage); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, | 
|  | 195 | "getProducerUsage(%p, ...): failed %d", handle, error); | 
|  | 196 |  | 
|  | 197 | return error; | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | status_t GraphicBufferMapper::getConsumerUsage(buffer_handle_t handle, | 
|  | 201 | uint64_t* outConsumerUsage) const | 
|  | 202 | { | 
|  | 203 | ATRACE_CALL(); | 
|  | 204 |  | 
|  | 205 | gralloc1_error_t error; | 
|  | 206 | if (mMapper->valid()) { | 
|  | 207 | mMapper->getConsumerUsage(handle, outConsumerUsage); | 
|  | 208 | error = GRALLOC1_ERROR_NONE; | 
|  | 209 | } else { | 
|  | 210 | error = mDevice->getConsumerUsage(handle, outConsumerUsage); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, | 
|  | 214 | "getConsumerUsage(%p, ...): failed %d", handle, error); | 
|  | 215 |  | 
|  | 216 | return error; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | status_t GraphicBufferMapper::getBackingStore(buffer_handle_t handle, | 
|  | 220 | uint64_t* outBackingStore) const | 
|  | 221 | { | 
|  | 222 | ATRACE_CALL(); | 
|  | 223 |  | 
|  | 224 | gralloc1_error_t error; | 
|  | 225 | if (mMapper->valid()) { | 
|  | 226 | mMapper->getBackingStore(handle, outBackingStore); | 
|  | 227 | error = GRALLOC1_ERROR_NONE; | 
|  | 228 | } else { | 
|  | 229 | error = mDevice->getBackingStore(handle, outBackingStore); | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, | 
|  | 233 | "getBackingStore(%p, ...): failed %d", handle, error); | 
|  | 234 |  | 
|  | 235 | return error; | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | status_t GraphicBufferMapper::getStride(buffer_handle_t handle, | 
|  | 239 | uint32_t* outStride) const | 
|  | 240 | { | 
|  | 241 | ATRACE_CALL(); | 
|  | 242 |  | 
|  | 243 | gralloc1_error_t error; | 
|  | 244 | if (mMapper->valid()) { | 
|  | 245 | mMapper->getStride(handle, outStride); | 
|  | 246 | error = GRALLOC1_ERROR_NONE; | 
|  | 247 | } else { | 
|  | 248 | error = mDevice->getStride(handle, outStride); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getStride(%p, ...): failed %d", | 
|  | 252 | handle, error); | 
|  | 253 |  | 
|  | 254 | return error; | 
|  | 255 | } | 
|  | 256 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 257 | status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, | 
|  | 258 | const Rect& bounds, void** vaddr) | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 259 | { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 260 | return lockAsync(handle, usage, bounds, vaddr, -1); | 
|  | 261 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 262 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 263 | status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, | 
|  | 264 | const Rect& bounds, android_ycbcr *ycbcr) | 
|  | 265 | { | 
|  | 266 | return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 269 | status_t GraphicBufferMapper::unlock(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 270 | { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 271 | int32_t fenceFd = -1; | 
|  | 272 | status_t error = unlockAsync(handle, &fenceFd); | 
|  | 273 | if (error == NO_ERROR) { | 
|  | 274 | sync_wait(fenceFd, -1); | 
|  | 275 | close(fenceFd); | 
|  | 276 | } | 
|  | 277 | return error; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 278 | } | 
|  | 279 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 280 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 281 | uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 282 | { | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 283 | return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd); | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, | 
|  | 287 | uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds, | 
|  | 288 | void** vaddr, int fenceFd) | 
|  | 289 | { | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 290 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 291 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 292 | gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 293 | gralloc1_error_t error; | 
|  | 294 | if (mMapper->valid()) { | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 295 | const Gralloc2::IMapper::Rect& accessRect = | 
|  | 296 | *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 297 | error = static_cast<gralloc1_error_t>(mMapper->lock( | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 298 | handle, producerUsage, consumerUsage, accessRect, | 
|  | 299 | fenceFd, vaddr)); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 300 | } else { | 
|  | 301 | sp<Fence> fence = new Fence(fenceFd); | 
|  | 302 | error = mDevice->lock(handle, | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 303 | static_cast<gralloc1_producer_usage_t>(producerUsage), | 
|  | 304 | static_cast<gralloc1_consumer_usage_t>(consumerUsage), | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 305 | &accessRegion, vaddr, fence); | 
|  | 306 | } | 
|  | 307 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 308 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle, | 
|  | 309 | error); | 
|  | 310 |  | 
|  | 311 | return error; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) { | 
|  | 315 | if (plane.bits_per_component != 8) { | 
|  | 316 | ALOGV("Invalid number of bits per component: %d", | 
|  | 317 | plane.bits_per_component); | 
|  | 318 | return false; | 
|  | 319 | } | 
|  | 320 | if (plane.bits_used != 8) { | 
|  | 321 | ALOGV("Invalid number of bits used: %d", plane.bits_used); | 
|  | 322 | return false; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 325 | bool hasValidIncrement = plane.h_increment == 1 || | 
|  | 326 | (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2); | 
|  | 327 | hasValidIncrement = hasValidIncrement && plane.v_increment > 0; | 
|  | 328 | if (!hasValidIncrement) { | 
|  | 329 | ALOGV("Invalid increment: h %d v %d", plane.h_increment, | 
|  | 330 | plane.v_increment); | 
|  | 331 | return false; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | return true; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
|  | 337 | status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 338 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 339 | { | 
|  | 340 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 341 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 342 | gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 343 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 344 | std::vector<android_flex_plane_t> planes; | 
|  | 345 | android_flex_layout_t flexLayout{}; | 
|  | 346 | gralloc1_error_t error; | 
|  | 347 |  | 
|  | 348 | if (mMapper->valid()) { | 
|  | 349 | const Gralloc2::IMapper::Rect& accessRect = | 
|  | 350 | *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion); | 
|  | 351 | Gralloc2::FlexLayout layout{}; | 
|  | 352 | error = static_cast<gralloc1_error_t>(mMapper->lock( | 
|  | 353 | handle, usage, usage, accessRect, fenceFd, &layout)); | 
|  | 354 |  | 
|  | 355 | if (error == GRALLOC1_ERROR_NONE) { | 
|  | 356 | planes.resize(layout.planes.size()); | 
|  | 357 | memcpy(planes.data(), layout.planes.data(), | 
|  | 358 | sizeof(planes[0]) * planes.size()); | 
|  | 359 |  | 
|  | 360 | flexLayout.format = static_cast<android_flex_format_t>( | 
|  | 361 | layout.format); | 
|  | 362 | flexLayout.num_planes = static_cast<uint32_t>(planes.size()); | 
|  | 363 | flexLayout.planes = planes.data(); | 
|  | 364 | } | 
|  | 365 | } else { | 
|  | 366 | sp<Fence> fence = new Fence(fenceFd); | 
|  | 367 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 368 | if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) { | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 369 | error = mDevice->lockYCbCr(handle, | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 370 | static_cast<gralloc1_producer_usage_t>(usage), | 
|  | 371 | static_cast<gralloc1_consumer_usage_t>(usage), | 
|  | 372 | &accessRegion, ycbcr, fence); | 
|  | 373 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, | 
|  | 374 | "lockYCbCr(%p, ...) failed: %d", handle, error); | 
|  | 375 | return error; | 
|  | 376 | } | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 377 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 378 | uint32_t numPlanes = 0; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 379 | error = mDevice->getNumFlexPlanes(handle, &numPlanes); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 380 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 381 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 382 | ALOGV("Failed to retrieve number of flex planes: %d", error); | 
|  | 383 | return error; | 
|  | 384 | } | 
|  | 385 | if (numPlanes < 3) { | 
|  | 386 | ALOGV("Not enough planes for YCbCr (%u found)", numPlanes); | 
|  | 387 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 388 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 389 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 390 | planes.resize(numPlanes); | 
|  | 391 | flexLayout.num_planes = numPlanes; | 
|  | 392 | flexLayout.planes = planes.data(); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 393 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 394 | error = mDevice->lockFlex(handle, | 
|  | 395 | static_cast<gralloc1_producer_usage_t>(usage), | 
|  | 396 | static_cast<gralloc1_consumer_usage_t>(usage), | 
|  | 397 | &accessRegion, &flexLayout, fence); | 
|  | 398 | } | 
|  | 399 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 400 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 401 | ALOGW("lockFlex(%p, ...) failed: %d", handle, error); | 
|  | 402 | return error; | 
|  | 403 | } | 
|  | 404 | if (flexLayout.format != FLEX_FORMAT_YCbCr) { | 
|  | 405 | ALOGV("Unable to convert flex-format buffer to YCbCr"); | 
|  | 406 | unlock(handle); | 
|  | 407 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | // Find planes | 
|  | 411 | auto yPlane = planes.cend(); | 
|  | 412 | auto cbPlane = planes.cend(); | 
|  | 413 | auto crPlane = planes.cend(); | 
|  | 414 | for (auto planeIter = planes.cbegin(); planeIter != planes.cend(); | 
|  | 415 | ++planeIter) { | 
|  | 416 | if (planeIter->component == FLEX_COMPONENT_Y) { | 
|  | 417 | yPlane = planeIter; | 
|  | 418 | } else if (planeIter->component == FLEX_COMPONENT_Cb) { | 
|  | 419 | cbPlane = planeIter; | 
|  | 420 | } else if (planeIter->component == FLEX_COMPONENT_Cr) { | 
|  | 421 | crPlane = planeIter; | 
|  | 422 | } | 
|  | 423 | } | 
|  | 424 | if (yPlane == planes.cend()) { | 
|  | 425 | ALOGV("Unable to find Y plane"); | 
|  | 426 | unlock(handle); | 
|  | 427 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 428 | } | 
|  | 429 | if (cbPlane == planes.cend()) { | 
|  | 430 | ALOGV("Unable to find Cb plane"); | 
|  | 431 | unlock(handle); | 
|  | 432 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 433 | } | 
|  | 434 | if (crPlane == planes.cend()) { | 
|  | 435 | ALOGV("Unable to find Cr plane"); | 
|  | 436 | unlock(handle); | 
|  | 437 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | // Validate planes | 
|  | 441 | if (!isValidYCbCrPlane(*yPlane)) { | 
|  | 442 | ALOGV("Y plane is invalid"); | 
|  | 443 | unlock(handle); | 
|  | 444 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 445 | } | 
|  | 446 | if (!isValidYCbCrPlane(*cbPlane)) { | 
|  | 447 | ALOGV("Cb plane is invalid"); | 
|  | 448 | unlock(handle); | 
|  | 449 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 450 | } | 
|  | 451 | if (!isValidYCbCrPlane(*crPlane)) { | 
|  | 452 | ALOGV("Cr plane is invalid"); | 
|  | 453 | unlock(handle); | 
|  | 454 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 455 | } | 
|  | 456 | if (cbPlane->v_increment != crPlane->v_increment) { | 
|  | 457 | ALOGV("Cb and Cr planes have different step (%d vs. %d)", | 
|  | 458 | cbPlane->v_increment, crPlane->v_increment); | 
|  | 459 | unlock(handle); | 
|  | 460 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 461 | } | 
|  | 462 | if (cbPlane->h_increment != crPlane->h_increment) { | 
|  | 463 | ALOGV("Cb and Cr planes have different stride (%d vs. %d)", | 
|  | 464 | cbPlane->h_increment, crPlane->h_increment); | 
|  | 465 | unlock(handle); | 
|  | 466 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | // Pack plane data into android_ycbcr struct | 
|  | 470 | ycbcr->y = yPlane->top_left; | 
|  | 471 | ycbcr->cb = cbPlane->top_left; | 
|  | 472 | ycbcr->cr = crPlane->top_left; | 
|  | 473 | ycbcr->ystride = static_cast<size_t>(yPlane->v_increment); | 
|  | 474 | ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment); | 
|  | 475 | ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment); | 
|  | 476 |  | 
|  | 477 | return error; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 478 | } | 
|  | 479 |  | 
|  | 480 | status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) | 
|  | 481 | { | 
|  | 482 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 483 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 484 | gralloc1_error_t error; | 
|  | 485 | if (mMapper->valid()) { | 
|  | 486 | *fenceFd = mMapper->unlock(handle); | 
|  | 487 | error = GRALLOC1_ERROR_NONE; | 
|  | 488 | } else { | 
|  | 489 | sp<Fence> fence = Fence::NO_FENCE; | 
|  | 490 | error = mDevice->unlock(handle, &fence); | 
|  | 491 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 492 | ALOGE("unlock(%p) failed: %d", handle, error); | 
|  | 493 | return error; | 
|  | 494 | } | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 495 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 496 | *fenceFd = fence->dup(); | 
|  | 497 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 498 | return error; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 499 | } | 
|  | 500 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 501 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 502 | }; // namespace android |