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