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"
18 #include "HandleImporter.h"
19
20 #include <aidl/android/hardware/graphics/common/Smpte2086.h>
21 #include <gralloctypes/Gralloc4.h>
22 #include <log/log.h>
23 #include <ui/GraphicBufferMapper.h>
24
25 namespace android {
26 namespace hardware {
27 namespace camera {
28 namespace common {
29 namespace helper {
30
31 using aidl::android::hardware::graphics::common::PlaneLayout;
32 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
33 using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
34 using aidl::android::hardware::graphics::common::Smpte2086;
35
HandleImporter()36 HandleImporter::HandleImporter() : mInitialized(false) {}
37
initializeLocked()38 void HandleImporter::initializeLocked() {
39 if (mInitialized) {
40 return;
41 }
42
43 GraphicBufferMapper::preloadHal();
44 mInitialized = true;
45 return;
46 }
47
cleanup()48 void HandleImporter::cleanup() {
49 mInitialized = false;
50 }
51
importBufferInternal(buffer_handle_t & handle)52 bool HandleImporter::importBufferInternal(buffer_handle_t& handle) {
53 buffer_handle_t importedHandle;
54 auto status = GraphicBufferMapper::get().importBufferNoValidate(handle, &importedHandle);
55 if (status != OK) {
56 ALOGE("%s: mapper importBuffer failed: %d", __FUNCTION__, status);
57 return false;
58 }
59
60 handle = importedHandle;
61 return true;
62 }
63
lockYCbCr(buffer_handle_t & buf,uint64_t cpuUsage,const android::Rect & accessRegion)64 android_ycbcr HandleImporter::lockYCbCr(buffer_handle_t& buf, uint64_t cpuUsage,
65 const android::Rect& accessRegion) {
66 Mutex::Autolock lock(mLock);
67
68 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
79 return layout;
80 }
81
getPlaneLayouts(buffer_handle_t & buf)82 std::vector<PlaneLayout> getPlaneLayouts(buffer_handle_t& buf) {
83 std::vector<PlaneLayout> planeLayouts;
84 status_t status = GraphicBufferMapper::get().getPlaneLayouts(buf, &planeLayouts);
85 if (status != OK) {
86 ALOGE("%s: failed to get PlaneLayouts! Status %d", __FUNCTION__, status);
87 }
88
89 return planeLayouts;
90 }
91
92 // 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.
importBuffer(buffer_handle_t & handle)95 bool HandleImporter::importBuffer(buffer_handle_t& handle) {
96 if (!handle->numFds && !handle->numInts) {
97 handle = nullptr;
98 return true;
99 }
100
101 Mutex::Autolock lock(mLock);
102 if (!mInitialized) {
103 initializeLocked();
104 }
105
106 return importBufferInternal(handle);
107 }
108
freeBuffer(buffer_handle_t handle)109 void HandleImporter::freeBuffer(buffer_handle_t handle) {
110 if (!handle) {
111 return;
112 }
113
114 Mutex::Autolock lock(mLock);
115 if (!mInitialized) {
116 initializeLocked();
117 }
118
119 status_t status = GraphicBufferMapper::get().freeBuffer(handle);
120 if (status != OK) {
121 ALOGE("%s: mapper freeBuffer failed. Status %d", __FUNCTION__, status);
122 }
123 }
124
importFence(const native_handle_t * handle,int & fd) const125 bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const {
126 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 {
135 ALOGE("invalid fence handle with %d file descriptors", handle->numFds);
136 return false;
137 }
138
139 return true;
140 }
141
closeFence(int fd) const142 void HandleImporter::closeFence(int fd) const {
143 if (fd >= 0) {
144 close(fd);
145 }
146 }
147
lock(buffer_handle_t & buf,uint64_t cpuUsage,size_t size)148 void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage, size_t size) {
149 android::Rect accessRegion{0, 0, static_cast<int>(size), 1};
150 return lock(buf, cpuUsage, accessRegion);
151 }
152
lock(buffer_handle_t & buf,uint64_t cpuUsage,const android::Rect & accessRegion)153 void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage,
154 const android::Rect& accessRegion) {
155 Mutex::Autolock lock(mLock);
156
157 if (!mInitialized) {
158 initializeLocked();
159 }
160
161 void* ret = nullptr;
162 status_t status = GraphicBufferMapper::get().lock(buf, cpuUsage, accessRegion, &ret);
163 if (status != OK) {
164 ALOGE("%s: failed to lock error %d!", __FUNCTION__, status);
165 }
166
167 ALOGV("%s: ptr %p accessRegion.top: %d accessRegion.left: %d accessRegion.width: %d "
168 "accessRegion.height: %d",
169 __FUNCTION__, ret, accessRegion.top, accessRegion.left, accessRegion.width(),
170 accessRegion.height());
171 return ret;
172 }
173
getMonoPlanarStrideBytes(buffer_handle_t & buf,uint32_t * stride)174 status_t HandleImporter::getMonoPlanarStrideBytes(buffer_handle_t& buf, uint32_t* stride /*out*/) {
175 if (stride == nullptr) {
176 return BAD_VALUE;
177 }
178
179 Mutex::Autolock lock(mLock);
180
181 if (!mInitialized) {
182 initializeLocked();
183 }
184
185 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;
189 }
190
191 *stride = planeLayouts[0].strideInBytes;
192
193 return OK;
194 }
195
unlock(buffer_handle_t & buf)196 int HandleImporter::unlock(buffer_handle_t& buf) {
197 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);
202 }
203
204 return releaseFence;
205 }
206
isSmpte2086Present(const buffer_handle_t & buf)207 bool HandleImporter::isSmpte2086Present(const buffer_handle_t& buf) {
208 Mutex::Autolock lock(mLock);
209
210 if (!mInitialized) {
211 initializeLocked();
212 }
213 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;
218 }
219
220 return metadata.has_value();
221 }
222
isSmpte2094_10Present(const buffer_handle_t & buf)223 bool HandleImporter::isSmpte2094_10Present(const buffer_handle_t& buf) {
224 Mutex::Autolock lock(mLock);
225
226 if (!mInitialized) {
227 initializeLocked();
228 }
229
230 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;
235 }
236
237 return metadata.has_value();
238 }
239
isSmpte2094_40Present(const buffer_handle_t & buf)240 bool HandleImporter::isSmpte2094_40Present(const buffer_handle_t& buf) {
241 Mutex::Autolock lock(mLock);
242
243 if (!mInitialized) {
244 initializeLocked();
245 }
246
247 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;
252 }
253
254 return metadata.has_value();
255 }
256
257 } // namespace helper
258 } // namespace common
259 } // namespace camera
260 } // namespace hardware
261 } // namespace android
262