Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "HandleImporter" |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 18 | #include "HandleImporter.h" |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <gralloctypes/Gralloc4.h> |
Steven Moreland | 4e7a307 | 2017-04-06 12:15:23 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace camera { |
| 26 | namespace common { |
| 27 | namespace V1_0 { |
| 28 | namespace helper { |
| 29 | |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 30 | using aidl::android::hardware::graphics::common::PlaneLayout; |
| 31 | using aidl::android::hardware::graphics::common::PlaneLayoutComponent; |
| 32 | using aidl::android::hardware::graphics::common::PlaneLayoutComponentType; |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame^] | 33 | using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType; |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 34 | using MapperErrorV2 = android::hardware::graphics::mapper::V2_0::Error; |
| 35 | using MapperErrorV3 = android::hardware::graphics::mapper::V3_0::Error; |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 36 | using MapperErrorV4 = android::hardware::graphics::mapper::V4_0::Error; |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 37 | using IMapperV3 = android::hardware::graphics::mapper::V3_0::IMapper; |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 38 | using IMapperV4 = android::hardware::graphics::mapper::V4_0::IMapper; |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 39 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 40 | HandleImporter::HandleImporter() : mInitialized(false) {} |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 41 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 42 | void HandleImporter::initializeLocked() { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 43 | if (mInitialized) { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 47 | mMapperV4 = IMapperV4::getService(); |
| 48 | if (mMapperV4 != nullptr) { |
| 49 | mInitialized = true; |
| 50 | return; |
| 51 | } |
| 52 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 53 | mMapperV3 = IMapperV3::getService(); |
| 54 | if (mMapperV3 != nullptr) { |
| 55 | mInitialized = true; |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | mMapperV2 = IMapper::getService(); |
| 60 | if (mMapperV2 == nullptr) { |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 61 | ALOGE("%s: cannnot acccess graphics mapper HAL!", __FUNCTION__); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | mInitialized = true; |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | void HandleImporter::cleanup() { |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 70 | mMapperV4.clear(); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 71 | mMapperV3.clear(); |
| 72 | mMapperV2.clear(); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 73 | mInitialized = false; |
| 74 | } |
| 75 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 76 | template<class M, class E> |
| 77 | bool HandleImporter::importBufferInternal(const sp<M> mapper, buffer_handle_t& handle) { |
| 78 | E error; |
| 79 | buffer_handle_t importedHandle; |
| 80 | auto ret = mapper->importBuffer( |
| 81 | hidl_handle(handle), |
| 82 | [&](const auto& tmpError, const auto& tmpBufferHandle) { |
| 83 | error = tmpError; |
| 84 | importedHandle = static_cast<buffer_handle_t>(tmpBufferHandle); |
| 85 | }); |
| 86 | |
| 87 | if (!ret.isOk()) { |
| 88 | ALOGE("%s: mapper importBuffer failed: %s", |
| 89 | __FUNCTION__, ret.description().c_str()); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (error != E::NONE) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | handle = importedHandle; |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | template<class M, class E> |
| 102 | YCbCrLayout HandleImporter::lockYCbCrInternal(const sp<M> mapper, buffer_handle_t& buf, |
| 103 | uint64_t cpuUsage, const IMapper::Rect& accessRegion) { |
| 104 | hidl_handle acquireFenceHandle; |
| 105 | auto buffer = const_cast<native_handle_t*>(buf); |
| 106 | YCbCrLayout layout = {}; |
| 107 | |
| 108 | typename M::Rect accessRegionCopy = {accessRegion.left, accessRegion.top, |
| 109 | accessRegion.width, accessRegion.height}; |
| 110 | mapper->lockYCbCr(buffer, cpuUsage, accessRegionCopy, acquireFenceHandle, |
| 111 | [&](const auto& tmpError, const auto& tmpLayout) { |
| 112 | if (tmpError == E::NONE) { |
| 113 | // Member by member copy from different versions of YCbCrLayout. |
| 114 | layout.y = tmpLayout.y; |
| 115 | layout.cb = tmpLayout.cb; |
| 116 | layout.cr = tmpLayout.cr; |
| 117 | layout.yStride = tmpLayout.yStride; |
| 118 | layout.cStride = tmpLayout.cStride; |
| 119 | layout.chromaStep = tmpLayout.chromaStep; |
| 120 | } else { |
| 121 | ALOGE("%s: failed to lockYCbCr error %d!", __FUNCTION__, tmpError); |
| 122 | } |
| 123 | }); |
| 124 | return layout; |
| 125 | } |
| 126 | |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame^] | 127 | bool isMetadataPesent(const sp<IMapperV4> mapper, const buffer_handle_t& buf, |
| 128 | MetadataType metadataType) { |
| 129 | auto buffer = const_cast<native_handle_t*>(buf); |
| 130 | mapper->get(buffer, metadataType, [] (const auto& tmpError, |
| 131 | const auto& tmpMetadata) { |
| 132 | if (tmpError == MapperErrorV4::NONE) { |
| 133 | return tmpMetadata.size() > 0; |
| 134 | } else { |
| 135 | ALOGE("%s: failed to get metadata %d!", __FUNCTION__, tmpError); |
| 136 | return false; |
| 137 | }}); |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
Emilian Peev | 6a2572b | 2021-05-24 16:51:17 -0700 | [diff] [blame] | 142 | std::vector<PlaneLayout> getPlaneLayouts(const sp<IMapperV4> mapper, buffer_handle_t& buf) { |
| 143 | auto buffer = const_cast<native_handle_t*>(buf); |
| 144 | std::vector<PlaneLayout> planeLayouts; |
| 145 | hidl_vec<uint8_t> encodedPlaneLayouts; |
| 146 | mapper->get(buffer, gralloc4::MetadataType_PlaneLayouts, |
| 147 | [&](const auto& tmpError, const auto& tmpEncodedPlaneLayouts) { |
| 148 | if (tmpError == MapperErrorV4::NONE) { |
| 149 | encodedPlaneLayouts = tmpEncodedPlaneLayouts; |
| 150 | } else { |
| 151 | ALOGE("%s: failed to get plane layouts %d!", __FUNCTION__, tmpError); |
| 152 | } |
| 153 | }); |
| 154 | |
| 155 | gralloc4::decodePlaneLayouts(encodedPlaneLayouts, &planeLayouts); |
| 156 | |
| 157 | return planeLayouts; |
| 158 | } |
| 159 | |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 160 | template <> |
| 161 | YCbCrLayout HandleImporter::lockYCbCrInternal<IMapperV4, MapperErrorV4>( |
| 162 | const sp<IMapperV4> mapper, buffer_handle_t& buf, uint64_t cpuUsage, |
| 163 | const IMapper::Rect& accessRegion) { |
| 164 | hidl_handle acquireFenceHandle; |
| 165 | auto buffer = const_cast<native_handle_t*>(buf); |
| 166 | YCbCrLayout layout = {}; |
| 167 | void* mapped = nullptr; |
| 168 | |
| 169 | typename IMapperV4::Rect accessRegionV4 = {accessRegion.left, accessRegion.top, |
| 170 | accessRegion.width, accessRegion.height}; |
| 171 | mapper->lock(buffer, cpuUsage, accessRegionV4, acquireFenceHandle, |
| 172 | [&](const auto& tmpError, const auto& tmpPtr) { |
| 173 | if (tmpError == MapperErrorV4::NONE) { |
| 174 | mapped = tmpPtr; |
| 175 | } else { |
| 176 | ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError); |
| 177 | } |
| 178 | }); |
| 179 | |
| 180 | if (mapped == nullptr) { |
| 181 | return layout; |
| 182 | } |
| 183 | |
Emilian Peev | 6a2572b | 2021-05-24 16:51:17 -0700 | [diff] [blame] | 184 | std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(mapper, buf); |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 185 | for (const auto& planeLayout : planeLayouts) { |
| 186 | for (const auto& planeLayoutComponent : planeLayout.components) { |
| 187 | const auto& type = planeLayoutComponent.type; |
| 188 | |
| 189 | if (!gralloc4::isStandardPlaneLayoutComponentType(type)) { |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | uint8_t* data = reinterpret_cast<uint8_t*>(mapped); |
| 194 | data += planeLayout.offsetInBytes; |
| 195 | data += planeLayoutComponent.offsetInBits / 8; |
| 196 | |
| 197 | switch (static_cast<PlaneLayoutComponentType>(type.value)) { |
| 198 | case PlaneLayoutComponentType::Y: |
| 199 | layout.y = data; |
| 200 | layout.yStride = planeLayout.strideInBytes; |
| 201 | break; |
| 202 | case PlaneLayoutComponentType::CB: |
| 203 | layout.cb = data; |
| 204 | layout.cStride = planeLayout.strideInBytes; |
| 205 | layout.chromaStep = planeLayout.sampleIncrementInBits / 8; |
| 206 | break; |
| 207 | case PlaneLayoutComponentType::CR: |
| 208 | layout.cr = data; |
| 209 | layout.cStride = planeLayout.strideInBytes; |
| 210 | layout.chromaStep = planeLayout.sampleIncrementInBits / 8; |
| 211 | break; |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return layout; |
| 219 | } |
| 220 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 221 | template<class M, class E> |
| 222 | int HandleImporter::unlockInternal(const sp<M> mapper, buffer_handle_t& buf) { |
| 223 | int releaseFence = -1; |
| 224 | auto buffer = const_cast<native_handle_t*>(buf); |
| 225 | |
| 226 | mapper->unlock( |
| 227 | buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) { |
| 228 | if (tmpError == E::NONE) { |
| 229 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 230 | if (fenceHandle) { |
| 231 | if (fenceHandle->numInts != 0 || fenceHandle->numFds != 1) { |
| 232 | ALOGE("%s: bad release fence numInts %d numFds %d", |
| 233 | __FUNCTION__, fenceHandle->numInts, fenceHandle->numFds); |
| 234 | return; |
| 235 | } |
| 236 | releaseFence = dup(fenceHandle->data[0]); |
| 237 | if (releaseFence < 0) { |
| 238 | ALOGE("%s: bad release fence FD %d", |
| 239 | __FUNCTION__, releaseFence); |
| 240 | } |
| 241 | } |
| 242 | } else { |
| 243 | ALOGE("%s: failed to unlock error %d!", __FUNCTION__, tmpError); |
| 244 | } |
| 245 | }); |
| 246 | return releaseFence; |
| 247 | } |
| 248 | |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 249 | // In IComposer, any buffer_handle_t is owned by the caller and we need to |
| 250 | // make a clone for hwcomposer2. We also need to translate empty handle |
| 251 | // to nullptr. This function does that, in-place. |
| 252 | bool HandleImporter::importBuffer(buffer_handle_t& handle) { |
| 253 | if (!handle->numFds && !handle->numInts) { |
| 254 | handle = nullptr; |
| 255 | return true; |
| 256 | } |
| 257 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 258 | Mutex::Autolock lock(mLock); |
| 259 | if (!mInitialized) { |
| 260 | initializeLocked(); |
| 261 | } |
| 262 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 263 | if (mMapperV4 != nullptr) { |
| 264 | return importBufferInternal<IMapperV4, MapperErrorV4>(mMapperV4, handle); |
| 265 | } |
| 266 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 267 | if (mMapperV3 != nullptr) { |
| 268 | return importBufferInternal<IMapperV3, MapperErrorV3>(mMapperV3, handle); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 271 | if (mMapperV2 != nullptr) { |
| 272 | return importBufferInternal<IMapper, MapperErrorV2>(mMapperV2, handle); |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 275 | ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 276 | return false; |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void HandleImporter::freeBuffer(buffer_handle_t handle) { |
| 280 | if (!handle) { |
| 281 | return; |
| 282 | } |
| 283 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 284 | Mutex::Autolock lock(mLock); |
Yin-Chia Yeh | 97978fb | 2020-01-25 18:15:00 -0800 | [diff] [blame] | 285 | if (!mInitialized) { |
| 286 | initializeLocked(); |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 289 | if (mMapperV4 != nullptr) { |
| 290 | auto ret = mMapperV4->freeBuffer(const_cast<native_handle_t*>(handle)); |
| 291 | if (!ret.isOk()) { |
| 292 | ALOGE("%s: mapper freeBuffer failed: %s", __FUNCTION__, ret.description().c_str()); |
| 293 | } |
| 294 | } else if (mMapperV3 != nullptr) { |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 295 | auto ret = mMapperV3->freeBuffer(const_cast<native_handle_t*>(handle)); |
| 296 | if (!ret.isOk()) { |
| 297 | ALOGE("%s: mapper freeBuffer failed: %s", |
| 298 | __FUNCTION__, ret.description().c_str()); |
| 299 | } |
| 300 | } else { |
| 301 | auto ret = mMapperV2->freeBuffer(const_cast<native_handle_t*>(handle)); |
| 302 | if (!ret.isOk()) { |
| 303 | ALOGE("%s: mapper freeBuffer failed: %s", |
| 304 | __FUNCTION__, ret.description().c_str()); |
| 305 | } |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 306 | } |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 309 | bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 310 | if (handle == nullptr || handle->numFds == 0) { |
| 311 | fd = -1; |
| 312 | } else if (handle->numFds == 1) { |
| 313 | fd = dup(handle->data[0]); |
| 314 | if (fd < 0) { |
| 315 | ALOGE("failed to dup fence fd %d", handle->data[0]); |
| 316 | return false; |
| 317 | } |
| 318 | } else { |
| 319 | ALOGE("invalid fence handle with %d file descriptors", |
| 320 | handle->numFds); |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 327 | void HandleImporter::closeFence(int fd) const { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 328 | if (fd >= 0) { |
| 329 | close(fd); |
| 330 | } |
| 331 | } |
| 332 | |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 333 | void* HandleImporter::lock( |
| 334 | buffer_handle_t& buf, uint64_t cpuUsage, size_t size) { |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 335 | IMapper::Rect accessRegion{0, 0, static_cast<int>(size), 1}; |
| 336 | return lock(buf, cpuUsage, accessRegion); |
| 337 | } |
| 338 | |
| 339 | void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage, |
| 340 | const IMapper::Rect& accessRegion) { |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 341 | Mutex::Autolock lock(mLock); |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 342 | |
| 343 | if (!mInitialized) { |
| 344 | initializeLocked(); |
| 345 | } |
| 346 | |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 347 | void* ret = nullptr; |
| 348 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 349 | if (mMapperV4 == nullptr && mMapperV3 == nullptr && mMapperV2 == nullptr) { |
| 350 | ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__); |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | hidl_handle acquireFenceHandle; |
| 355 | auto buffer = const_cast<native_handle_t*>(buf); |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 356 | if (mMapperV4 != nullptr) { |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 357 | IMapperV4::Rect accessRegionV4{accessRegion.left, accessRegion.top, accessRegion.width, |
| 358 | accessRegion.height}; |
| 359 | |
| 360 | mMapperV4->lock(buffer, cpuUsage, accessRegionV4, acquireFenceHandle, |
Marissa Wall | 9c5ebfc | 2019-11-05 14:59:27 -0800 | [diff] [blame] | 361 | [&](const auto& tmpError, const auto& tmpPtr) { |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 362 | if (tmpError == MapperErrorV4::NONE) { |
| 363 | ret = tmpPtr; |
| 364 | } else { |
| 365 | ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError); |
| 366 | } |
| 367 | }); |
| 368 | } else if (mMapperV3 != nullptr) { |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 369 | IMapperV3::Rect accessRegionV3{accessRegion.left, accessRegion.top, accessRegion.width, |
| 370 | accessRegion.height}; |
| 371 | |
| 372 | mMapperV3->lock(buffer, cpuUsage, accessRegionV3, acquireFenceHandle, |
| 373 | [&](const auto& tmpError, const auto& tmpPtr, const auto& /*bytesPerPixel*/, |
| 374 | const auto& /*bytesPerStride*/) { |
| 375 | if (tmpError == MapperErrorV3::NONE) { |
| 376 | ret = tmpPtr; |
| 377 | } else { |
| 378 | ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError); |
| 379 | } |
| 380 | }); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 381 | } else { |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 382 | mMapperV2->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle, |
| 383 | [&](const auto& tmpError, const auto& tmpPtr) { |
| 384 | if (tmpError == MapperErrorV2::NONE) { |
| 385 | ret = tmpPtr; |
| 386 | } else { |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 387 | ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 388 | } |
| 389 | }); |
| 390 | } |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 391 | |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 392 | ALOGV("%s: ptr %p accessRegion.top: %d accessRegion.left: %d accessRegion.width: %d " |
| 393 | "accessRegion.height: %d", |
| 394 | __FUNCTION__, ret, accessRegion.top, accessRegion.left, accessRegion.width, |
| 395 | accessRegion.height); |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 399 | YCbCrLayout HandleImporter::lockYCbCr( |
| 400 | buffer_handle_t& buf, uint64_t cpuUsage, |
| 401 | const IMapper::Rect& accessRegion) { |
| 402 | Mutex::Autolock lock(mLock); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 403 | |
| 404 | if (!mInitialized) { |
| 405 | initializeLocked(); |
| 406 | } |
| 407 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 408 | if (mMapperV4 != nullptr) { |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 409 | return lockYCbCrInternal<IMapperV4, MapperErrorV4>(mMapperV4, buf, cpuUsage, accessRegion); |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 412 | if (mMapperV3 != nullptr) { |
| 413 | return lockYCbCrInternal<IMapperV3, MapperErrorV3>( |
| 414 | mMapperV3, buf, cpuUsage, accessRegion); |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 417 | if (mMapperV2 != nullptr) { |
| 418 | return lockYCbCrInternal<IMapper, MapperErrorV2>( |
| 419 | mMapperV2, buf, cpuUsage, accessRegion); |
| 420 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 421 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 422 | ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 423 | return {}; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Emilian Peev | 6a2572b | 2021-05-24 16:51:17 -0700 | [diff] [blame] | 426 | status_t HandleImporter::getMonoPlanarStrideBytes(buffer_handle_t &buf, uint32_t *stride /*out*/) { |
| 427 | if (stride == nullptr) { |
| 428 | return BAD_VALUE; |
| 429 | } |
| 430 | |
| 431 | Mutex::Autolock lock(mLock); |
| 432 | |
| 433 | if (!mInitialized) { |
| 434 | initializeLocked(); |
| 435 | } |
| 436 | |
| 437 | if (mMapperV4 != nullptr) { |
| 438 | std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(mMapperV4, buf); |
| 439 | if (planeLayouts.size() != 1) { |
| 440 | ALOGE("%s: Unexpected number of planes %zu!", __FUNCTION__, planeLayouts.size()); |
| 441 | return BAD_VALUE; |
| 442 | } |
| 443 | |
| 444 | *stride = planeLayouts[0].strideInBytes; |
| 445 | } else { |
| 446 | ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__); |
| 447 | return NO_INIT; |
| 448 | } |
| 449 | |
| 450 | return OK; |
| 451 | } |
| 452 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 453 | int HandleImporter::unlock(buffer_handle_t& buf) { |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 454 | if (mMapperV4 != nullptr) { |
| 455 | return unlockInternal<IMapperV4, MapperErrorV4>(mMapperV4, buf); |
| 456 | } |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 457 | if (mMapperV3 != nullptr) { |
| 458 | return unlockInternal<IMapperV3, MapperErrorV3>(mMapperV3, buf); |
| 459 | } |
| 460 | if (mMapperV2 != nullptr) { |
| 461 | return unlockInternal<IMapper, MapperErrorV2>(mMapperV2, buf); |
| 462 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 463 | |
Marissa Wall | a51eb93 | 2019-06-21 09:13:35 -0700 | [diff] [blame] | 464 | ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 465 | return -1; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame^] | 468 | bool HandleImporter::isSmpte2086Present(const buffer_handle_t& buf) { |
| 469 | Mutex::Autolock lock(mLock); |
| 470 | |
| 471 | if (!mInitialized) { |
| 472 | initializeLocked(); |
| 473 | } |
| 474 | |
| 475 | if (mMapperV4 != nullptr) { |
| 476 | return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2086); |
| 477 | } else { |
| 478 | ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__); |
| 479 | } |
| 480 | |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | bool HandleImporter::isSmpte2094_10Present(const buffer_handle_t& buf) { |
| 485 | Mutex::Autolock lock(mLock); |
| 486 | |
| 487 | if (!mInitialized) { |
| 488 | initializeLocked(); |
| 489 | } |
| 490 | |
| 491 | if (mMapperV4 != nullptr) { |
| 492 | return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2094_10); |
| 493 | } else { |
| 494 | ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__); |
| 495 | } |
| 496 | |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | bool HandleImporter::isSmpte2094_40Present(const buffer_handle_t& buf) { |
| 501 | Mutex::Autolock lock(mLock); |
| 502 | |
| 503 | if (!mInitialized) { |
| 504 | initializeLocked(); |
| 505 | } |
| 506 | |
| 507 | if (mMapperV4 != nullptr) { |
| 508 | return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2094_40); |
| 509 | } else { |
| 510 | ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__); |
| 511 | } |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 517 | } // namespace helper |
| 518 | } // namespace V1_0 |
| 519 | } // namespace common |
| 520 | } // namespace camera |
| 521 | } // namespace hardware |
| 522 | } // namespace android |