blob: 2f4d5fbc778980945bc85065fb72d687749b469b [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
2 * Copyright (C) 2007 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
Mathias Agopian3330b202009-10-05 17:07:12 -070017#define LOG_TAG "GraphicBufferMapper"
Mathias Agopiancf563192012-02-29 20:43:29 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Dan Stoza8deb4da2016-06-01 18:21:44 -070019//#define LOG_NDEBUG 0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070020
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080021#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022
Chia-I Wu5bac7f32017-04-06 12:34:32 -070023#include <grallocusage/GrallocUsageConversion.h>
24
Dan Stozad3182402014-11-17 12:03:59 -080025// We would eliminate the non-conforming zero-length array, but we can't since
26// this is effectively included from the Linux kernel
27#pragma clang diagnostic push
28#pragma clang diagnostic ignored "-Wzero-length-array"
Francis Hart8f396012014-04-01 15:30:53 +030029#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080030#pragma clang diagnostic pop
Francis Hart8f396012014-04-01 15:30:53 +030031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <utils/Log.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080033#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034
Chia-I Wu5bac7f32017-04-06 12:34:32 -070035#include <ui/Gralloc2.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036#include <ui/GraphicBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037
Dan Stoza8deb4da2016-06-01 18:21:44 -070038#include <system/graphics.h>
Mathias Agopian8b765b72009-04-10 20:34:46 -070039
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040namespace android {
41// ---------------------------------------------------------------------------
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070044
Mathias Agopian3330b202009-10-05 17:07:12 -070045GraphicBufferMapper::GraphicBufferMapper()
Chia-I Wu9ba189d2016-09-22 17:13:08 +080046 : mMapper(std::make_unique<const Gralloc2::Mapper>())
47{
48 if (!mMapper->valid()) {
49 mLoader = std::make_unique<Gralloc1::Loader>();
50 mDevice = mLoader->getDevice();
51 }
52}
Dan Stoza8deb4da2016-06-01 18:21:44 -070053
Chia-I Wu5bac7f32017-04-06 12:34:32 -070054status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
55 buffer_handle_t* outHandle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056{
Mathias Agopiancf563192012-02-29 20:43:29 -080057 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080058
Chia-I Wu5bac7f32017-04-06 12:34:32 -070059 Gralloc2::Error error;
60 if (mMapper->valid()) {
61 error = mMapper->importBuffer(hardware::hidl_handle(rawHandle),
62 outHandle);
63 } else {
64 error = Gralloc2::Error::UNSUPPORTED;
65 }
66
67 ALOGW_IF(error != Gralloc2::Error::NONE, "importBuffer(%p) failed: %d",
68 rawHandle, error);
69
70 return static_cast<status_t>(error);
71}
72
73status_t GraphicBufferMapper::importBuffer(const GraphicBuffer* buffer)
74{
75 ATRACE_CALL();
76
77 ANativeWindowBuffer* nativeBuffer = buffer->getNativeBuffer();
78 buffer_handle_t rawHandle = nativeBuffer->handle;
79
Chia-I Wu9ba189d2016-09-22 17:13:08 +080080 gralloc1_error_t error;
81 if (mMapper->valid()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -070082 buffer_handle_t importedHandle;
83 error = static_cast<gralloc1_error_t>(mMapper->importBuffer(
84 hardware::hidl_handle(rawHandle), &importedHandle));
85 if (error == GRALLOC1_ERROR_NONE) {
86 nativeBuffer->handle = importedHandle;
87 }
Chia-I Wu9ba189d2016-09-22 17:13:08 +080088 } else {
Chia-I Wu5bac7f32017-04-06 12:34:32 -070089 native_handle_t* clonedHandle = native_handle_clone(rawHandle);
90 if (clonedHandle) {
91 nativeBuffer->handle = clonedHandle;
92 error = mDevice->retain(buffer);
93 if (error != GRALLOC1_ERROR_NONE) {
94 nativeBuffer->handle = rawHandle;
95 native_handle_close(clonedHandle);
96 native_handle_delete(clonedHandle);
97 }
98 } else {
99 error = GRALLOC1_ERROR_NO_RESOURCES;
Chia-I Wu7bd8adb2017-02-10 14:53:12 -0800100 }
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800101 }
102
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700103 // the raw handle is owned by GraphicBuffer and is now replaced
104 if (error == GRALLOC1_ERROR_NONE) {
105 native_handle_close(rawHandle);
106 native_handle_delete(const_cast<native_handle_t*>(rawHandle));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800107 }
108
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700109 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "importBuffer(%p) failed: %d",
110 rawHandle, error);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700111
112 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113}
114
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700115status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700116{
Mathias Agopiancf563192012-02-29 20:43:29 -0800117 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800118
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800119 gralloc1_error_t error;
120 if (mMapper->valid()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700121 mMapper->freeBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800122 error = GRALLOC1_ERROR_NONE;
123 } else {
124 error = mDevice->release(handle);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700125 native_handle_close(handle);
126 native_handle_delete(const_cast<native_handle_t*>(handle));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800127 }
128
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700129 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "freeBuffer(%p): failed %d",
Dan Stoza8deb4da2016-06-01 18:21:44 -0700130 handle, error);
Mathias Agopian0a757812010-12-08 16:40:01 -0800131
Dan Stoza8deb4da2016-06-01 18:21:44 -0700132 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133}
134
Dan Stoza8deb4da2016-06-01 18:21:44 -0700135static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) {
136 gralloc1_rect_t outRect{};
137 outRect.left = rect.left;
138 outRect.top = rect.top;
139 outRect.width = rect.width();
140 outRect.height = rect.height();
141 return outRect;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142}
143
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700144static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) {
145 Gralloc2::IMapper::Rect outRect{};
146 outRect.left = rect.left;
147 outRect.top = rect.top;
148 outRect.width = rect.width();
149 outRect.height = rect.height();
150 return outRect;
Craig Donner58a1ef22017-02-02 12:40:05 -0800151}
152
Dan Stoza8deb4da2016-06-01 18:21:44 -0700153status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
154 const Rect& bounds, void** vaddr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700155{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700156 return lockAsync(handle, usage, bounds, vaddr, -1);
157}
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700158
Dan Stoza8deb4da2016-06-01 18:21:44 -0700159status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
160 const Rect& bounds, android_ycbcr *ycbcr)
161{
162 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700163}
164
Mathias Agopian3330b202009-10-05 17:07:12 -0700165status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700167 int32_t fenceFd = -1;
168 status_t error = unlockAsync(handle, &fenceFd);
169 if (error == NO_ERROR) {
170 sync_wait(fenceFd, -1);
171 close(fenceFd);
172 }
173 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700174}
175
Francis Hart8f396012014-04-01 15:30:53 +0300176status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800177 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300178{
Craig Donnere96a3252017-02-02 12:13:34 -0800179 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
180}
181
182status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
183 uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
184 void** vaddr, int fenceFd)
185{
Francis Hart8f396012014-04-01 15:30:53 +0300186 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300187
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800188 gralloc1_error_t error;
189 if (mMapper->valid()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700190 const uint64_t usage =
191 static_cast<uint64_t>(android_convertGralloc1To0Usage(
192 producerUsage, consumerUsage));
193 error = static_cast<gralloc1_error_t>(mMapper->lock(handle,
194 usage, asGralloc2Rect(bounds), fenceFd, vaddr));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800195 } else {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700196 gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800197 sp<Fence> fence = new Fence(fenceFd);
198 error = mDevice->lock(handle,
Craig Donnere96a3252017-02-02 12:13:34 -0800199 static_cast<gralloc1_producer_usage_t>(producerUsage),
200 static_cast<gralloc1_consumer_usage_t>(consumerUsage),
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800201 &accessRegion, vaddr, fence);
202 }
203
Dan Stoza8deb4da2016-06-01 18:21:44 -0700204 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle,
205 error);
206
207 return error;
208}
209
210static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) {
211 if (plane.bits_per_component != 8) {
212 ALOGV("Invalid number of bits per component: %d",
213 plane.bits_per_component);
214 return false;
215 }
216 if (plane.bits_used != 8) {
217 ALOGV("Invalid number of bits used: %d", plane.bits_used);
218 return false;
Francis Hart8f396012014-04-01 15:30:53 +0300219 }
220
Dan Stoza8deb4da2016-06-01 18:21:44 -0700221 bool hasValidIncrement = plane.h_increment == 1 ||
222 (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2);
223 hasValidIncrement = hasValidIncrement && plane.v_increment > 0;
224 if (!hasValidIncrement) {
225 ALOGV("Invalid increment: h %d v %d", plane.h_increment,
226 plane.v_increment);
227 return false;
228 }
229
230 return true;
Francis Hart8f396012014-04-01 15:30:53 +0300231}
232
233status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800234 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300235{
236 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300237
Dan Stoza8deb4da2016-06-01 18:21:44 -0700238 gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700239
Chia-I Wu31669472016-12-07 14:55:24 +0800240 std::vector<android_flex_plane_t> planes;
241 android_flex_layout_t flexLayout{};
242 gralloc1_error_t error;
243
244 if (mMapper->valid()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700245 Gralloc2::YCbCrLayout layout;
246 error = static_cast<gralloc1_error_t>(mMapper->lock(handle, usage,
247 asGralloc2Rect(bounds), fenceFd, &layout));
Chia-I Wu31669472016-12-07 14:55:24 +0800248 if (error == GRALLOC1_ERROR_NONE) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700249 ycbcr->y = layout.y;
250 ycbcr->cb = layout.cb;
251 ycbcr->cr = layout.cr;
252 ycbcr->ystride = static_cast<size_t>(layout.yStride);
253 ycbcr->cstride = static_cast<size_t>(layout.cStride);
254 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
Chia-I Wu31669472016-12-07 14:55:24 +0800255 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700256
257 return error;
Chia-I Wu31669472016-12-07 14:55:24 +0800258 } else {
259 sp<Fence> fence = new Fence(fenceFd);
260
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800261 if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
Chia-I Wu31669472016-12-07 14:55:24 +0800262 error = mDevice->lockYCbCr(handle,
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800263 static_cast<gralloc1_producer_usage_t>(usage),
264 static_cast<gralloc1_consumer_usage_t>(usage),
265 &accessRegion, ycbcr, fence);
266 ALOGW_IF(error != GRALLOC1_ERROR_NONE,
267 "lockYCbCr(%p, ...) failed: %d", handle, error);
268 return error;
269 }
Francis Hart8f396012014-04-01 15:30:53 +0300270
Chia-I Wu31669472016-12-07 14:55:24 +0800271 uint32_t numPlanes = 0;
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800272 error = mDevice->getNumFlexPlanes(handle, &numPlanes);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800273
Chia-I Wu31669472016-12-07 14:55:24 +0800274 if (error != GRALLOC1_ERROR_NONE) {
275 ALOGV("Failed to retrieve number of flex planes: %d", error);
276 return error;
277 }
278 if (numPlanes < 3) {
279 ALOGV("Not enough planes for YCbCr (%u found)", numPlanes);
280 return GRALLOC1_ERROR_UNSUPPORTED;
281 }
Dan Stoza8deb4da2016-06-01 18:21:44 -0700282
Chia-I Wu31669472016-12-07 14:55:24 +0800283 planes.resize(numPlanes);
284 flexLayout.num_planes = numPlanes;
285 flexLayout.planes = planes.data();
Dan Stoza8deb4da2016-06-01 18:21:44 -0700286
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800287 error = mDevice->lockFlex(handle,
288 static_cast<gralloc1_producer_usage_t>(usage),
289 static_cast<gralloc1_consumer_usage_t>(usage),
290 &accessRegion, &flexLayout, fence);
291 }
292
Dan Stoza8deb4da2016-06-01 18:21:44 -0700293 if (error != GRALLOC1_ERROR_NONE) {
294 ALOGW("lockFlex(%p, ...) failed: %d", handle, error);
295 return error;
296 }
297 if (flexLayout.format != FLEX_FORMAT_YCbCr) {
298 ALOGV("Unable to convert flex-format buffer to YCbCr");
299 unlock(handle);
300 return GRALLOC1_ERROR_UNSUPPORTED;
301 }
302
303 // Find planes
304 auto yPlane = planes.cend();
305 auto cbPlane = planes.cend();
306 auto crPlane = planes.cend();
307 for (auto planeIter = planes.cbegin(); planeIter != planes.cend();
308 ++planeIter) {
309 if (planeIter->component == FLEX_COMPONENT_Y) {
310 yPlane = planeIter;
311 } else if (planeIter->component == FLEX_COMPONENT_Cb) {
312 cbPlane = planeIter;
313 } else if (planeIter->component == FLEX_COMPONENT_Cr) {
314 crPlane = planeIter;
315 }
316 }
317 if (yPlane == planes.cend()) {
318 ALOGV("Unable to find Y plane");
319 unlock(handle);
320 return GRALLOC1_ERROR_UNSUPPORTED;
321 }
322 if (cbPlane == planes.cend()) {
323 ALOGV("Unable to find Cb plane");
324 unlock(handle);
325 return GRALLOC1_ERROR_UNSUPPORTED;
326 }
327 if (crPlane == planes.cend()) {
328 ALOGV("Unable to find Cr plane");
329 unlock(handle);
330 return GRALLOC1_ERROR_UNSUPPORTED;
331 }
332
333 // Validate planes
334 if (!isValidYCbCrPlane(*yPlane)) {
335 ALOGV("Y plane is invalid");
336 unlock(handle);
337 return GRALLOC1_ERROR_UNSUPPORTED;
338 }
339 if (!isValidYCbCrPlane(*cbPlane)) {
340 ALOGV("Cb plane is invalid");
341 unlock(handle);
342 return GRALLOC1_ERROR_UNSUPPORTED;
343 }
344 if (!isValidYCbCrPlane(*crPlane)) {
345 ALOGV("Cr plane is invalid");
346 unlock(handle);
347 return GRALLOC1_ERROR_UNSUPPORTED;
348 }
349 if (cbPlane->v_increment != crPlane->v_increment) {
350 ALOGV("Cb and Cr planes have different step (%d vs. %d)",
351 cbPlane->v_increment, crPlane->v_increment);
352 unlock(handle);
353 return GRALLOC1_ERROR_UNSUPPORTED;
354 }
355 if (cbPlane->h_increment != crPlane->h_increment) {
356 ALOGV("Cb and Cr planes have different stride (%d vs. %d)",
357 cbPlane->h_increment, crPlane->h_increment);
358 unlock(handle);
359 return GRALLOC1_ERROR_UNSUPPORTED;
360 }
361
362 // Pack plane data into android_ycbcr struct
363 ycbcr->y = yPlane->top_left;
364 ycbcr->cb = cbPlane->top_left;
365 ycbcr->cr = crPlane->top_left;
366 ycbcr->ystride = static_cast<size_t>(yPlane->v_increment);
367 ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment);
368 ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment);
369
370 return error;
Francis Hart8f396012014-04-01 15:30:53 +0300371}
372
373status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
374{
375 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300376
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800377 gralloc1_error_t error;
378 if (mMapper->valid()) {
379 *fenceFd = mMapper->unlock(handle);
380 error = GRALLOC1_ERROR_NONE;
381 } else {
382 sp<Fence> fence = Fence::NO_FENCE;
383 error = mDevice->unlock(handle, &fence);
384 if (error != GRALLOC1_ERROR_NONE) {
385 ALOGE("unlock(%p) failed: %d", handle, error);
386 return error;
387 }
Francis Hart8f396012014-04-01 15:30:53 +0300388
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800389 *fenceFd = fence->dup();
390 }
Dan Stoza8deb4da2016-06-01 18:21:44 -0700391 return error;
Francis Hart8f396012014-04-01 15:30:53 +0300392}
393
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700394// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700395}; // namespace android