Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [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 "GrallocMapper" |
| 18 | |
| 19 | #include <array> |
| 20 | #include <string> |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | #include <ui/GrallocMapper.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | namespace Gralloc2 { |
| 28 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 29 | static constexpr Error kDefaultError = Error::NO_RESOURCES; |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 30 | |
| 31 | Mapper::Mapper() |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 32 | { |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 33 | mMapper = IMapper::getService("gralloc-mapper"); |
| 34 | if (mMapper != nullptr && mMapper->isRemote()) { |
| 35 | LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 39 | Error Mapper::retain(buffer_handle_t handle) const |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 40 | { |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 41 | auto ret = mMapper->retain(handle); |
| 42 | return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError; |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void Mapper::release(buffer_handle_t handle) const |
| 46 | { |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 47 | auto ret = mMapper->release(handle); |
| 48 | |
| 49 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError; |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 50 | ALOGE_IF(error != Error::NONE, |
| 51 | "release(%p) failed with %d", handle, error); |
| 52 | } |
| 53 | |
Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame^] | 54 | Error Mapper::getDimensions(buffer_handle_t handle, |
| 55 | uint32_t* outWidth, uint32_t* outHeight) const |
| 56 | { |
| 57 | Error error = kDefaultError; |
| 58 | mMapper->getDimensions(handle, |
| 59 | [&](const auto& tmpError, const auto& tmpWidth, |
| 60 | const auto& tmpHeight) |
| 61 | { |
| 62 | error = tmpError; |
| 63 | if (error != Error::NONE) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | *outWidth = tmpWidth; |
| 68 | *outHeight = tmpHeight; |
| 69 | }); |
| 70 | |
| 71 | return error; |
| 72 | } |
| 73 | |
| 74 | Error Mapper::getFormat(buffer_handle_t handle, int32_t* outFormat) const |
| 75 | { |
| 76 | Error error = kDefaultError; |
| 77 | mMapper->getFormat(handle, |
| 78 | [&](const auto& tmpError, const auto& tmpFormat) |
| 79 | { |
| 80 | error = tmpError; |
| 81 | if (error != Error::NONE) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | *outFormat = static_cast<int32_t>(tmpFormat); |
| 86 | }); |
| 87 | |
| 88 | return error; |
| 89 | } |
| 90 | |
| 91 | Error Mapper::getLayerCount(buffer_handle_t handle, |
| 92 | uint32_t* outLayerCount) const |
| 93 | { |
| 94 | Error error = kDefaultError; |
| 95 | mMapper->getLayerCount(handle, |
| 96 | [&](const auto& tmpError, const auto& tmpLayerCount) |
| 97 | { |
| 98 | error = tmpError; |
| 99 | if (error != Error::NONE) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | *outLayerCount = tmpLayerCount; |
| 104 | }); |
| 105 | |
| 106 | return error; |
| 107 | } |
| 108 | |
| 109 | Error Mapper::getProducerUsage(buffer_handle_t handle, |
| 110 | uint64_t* outProducerUsage) const |
| 111 | { |
| 112 | Error error = kDefaultError; |
| 113 | mMapper->getProducerUsageMask(handle, |
| 114 | [&](const auto& tmpError, const auto& tmpProducerUsage) |
| 115 | { |
| 116 | error = tmpError; |
| 117 | if (error != Error::NONE) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | *outProducerUsage = tmpProducerUsage; |
| 122 | }); |
| 123 | |
| 124 | return error; |
| 125 | } |
| 126 | |
| 127 | Error Mapper::getConsumerUsage(buffer_handle_t handle, |
| 128 | uint64_t* outConsumerUsage) const |
| 129 | { |
| 130 | Error error = kDefaultError; |
| 131 | mMapper->getConsumerUsageMask(handle, |
| 132 | [&](const auto& tmpError, const auto& tmpConsumerUsage) |
| 133 | { |
| 134 | error = tmpError; |
| 135 | if (error != Error::NONE) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | *outConsumerUsage = tmpConsumerUsage; |
| 140 | }); |
| 141 | |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | Error Mapper::getBackingStore(buffer_handle_t handle, |
| 146 | uint64_t* outBackingStore) const |
| 147 | { |
| 148 | Error error = kDefaultError; |
| 149 | mMapper->getBackingStore(handle, |
| 150 | [&](const auto& tmpError, const auto& tmpStore) |
| 151 | { |
| 152 | error = tmpError; |
| 153 | if (error != Error::NONE) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | *outBackingStore = tmpStore; |
| 158 | }); |
| 159 | |
| 160 | return error; |
| 161 | } |
| 162 | |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 163 | Error Mapper::getStride(buffer_handle_t handle, uint32_t* outStride) const |
| 164 | { |
| 165 | Error error = kDefaultError; |
| 166 | mMapper->getStride(handle, |
| 167 | [&](const auto& tmpError, const auto& tmpStride) |
| 168 | { |
| 169 | error = tmpError; |
| 170 | if (error != Error::NONE) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | *outStride = tmpStride; |
| 175 | }); |
| 176 | |
| 177 | return error; |
| 178 | } |
| 179 | |
| 180 | Error Mapper::lock(buffer_handle_t handle, |
Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame^] | 181 | uint64_t producerUsage, |
| 182 | uint64_t consumerUsage, |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 183 | const IMapper::Rect& accessRegion, |
| 184 | int acquireFence, void** outData) const |
| 185 | { |
| 186 | hardware::hidl_handle acquireFenceHandle; |
| 187 | |
| 188 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 189 | if (acquireFence >= 0) { |
| 190 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 191 | h->data[0] = acquireFence; |
| 192 | acquireFenceHandle = h; |
| 193 | } |
| 194 | |
| 195 | Error error = kDefaultError; |
Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame^] | 196 | mMapper->lock(handle, producerUsage, consumerUsage, |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 197 | accessRegion, acquireFenceHandle, |
| 198 | [&](const auto& tmpError, const auto& tmpData) |
| 199 | { |
| 200 | error = tmpError; |
| 201 | if (error != Error::NONE) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | *outData = tmpData; |
| 206 | }); |
| 207 | |
| 208 | if (error == Error::NONE && acquireFence >= 0) { |
| 209 | close(acquireFence); |
| 210 | } |
| 211 | |
| 212 | return error; |
| 213 | } |
| 214 | |
| 215 | Error Mapper::lock(buffer_handle_t handle, |
Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame^] | 216 | uint64_t producerUsage, |
| 217 | uint64_t consumerUsage, |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 218 | const IMapper::Rect& accessRegion, |
| 219 | int acquireFence, FlexLayout* outLayout) const |
| 220 | { |
| 221 | hardware::hidl_handle acquireFenceHandle; |
| 222 | |
| 223 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 224 | if (acquireFence >= 0) { |
| 225 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 226 | h->data[0] = acquireFence; |
| 227 | acquireFenceHandle = h; |
| 228 | } |
| 229 | |
| 230 | Error error = kDefaultError; |
Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame^] | 231 | mMapper->lockFlex(handle, producerUsage, consumerUsage, |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 232 | accessRegion, acquireFenceHandle, |
| 233 | [&](const auto& tmpError, const auto& tmpLayout) |
| 234 | { |
| 235 | error = tmpError; |
| 236 | if (error != Error::NONE) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | *outLayout = tmpLayout; |
| 241 | }); |
| 242 | |
| 243 | if (error == Error::NONE && acquireFence >= 0) { |
| 244 | close(acquireFence); |
| 245 | } |
| 246 | |
| 247 | return error; |
| 248 | } |
| 249 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 250 | int Mapper::unlock(buffer_handle_t handle) const |
| 251 | { |
Fabien Sanglard | 521698f | 2017-01-10 13:45:45 -0800 | [diff] [blame] | 252 | int releaseFence = -1; |
Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 253 | |
| 254 | Error error = kDefaultError; |
| 255 | mMapper->unlock(handle, |
| 256 | [&](const auto& tmpError, const auto& tmpReleaseFence) |
| 257 | { |
| 258 | error = tmpError; |
| 259 | if (error != Error::NONE) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 264 | if (fenceHandle && fenceHandle->numFds == 1) { |
| 265 | int fd = dup(fenceHandle->data[0]); |
| 266 | if (fd >= 0) { |
| 267 | releaseFence = fd; |
| 268 | } else { |
| 269 | error = Error::NO_RESOURCES; |
| 270 | } |
| 271 | } else { |
| 272 | releaseFence = -1; |
| 273 | } |
| 274 | }); |
| 275 | |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 276 | if (error != Error::NONE) { |
| 277 | ALOGE("unlock(%p) failed with %d", handle, error); |
| 278 | releaseFence = -1; |
| 279 | } |
| 280 | |
| 281 | return releaseFence; |
| 282 | } |
| 283 | |
| 284 | } // namespace Gralloc2 |
| 285 | |
| 286 | } // namespace android |