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 | 74c35e6 | 2018-08-25 15:00:25 -0600 | [diff] [blame] | 51 | |
| 52 | #ifdef 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 | #endif |
| 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 | } |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 120 | return NO_ERROR; |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 123 | status_t Gralloc2Mapper::createDescriptor(void* bufferDescriptorInfo, |
| 124 | void* outBufferDescriptor) const { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 125 | IMapper::BufferDescriptorInfo* descriptorInfo = |
| 126 | static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo); |
| 127 | BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor); |
| 128 | |
| 129 | status_t status = validateBufferDescriptorInfo(descriptorInfo); |
| 130 | if (status != NO_ERROR) { |
| 131 | return status; |
Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 134 | Error error; |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 135 | auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) |
| 136 | { |
| 137 | error = tmpError; |
| 138 | if (error != Error::NONE) { |
| 139 | return; |
| 140 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 141 | |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 142 | *outDescriptor = tmpDescriptor; |
| 143 | }; |
| 144 | |
| 145 | hardware::Return<void> ret; |
| 146 | if (mMapperV2_1 != nullptr) { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 147 | ret = mMapperV2_1->createDescriptor_2_1(*descriptorInfo, hidl_cb); |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 148 | } else { |
| 149 | const hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo info = { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 150 | descriptorInfo->width, |
| 151 | descriptorInfo->height, |
| 152 | descriptorInfo->layerCount, |
| 153 | static_cast<hardware::graphics::common::V1_0::PixelFormat>(descriptorInfo->format), |
| 154 | descriptorInfo->usage, |
Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 155 | }; |
| 156 | ret = mMapper->createDescriptor(info, hidl_cb); |
| 157 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 158 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 159 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 162 | status_t Gralloc2Mapper::importBuffer(const hardware::hidl_handle& rawHandle, |
| 163 | buffer_handle_t* outBufferHandle) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 164 | Error error; |
| 165 | auto ret = mMapper->importBuffer(rawHandle, |
| 166 | [&](const auto& tmpError, const auto& tmpBuffer) |
| 167 | { |
| 168 | error = tmpError; |
| 169 | if (error != Error::NONE) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer); |
| 174 | }); |
| 175 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 176 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 179 | void Gralloc2Mapper::freeBuffer(buffer_handle_t bufferHandle) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 180 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 181 | auto ret = mMapper->freeBuffer(buffer); |
| 182 | |
| 183 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError; |
| 184 | ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", |
| 185 | buffer, error); |
| 186 | } |
| 187 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 188 | status_t Gralloc2Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, |
| 189 | uint32_t height, android::PixelFormat format, |
| 190 | uint32_t layerCount, uint64_t usage, |
| 191 | uint32_t stride) const { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 192 | if (mMapperV2_1 == nullptr) { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 193 | return NO_ERROR; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 196 | IMapper::BufferDescriptorInfo descriptorInfo = {}; |
| 197 | descriptorInfo.width = width; |
| 198 | descriptorInfo.height = height; |
| 199 | descriptorInfo.layerCount = layerCount; |
| 200 | descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format); |
| 201 | descriptorInfo.usage = usage; |
| 202 | |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 203 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 204 | auto ret = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride); |
| 205 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 206 | 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] | 207 | } |
| 208 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 209 | void Gralloc2Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds, |
| 210 | uint32_t* outNumInts) const { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 211 | *outNumFds = uint32_t(bufferHandle->numFds); |
| 212 | *outNumInts = uint32_t(bufferHandle->numInts); |
| 213 | |
| 214 | if (mMapperV2_1 == nullptr) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | Error error; |
| 219 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 220 | auto ret = mMapperV2_1->getTransportSize(buffer, |
| 221 | [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) { |
| 222 | error = tmpError; |
| 223 | if (error != Error::NONE) { |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | *outNumFds = tmpNumFds; |
| 228 | *outNumInts = tmpNumInts; |
| 229 | }); |
| 230 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 231 | error = (ret.isOk()) ? error : kTransactionError; |
| 232 | |
| 233 | 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] | 234 | } |
| 235 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 236 | 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] | 237 | int acquireFence, void** outData, int32_t* outBytesPerPixel, |
| 238 | int32_t* outBytesPerStride) const { |
| 239 | if (outBytesPerPixel) { |
| 240 | *outBytesPerPixel = -1; |
| 241 | } |
| 242 | if (outBytesPerStride) { |
| 243 | *outBytesPerStride = -1; |
| 244 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 245 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 246 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 247 | IMapper::Rect accessRegion = sGralloc2Rect(bounds); |
| 248 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 249 | // put acquireFence in a hidl_handle |
| 250 | hardware::hidl_handle acquireFenceHandle; |
| 251 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 252 | if (acquireFence >= 0) { |
| 253 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 254 | h->data[0] = acquireFence; |
| 255 | acquireFenceHandle = h; |
| 256 | } |
| 257 | |
| 258 | Error error; |
| 259 | auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, |
| 260 | [&](const auto& tmpError, const auto& tmpData) |
| 261 | { |
| 262 | error = tmpError; |
| 263 | if (error != Error::NONE) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | *outData = tmpData; |
| 268 | }); |
| 269 | |
| 270 | // we own acquireFence even on errors |
| 271 | if (acquireFence >= 0) { |
| 272 | close(acquireFence); |
| 273 | } |
| 274 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 275 | error = (ret.isOk()) ? error : kTransactionError; |
| 276 | |
| 277 | ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error); |
| 278 | |
| 279 | return static_cast<status_t>(error); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 282 | status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, |
| 283 | int acquireFence, android_ycbcr* ycbcr) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 284 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 285 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 286 | IMapper::Rect accessRegion = sGralloc2Rect(bounds); |
| 287 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 288 | // put acquireFence in a hidl_handle |
| 289 | hardware::hidl_handle acquireFenceHandle; |
| 290 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 291 | if (acquireFence >= 0) { |
| 292 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 293 | h->data[0] = acquireFence; |
| 294 | acquireFenceHandle = h; |
| 295 | } |
| 296 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 297 | YCbCrLayout layout; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 298 | Error error; |
| 299 | auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, |
| 300 | acquireFenceHandle, |
| 301 | [&](const auto& tmpError, const auto& tmpLayout) |
| 302 | { |
| 303 | error = tmpError; |
| 304 | if (error != Error::NONE) { |
| 305 | return; |
| 306 | } |
| 307 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 308 | layout = tmpLayout; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 309 | }); |
| 310 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 311 | if (error == Error::NONE) { |
| 312 | ycbcr->y = layout.y; |
| 313 | ycbcr->cb = layout.cb; |
| 314 | ycbcr->cr = layout.cr; |
| 315 | ycbcr->ystride = static_cast<size_t>(layout.yStride); |
| 316 | ycbcr->cstride = static_cast<size_t>(layout.cStride); |
| 317 | ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep); |
| 318 | } |
| 319 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 320 | // we own acquireFence even on errors |
| 321 | if (acquireFence >= 0) { |
| 322 | close(acquireFence); |
| 323 | } |
| 324 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 325 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 328 | int Gralloc2Mapper::unlock(buffer_handle_t bufferHandle) const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 329 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 330 | |
| 331 | int releaseFence = -1; |
| 332 | Error error; |
| 333 | auto ret = mMapper->unlock(buffer, |
| 334 | [&](const auto& tmpError, const auto& tmpReleaseFence) |
| 335 | { |
| 336 | error = tmpError; |
| 337 | if (error != Error::NONE) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 342 | if (fenceHandle && fenceHandle->numFds == 1) { |
| 343 | int fd = dup(fenceHandle->data[0]); |
| 344 | if (fd >= 0) { |
| 345 | releaseFence = fd; |
| 346 | } else { |
| 347 | ALOGD("failed to dup unlock release fence"); |
| 348 | sync_wait(fenceHandle->data[0], -1); |
| 349 | } |
| 350 | } |
| 351 | }); |
| 352 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 353 | error = (ret.isOk()) ? error : kTransactionError; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 354 | if (error != Error::NONE) { |
| 355 | ALOGE("unlock(%p) failed with %d", buffer, error); |
| 356 | } |
| 357 | |
| 358 | return releaseFence; |
| 359 | } |
| 360 | |
Valerie Hau | ddbfaeb | 2019-02-01 09:54:20 -0800 | [diff] [blame] | 361 | status_t Gralloc2Mapper::isSupported(uint32_t /*width*/, uint32_t /*height*/, |
| 362 | android::PixelFormat /*format*/, uint32_t /*layerCount*/, |
| 363 | uint64_t /*usage*/, bool* /*outSupported*/) const { |
| 364 | return INVALID_OPERATION; |
| 365 | } |
| 366 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 367 | Gralloc2Allocator::Gralloc2Allocator(const Gralloc2Mapper& mapper) : mMapper(mapper) { |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 368 | mAllocator = IAllocator::getService(); |
| 369 | if (mAllocator == nullptr) { |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 370 | ALOGW("allocator 2.x is not supported"); |
| 371 | return; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 375 | bool Gralloc2Allocator::isLoaded() const { |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 376 | return mAllocator != nullptr; |
| 377 | } |
| 378 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 379 | std::string Gralloc2Allocator::dumpDebugInfo() const { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 380 | std::string debugInfo; |
| 381 | |
| 382 | mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { |
| 383 | debugInfo = tmpDebugInfo.c_str(); |
| 384 | }); |
| 385 | |
| 386 | return debugInfo; |
| 387 | } |
| 388 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 389 | status_t Gralloc2Allocator::allocate(uint32_t width, uint32_t height, PixelFormat format, |
| 390 | uint32_t layerCount, uint64_t usage, uint32_t bufferCount, |
| 391 | uint32_t* outStride, buffer_handle_t* outBufferHandles) const { |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 392 | IMapper::BufferDescriptorInfo descriptorInfo = {}; |
| 393 | descriptorInfo.width = width; |
| 394 | descriptorInfo.height = height; |
| 395 | descriptorInfo.layerCount = layerCount; |
| 396 | descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format); |
| 397 | descriptorInfo.usage = usage; |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 398 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 399 | BufferDescriptor descriptor; |
| 400 | status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo), |
| 401 | static_cast<void*>(&descriptor)); |
| 402 | if (error != NO_ERROR) { |
| 403 | return error; |
| 404 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 405 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 406 | auto ret = mAllocator->allocate(descriptor, bufferCount, |
| 407 | [&](const auto& tmpError, const auto& tmpStride, |
| 408 | const auto& tmpBuffers) { |
| 409 | error = static_cast<status_t>(tmpError); |
| 410 | if (tmpError != Error::NONE) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | // import buffers |
| 415 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 416 | error = mMapper.importBuffer(tmpBuffers[i], |
| 417 | &outBufferHandles[i]); |
| 418 | if (error != NO_ERROR) { |
| 419 | for (uint32_t j = 0; j < i; j++) { |
| 420 | mMapper.freeBuffer(outBufferHandles[j]); |
| 421 | outBufferHandles[j] = nullptr; |
| 422 | } |
| 423 | return; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | *outStride = tmpStride; |
| 428 | }); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 429 | |
Chia-I Wu | d8091b9 | 2017-05-16 14:30:34 -0700 | [diff] [blame] | 430 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now |
| 431 | hardware::IPCThreadState::self()->flushCommands(); |
| 432 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 433 | return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 436 | } // namespace android |