| 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 | a3c428a | 2017-04-11 10:03:38 -0700 | [diff] [blame] | 125 | if (!mDevice->hasCapability(GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE)) { | 
|  | 126 | native_handle_close(handle); | 
|  | 127 | native_handle_delete(const_cast<native_handle_t*>(handle)); | 
|  | 128 | } | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 131 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "freeBuffer(%p): failed %d", | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 132 | handle, error); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 133 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 134 | return error; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 137 | static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) { | 
|  | 138 | gralloc1_rect_t outRect{}; | 
|  | 139 | outRect.left = rect.left; | 
|  | 140 | outRect.top = rect.top; | 
|  | 141 | outRect.width = rect.width(); | 
|  | 142 | outRect.height = rect.height(); | 
|  | 143 | return outRect; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 146 | static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) { | 
|  | 147 | Gralloc2::IMapper::Rect outRect{}; | 
|  | 148 | outRect.left = rect.left; | 
|  | 149 | outRect.top = rect.top; | 
|  | 150 | outRect.width = rect.width(); | 
|  | 151 | outRect.height = rect.height(); | 
|  | 152 | return outRect; | 
| Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 155 | status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, | 
|  | 156 | const Rect& bounds, void** vaddr) | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 157 | { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 158 | return lockAsync(handle, usage, bounds, vaddr, -1); | 
|  | 159 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 160 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 161 | status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, | 
|  | 162 | const Rect& bounds, android_ycbcr *ycbcr) | 
|  | 163 | { | 
|  | 164 | return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 167 | status_t GraphicBufferMapper::unlock(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 168 | { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 169 | int32_t fenceFd = -1; | 
|  | 170 | status_t error = unlockAsync(handle, &fenceFd); | 
|  | 171 | if (error == NO_ERROR) { | 
|  | 172 | sync_wait(fenceFd, -1); | 
|  | 173 | close(fenceFd); | 
|  | 174 | } | 
|  | 175 | return error; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 178 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 179 | uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 180 | { | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 181 | return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, | 
|  | 185 | uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds, | 
|  | 186 | void** vaddr, int fenceFd) | 
|  | 187 | { | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 188 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 189 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 190 | gralloc1_error_t error; | 
|  | 191 | if (mMapper->valid()) { | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 192 | const uint64_t usage = | 
|  | 193 | static_cast<uint64_t>(android_convertGralloc1To0Usage( | 
|  | 194 | producerUsage, consumerUsage)); | 
|  | 195 | error = static_cast<gralloc1_error_t>(mMapper->lock(handle, | 
|  | 196 | usage, asGralloc2Rect(bounds), fenceFd, vaddr)); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 197 | } else { | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 198 | gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 199 | sp<Fence> fence = new Fence(fenceFd); | 
|  | 200 | error = mDevice->lock(handle, | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 201 | static_cast<gralloc1_producer_usage_t>(producerUsage), | 
|  | 202 | static_cast<gralloc1_consumer_usage_t>(consumerUsage), | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 203 | &accessRegion, vaddr, fence); | 
|  | 204 | } | 
|  | 205 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 206 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle, | 
|  | 207 | error); | 
|  | 208 |  | 
|  | 209 | return error; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) { | 
|  | 213 | if (plane.bits_per_component != 8) { | 
|  | 214 | ALOGV("Invalid number of bits per component: %d", | 
|  | 215 | plane.bits_per_component); | 
|  | 216 | return false; | 
|  | 217 | } | 
|  | 218 | if (plane.bits_used != 8) { | 
|  | 219 | ALOGV("Invalid number of bits used: %d", plane.bits_used); | 
|  | 220 | return false; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 223 | bool hasValidIncrement = plane.h_increment == 1 || | 
|  | 224 | (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2); | 
|  | 225 | hasValidIncrement = hasValidIncrement && plane.v_increment > 0; | 
|  | 226 | if (!hasValidIncrement) { | 
|  | 227 | ALOGV("Invalid increment: h %d v %d", plane.h_increment, | 
|  | 228 | plane.v_increment); | 
|  | 229 | return false; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | return true; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
|  | 235 | status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 236 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 237 | { | 
|  | 238 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 239 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 240 | gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 241 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 242 | std::vector<android_flex_plane_t> planes; | 
|  | 243 | android_flex_layout_t flexLayout{}; | 
|  | 244 | gralloc1_error_t error; | 
|  | 245 |  | 
|  | 246 | if (mMapper->valid()) { | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 247 | Gralloc2::YCbCrLayout layout; | 
|  | 248 | error = static_cast<gralloc1_error_t>(mMapper->lock(handle, usage, | 
|  | 249 | asGralloc2Rect(bounds), fenceFd, &layout)); | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 250 | if (error == GRALLOC1_ERROR_NONE) { | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 251 | ycbcr->y = layout.y; | 
|  | 252 | ycbcr->cb = layout.cb; | 
|  | 253 | ycbcr->cr = layout.cr; | 
|  | 254 | ycbcr->ystride = static_cast<size_t>(layout.yStride); | 
|  | 255 | ycbcr->cstride = static_cast<size_t>(layout.cStride); | 
|  | 256 | ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep); | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 257 | } | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 258 |  | 
|  | 259 | return error; | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 260 | } else { | 
|  | 261 | sp<Fence> fence = new Fence(fenceFd); | 
|  | 262 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 263 | if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) { | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 264 | error = mDevice->lockYCbCr(handle, | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 265 | static_cast<gralloc1_producer_usage_t>(usage), | 
|  | 266 | static_cast<gralloc1_consumer_usage_t>(usage), | 
|  | 267 | &accessRegion, ycbcr, fence); | 
|  | 268 | ALOGW_IF(error != GRALLOC1_ERROR_NONE, | 
|  | 269 | "lockYCbCr(%p, ...) failed: %d", handle, error); | 
|  | 270 | return error; | 
|  | 271 | } | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 272 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 273 | uint32_t numPlanes = 0; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 274 | error = mDevice->getNumFlexPlanes(handle, &numPlanes); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 275 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 276 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 277 | ALOGV("Failed to retrieve number of flex planes: %d", error); | 
|  | 278 | return error; | 
|  | 279 | } | 
|  | 280 | if (numPlanes < 3) { | 
|  | 281 | ALOGV("Not enough planes for YCbCr (%u found)", numPlanes); | 
|  | 282 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 283 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 284 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 285 | planes.resize(numPlanes); | 
|  | 286 | flexLayout.num_planes = numPlanes; | 
|  | 287 | flexLayout.planes = planes.data(); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 288 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 289 | error = mDevice->lockFlex(handle, | 
|  | 290 | static_cast<gralloc1_producer_usage_t>(usage), | 
|  | 291 | static_cast<gralloc1_consumer_usage_t>(usage), | 
|  | 292 | &accessRegion, &flexLayout, fence); | 
|  | 293 | } | 
|  | 294 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 295 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 296 | ALOGW("lockFlex(%p, ...) failed: %d", handle, error); | 
|  | 297 | return error; | 
|  | 298 | } | 
|  | 299 | if (flexLayout.format != FLEX_FORMAT_YCbCr) { | 
|  | 300 | ALOGV("Unable to convert flex-format buffer to YCbCr"); | 
|  | 301 | unlock(handle); | 
|  | 302 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | // Find planes | 
|  | 306 | auto yPlane = planes.cend(); | 
|  | 307 | auto cbPlane = planes.cend(); | 
|  | 308 | auto crPlane = planes.cend(); | 
|  | 309 | for (auto planeIter = planes.cbegin(); planeIter != planes.cend(); | 
|  | 310 | ++planeIter) { | 
|  | 311 | if (planeIter->component == FLEX_COMPONENT_Y) { | 
|  | 312 | yPlane = planeIter; | 
|  | 313 | } else if (planeIter->component == FLEX_COMPONENT_Cb) { | 
|  | 314 | cbPlane = planeIter; | 
|  | 315 | } else if (planeIter->component == FLEX_COMPONENT_Cr) { | 
|  | 316 | crPlane = planeIter; | 
|  | 317 | } | 
|  | 318 | } | 
|  | 319 | if (yPlane == planes.cend()) { | 
|  | 320 | ALOGV("Unable to find Y plane"); | 
|  | 321 | unlock(handle); | 
|  | 322 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 323 | } | 
|  | 324 | if (cbPlane == planes.cend()) { | 
|  | 325 | ALOGV("Unable to find Cb plane"); | 
|  | 326 | unlock(handle); | 
|  | 327 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 328 | } | 
|  | 329 | if (crPlane == planes.cend()) { | 
|  | 330 | ALOGV("Unable to find Cr plane"); | 
|  | 331 | unlock(handle); | 
|  | 332 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | // Validate planes | 
|  | 336 | if (!isValidYCbCrPlane(*yPlane)) { | 
|  | 337 | ALOGV("Y plane is invalid"); | 
|  | 338 | unlock(handle); | 
|  | 339 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 340 | } | 
|  | 341 | if (!isValidYCbCrPlane(*cbPlane)) { | 
|  | 342 | ALOGV("Cb plane is invalid"); | 
|  | 343 | unlock(handle); | 
|  | 344 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 345 | } | 
|  | 346 | if (!isValidYCbCrPlane(*crPlane)) { | 
|  | 347 | ALOGV("Cr plane is invalid"); | 
|  | 348 | unlock(handle); | 
|  | 349 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 350 | } | 
|  | 351 | if (cbPlane->v_increment != crPlane->v_increment) { | 
|  | 352 | ALOGV("Cb and Cr planes have different step (%d vs. %d)", | 
|  | 353 | cbPlane->v_increment, crPlane->v_increment); | 
|  | 354 | unlock(handle); | 
|  | 355 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 356 | } | 
|  | 357 | if (cbPlane->h_increment != crPlane->h_increment) { | 
|  | 358 | ALOGV("Cb and Cr planes have different stride (%d vs. %d)", | 
|  | 359 | cbPlane->h_increment, crPlane->h_increment); | 
|  | 360 | unlock(handle); | 
|  | 361 | return GRALLOC1_ERROR_UNSUPPORTED; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | // Pack plane data into android_ycbcr struct | 
|  | 365 | ycbcr->y = yPlane->top_left; | 
|  | 366 | ycbcr->cb = cbPlane->top_left; | 
|  | 367 | ycbcr->cr = crPlane->top_left; | 
|  | 368 | ycbcr->ystride = static_cast<size_t>(yPlane->v_increment); | 
|  | 369 | ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment); | 
|  | 370 | ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment); | 
|  | 371 |  | 
|  | 372 | return error; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 373 | } | 
|  | 374 |  | 
|  | 375 | status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) | 
|  | 376 | { | 
|  | 377 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 378 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 379 | gralloc1_error_t error; | 
|  | 380 | if (mMapper->valid()) { | 
|  | 381 | *fenceFd = mMapper->unlock(handle); | 
|  | 382 | error = GRALLOC1_ERROR_NONE; | 
|  | 383 | } else { | 
|  | 384 | sp<Fence> fence = Fence::NO_FENCE; | 
|  | 385 | error = mDevice->unlock(handle, &fence); | 
|  | 386 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 387 | ALOGE("unlock(%p) failed: %d", handle, error); | 
|  | 388 | return error; | 
|  | 389 | } | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 390 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 391 | *fenceFd = fence->dup(); | 
|  | 392 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 393 | return error; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 396 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 397 | }; // namespace android |