blob: 9c579e59a1001e1a61566a5fce1bc6269907f885 [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
Devin Moore5e154092023-09-13 16:18:30 +000020#include <aidl/android/hardware/graphics/common/Smpte2086.h>
Jason Macnakeda6dca2020-04-15 15:20:59 -070021#include <gralloctypes/Gralloc4.h>
Steven Moreland4e7a3072017-04-06 12:15:23 -070022#include <log/log.h>
Devin Moore5e154092023-09-13 16:18:30 +000023#include <ui/GraphicBufferMapper.h>
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080024
25namespace android {
26namespace hardware {
27namespace camera {
28namespace common {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080029namespace helper {
30
Jason Macnakeda6dca2020-04-15 15:20:59 -070031using aidl::android::hardware::graphics::common::PlaneLayout;
32using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
33using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
Emilian Peevdda1eb72022-07-28 16:37:40 -070034using aidl::android::hardware::graphics::common::Smpte2086;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080035
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070036HandleImporter::HandleImporter() : mInitialized(false) {}
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080037
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070038void HandleImporter::initializeLocked() {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080039 if (mInitialized) {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080040 return;
41 }
42
Devin Moore5e154092023-09-13 16:18:30 +000043 GraphicBufferMapper::preloadHal();
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070044 mInitialized = true;
45 return;
46}
47
48void HandleImporter::cleanup() {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080049 mInitialized = false;
50}
51
Devin Moore5e154092023-09-13 16:18:30 +000052bool HandleImporter::importBufferInternal(buffer_handle_t& handle) {
Shuzhen Wang915115e2019-05-10 12:07:14 -070053 buffer_handle_t importedHandle;
Devin Moore5e154092023-09-13 16:18:30 +000054 auto status = GraphicBufferMapper::get().importBufferNoValidate(handle, &importedHandle);
55 if (status != OK) {
56 ALOGE("%s: mapper importBuffer failed: %d", __FUNCTION__, status);
Shuzhen Wang915115e2019-05-10 12:07:14 -070057 return false;
58 }
59
60 handle = importedHandle;
61 return true;
62}
63
Devin Moore5e154092023-09-13 16:18:30 +000064android_ycbcr HandleImporter::lockYCbCr(buffer_handle_t& buf, uint64_t cpuUsage,
65 const android::Rect& accessRegion) {
66 Mutex::Autolock lock(mLock);
Shuzhen Wang915115e2019-05-10 12:07:14 -070067
Devin Moore5e154092023-09-13 16:18:30 +000068 if (!mInitialized) {
69 initializeLocked();
70 }
71 android_ycbcr layout;
72
73 status_t status = GraphicBufferMapper::get().lockYCbCr(buf, cpuUsage, accessRegion, &layout);
74
75 if (status != OK) {
76 ALOGE("%s: failed to lockYCbCr error %d!", __FUNCTION__, status);
77 }
78
Shuzhen Wang915115e2019-05-10 12:07:14 -070079 return layout;
80}
81
Devin Moore5e154092023-09-13 16:18:30 +000082std::vector<PlaneLayout> getPlaneLayouts(buffer_handle_t& buf) {
Emilian Peev6a2572b2021-05-24 16:51:17 -070083 std::vector<PlaneLayout> planeLayouts;
Devin Moore5e154092023-09-13 16:18:30 +000084 status_t status = GraphicBufferMapper::get().getPlaneLayouts(buf, &planeLayouts);
85 if (status != OK) {
86 ALOGE("%s: failed to get PlaneLayouts! Status %d", __FUNCTION__, status);
87 }
Emilian Peev6a2572b2021-05-24 16:51:17 -070088
89 return planeLayouts;
90}
91
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080092// In IComposer, any buffer_handle_t is owned by the caller and we need to
93// make a clone for hwcomposer2. We also need to translate empty handle
94// to nullptr. This function does that, in-place.
95bool HandleImporter::importBuffer(buffer_handle_t& handle) {
96 if (!handle->numFds && !handle->numInts) {
97 handle = nullptr;
98 return true;
99 }
100
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700101 Mutex::Autolock lock(mLock);
102 if (!mInitialized) {
103 initializeLocked();
104 }
105
Devin Moore5e154092023-09-13 16:18:30 +0000106 return importBufferInternal(handle);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800107}
108
109void HandleImporter::freeBuffer(buffer_handle_t handle) {
110 if (!handle) {
111 return;
112 }
113
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700114 Mutex::Autolock lock(mLock);
Yin-Chia Yeh97978fb2020-01-25 18:15:00 -0800115 if (!mInitialized) {
116 initializeLocked();
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700117 }
118
Devin Moore5e154092023-09-13 16:18:30 +0000119 status_t status = GraphicBufferMapper::get().freeBuffer(handle);
120 if (status != OK) {
121 ALOGE("%s: mapper freeBuffer failed. Status %d", __FUNCTION__, status);
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700122 }
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800123}
124
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700125bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800126 if (handle == nullptr || handle->numFds == 0) {
127 fd = -1;
128 } else if (handle->numFds == 1) {
129 fd = dup(handle->data[0]);
130 if (fd < 0) {
131 ALOGE("failed to dup fence fd %d", handle->data[0]);
132 return false;
133 }
134 } else {
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700135 ALOGE("invalid fence handle with %d file descriptors", handle->numFds);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800136 return false;
137 }
138
139 return true;
140}
141
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700142void HandleImporter::closeFence(int fd) const {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800143 if (fd >= 0) {
144 close(fd);
145 }
146}
147
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700148void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage, size_t size) {
Devin Moore5e154092023-09-13 16:18:30 +0000149 android::Rect accessRegion{0, 0, static_cast<int>(size), 1};
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700150 return lock(buf, cpuUsage, accessRegion);
151}
152
153void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage,
Devin Moore5e154092023-09-13 16:18:30 +0000154 const android::Rect& accessRegion) {
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800155 Mutex::Autolock lock(mLock);
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800156
157 if (!mInitialized) {
158 initializeLocked();
159 }
160
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700161 void* ret = nullptr;
Devin Moore5e154092023-09-13 16:18:30 +0000162 status_t status = GraphicBufferMapper::get().lock(buf, cpuUsage, accessRegion, &ret);
163 if (status != OK) {
164 ALOGE("%s: failed to lock error %d!", __FUNCTION__, status);
Shuzhen Wang915115e2019-05-10 12:07:14 -0700165 }
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800166
Jason Macnakf2c9ed12020-04-20 14:43:58 -0700167 ALOGV("%s: ptr %p accessRegion.top: %d accessRegion.left: %d accessRegion.width: %d "
168 "accessRegion.height: %d",
Devin Moore5e154092023-09-13 16:18:30 +0000169 __FUNCTION__, ret, accessRegion.top, accessRegion.left, accessRegion.width(),
170 accessRegion.height());
Yuriy Romanenkod0bd4f12018-01-19 16:00:00 -0800171 return ret;
172}
173
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700174status_t HandleImporter::getMonoPlanarStrideBytes(buffer_handle_t& buf, uint32_t* stride /*out*/) {
Emilian Peev6a2572b2021-05-24 16:51:17 -0700175 if (stride == nullptr) {
176 return BAD_VALUE;
177 }
178
179 Mutex::Autolock lock(mLock);
180
181 if (!mInitialized) {
182 initializeLocked();
183 }
184
Devin Moore5e154092023-09-13 16:18:30 +0000185 std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(buf);
186 if (planeLayouts.size() != 1) {
187 ALOGE("%s: Unexpected number of planes %zu!", __FUNCTION__, planeLayouts.size());
188 return BAD_VALUE;
Emilian Peev6a2572b2021-05-24 16:51:17 -0700189 }
190
Devin Moore5e154092023-09-13 16:18:30 +0000191 *stride = planeLayouts[0].strideInBytes;
192
Emilian Peev6a2572b2021-05-24 16:51:17 -0700193 return OK;
194}
195
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700196int HandleImporter::unlock(buffer_handle_t& buf) {
Devin Moore5e154092023-09-13 16:18:30 +0000197 int releaseFence = -1;
198
199 status_t status = GraphicBufferMapper::get().unlockAsync(buf, &releaseFence);
200 if (status != OK) {
201 ALOGE("%s: failed to unlock error %d!", __FUNCTION__, status);
Shuzhen Wang915115e2019-05-10 12:07:14 -0700202 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700203
Devin Moore5e154092023-09-13 16:18:30 +0000204 return releaseFence;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700205}
206
Emilian Peevb5f634f2021-12-13 15:13:46 -0800207bool HandleImporter::isSmpte2086Present(const buffer_handle_t& buf) {
208 Mutex::Autolock lock(mLock);
209
210 if (!mInitialized) {
211 initializeLocked();
212 }
Devin Moore5e154092023-09-13 16:18:30 +0000213 std::optional<ui::Smpte2086> metadata;
214 status_t status = GraphicBufferMapper::get().getSmpte2086(buf, &metadata);
215 if (status != OK) {
216 ALOGE("%s: Mapper failed to get Smpte2094_40 metadata! Status: %d", __FUNCTION__, status);
217 return false;
Emilian Peevb5f634f2021-12-13 15:13:46 -0800218 }
219
Devin Moore5e154092023-09-13 16:18:30 +0000220 return metadata.has_value();
Emilian Peevb5f634f2021-12-13 15:13:46 -0800221}
222
223bool HandleImporter::isSmpte2094_10Present(const buffer_handle_t& buf) {
224 Mutex::Autolock lock(mLock);
225
226 if (!mInitialized) {
227 initializeLocked();
228 }
229
Devin Moore5e154092023-09-13 16:18:30 +0000230 std::optional<std::vector<uint8_t>> metadata;
231 status_t status = GraphicBufferMapper::get().getSmpte2094_10(buf, &metadata);
232 if (status != OK) {
233 ALOGE("%s: Mapper failed to get Smpte2094_40 metadata! Status: %d", __FUNCTION__, status);
234 return false;
Emilian Peevb5f634f2021-12-13 15:13:46 -0800235 }
236
Devin Moore5e154092023-09-13 16:18:30 +0000237 return metadata.has_value();
Emilian Peevb5f634f2021-12-13 15:13:46 -0800238}
239
240bool HandleImporter::isSmpte2094_40Present(const buffer_handle_t& buf) {
241 Mutex::Autolock lock(mLock);
242
243 if (!mInitialized) {
244 initializeLocked();
245 }
246
Devin Moore5e154092023-09-13 16:18:30 +0000247 std::optional<std::vector<uint8_t>> metadata;
248 status_t status = GraphicBufferMapper::get().getSmpte2094_40(buf, &metadata);
249 if (status != OK) {
250 ALOGE("%s: Mapper failed to get Smpte2094_40 metadata! Status: %d", __FUNCTION__, status);
251 return false;
Emilian Peevb5f634f2021-12-13 15:13:46 -0800252 }
253
Devin Moore5e154092023-09-13 16:18:30 +0000254 return metadata.has_value();
Emilian Peevb5f634f2021-12-13 15:13:46 -0800255}
256
Avichal Rakesh0d2d8a42022-06-14 17:23:40 -0700257} // namespace helper
258} // namespace common
259} // namespace camera
260} // namespace hardware
261} // namespace android