Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | #define LOG_TAG "Gralloc2" |
| 18 | |
Jesse Hall | 5dac781 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 19 | #include <hidl/ServiceManagement.h> |
Chia-I Wu | d8091b9 | 2017-05-16 14:30:34 -0700 | [diff] [blame] | 20 | #include <hwbinder/IPCThreadState.h> |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 21 | #include <ui/Gralloc2.h> |
| 22 | |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 24 | #include <log/log.h> |
| 25 | #pragma clang diagnostic push |
| 26 | #pragma clang diagnostic ignored "-Wzero-length-array" |
| 27 | #include <sync/sync.h> |
| 28 | #pragma clang diagnostic pop |
| 29 | |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 30 | using android::hardware::graphics::allocator::V2_0::IAllocator; |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 31 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 32 | using android::hardware::graphics::common::V1_1::PixelFormat; |
| 33 | using android::hardware::graphics::mapper::V2_0::BufferDescriptor; |
| 34 | using android::hardware::graphics::mapper::V2_0::Error; |
| 35 | using android::hardware::graphics::mapper::V2_0::YCbCrLayout; |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 36 | using android::hardware::graphics::mapper::V2_1::IMapper; |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 37 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 40 | namespace { |
| 41 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 42 | static constexpr Error kTransactionError = Error::NO_RESOURCES; |
| 43 | |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 44 | uint64_t getValid10UsageBits() { |
| 45 | static const uint64_t valid10UsageBits = []() -> uint64_t { |
| 46 | using hardware::graphics::common::V1_0::BufferUsage; |
| 47 | uint64_t bits = 0; |
Steven Moreland | 3cde875 | 2018-05-01 16:54:17 -0700 | [diff] [blame] | 48 | for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) { |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 49 | bits = bits | bit; |
| 50 | } |
Kevin F. Haggerty | dc5fbd4 | 2021-10-27 23:02:31 +0100 | [diff] [blame] | 51 | |
Michael Bestas | 627a733 | 2024-01-11 00:27:37 +0200 | [diff] [blame^] | 52 | if (ADDNL_GRALLOC_10_USAGE_BITS) { |
| 53 | uint64_t addnl_bits = static_cast<uint64_t>(ADDNL_GRALLOC_10_USAGE_BITS); |
| 54 | ALOGI("Adding additional valid usage bits: 0x%" PRIx64, addnl_bits); |
| 55 | bits = bits | addnl_bits; |
| 56 | } |
Kevin F. Haggerty | dc5fbd4 | 2021-10-27 23:02:31 +0100 | [diff] [blame] | 57 | |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 58 | return bits; |
| 59 | }(); |
| 60 | return valid10UsageBits; |
| 61 | } |
| 62 | |
| 63 | uint64_t getValid11UsageBits() { |
| 64 | static const uint64_t valid11UsageBits = []() -> uint64_t { |
| 65 | using hardware::graphics::common::V1_1::BufferUsage; |
| 66 | uint64_t bits = 0; |
Steven Moreland | 3cde875 | 2018-05-01 16:54:17 -0700 | [diff] [blame] | 67 | for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) { |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 68 | bits = bits | bit; |
| 69 | } |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 70 | return bits; |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 71 | }(); |
| 72 | return valid11UsageBits; |
| 73 | } |
| 74 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 75 | static inline IMapper::Rect sGralloc2Rect(const Rect& rect) { |
| 76 | IMapper::Rect outRect{}; |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 77 | outRect.left = rect.left; |
| 78 | outRect.top = rect.top; |
| 79 | outRect.width = rect.width(); |
| 80 | outRect.height = rect.height(); |
| 81 | return outRect; |
| 82 | } |
| 83 | |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 84 | } // anonymous namespace |
| 85 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 86 | void Gralloc2Mapper::preload() { |
Jesse Hall | 5dac781 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 87 | android::hardware::preloadPassthroughService<hardware::graphics::mapper::V2_0::IMapper>(); |
| 88 | } |
| 89 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 90 | Gralloc2Mapper::Gralloc2Mapper() { |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 91 | mMapper = hardware::graphics::mapper::V2_0::IMapper::getService(); |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 92 | if (mMapper == nullptr) { |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 93 | ALOGW("mapper 2.x is not supported"); |
| 94 | return; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 95 | } |
| 96 | if (mMapper->isRemote()) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 97 | LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); |
| 98 | } |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 99 | |
| 100 | // IMapper 2.1 is optional |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 101 | mMapperV2_1 = IMapper::castFrom(mMapper); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 104 | bool Gralloc2Mapper::isLoaded() const { |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 105 | return mMapper != nullptr; |
| 106 | } |
| 107 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 108 | status_t Gralloc2Mapper::validateBufferDescriptorInfo( |
| 109 | IMapper::BufferDescriptorInfo* descriptorInfo) const { |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 110 | uint64_t validUsageBits = getValid10UsageBits(); |
| 111 | if (mMapperV2_1 != nullptr) { |
| 112 | validUsageBits = validUsageBits | getValid11UsageBits(); |
| 113 | } |
| 114 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 115 | if (descriptorInfo->usage & ~validUsageBits) { |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 116 | ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 117 | descriptorInfo->usage & ~validUsageBits); |
| 118 | return BAD_VALUE; |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 119 | } |
Chris Forbes | 6c09ee7 | 2022-01-26 18:48:55 +1300 | [diff] [blame] | 120 | |
| 121 | // Gralloc2 implementations never understand non-BLOB with GPU_DATA_BUFFER |
| 122 | // and do not reliably reject it. |
| 123 | if (descriptorInfo->usage & BufferUsage::GPU_DATA_BUFFER && |
| 124 | descriptorInfo->format != hardware::graphics::common::V1_1::PixelFormat::BLOB) { |
| 125 | ALOGE("gralloc2 does not support non-BLOB pixel formats with GPU_DATA_BUFFER usage"); |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 129 | return NO_ERROR; |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 132 | status_t Gralloc2Mapper::createDescriptor(void* bufferDescriptorInfo, |
| 133 | void* outBufferDescriptor) const { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 134 | IMapper::BufferDescriptorInfo* descriptorInfo = |
| 135 | static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo); |
| 136 | BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor); |
| 137 | |
| 138 | status_t status = validateBufferDescriptorInfo(descriptorInfo); |
| 139 | if (status != NO_ERROR) { |
| 140 | return status; |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 143 | Error error; |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 144 | auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) |
| 145 | { |
| 146 | error = tmpError; |
| 147 | if (error != Error::NONE) { |
| 148 | return; |
| 149 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 150 | |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 151 | *outDescriptor = tmpDescriptor; |
| 152 | }; |
| 153 | |
| 154 | hardware::Return<void> ret; |
| 155 | if (mMapperV2_1 != nullptr) { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 156 | ret = mMapperV2_1->createDescriptor_2_1(*descriptorInfo, hidl_cb); |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 157 | } else { |
| 158 | const hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo info = { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 159 | descriptorInfo->width, |
| 160 | descriptorInfo->height, |
| 161 | descriptorInfo->layerCount, |
| 162 | static_cast<hardware::graphics::common::V1_0::PixelFormat>(descriptorInfo->format), |
| 163 | descriptorInfo->usage, |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 164 | }; |
| 165 | ret = mMapper->createDescriptor(info, hidl_cb); |
| 166 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 167 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 168 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 171 | status_t Gralloc2Mapper::importBuffer(const hardware::hidl_handle& rawHandle, |
| 172 | buffer_handle_t* outBufferHandle) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 173 | Error error; |
| 174 | auto ret = mMapper->importBuffer(rawHandle, |
| 175 | [&](const auto& tmpError, const auto& tmpBuffer) |
| 176 | { |
| 177 | error = tmpError; |
| 178 | if (error != Error::NONE) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer); |
| 183 | }); |
| 184 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 185 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 188 | void Gralloc2Mapper::freeBuffer(buffer_handle_t bufferHandle) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 189 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 190 | auto ret = mMapper->freeBuffer(buffer); |
| 191 | |
| 192 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError; |
| 193 | ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", |
| 194 | buffer, error); |
| 195 | } |
| 196 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 197 | status_t Gralloc2Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, |
| 198 | uint32_t height, android::PixelFormat format, |
| 199 | uint32_t layerCount, uint64_t usage, |
| 200 | uint32_t stride) const { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 201 | if (mMapperV2_1 == nullptr) { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 202 | return NO_ERROR; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 205 | IMapper::BufferDescriptorInfo descriptorInfo = {}; |
| 206 | descriptorInfo.width = width; |
| 207 | descriptorInfo.height = height; |
| 208 | descriptorInfo.layerCount = layerCount; |
| 209 | descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format); |
| 210 | descriptorInfo.usage = usage; |
| 211 | |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 212 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 213 | auto ret = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride); |
| 214 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 215 | return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError); |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 218 | void Gralloc2Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds, |
| 219 | uint32_t* outNumInts) const { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 220 | *outNumFds = uint32_t(bufferHandle->numFds); |
| 221 | *outNumInts = uint32_t(bufferHandle->numInts); |
| 222 | |
| 223 | if (mMapperV2_1 == nullptr) { |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | Error error; |
| 228 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 229 | auto ret = mMapperV2_1->getTransportSize(buffer, |
| 230 | [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) { |
| 231 | error = tmpError; |
| 232 | if (error != Error::NONE) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | *outNumFds = tmpNumFds; |
| 237 | *outNumInts = tmpNumInts; |
| 238 | }); |
| 239 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 240 | error = (ret.isOk()) ? error : kTransactionError; |
| 241 | |
| 242 | ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error); |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 245 | status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 246 | int acquireFence, void** outData, int32_t* outBytesPerPixel, |
| 247 | int32_t* outBytesPerStride) const { |
| 248 | if (outBytesPerPixel) { |
| 249 | *outBytesPerPixel = -1; |
| 250 | } |
| 251 | if (outBytesPerStride) { |
| 252 | *outBytesPerStride = -1; |
| 253 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 254 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 255 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 256 | IMapper::Rect accessRegion = sGralloc2Rect(bounds); |
| 257 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 258 | // put acquireFence in a hidl_handle |
| 259 | hardware::hidl_handle acquireFenceHandle; |
| 260 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 261 | if (acquireFence >= 0) { |
| 262 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 263 | h->data[0] = acquireFence; |
| 264 | acquireFenceHandle = h; |
| 265 | } |
| 266 | |
| 267 | Error error; |
| 268 | auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, |
| 269 | [&](const auto& tmpError, const auto& tmpData) |
| 270 | { |
| 271 | error = tmpError; |
| 272 | if (error != Error::NONE) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | *outData = tmpData; |
| 277 | }); |
| 278 | |
| 279 | // we own acquireFence even on errors |
| 280 | if (acquireFence >= 0) { |
| 281 | close(acquireFence); |
| 282 | } |
| 283 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 284 | error = (ret.isOk()) ? error : kTransactionError; |
| 285 | |
| 286 | ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error); |
| 287 | |
| 288 | return static_cast<status_t>(error); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 291 | status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, |
| 292 | int acquireFence, android_ycbcr* ycbcr) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 293 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 294 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 295 | IMapper::Rect accessRegion = sGralloc2Rect(bounds); |
| 296 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 297 | // put acquireFence in a hidl_handle |
| 298 | hardware::hidl_handle acquireFenceHandle; |
| 299 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 300 | if (acquireFence >= 0) { |
| 301 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 302 | h->data[0] = acquireFence; |
| 303 | acquireFenceHandle = h; |
| 304 | } |
| 305 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 306 | YCbCrLayout layout; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 307 | Error error; |
| 308 | auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, |
| 309 | acquireFenceHandle, |
| 310 | [&](const auto& tmpError, const auto& tmpLayout) |
| 311 | { |
| 312 | error = tmpError; |
| 313 | if (error != Error::NONE) { |
| 314 | return; |
| 315 | } |
| 316 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 317 | layout = tmpLayout; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 318 | }); |
| 319 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 320 | if (error == Error::NONE) { |
| 321 | ycbcr->y = layout.y; |
| 322 | ycbcr->cb = layout.cb; |
| 323 | ycbcr->cr = layout.cr; |
| 324 | ycbcr->ystride = static_cast<size_t>(layout.yStride); |
| 325 | ycbcr->cstride = static_cast<size_t>(layout.cStride); |
| 326 | ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep); |
| 327 | } |
| 328 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 329 | // we own acquireFence even on errors |
| 330 | if (acquireFence >= 0) { |
| 331 | close(acquireFence); |
| 332 | } |
| 333 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 334 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 337 | int Gralloc2Mapper::unlock(buffer_handle_t bufferHandle) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 338 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 339 | |
| 340 | int releaseFence = -1; |
| 341 | Error error; |
| 342 | auto ret = mMapper->unlock(buffer, |
| 343 | [&](const auto& tmpError, const auto& tmpReleaseFence) |
| 344 | { |
| 345 | error = tmpError; |
| 346 | if (error != Error::NONE) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 351 | if (fenceHandle && fenceHandle->numFds == 1) { |
| 352 | int fd = dup(fenceHandle->data[0]); |
| 353 | if (fd >= 0) { |
| 354 | releaseFence = fd; |
| 355 | } else { |
| 356 | ALOGD("failed to dup unlock release fence"); |
| 357 | sync_wait(fenceHandle->data[0], -1); |
| 358 | } |
| 359 | } |
| 360 | }); |
| 361 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 362 | error = (ret.isOk()) ? error : kTransactionError; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 363 | if (error != Error::NONE) { |
| 364 | ALOGE("unlock(%p) failed with %d", buffer, error); |
| 365 | } |
| 366 | |
| 367 | return releaseFence; |
| 368 | } |
| 369 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 370 | Gralloc2Allocator::Gralloc2Allocator(const Gralloc2Mapper& mapper) : mMapper(mapper) { |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 371 | mAllocator = IAllocator::getService(); |
| 372 | if (mAllocator == nullptr) { |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 373 | ALOGW("allocator 2.x is not supported"); |
| 374 | return; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 378 | bool Gralloc2Allocator::isLoaded() const { |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 379 | return mAllocator != nullptr; |
| 380 | } |
| 381 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 382 | std::string Gralloc2Allocator::dumpDebugInfo(bool /*less*/) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 383 | std::string debugInfo; |
| 384 | |
| 385 | mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { |
| 386 | debugInfo = tmpDebugInfo.c_str(); |
| 387 | }); |
| 388 | |
| 389 | return debugInfo; |
| 390 | } |
| 391 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 392 | status_t Gralloc2Allocator::allocate(std::string /*requestorName*/, uint32_t width, uint32_t height, |
| 393 | PixelFormat format, uint32_t layerCount, uint64_t usage, |
| 394 | uint32_t bufferCount, uint32_t* outStride, |
| 395 | buffer_handle_t* outBufferHandles, bool importBuffers) const { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 396 | IMapper::BufferDescriptorInfo descriptorInfo = {}; |
| 397 | descriptorInfo.width = width; |
| 398 | descriptorInfo.height = height; |
| 399 | descriptorInfo.layerCount = layerCount; |
| 400 | descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format); |
| 401 | descriptorInfo.usage = usage; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 402 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 403 | BufferDescriptor descriptor; |
| 404 | status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo), |
| 405 | static_cast<void*>(&descriptor)); |
| 406 | if (error != NO_ERROR) { |
| 407 | return error; |
| 408 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 409 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 410 | auto ret = mAllocator->allocate(descriptor, bufferCount, |
| 411 | [&](const auto& tmpError, const auto& tmpStride, |
| 412 | const auto& tmpBuffers) { |
| 413 | error = static_cast<status_t>(tmpError); |
| 414 | if (tmpError != Error::NONE) { |
| 415 | return; |
| 416 | } |
| 417 | |
Marissa Wall | bfcf81f | 2019-11-27 10:36:29 -0800 | [diff] [blame] | 418 | if (importBuffers) { |
| 419 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 420 | error = mMapper.importBuffer(tmpBuffers[i], |
| 421 | &outBufferHandles[i]); |
| 422 | if (error != NO_ERROR) { |
| 423 | for (uint32_t j = 0; j < i; j++) { |
| 424 | mMapper.freeBuffer(outBufferHandles[j]); |
| 425 | outBufferHandles[j] = nullptr; |
| 426 | } |
| 427 | return; |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 428 | } |
Marissa Wall | bfcf81f | 2019-11-27 10:36:29 -0800 | [diff] [blame] | 429 | } |
| 430 | } else { |
| 431 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 432 | outBufferHandles[i] = native_handle_clone( |
| 433 | tmpBuffers[i].getNativeHandle()); |
| 434 | if (!outBufferHandles[i]) { |
| 435 | for (uint32_t j = 0; j < i; j++) { |
| 436 | auto buffer = const_cast<native_handle_t*>( |
| 437 | outBufferHandles[j]); |
| 438 | native_handle_close(buffer); |
| 439 | native_handle_delete(buffer); |
| 440 | outBufferHandles[j] = nullptr; |
| 441 | } |
| 442 | } |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 445 | *outStride = tmpStride; |
| 446 | }); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 447 | |
Chia-I Wu | d8091b9 | 2017-05-16 14:30:34 -0700 | [diff] [blame] | 448 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now |
| 449 | hardware::IPCThreadState::self()->flushCommands(); |
| 450 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 451 | return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 454 | } // namespace android |