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 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 23 | #include <grallocusage/GrallocUsageConversion.h> |
| 24 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 25 | // We would eliminate the non-conforming zero-length array, but we can't since |
| 26 | // this is effectively included from the Linux kernel |
| 27 | #pragma clang diagnostic push |
| 28 | #pragma clang diagnostic ignored "-Wzero-length-array" |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 29 | #include <sync/sync.h> |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 30 | #pragma clang diagnostic pop |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 31 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <utils/Log.h> |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 34 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 35 | #include <ui/Gralloc2.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 36 | #include <ui/GraphicBuffer.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 38 | #include <system/graphics.h> |
Mathias Agopian | 8b765b7 | 2009-04-10 20:34:46 -0700 | [diff] [blame] | 39 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | // --------------------------------------------------------------------------- |
| 42 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 43 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper ) |
Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 44 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 45 | GraphicBufferMapper::GraphicBufferMapper() |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 46 | : mMapper(std::make_unique<const Gralloc2::Mapper>()) |
| 47 | { |
| 48 | if (!mMapper->valid()) { |
| 49 | mLoader = std::make_unique<Gralloc1::Loader>(); |
| 50 | mDevice = mLoader->getDevice(); |
| 51 | } |
| 52 | } |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 53 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 54 | status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle, |
| 55 | buffer_handle_t* outHandle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 56 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 57 | ATRACE_CALL(); |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 58 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 59 | Gralloc2::Error error; |
| 60 | if (mMapper->valid()) { |
| 61 | error = mMapper->importBuffer(hardware::hidl_handle(rawHandle), |
| 62 | outHandle); |
| 63 | } else { |
| 64 | error = Gralloc2::Error::UNSUPPORTED; |
| 65 | } |
| 66 | |
| 67 | ALOGW_IF(error != Gralloc2::Error::NONE, "importBuffer(%p) failed: %d", |
| 68 | rawHandle, error); |
| 69 | |
| 70 | return static_cast<status_t>(error); |
| 71 | } |
| 72 | |
| 73 | status_t GraphicBufferMapper::importBuffer(const GraphicBuffer* buffer) |
| 74 | { |
| 75 | ATRACE_CALL(); |
| 76 | |
| 77 | ANativeWindowBuffer* nativeBuffer = buffer->getNativeBuffer(); |
| 78 | buffer_handle_t rawHandle = nativeBuffer->handle; |
| 79 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 80 | gralloc1_error_t error; |
| 81 | if (mMapper->valid()) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 82 | buffer_handle_t importedHandle; |
| 83 | error = static_cast<gralloc1_error_t>(mMapper->importBuffer( |
| 84 | hardware::hidl_handle(rawHandle), &importedHandle)); |
| 85 | if (error == GRALLOC1_ERROR_NONE) { |
| 86 | nativeBuffer->handle = importedHandle; |
| 87 | } |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 88 | } else { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 89 | native_handle_t* clonedHandle = native_handle_clone(rawHandle); |
| 90 | if (clonedHandle) { |
| 91 | nativeBuffer->handle = clonedHandle; |
| 92 | error = mDevice->retain(buffer); |
| 93 | if (error != GRALLOC1_ERROR_NONE) { |
| 94 | nativeBuffer->handle = rawHandle; |
| 95 | native_handle_close(clonedHandle); |
| 96 | native_handle_delete(clonedHandle); |
| 97 | } |
| 98 | } else { |
| 99 | error = GRALLOC1_ERROR_NO_RESOURCES; |
Chia-I Wu | 7bd8adb | 2017-02-10 14:53:12 -0800 | [diff] [blame] | 100 | } |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 101 | } |
| 102 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 103 | // the raw handle is owned by GraphicBuffer and is now replaced |
| 104 | if (error == GRALLOC1_ERROR_NONE) { |
| 105 | native_handle_close(rawHandle); |
| 106 | native_handle_delete(const_cast<native_handle_t*>(rawHandle)); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 107 | } |
| 108 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 109 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "importBuffer(%p) failed: %d", |
| 110 | rawHandle, error); |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 111 | |
| 112 | return error; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 115 | status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 117 | ATRACE_CALL(); |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 118 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 119 | gralloc1_error_t error; |
| 120 | if (mMapper->valid()) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 121 | mMapper->freeBuffer(handle); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 122 | error = GRALLOC1_ERROR_NONE; |
| 123 | } else { |
| 124 | error = mDevice->release(handle); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 125 | native_handle_close(handle); |
| 126 | native_handle_delete(const_cast<native_handle_t*>(handle)); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 129 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "freeBuffer(%p): failed %d", |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 130 | handle, error); |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 131 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 132 | return error; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 135 | static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) { |
| 136 | gralloc1_rect_t outRect{}; |
| 137 | outRect.left = rect.left; |
| 138 | outRect.top = rect.top; |
| 139 | outRect.width = rect.width(); |
| 140 | outRect.height = rect.height(); |
| 141 | return outRect; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 144 | static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) { |
| 145 | Gralloc2::IMapper::Rect outRect{}; |
| 146 | outRect.left = rect.left; |
| 147 | outRect.top = rect.top; |
| 148 | outRect.width = rect.width(); |
| 149 | outRect.height = rect.height(); |
| 150 | return outRect; |
Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 153 | status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, |
| 154 | const Rect& bounds, void** vaddr) |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 155 | { |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 156 | return lockAsync(handle, usage, bounds, vaddr, -1); |
| 157 | } |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 158 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 159 | status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, |
| 160 | const Rect& bounds, android_ycbcr *ycbcr) |
| 161 | { |
| 162 | return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 165 | status_t GraphicBufferMapper::unlock(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 166 | { |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 167 | int32_t fenceFd = -1; |
| 168 | status_t error = unlockAsync(handle, &fenceFd); |
| 169 | if (error == NO_ERROR) { |
| 170 | sync_wait(fenceFd, -1); |
| 171 | close(fenceFd); |
| 172 | } |
| 173 | return error; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 176 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 177 | uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 178 | { |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 179 | return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd); |
| 180 | } |
| 181 | |
| 182 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, |
| 183 | uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds, |
| 184 | void** vaddr, int fenceFd) |
| 185 | { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 186 | ATRACE_CALL(); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 187 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 188 | gralloc1_error_t error; |
| 189 | if (mMapper->valid()) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 190 | const uint64_t usage = |
| 191 | static_cast<uint64_t>(android_convertGralloc1To0Usage( |
| 192 | producerUsage, consumerUsage)); |
| 193 | error = static_cast<gralloc1_error_t>(mMapper->lock(handle, |
| 194 | usage, asGralloc2Rect(bounds), fenceFd, vaddr)); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 195 | } else { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 196 | gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 197 | sp<Fence> fence = new Fence(fenceFd); |
| 198 | error = mDevice->lock(handle, |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 199 | static_cast<gralloc1_producer_usage_t>(producerUsage), |
| 200 | static_cast<gralloc1_consumer_usage_t>(consumerUsage), |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 201 | &accessRegion, vaddr, fence); |
| 202 | } |
| 203 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 204 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle, |
| 205 | error); |
| 206 | |
| 207 | return error; |
| 208 | } |
| 209 | |
| 210 | static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) { |
| 211 | if (plane.bits_per_component != 8) { |
| 212 | ALOGV("Invalid number of bits per component: %d", |
| 213 | plane.bits_per_component); |
| 214 | return false; |
| 215 | } |
| 216 | if (plane.bits_used != 8) { |
| 217 | ALOGV("Invalid number of bits used: %d", plane.bits_used); |
| 218 | return false; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 219 | } |
| 220 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 221 | bool hasValidIncrement = plane.h_increment == 1 || |
| 222 | (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2); |
| 223 | hasValidIncrement = hasValidIncrement && plane.v_increment > 0; |
| 224 | if (!hasValidIncrement) { |
| 225 | ALOGV("Invalid increment: h %d v %d", plane.h_increment, |
| 226 | plane.v_increment); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return true; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 234 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 235 | { |
| 236 | ATRACE_CALL(); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 237 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 238 | gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 239 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 240 | std::vector<android_flex_plane_t> planes; |
| 241 | android_flex_layout_t flexLayout{}; |
| 242 | gralloc1_error_t error; |
| 243 | |
| 244 | if (mMapper->valid()) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 245 | Gralloc2::YCbCrLayout layout; |
| 246 | error = static_cast<gralloc1_error_t>(mMapper->lock(handle, usage, |
| 247 | asGralloc2Rect(bounds), fenceFd, &layout)); |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 248 | if (error == GRALLOC1_ERROR_NONE) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 249 | ycbcr->y = layout.y; |
| 250 | ycbcr->cb = layout.cb; |
| 251 | ycbcr->cr = layout.cr; |
| 252 | ycbcr->ystride = static_cast<size_t>(layout.yStride); |
| 253 | ycbcr->cstride = static_cast<size_t>(layout.cStride); |
| 254 | ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep); |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 255 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame^] | 256 | |
| 257 | return error; |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 258 | } else { |
| 259 | sp<Fence> fence = new Fence(fenceFd); |
| 260 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 261 | if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) { |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 262 | error = mDevice->lockYCbCr(handle, |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 263 | static_cast<gralloc1_producer_usage_t>(usage), |
| 264 | static_cast<gralloc1_consumer_usage_t>(usage), |
| 265 | &accessRegion, ycbcr, fence); |
| 266 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, |
| 267 | "lockYCbCr(%p, ...) failed: %d", handle, error); |
| 268 | return error; |
| 269 | } |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 270 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 271 | uint32_t numPlanes = 0; |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 272 | error = mDevice->getNumFlexPlanes(handle, &numPlanes); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 273 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 274 | if (error != GRALLOC1_ERROR_NONE) { |
| 275 | ALOGV("Failed to retrieve number of flex planes: %d", error); |
| 276 | return error; |
| 277 | } |
| 278 | if (numPlanes < 3) { |
| 279 | ALOGV("Not enough planes for YCbCr (%u found)", numPlanes); |
| 280 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 281 | } |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 282 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 283 | planes.resize(numPlanes); |
| 284 | flexLayout.num_planes = numPlanes; |
| 285 | flexLayout.planes = planes.data(); |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 286 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 287 | error = mDevice->lockFlex(handle, |
| 288 | static_cast<gralloc1_producer_usage_t>(usage), |
| 289 | static_cast<gralloc1_consumer_usage_t>(usage), |
| 290 | &accessRegion, &flexLayout, fence); |
| 291 | } |
| 292 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 293 | if (error != GRALLOC1_ERROR_NONE) { |
| 294 | ALOGW("lockFlex(%p, ...) failed: %d", handle, error); |
| 295 | return error; |
| 296 | } |
| 297 | if (flexLayout.format != FLEX_FORMAT_YCbCr) { |
| 298 | ALOGV("Unable to convert flex-format buffer to YCbCr"); |
| 299 | unlock(handle); |
| 300 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 301 | } |
| 302 | |
| 303 | // Find planes |
| 304 | auto yPlane = planes.cend(); |
| 305 | auto cbPlane = planes.cend(); |
| 306 | auto crPlane = planes.cend(); |
| 307 | for (auto planeIter = planes.cbegin(); planeIter != planes.cend(); |
| 308 | ++planeIter) { |
| 309 | if (planeIter->component == FLEX_COMPONENT_Y) { |
| 310 | yPlane = planeIter; |
| 311 | } else if (planeIter->component == FLEX_COMPONENT_Cb) { |
| 312 | cbPlane = planeIter; |
| 313 | } else if (planeIter->component == FLEX_COMPONENT_Cr) { |
| 314 | crPlane = planeIter; |
| 315 | } |
| 316 | } |
| 317 | if (yPlane == planes.cend()) { |
| 318 | ALOGV("Unable to find Y plane"); |
| 319 | unlock(handle); |
| 320 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 321 | } |
| 322 | if (cbPlane == planes.cend()) { |
| 323 | ALOGV("Unable to find Cb plane"); |
| 324 | unlock(handle); |
| 325 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 326 | } |
| 327 | if (crPlane == planes.cend()) { |
| 328 | ALOGV("Unable to find Cr plane"); |
| 329 | unlock(handle); |
| 330 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 331 | } |
| 332 | |
| 333 | // Validate planes |
| 334 | if (!isValidYCbCrPlane(*yPlane)) { |
| 335 | ALOGV("Y plane is invalid"); |
| 336 | unlock(handle); |
| 337 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 338 | } |
| 339 | if (!isValidYCbCrPlane(*cbPlane)) { |
| 340 | ALOGV("Cb plane is invalid"); |
| 341 | unlock(handle); |
| 342 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 343 | } |
| 344 | if (!isValidYCbCrPlane(*crPlane)) { |
| 345 | ALOGV("Cr plane is invalid"); |
| 346 | unlock(handle); |
| 347 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 348 | } |
| 349 | if (cbPlane->v_increment != crPlane->v_increment) { |
| 350 | ALOGV("Cb and Cr planes have different step (%d vs. %d)", |
| 351 | cbPlane->v_increment, crPlane->v_increment); |
| 352 | unlock(handle); |
| 353 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 354 | } |
| 355 | if (cbPlane->h_increment != crPlane->h_increment) { |
| 356 | ALOGV("Cb and Cr planes have different stride (%d vs. %d)", |
| 357 | cbPlane->h_increment, crPlane->h_increment); |
| 358 | unlock(handle); |
| 359 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 360 | } |
| 361 | |
| 362 | // Pack plane data into android_ycbcr struct |
| 363 | ycbcr->y = yPlane->top_left; |
| 364 | ycbcr->cb = cbPlane->top_left; |
| 365 | ycbcr->cr = crPlane->top_left; |
| 366 | ycbcr->ystride = static_cast<size_t>(yPlane->v_increment); |
| 367 | ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment); |
| 368 | ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment); |
| 369 | |
| 370 | return error; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) |
| 374 | { |
| 375 | ATRACE_CALL(); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 376 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 377 | gralloc1_error_t error; |
| 378 | if (mMapper->valid()) { |
| 379 | *fenceFd = mMapper->unlock(handle); |
| 380 | error = GRALLOC1_ERROR_NONE; |
| 381 | } else { |
| 382 | sp<Fence> fence = Fence::NO_FENCE; |
| 383 | error = mDevice->unlock(handle, &fence); |
| 384 | if (error != GRALLOC1_ERROR_NONE) { |
| 385 | ALOGE("unlock(%p) failed: %d", handle, error); |
| 386 | return error; |
| 387 | } |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 388 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 389 | *fenceFd = fence->dup(); |
| 390 | } |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 391 | return error; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 392 | } |
| 393 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 394 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 395 | }; // namespace android |