1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "GraphicBufferMapper"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define LOG_NDEBUG 0
#include <stdint.h>
#include <errno.h>
// We would eliminate the non-conforming zero-length array, but we can't since
// this is effectively included from the Linux kernel
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wzero-length-array"
#include <sync/sync.h>
#pragma clang diagnostic pop
#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/Trace.h>
#include <ui/Gralloc1On0Adapter.h>
#include <ui/GrallocMapper.h>
#include <ui/GraphicBufferMapper.h>
#include <ui/Rect.h>
#include <system/graphics.h>
namespace android {
// ---------------------------------------------------------------------------
ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
GraphicBufferMapper::GraphicBufferMapper()
: mMapper(std::make_unique<const Gralloc2::Mapper>())
{
if (!mMapper->valid()) {
mLoader = std::make_unique<Gralloc1::Loader>();
mDevice = mLoader->getDevice();
}
}
status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
error = static_cast<gralloc1_error_t>(mMapper->retain(handle));
} else {
// This always returns GRALLOC1_BAD_HANDLE when handle is from a
// remote process and mDevice is backed by Gralloc1On0Adapter.
error = mDevice->retain(handle);
if (error == GRALLOC1_ERROR_BAD_HANDLE &&
mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
ALOGE("registerBuffer by handle is not supported with "
"Gralloc1On0Adapter");
}
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d",
handle, error);
return error;
}
status_t GraphicBufferMapper::registerBuffer(const GraphicBuffer* buffer)
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
error = static_cast<gralloc1_error_t>(
mMapper->retain(buffer->getNativeBuffer()->handle));
} else {
error = mDevice->retain(buffer);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d",
buffer->getNativeBuffer()->handle, error);
return error;
}
status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->release(handle);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->release(handle);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "unregisterBuffer(%p): failed %d",
handle, error);
return error;
}
static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) {
gralloc1_rect_t outRect{};
outRect.left = rect.left;
outRect.top = rect.top;
outRect.width = rect.width();
outRect.height = rect.height();
return outRect;
}
status_t GraphicBufferMapper::getDimensions(buffer_handle_t handle,
uint32_t* outWidth, uint32_t* outHeight) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getDimensions(handle, outWidth, outHeight);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getDimensions(handle, outWidth, outHeight);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getDimensions(%p, ...): failed %d",
handle, error);
return error;
}
status_t GraphicBufferMapper::getFormat(buffer_handle_t handle,
int32_t* outFormat) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getFormat(handle, outFormat);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getFormat(handle, outFormat);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getFormat(%p, ...): failed %d",
handle, error);
return error;
}
status_t GraphicBufferMapper::getLayerCount(buffer_handle_t handle,
uint32_t* outLayerCount) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getLayerCount(handle, outLayerCount);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getLayerCount(handle, outLayerCount);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getLayerCount(%p, ...): failed %d",
handle, error);
return error;
}
status_t GraphicBufferMapper::getProducerUsage(buffer_handle_t handle,
uint64_t* outProducerUsage) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getProducerUsage(handle, outProducerUsage);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getProducerUsage(handle, outProducerUsage);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE,
"getProducerUsage(%p, ...): failed %d", handle, error);
return error;
}
status_t GraphicBufferMapper::getConsumerUsage(buffer_handle_t handle,
uint64_t* outConsumerUsage) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getConsumerUsage(handle, outConsumerUsage);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getConsumerUsage(handle, outConsumerUsage);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE,
"getConsumerUsage(%p, ...): failed %d", handle, error);
return error;
}
status_t GraphicBufferMapper::getBackingStore(buffer_handle_t handle,
uint64_t* outBackingStore) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getBackingStore(handle, outBackingStore);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getBackingStore(handle, outBackingStore);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE,
"getBackingStore(%p, ...): failed %d", handle, error);
return error;
}
status_t GraphicBufferMapper::getStride(buffer_handle_t handle,
uint32_t* outStride) const
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
mMapper->getStride(handle, outStride);
error = GRALLOC1_ERROR_NONE;
} else {
error = mDevice->getStride(handle, outStride);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "getStride(%p, ...): failed %d",
handle, error);
return error;
}
status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
const Rect& bounds, void** vaddr)
{
return lockAsync(handle, usage, bounds, vaddr, -1);
}
status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
const Rect& bounds, android_ycbcr *ycbcr)
{
return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
}
status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
{
int32_t fenceFd = -1;
status_t error = unlockAsync(handle, &fenceFd);
if (error == NO_ERROR) {
sync_wait(fenceFd, -1);
close(fenceFd);
}
return error;
}
status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
{
return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
}
status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
void** vaddr, int fenceFd)
{
ATRACE_CALL();
gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
gralloc1_error_t error;
if (mMapper->valid()) {
const Gralloc2::IMapper::Rect& accessRect =
*reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion);
error = static_cast<gralloc1_error_t>(mMapper->lock(
handle, producerUsage, consumerUsage, accessRect,
fenceFd, vaddr));
} else {
sp<Fence> fence = new Fence(fenceFd);
error = mDevice->lock(handle,
static_cast<gralloc1_producer_usage_t>(producerUsage),
static_cast<gralloc1_consumer_usage_t>(consumerUsage),
&accessRegion, vaddr, fence);
}
ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle,
error);
return error;
}
static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) {
if (plane.bits_per_component != 8) {
ALOGV("Invalid number of bits per component: %d",
plane.bits_per_component);
return false;
}
if (plane.bits_used != 8) {
ALOGV("Invalid number of bits used: %d", plane.bits_used);
return false;
}
bool hasValidIncrement = plane.h_increment == 1 ||
(plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2);
hasValidIncrement = hasValidIncrement && plane.v_increment > 0;
if (!hasValidIncrement) {
ALOGV("Invalid increment: h %d v %d", plane.h_increment,
plane.v_increment);
return false;
}
return true;
}
status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
{
ATRACE_CALL();
gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
std::vector<android_flex_plane_t> planes;
android_flex_layout_t flexLayout{};
gralloc1_error_t error;
if (mMapper->valid()) {
const Gralloc2::IMapper::Rect& accessRect =
*reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion);
Gralloc2::FlexLayout layout{};
error = static_cast<gralloc1_error_t>(mMapper->lock(
handle, usage, usage, accessRect, fenceFd, &layout));
if (error == GRALLOC1_ERROR_NONE) {
planes.resize(layout.planes.size());
memcpy(planes.data(), layout.planes.data(),
sizeof(planes[0]) * planes.size());
flexLayout.format = static_cast<android_flex_format_t>(
layout.format);
flexLayout.num_planes = static_cast<uint32_t>(planes.size());
flexLayout.planes = planes.data();
}
} else {
sp<Fence> fence = new Fence(fenceFd);
if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
error = mDevice->lockYCbCr(handle,
static_cast<gralloc1_producer_usage_t>(usage),
static_cast<gralloc1_consumer_usage_t>(usage),
&accessRegion, ycbcr, fence);
ALOGW_IF(error != GRALLOC1_ERROR_NONE,
"lockYCbCr(%p, ...) failed: %d", handle, error);
return error;
}
uint32_t numPlanes = 0;
error = mDevice->getNumFlexPlanes(handle, &numPlanes);
if (error != GRALLOC1_ERROR_NONE) {
ALOGV("Failed to retrieve number of flex planes: %d", error);
return error;
}
if (numPlanes < 3) {
ALOGV("Not enough planes for YCbCr (%u found)", numPlanes);
return GRALLOC1_ERROR_UNSUPPORTED;
}
planes.resize(numPlanes);
flexLayout.num_planes = numPlanes;
flexLayout.planes = planes.data();
error = mDevice->lockFlex(handle,
static_cast<gralloc1_producer_usage_t>(usage),
static_cast<gralloc1_consumer_usage_t>(usage),
&accessRegion, &flexLayout, fence);
}
if (error != GRALLOC1_ERROR_NONE) {
ALOGW("lockFlex(%p, ...) failed: %d", handle, error);
return error;
}
if (flexLayout.format != FLEX_FORMAT_YCbCr) {
ALOGV("Unable to convert flex-format buffer to YCbCr");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
// Find planes
auto yPlane = planes.cend();
auto cbPlane = planes.cend();
auto crPlane = planes.cend();
for (auto planeIter = planes.cbegin(); planeIter != planes.cend();
++planeIter) {
if (planeIter->component == FLEX_COMPONENT_Y) {
yPlane = planeIter;
} else if (planeIter->component == FLEX_COMPONENT_Cb) {
cbPlane = planeIter;
} else if (planeIter->component == FLEX_COMPONENT_Cr) {
crPlane = planeIter;
}
}
if (yPlane == planes.cend()) {
ALOGV("Unable to find Y plane");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
if (cbPlane == planes.cend()) {
ALOGV("Unable to find Cb plane");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
if (crPlane == planes.cend()) {
ALOGV("Unable to find Cr plane");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
// Validate planes
if (!isValidYCbCrPlane(*yPlane)) {
ALOGV("Y plane is invalid");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
if (!isValidYCbCrPlane(*cbPlane)) {
ALOGV("Cb plane is invalid");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
if (!isValidYCbCrPlane(*crPlane)) {
ALOGV("Cr plane is invalid");
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
if (cbPlane->v_increment != crPlane->v_increment) {
ALOGV("Cb and Cr planes have different step (%d vs. %d)",
cbPlane->v_increment, crPlane->v_increment);
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
if (cbPlane->h_increment != crPlane->h_increment) {
ALOGV("Cb and Cr planes have different stride (%d vs. %d)",
cbPlane->h_increment, crPlane->h_increment);
unlock(handle);
return GRALLOC1_ERROR_UNSUPPORTED;
}
// Pack plane data into android_ycbcr struct
ycbcr->y = yPlane->top_left;
ycbcr->cb = cbPlane->top_left;
ycbcr->cr = crPlane->top_left;
ycbcr->ystride = static_cast<size_t>(yPlane->v_increment);
ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment);
ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment);
return error;
}
status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
{
ATRACE_CALL();
gralloc1_error_t error;
if (mMapper->valid()) {
*fenceFd = mMapper->unlock(handle);
error = GRALLOC1_ERROR_NONE;
} else {
sp<Fence> fence = Fence::NO_FENCE;
error = mDevice->unlock(handle, &fence);
if (error != GRALLOC1_ERROR_NONE) {
ALOGE("unlock(%p) failed: %d", handle, error);
return error;
}
*fenceFd = fence->dup();
}
return error;
}
// ---------------------------------------------------------------------------
}; // namespace android
|