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