blob: 1145baa5f150ddfbc8d4e8b4bcfd883a9b942b6e [file] [log] [blame]
Yin-Chia Yeh248ed702017-01-23 17:27:26 -08001/*
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 Yeh248ed702017-01-23 17:27:26 -080018#include "HandleImporter.h"
Jason Macnakeda6dca2020-04-15 15:20:59 -070019
20#include <gralloctypes/Gralloc4.h>
Steven Moreland4e7a3072017-04-06 12:15:23 -070021#include <log/log.h>
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070022#include "aidl/android/hardware/graphics/common/Smpte2086.h"
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080023
24namespace android {
25namespace hardware {
26namespace camera {
27namespace common {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080028namespace helper {
29
Jason Macnakeda6dca2020-04-15 15:20:59 -070030using aidl::android::hardware::graphics::common::PlaneLayout;
31using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
32using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
Emilian Peevdda1eb72022-07-28 16:37:40 -070033using aidl::android::hardware::graphics::common::Smpte2086;
Emilian Peevb5f634f2021-12-13 15:13:46 -080034using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
Shuzhen Wang915115e2019-05-10 12:07:14 -070035using MapperErrorV2 = android::hardware::graphics::mapper::V2_0::Error;
36using MapperErrorV3 = android::hardware::graphics::mapper::V3_0::Error;
Marissa Walla51eb932019-06-21 09:13:35 -070037using MapperErrorV4 = android::hardware::graphics::mapper::V4_0::Error;
Shuzhen Wang915115e2019-05-10 12:07:14 -070038using IMapperV3 = android::hardware::graphics::mapper::V3_0::IMapper;
Marissa Walla51eb932019-06-21 09:13:35 -070039using IMapperV4 = android::hardware::graphics::mapper::V4_0::IMapper;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080040
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070041HandleImporter::HandleImporter() : mInitialized(false) {}
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080042
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070043void HandleImporter::initializeLocked() {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080044 if (mInitialized) {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080045 return;
46 }
47
Marissa Walla51eb932019-06-21 09:13:35 -070048 mMapperV4 = IMapperV4::getService();
49 if (mMapperV4 != nullptr) {
50 mInitialized = true;
51 return;
52 }
53
Shuzhen Wang915115e2019-05-10 12:07:14 -070054 mMapperV3 = IMapperV3::getService();
55 if (mMapperV3 != nullptr) {
56 mInitialized = true;
57 return;
58 }
59
60 mMapperV2 = IMapper::getService();
61 if (mMapperV2 == nullptr) {
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070062 ALOGE("%s: cannnot acccess graphics mapper HAL!", __FUNCTION__);
63 return;
64 }
65
66 mInitialized = true;
67 return;
68}
69
70void HandleImporter::cleanup() {
Marissa Walla51eb932019-06-21 09:13:35 -070071 mMapperV4.clear();
Shuzhen Wang915115e2019-05-10 12:07:14 -070072 mMapperV3.clear();
73 mMapperV2.clear();
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080074 mInitialized = false;
75}
76
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070077template <class M, class E>
Shuzhen Wang915115e2019-05-10 12:07:14 -070078bool HandleImporter::importBufferInternal(const sp<M> mapper, buffer_handle_t& handle) {
79 E error;
80 buffer_handle_t importedHandle;
81 auto ret = mapper->importBuffer(
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070082 hidl_handle(handle), [&](const auto& tmpError, const auto& tmpBufferHandle) {
83 error = tmpError;
84 importedHandle = static_cast<buffer_handle_t>(tmpBufferHandle);
85 });
Shuzhen Wang915115e2019-05-10 12:07:14 -070086
87 if (!ret.isOk()) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -070088 ALOGE("%s: mapper importBuffer failed: %s", __FUNCTION__, ret.description().c_str());
Shuzhen Wang915115e2019-05-10 12:07:14 -070089 return false;
90 }
91
92 if (error != E::NONE) {
93 return false;
94 }
95
96 handle = importedHandle;
97 return true;
98}
99
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700100template <class M, class E>
Shuzhen Wang915115e2019-05-10 12:07:14 -0700101YCbCrLayout HandleImporter::lockYCbCrInternal(const sp<M> mapper, buffer_handle_t& buf,
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700102 uint64_t cpuUsage,
103 const IMapper::Rect& accessRegion) {
Shuzhen Wang915115e2019-05-10 12:07:14 -0700104 hidl_handle acquireFenceHandle;
105 auto buffer = const_cast<native_handle_t*>(buf);
106 YCbCrLayout layout = {};
107
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700108 typename M::Rect accessRegionCopy = {accessRegion.left, accessRegion.top, accessRegion.width,
109 accessRegion.height};
Shuzhen Wang915115e2019-05-10 12:07:14 -0700110 mapper->lockYCbCr(buffer, cpuUsage, accessRegionCopy, acquireFenceHandle,
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700111 [&](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 });
Shuzhen Wang915115e2019-05-10 12:07:14 -0700124 return layout;
125}
126
Emilian Peevb5f634f2021-12-13 15:13:46 -0800127bool isMetadataPesent(const sp<IMapperV4> mapper, const buffer_handle_t& buf,
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700128 MetadataType metadataType) {
Emilian Peevb5f634f2021-12-13 15:13:46 -0800129 auto buffer = const_cast<native_handle_t*>(buf);
Emilian Peevdda1eb72022-07-28 16:37:40 -0700130 bool ret = false;
131 hidl_vec<uint8_t> vec;
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700132 mapper->get(buffer, metadataType, [&](const auto& tmpError, const auto& tmpMetadata) {
133 if (tmpError == MapperErrorV4::NONE) {
134 vec = tmpMetadata;
135 } else {
136 ALOGE("%s: failed to get metadata %d!", __FUNCTION__, tmpError);
137 }
138 });
Emilian Peevb5f634f2021-12-13 15:13:46 -0800139
Emilian Peevdda1eb72022-07-28 16:37:40 -0700140 if (vec.size() > 0) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700141 if (metadataType == gralloc4::MetadataType_Smpte2086) {
142 std::optional<Smpte2086> realSmpte2086;
143 gralloc4::decodeSmpte2086(vec, &realSmpte2086);
144 ret = realSmpte2086.has_value();
145 } else if (metadataType == gralloc4::MetadataType_Smpte2094_10) {
146 std::optional<std::vector<uint8_t>> realSmpte2094_10;
147 gralloc4::decodeSmpte2094_10(vec, &realSmpte2094_10);
148 ret = realSmpte2094_10.has_value();
149 } else if (metadataType == gralloc4::MetadataType_Smpte2094_40) {
150 std::optional<std::vector<uint8_t>> realSmpte2094_40;
151 gralloc4::decodeSmpte2094_40(vec, &realSmpte2094_40);
152 ret = realSmpte2094_40.has_value();
153 } else {
154 ALOGE("%s: Unknown metadata type!", __FUNCTION__);
155 }
Emilian Peevdda1eb72022-07-28 16:37:40 -0700156 }
157
158 return ret;
Emilian Peevb5f634f2021-12-13 15:13:46 -0800159}
160
Emilian Peev6a2572b2021-05-24 16:51:17 -0700161std::vector<PlaneLayout> getPlaneLayouts(const sp<IMapperV4> mapper, buffer_handle_t& buf) {
162 auto buffer = const_cast<native_handle_t*>(buf);
163 std::vector<PlaneLayout> planeLayouts;
164 hidl_vec<uint8_t> encodedPlaneLayouts;
165 mapper->get(buffer, gralloc4::MetadataType_PlaneLayouts,
166 [&](const auto& tmpError, const auto& tmpEncodedPlaneLayouts) {
167 if (tmpError == MapperErrorV4::NONE) {
168 encodedPlaneLayouts = tmpEncodedPlaneLayouts;
169 } else {
170 ALOGE("%s: failed to get plane layouts %d!", __FUNCTION__, tmpError);
171 }
172 });
173
174 gralloc4::decodePlaneLayouts(encodedPlaneLayouts, &planeLayouts);
175
176 return planeLayouts;
177}
178
Jason Macnakeda6dca2020-04-15 15:20:59 -0700179template <>
180YCbCrLayout HandleImporter::lockYCbCrInternal<IMapperV4, MapperErrorV4>(
181 const sp<IMapperV4> mapper, buffer_handle_t& buf, uint64_t cpuUsage,
182 const IMapper::Rect& accessRegion) {
183 hidl_handle acquireFenceHandle;
184 auto buffer = const_cast<native_handle_t*>(buf);
185 YCbCrLayout layout = {};
186 void* mapped = nullptr;
187
188 typename IMapperV4::Rect accessRegionV4 = {accessRegion.left, accessRegion.top,
189 accessRegion.width, accessRegion.height};
190 mapper->lock(buffer, cpuUsage, accessRegionV4, acquireFenceHandle,
191 [&](const auto& tmpError, const auto& tmpPtr) {
192 if (tmpError == MapperErrorV4::NONE) {
193 mapped = tmpPtr;
194 } else {
195 ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
196 }
197 });
198
199 if (mapped == nullptr) {
200 return layout;
201 }
202
Emilian Peev6a2572b2021-05-24 16:51:17 -0700203 std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(mapper, buf);
Jason Macnakeda6dca2020-04-15 15:20:59 -0700204 for (const auto& planeLayout : planeLayouts) {
205 for (const auto& planeLayoutComponent : planeLayout.components) {
206 const auto& type = planeLayoutComponent.type;
207
208 if (!gralloc4::isStandardPlaneLayoutComponentType(type)) {
209 continue;
210 }
211
212 uint8_t* data = reinterpret_cast<uint8_t*>(mapped);
213 data += planeLayout.offsetInBytes;
214 data += planeLayoutComponent.offsetInBits / 8;
215
216 switch (static_cast<PlaneLayoutComponentType>(type.value)) {
217 case PlaneLayoutComponentType::Y:
218 layout.y = data;
219 layout.yStride = planeLayout.strideInBytes;
220 break;
221 case PlaneLayoutComponentType::CB:
222 layout.cb = data;
223 layout.cStride = planeLayout.strideInBytes;
224 layout.chromaStep = planeLayout.sampleIncrementInBits / 8;
225 break;
226 case PlaneLayoutComponentType::CR:
227 layout.cr = data;
228 layout.cStride = planeLayout.strideInBytes;
229 layout.chromaStep = planeLayout.sampleIncrementInBits / 8;
230 break;
231 default:
232 break;
233 }
234 }
235 }
236
237 return layout;
238}
239
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700240template <class M, class E>
Shuzhen Wang915115e2019-05-10 12:07:14 -0700241int HandleImporter::unlockInternal(const sp<M> mapper, buffer_handle_t& buf) {
242 int releaseFence = -1;
243 auto buffer = const_cast<native_handle_t*>(buf);
244
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700245 mapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
246 if (tmpError == E::NONE) {
247 auto fenceHandle = tmpReleaseFence.getNativeHandle();
248 if (fenceHandle) {
249 if (fenceHandle->numInts != 0 || fenceHandle->numFds != 1) {
250 ALOGE("%s: bad release fence numInts %d numFds %d", __FUNCTION__,
251 fenceHandle->numInts, fenceHandle->numFds);
252 return;
Shuzhen Wang915115e2019-05-10 12:07:14 -0700253 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700254 releaseFence = dup(fenceHandle->data[0]);
255 if (releaseFence < 0) {
256 ALOGE("%s: bad release fence FD %d", __FUNCTION__, releaseFence);
257 }
Shuzhen Wang915115e2019-05-10 12:07:14 -0700258 }
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700259 } else {
260 ALOGE("%s: failed to unlock error %d!", __FUNCTION__, tmpError);
261 }
262 });
Shuzhen Wang915115e2019-05-10 12:07:14 -0700263 return releaseFence;
264}
265
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800266// In IComposer, any buffer_handle_t is owned by the caller and we need to
267// make a clone for hwcomposer2. We also need to translate empty handle
268// to nullptr. This function does that, in-place.
269bool HandleImporter::importBuffer(buffer_handle_t& handle) {
270 if (!handle->numFds && !handle->numInts) {
271 handle = nullptr;
272 return true;
273 }
274
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700275 Mutex::Autolock lock(mLock);
276 if (!mInitialized) {
277 initializeLocked();
278 }
279
Marissa Walla51eb932019-06-21 09:13:35 -0700280 if (mMapperV4 != nullptr) {
281 return importBufferInternal<IMapperV4, MapperErrorV4>(mMapperV4, handle);
282 }
283
Shuzhen Wang915115e2019-05-10 12:07:14 -0700284 if (mMapperV3 != nullptr) {
285 return importBufferInternal<IMapperV3, MapperErrorV3>(mMapperV3, handle);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800286 }
287
Shuzhen Wang915115e2019-05-10 12:07:14 -0700288 if (mMapperV2 != nullptr) {
289 return importBufferInternal<IMapper, MapperErrorV2>(mMapperV2, handle);
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700290 }
291
Marissa Walla51eb932019-06-21 09:13:35 -0700292 ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
Shuzhen Wang915115e2019-05-10 12:07:14 -0700293 return false;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800294}
295
296void HandleImporter::freeBuffer(buffer_handle_t handle) {
297 if (!handle) {
298 return;
299 }
300
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700301 Mutex::Autolock lock(mLock);
Yin-Chia Yeh97978fb2020-01-25 18:15:00 -0800302 if (!mInitialized) {
303 initializeLocked();
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700304 }
305
Marissa Walla51eb932019-06-21 09:13:35 -0700306 if (mMapperV4 != nullptr) {
307 auto ret = mMapperV4->freeBuffer(const_cast<native_handle_t*>(handle));
308 if (!ret.isOk()) {
309 ALOGE("%s: mapper freeBuffer failed: %s", __FUNCTION__, ret.description().c_str());
310 }
311 } else if (mMapperV3 != nullptr) {
Shuzhen Wang915115e2019-05-10 12:07:14 -0700312 auto ret = mMapperV3->freeBuffer(const_cast<native_handle_t*>(handle));
313 if (!ret.isOk()) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700314 ALOGE("%s: mapper freeBuffer failed: %s", __FUNCTION__, ret.description().c_str());
Shuzhen Wang915115e2019-05-10 12:07:14 -0700315 }
316 } else {
317 auto ret = mMapperV2->freeBuffer(const_cast<native_handle_t*>(handle));
318 if (!ret.isOk()) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700319 ALOGE("%s: mapper freeBuffer failed: %s", __FUNCTION__, ret.description().c_str());
Shuzhen Wang915115e2019-05-10 12:07:14 -0700320 }
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700321 }
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800322}
323
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700324bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800325 if (handle == nullptr || handle->numFds == 0) {
326 fd = -1;
327 } else if (handle->numFds == 1) {
328 fd = dup(handle->data[0]);
329 if (fd < 0) {
330 ALOGE("failed to dup fence fd %d", handle->data[0]);
331 return false;
332 }
333 } else {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700334 ALOGE("invalid fence handle with %d file descriptors", handle->numFds);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800335 return false;
336 }
337
338 return true;
339}
340
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700341void HandleImporter::closeFence(int fd) const {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800342 if (fd >= 0) {
343 close(fd);
344 }
345}
346
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700347void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage, size_t size) {
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700348 IMapper::Rect accessRegion{0, 0, static_cast<int>(size), 1};
349 return lock(buf, cpuUsage, accessRegion);
350}
351
352void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage,
353 const IMapper::Rect& accessRegion) {
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800354 Mutex::Autolock lock(mLock);
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800355
356 if (!mInitialized) {
357 initializeLocked();
358 }
359
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700360 void* ret = nullptr;
361
Marissa Walla51eb932019-06-21 09:13:35 -0700362 if (mMapperV4 == nullptr && mMapperV3 == nullptr && mMapperV2 == nullptr) {
363 ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800364 return ret;
365 }
366
367 hidl_handle acquireFenceHandle;
368 auto buffer = const_cast<native_handle_t*>(buf);
Marissa Walla51eb932019-06-21 09:13:35 -0700369 if (mMapperV4 != nullptr) {
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700370 IMapperV4::Rect accessRegionV4{accessRegion.left, accessRegion.top, accessRegion.width,
371 accessRegion.height};
372
373 mMapperV4->lock(buffer, cpuUsage, accessRegionV4, acquireFenceHandle,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800374 [&](const auto& tmpError, const auto& tmpPtr) {
Marissa Walla51eb932019-06-21 09:13:35 -0700375 if (tmpError == MapperErrorV4::NONE) {
376 ret = tmpPtr;
377 } else {
378 ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
379 }
380 });
381 } else if (mMapperV3 != nullptr) {
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700382 IMapperV3::Rect accessRegionV3{accessRegion.left, accessRegion.top, accessRegion.width,
383 accessRegion.height};
384
385 mMapperV3->lock(buffer, cpuUsage, accessRegionV3, acquireFenceHandle,
386 [&](const auto& tmpError, const auto& tmpPtr, const auto& /*bytesPerPixel*/,
387 const auto& /*bytesPerStride*/) {
388 if (tmpError == MapperErrorV3::NONE) {
389 ret = tmpPtr;
390 } else {
391 ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
392 }
393 });
Shuzhen Wang915115e2019-05-10 12:07:14 -0700394 } else {
Shuzhen Wang915115e2019-05-10 12:07:14 -0700395 mMapperV2->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700396 [&](const auto& tmpError, const auto& tmpPtr) {
397 if (tmpError == MapperErrorV2::NONE) {
398 ret = tmpPtr;
399 } else {
400 ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
401 }
402 });
Shuzhen Wang915115e2019-05-10 12:07:14 -0700403 }
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800404
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700405 ALOGV("%s: ptr %p accessRegion.top: %d accessRegion.left: %d accessRegion.width: %d "
406 "accessRegion.height: %d",
407 __FUNCTION__, ret, accessRegion.top, accessRegion.left, accessRegion.width,
408 accessRegion.height);
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800409 return ret;
410}
411
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700412YCbCrLayout HandleImporter::lockYCbCr(buffer_handle_t& buf, uint64_t cpuUsage,
413 const IMapper::Rect& accessRegion) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700414 Mutex::Autolock lock(mLock);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700415
416 if (!mInitialized) {
417 initializeLocked();
418 }
419
Marissa Walla51eb932019-06-21 09:13:35 -0700420 if (mMapperV4 != nullptr) {
Jason Macnakeda6dca2020-04-15 15:20:59 -0700421 return lockYCbCrInternal<IMapperV4, MapperErrorV4>(mMapperV4, buf, cpuUsage, accessRegion);
Marissa Walla51eb932019-06-21 09:13:35 -0700422 }
423
Shuzhen Wang915115e2019-05-10 12:07:14 -0700424 if (mMapperV3 != nullptr) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700425 return lockYCbCrInternal<IMapperV3, MapperErrorV3>(mMapperV3, buf, cpuUsage, accessRegion);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700426 }
427
Shuzhen Wang915115e2019-05-10 12:07:14 -0700428 if (mMapperV2 != nullptr) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700429 return lockYCbCrInternal<IMapper, MapperErrorV2>(mMapperV2, buf, cpuUsage, accessRegion);
Shuzhen Wang915115e2019-05-10 12:07:14 -0700430 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700431
Marissa Walla51eb932019-06-21 09:13:35 -0700432 ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
Shuzhen Wang915115e2019-05-10 12:07:14 -0700433 return {};
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700434}
435
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700436status_t HandleImporter::getMonoPlanarStrideBytes(buffer_handle_t& buf, uint32_t* stride /*out*/) {
Emilian Peev6a2572b2021-05-24 16:51:17 -0700437 if (stride == nullptr) {
438 return BAD_VALUE;
439 }
440
441 Mutex::Autolock lock(mLock);
442
443 if (!mInitialized) {
444 initializeLocked();
445 }
446
447 if (mMapperV4 != nullptr) {
448 std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(mMapperV4, buf);
449 if (planeLayouts.size() != 1) {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700450 ALOGE("%s: Unexpected number of planes %zu!", __FUNCTION__, planeLayouts.size());
Emilian Peev6a2572b2021-05-24 16:51:17 -0700451 return BAD_VALUE;
452 }
453
454 *stride = planeLayouts[0].strideInBytes;
455 } else {
456 ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
457 return NO_INIT;
458 }
459
460 return OK;
461}
462
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700463int HandleImporter::unlock(buffer_handle_t& buf) {
Marissa Walla51eb932019-06-21 09:13:35 -0700464 if (mMapperV4 != nullptr) {
465 return unlockInternal<IMapperV4, MapperErrorV4>(mMapperV4, buf);
466 }
Shuzhen Wang915115e2019-05-10 12:07:14 -0700467 if (mMapperV3 != nullptr) {
468 return unlockInternal<IMapperV3, MapperErrorV3>(mMapperV3, buf);
469 }
470 if (mMapperV2 != nullptr) {
471 return unlockInternal<IMapper, MapperErrorV2>(mMapperV2, buf);
472 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700473
Marissa Walla51eb932019-06-21 09:13:35 -0700474 ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
Shuzhen Wang915115e2019-05-10 12:07:14 -0700475 return -1;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700476}
477
Emilian Peevb5f634f2021-12-13 15:13:46 -0800478bool HandleImporter::isSmpte2086Present(const buffer_handle_t& buf) {
479 Mutex::Autolock lock(mLock);
480
481 if (!mInitialized) {
482 initializeLocked();
483 }
484
485 if (mMapperV4 != nullptr) {
486 return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2086);
487 } else {
488 ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
489 }
490
491 return false;
492}
493
494bool HandleImporter::isSmpte2094_10Present(const buffer_handle_t& buf) {
495 Mutex::Autolock lock(mLock);
496
497 if (!mInitialized) {
498 initializeLocked();
499 }
500
501 if (mMapperV4 != nullptr) {
502 return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2094_10);
503 } else {
504 ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
505 }
506
507 return false;
508}
509
510bool HandleImporter::isSmpte2094_40Present(const buffer_handle_t& buf) {
511 Mutex::Autolock lock(mLock);
512
513 if (!mInitialized) {
514 initializeLocked();
515 }
516
517 if (mMapperV4 != nullptr) {
518 return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2094_40);
519 } else {
520 ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
521 }
522
523 return false;
524}
525
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700526} // namespace helper
527} // namespace common
528} // namespace camera
529} // namespace hardware
530} // namespace android