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