1 /*
2 * Copyright (c) 2011-2018, 2020 The Linux Foundation. All rights reserved.
3 * Not a Contribution
4 *
5 * Copyright (C) 2010 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #define DEBUG 0
21
22 #include "gr_buf_mgr.h"
23
24 #include <QtiGralloc.h>
25 #include <QtiGrallocPriv.h>
26 #include <gralloctypes/Gralloc4.h>
27 #include <sys/mman.h>
28
29 #include <iomanip>
30 #include <sstream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34
35 #include "gr_adreno_info.h"
36 #include "gr_buf_descriptor.h"
37 #include "gr_priv_handle.h"
38 #include "gr_utils.h"
39 #include "qdMetaData.h"
40 #include "qd_utils.h"
41
42 namespace gralloc {
43
44 using aidl::android::hardware::graphics::common::BlendMode;
45 using aidl::android::hardware::graphics::common::Cta861_3;
46 using aidl::android::hardware::graphics::common::Dataspace;
47 using aidl::android::hardware::graphics::common::PlaneLayout;
48 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
49 using aidl::android::hardware::graphics::common::Rect;
50 using aidl::android::hardware::graphics::common::Smpte2086;
51 using aidl::android::hardware::graphics::common::StandardMetadataType;
52 using aidl::android::hardware::graphics::common::XyColor;
53 using ::android::hardware::graphics::common::V1_2::PixelFormat;
54
GetBufferInfo(const BufferDescriptor & descriptor)55 static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
56 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
57 descriptor.GetUsage());
58 }
59
getMetaDataSize(uint64_t reserved_region_size)60 static uint64_t getMetaDataSize(uint64_t reserved_region_size) {
61 // Only include the reserved region size when using Metadata_t V2
62 #ifndef METADATA_V2
63 reserved_region_size = 0;
64 #endif
65 return static_cast<uint64_t>(ROUND_UP_PAGESIZE(sizeof(MetaData_t) +
66 static_cast<uint32_t>(reserved_region_size)));
67 }
68
unmapAndReset(private_handle_t * handle,uint64_t reserved_region_size=0)69 static void unmapAndReset(private_handle_t *handle, uint64_t reserved_region_size = 0) {
70 if (private_handle_t::validate(handle) == 0 && handle->base_metadata) {
71 munmap(reinterpret_cast<void *>(handle->base_metadata),
72 static_cast<uint32_t>(getMetaDataSize(reserved_region_size)));
73 handle->base_metadata = 0;
74 }
75 }
76
validateAndMap(private_handle_t * handle,uint64_t reserved_region_size=0)77 static int validateAndMap(private_handle_t *handle, uint64_t reserved_region_size = 0) {
78 if (private_handle_t::validate(handle)) {
79 ALOGE("%s: Private handle is invalid - handle:%p", __func__, handle);
80 return -1;
81 }
82 if (handle->fd_metadata < 0) {
83 // Silently return, metadata cannot be used
84 return -1;
85 }
86
87 if (!handle->base_metadata) {
88 uint64_t size = getMetaDataSize(reserved_region_size);
89 void *base = mmap(NULL, static_cast<uint32_t>(size), PROT_READ | PROT_WRITE,
90 MAP_SHARED, handle->fd_metadata, 0);
91 if (base == reinterpret_cast<void *>(MAP_FAILED)) {
92 ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s", __func__, handle,
93 handle->fd_metadata, strerror(errno));
94 return -1;
95 }
96 handle->base_metadata = (uintptr_t)base;
97 #ifdef METADATA_V2
98 // The allocator process gets the reserved region size from the BufferDescriptor.
99 // When importing to another process, the reserved size is unknown until mapping the metadata,
100 // hence the re-mapping below
101 auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
102 if (reserved_region_size == 0 && metadata->reservedSize) {
103 size = getMetaDataSize(metadata->reservedSize);
104 unmapAndReset(handle);
105 void *new_base = mmap(NULL, static_cast<uint32_t>(size), PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd_metadata, 0);
106 if (new_base == reinterpret_cast<void *>(MAP_FAILED)) {
107 ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s", __func__, handle,
108 handle->fd_metadata, strerror(errno));
109 return -1;
110 }
111 handle->base_metadata = (uintptr_t)new_base;
112 }
113 #endif
114 }
115 return 0;
116 }
117
dataspaceToColorMetadata(Dataspace dataspace,ColorMetaData * color_metadata)118 static Error dataspaceToColorMetadata(Dataspace dataspace, ColorMetaData *color_metadata) {
119 ColorMetaData out;
120 uint32_t primaries = (uint32_t)dataspace & (uint32_t)Dataspace::STANDARD_MASK;
121 uint32_t transfer = (uint32_t)dataspace & (uint32_t)Dataspace::TRANSFER_MASK;
122 uint32_t range = (uint32_t)dataspace & (uint32_t)Dataspace::RANGE_MASK;
123
124 switch (primaries) {
125 case (uint32_t)Dataspace::STANDARD_BT709:
126 out.colorPrimaries = ColorPrimaries_BT709_5;
127 break;
128 // TODO(tbalacha): verify this is equivalent
129 case (uint32_t)Dataspace::STANDARD_BT470M:
130 out.colorPrimaries = ColorPrimaries_BT470_6M;
131 break;
132 case (uint32_t)Dataspace::STANDARD_BT601_625:
133 case (uint32_t)Dataspace::STANDARD_BT601_625_UNADJUSTED:
134 out.colorPrimaries = ColorPrimaries_BT601_6_625;
135 break;
136 case (uint32_t)Dataspace::STANDARD_BT601_525:
137 case (uint32_t)Dataspace::STANDARD_BT601_525_UNADJUSTED:
138 out.colorPrimaries = ColorPrimaries_BT601_6_525;
139 break;
140 case (uint32_t)Dataspace::STANDARD_FILM:
141 out.colorPrimaries = ColorPrimaries_GenericFilm;
142 break;
143 case (uint32_t)Dataspace::STANDARD_BT2020:
144 out.colorPrimaries = ColorPrimaries_BT2020;
145 break;
146 case (uint32_t)Dataspace::STANDARD_ADOBE_RGB:
147 out.colorPrimaries = ColorPrimaries_AdobeRGB;
148 break;
149 case (uint32_t)Dataspace::STANDARD_DCI_P3:
150 out.colorPrimaries = ColorPrimaries_DCIP3;
151 break;
152 default:
153 return Error::UNSUPPORTED;
154 /*
155 ColorPrimaries_SMPTE_240M;
156 ColorPrimaries_SMPTE_ST428;
157 ColorPrimaries_EBU3213;
158 */
159 }
160
161 switch (transfer) {
162 case (uint32_t)Dataspace::TRANSFER_SRGB:
163 out.transfer = Transfer_sRGB;
164 break;
165 case (uint32_t)Dataspace::TRANSFER_GAMMA2_2:
166 out.transfer = Transfer_Gamma2_2;
167 break;
168 case (uint32_t)Dataspace::TRANSFER_GAMMA2_8:
169 out.transfer = Transfer_Gamma2_8;
170 break;
171 case (uint32_t)Dataspace::TRANSFER_SMPTE_170M:
172 out.transfer = Transfer_SMPTE_170M;
173 break;
174 case (uint32_t)Dataspace::TRANSFER_LINEAR:
175 out.transfer = Transfer_Linear;
176 break;
177 case (uint32_t)Dataspace::TRANSFER_HLG:
178 out.transfer = Transfer_HLG;
179 break;
180 default:
181 return Error::UNSUPPORTED;
182 /*
183 Transfer_SMPTE_240M
184 Transfer_Log
185 Transfer_Log_Sqrt
186 Transfer_XvYCC
187 Transfer_BT1361
188 Transfer_sYCC
189 Transfer_BT2020_2_1
190 Transfer_BT2020_2_2
191 Transfer_SMPTE_ST2084
192 Transfer_ST_428
193 */
194 }
195
196 switch (range) {
197 case (uint32_t)Dataspace::RANGE_FULL:
198 out.range = Range_Full;
199 break;
200 case (uint32_t)Dataspace::RANGE_LIMITED:
201 out.range = Range_Limited;
202 break;
203 case (uint32_t)Dataspace::RANGE_EXTENDED:
204 out.range = Range_Extended;
205 break;
206 default:
207 return Error::UNSUPPORTED;
208 }
209
210 color_metadata->colorPrimaries = out.colorPrimaries;
211 color_metadata->transfer = out.transfer;
212 color_metadata->range = out.range;
213 return Error::NONE;
214 }
colorMetadataToDataspace(ColorMetaData color_metadata,Dataspace * dataspace)215 static Error colorMetadataToDataspace(ColorMetaData color_metadata, Dataspace *dataspace) {
216 Dataspace primaries, transfer, range = Dataspace::UNKNOWN;
217
218 switch (color_metadata.colorPrimaries) {
219 case ColorPrimaries_BT709_5:
220 primaries = Dataspace::STANDARD_BT709;
221 break;
222 // TODO(tbalacha): verify this is equivalent
223 case ColorPrimaries_BT470_6M:
224 primaries = Dataspace::STANDARD_BT470M;
225 break;
226 case ColorPrimaries_BT601_6_625:
227 primaries = Dataspace::STANDARD_BT601_625;
228 break;
229 case ColorPrimaries_BT601_6_525:
230 primaries = Dataspace::STANDARD_BT601_525;
231 break;
232 case ColorPrimaries_GenericFilm:
233 primaries = Dataspace::STANDARD_FILM;
234 break;
235 case ColorPrimaries_BT2020:
236 primaries = Dataspace::STANDARD_BT2020;
237 break;
238 case ColorPrimaries_AdobeRGB:
239 primaries = Dataspace::STANDARD_ADOBE_RGB;
240 break;
241 case ColorPrimaries_DCIP3:
242 primaries = Dataspace::STANDARD_DCI_P3;
243 break;
244 default:
245 return Error::UNSUPPORTED;
246 /*
247 ColorPrimaries_SMPTE_240M;
248 ColorPrimaries_SMPTE_ST428;
249 ColorPrimaries_EBU3213;
250 */
251 }
252
253 switch (color_metadata.transfer) {
254 case Transfer_sRGB:
255 transfer = Dataspace::TRANSFER_SRGB;
256 break;
257 case Transfer_Gamma2_2:
258 transfer = Dataspace::TRANSFER_GAMMA2_2;
259 break;
260 case Transfer_Gamma2_8:
261 transfer = Dataspace::TRANSFER_GAMMA2_8;
262 break;
263 case Transfer_SMPTE_170M:
264 transfer = Dataspace::TRANSFER_SMPTE_170M;
265 break;
266 case Transfer_Linear:
267 transfer = Dataspace::TRANSFER_LINEAR;
268 break;
269 case Transfer_HLG:
270 transfer = Dataspace::TRANSFER_HLG;
271 break;
272 default:
273 return Error::UNSUPPORTED;
274 /*
275 Transfer_SMPTE_240M
276 Transfer_Log
277 Transfer_Log_Sqrt
278 Transfer_XvYCC
279 Transfer_BT1361
280 Transfer_sYCC
281 Transfer_BT2020_2_1
282 Transfer_BT2020_2_2
283 Transfer_SMPTE_ST2084
284 Transfer_ST_428
285 */
286 }
287
288 switch (color_metadata.range) {
289 case Range_Full:
290 range = Dataspace::RANGE_FULL;
291 break;
292 case Range_Limited:
293 range = Dataspace::RANGE_LIMITED;
294 break;
295 case Range_Extended:
296 range = Dataspace::RANGE_EXTENDED;
297 break;
298 default:
299 return Error::UNSUPPORTED;
300 }
301
302 *dataspace = (Dataspace)((uint32_t)primaries | (uint32_t)transfer | (uint32_t)range);
303 return Error::NONE;
304 }
305
getComponentSizeAndOffset(int32_t format,PlaneLayoutComponent & comp)306 static Error getComponentSizeAndOffset(int32_t format, PlaneLayoutComponent &comp) {
307 switch (format) {
308 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_8888):
309 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBX_8888):
310 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGB_888):
311 comp.sizeInBits = 8;
312 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
313 comp.offsetInBits = 0;
314 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
315 comp.offsetInBits = 8;
316 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
317 comp.offsetInBits = 16;
318 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value &&
319 format != HAL_PIXEL_FORMAT_RGB_888) {
320 comp.offsetInBits = 24;
321 } else {
322 return Error::BAD_VALUE;
323 }
324 break;
325 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGB_565):
326 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
327 comp.offsetInBits = 0;
328 comp.sizeInBits = 5;
329 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
330 comp.offsetInBits = 5;
331 comp.sizeInBits = 6;
332 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
333 comp.offsetInBits = 11;
334 comp.sizeInBits = 5;
335 } else {
336 return Error::BAD_VALUE;
337 }
338 break;
339 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGR_565):
340 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
341 comp.offsetInBits = 11;
342 comp.sizeInBits = 5;
343 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
344 comp.offsetInBits = 5;
345 comp.sizeInBits = 6;
346 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
347 comp.offsetInBits = 0;
348 comp.sizeInBits = 5;
349 } else {
350 return Error::BAD_VALUE;
351 }
352 break;
353 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGRA_8888):
354 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGRX_8888):
355 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGR_888):
356 comp.sizeInBits = 8;
357 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
358 comp.offsetInBits = 16;
359 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
360 comp.offsetInBits = 8;
361 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
362 comp.offsetInBits = 0;
363 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value &&
364 format != HAL_PIXEL_FORMAT_BGR_888) {
365 comp.offsetInBits = 24;
366 } else {
367 return Error::BAD_VALUE;
368 }
369 break;
370 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_5551):
371 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
372 comp.sizeInBits = 5;
373 comp.offsetInBits = 0;
374 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
375 comp.sizeInBits = 5;
376 comp.offsetInBits = 5;
377 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
378 comp.sizeInBits = 5;
379 comp.offsetInBits = 10;
380 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
381 comp.sizeInBits = 1;
382 comp.offsetInBits = 15;
383 } else {
384 return Error::BAD_VALUE;
385 }
386 break;
387 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_4444):
388 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
389 comp.sizeInBits = 4;
390 comp.offsetInBits = 0;
391 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
392 comp.sizeInBits = 4;
393 comp.offsetInBits = 4;
394 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
395 comp.sizeInBits = 4;
396 comp.offsetInBits = 8;
397 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
398 comp.sizeInBits = 4;
399 comp.offsetInBits = 12;
400 } else {
401 return Error::BAD_VALUE;
402 }
403 break;
404 case static_cast<int32_t>(HAL_PIXEL_FORMAT_R_8):
405 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RG_88):
406 comp.sizeInBits = 8;
407 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
408 comp.offsetInBits = 0;
409 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value &&
410 format != HAL_PIXEL_FORMAT_R_8) {
411 comp.offsetInBits = 8;
412 } else {
413 return Error::BAD_VALUE;
414 }
415 break;
416 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_1010102):
417 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBX_1010102):
418 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
419 comp.sizeInBits = 10;
420 comp.offsetInBits = 0;
421 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
422 comp.sizeInBits = 10;
423 comp.offsetInBits = 10;
424 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
425 comp.sizeInBits = 10;
426 comp.offsetInBits = 20;
427 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
428 comp.sizeInBits = 2;
429 comp.offsetInBits = 30;
430 } else {
431 return Error::BAD_VALUE;
432 }
433 break;
434 case static_cast<int32_t>(HAL_PIXEL_FORMAT_ARGB_2101010):
435 case static_cast<int32_t>(HAL_PIXEL_FORMAT_XRGB_2101010):
436 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
437 comp.sizeInBits = 10;
438 comp.offsetInBits = 2;
439 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
440 comp.sizeInBits = 10;
441 comp.offsetInBits = 12;
442 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
443 comp.sizeInBits = 10;
444 comp.offsetInBits = 22;
445 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
446 comp.sizeInBits = 2;
447 comp.offsetInBits = 0;
448 } else {
449 return Error::BAD_VALUE;
450 }
451 break;
452 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGRA_1010102):
453 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGRX_1010102):
454 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
455 comp.sizeInBits = 10;
456 comp.offsetInBits = 20;
457 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
458 comp.sizeInBits = 10;
459 comp.offsetInBits = 10;
460 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
461 comp.sizeInBits = 10;
462 comp.offsetInBits = 0;
463 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
464 comp.sizeInBits = 2;
465 comp.offsetInBits = 30;
466 } else {
467 return Error::BAD_VALUE;
468 }
469 break;
470 case static_cast<int32_t>(HAL_PIXEL_FORMAT_ABGR_2101010):
471 case static_cast<int32_t>(HAL_PIXEL_FORMAT_XBGR_2101010):
472 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
473 comp.sizeInBits = 10;
474 comp.offsetInBits = 22;
475 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
476 comp.sizeInBits = 10;
477 comp.offsetInBits = 12;
478 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
479 comp.sizeInBits = 10;
480 comp.offsetInBits = 2;
481 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
482 comp.sizeInBits = 2;
483 comp.offsetInBits = 0;
484 } else {
485 return Error::BAD_VALUE;
486 }
487 break;
488 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_FP16):
489 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
490 comp.sizeInBits = 16;
491 comp.offsetInBits = 0;
492 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
493 comp.sizeInBits = 16;
494 comp.offsetInBits = 16;
495 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
496 comp.sizeInBits = 16;
497 comp.offsetInBits = 32;
498 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
499 comp.sizeInBits = 16;
500 comp.offsetInBits = 48;
501 } else {
502 return Error::BAD_VALUE;
503 }
504 break;
505 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCbCr_420_SP):
506 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCbCr_422_SP):
507 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS):
508 case static_cast<int32_t>(HAL_PIXEL_FORMAT_NV12_ENCODEABLE):
509 comp.sizeInBits = 8;
510 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value ||
511 comp.type.value == android::gralloc4::PlaneLayoutComponentType_CB.value) {
512 comp.offsetInBits = 0;
513 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
514 comp.offsetInBits = 8;
515 } else {
516 return Error::BAD_VALUE;
517 }
518 break;
519 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCrCb_420_SP):
520 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCrCb_422_SP):
521 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO):
522 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS):
523 case static_cast<int32_t>(HAL_PIXEL_FORMAT_NV21_ZSL):
524 comp.sizeInBits = 8;
525 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value ||
526 comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
527 comp.offsetInBits = 0;
528 } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_CB.value) {
529 comp.offsetInBits = 8;
530 } else {
531 return Error::BAD_VALUE;
532 }
533 break;
534 case static_cast<int32_t>(HAL_PIXEL_FORMAT_Y16):
535 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value) {
536 comp.offsetInBits = 0;
537 comp.sizeInBits = 16;
538 } else {
539 return Error::BAD_VALUE;
540 }
541 break;
542 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YV12):
543 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value ||
544 comp.type.value == android::gralloc4::PlaneLayoutComponentType_CB.value ||
545 comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
546 comp.offsetInBits = 0;
547 comp.sizeInBits = 8;
548 } else {
549 return Error::BAD_VALUE;
550 }
551 break;
552 case static_cast<int32_t>(HAL_PIXEL_FORMAT_Y8):
553 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value) {
554 comp.offsetInBits = 0;
555 comp.sizeInBits = 8;
556 } else {
557 return Error::BAD_VALUE;
558 }
559 break;
560 case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCbCr_420_P010):
561 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value ||
562 comp.type.value == android::gralloc4::PlaneLayoutComponentType_CB.value ||
563 comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
564 comp.offsetInBits = 0;
565 comp.sizeInBits = 10;
566 } else {
567 return Error::BAD_VALUE;
568 }
569 break;
570 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RAW16):
571 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_RAW.value) {
572 comp.offsetInBits = 0;
573 comp.sizeInBits = 16;
574 } else {
575 return Error::BAD_VALUE;
576 }
577 break;
578 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RAW12):
579 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RAW10):
580 case static_cast<int32_t>(HAL_PIXEL_FORMAT_BLOB):
581 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_RAW.value) {
582 comp.offsetInBits = 0;
583 comp.sizeInBits = -1;
584 } else {
585 return Error::BAD_VALUE;
586 }
587 break;
588 case static_cast<int32_t>(HAL_PIXEL_FORMAT_RAW8):
589 if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_RAW.value) {
590 comp.offsetInBits = 0;
591 comp.sizeInBits = 8;
592 } else {
593 return Error::BAD_VALUE;
594 }
595 break;
596 default:
597 ALOGI_IF(DEBUG, "Offset and size in bits unknown for format %d", format);
598 return Error::UNSUPPORTED;
599 }
600 return Error::NONE;
601 }
602
grallocToStandardPlaneLayoutComponentType(uint32_t in,std::vector<PlaneLayoutComponent> * components,int32_t format)603 static void grallocToStandardPlaneLayoutComponentType(uint32_t in,
604 std::vector<PlaneLayoutComponent> *components,
605 int32_t format) {
606 PlaneLayoutComponent comp;
607 comp.offsetInBits = -1;
608 comp.sizeInBits = -1;
609
610 if (in & PLANE_COMPONENT_Y) {
611 comp.type = android::gralloc4::PlaneLayoutComponentType_Y;
612 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
613 components->push_back(comp);
614 }
615
616 if (in & PLANE_COMPONENT_Cb) {
617 comp.type = android::gralloc4::PlaneLayoutComponentType_CB;
618 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
619 components->push_back(comp);
620 }
621
622 if (in & PLANE_COMPONENT_Cr) {
623 comp.type = android::gralloc4::PlaneLayoutComponentType_CR;
624 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
625 components->push_back(comp);
626 }
627
628 if (in & PLANE_COMPONENT_R) {
629 comp.type = android::gralloc4::PlaneLayoutComponentType_R;
630 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
631 components->push_back(comp);
632 }
633
634 if (in & PLANE_COMPONENT_G) {
635 comp.type = android::gralloc4::PlaneLayoutComponentType_G;
636 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
637 components->push_back(comp);
638 }
639
640 if (in & PLANE_COMPONENT_B) {
641 comp.type = android::gralloc4::PlaneLayoutComponentType_B;
642 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
643 components->push_back(comp);
644 }
645
646 if (in & PLANE_COMPONENT_A) {
647 comp.type = android::gralloc4::PlaneLayoutComponentType_A;
648 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
649 components->push_back(comp);
650 }
651
652 if (in & PLANE_COMPONENT_RAW) {
653 comp.type = android::gralloc4::PlaneLayoutComponentType_RAW;
654 if (getComponentSizeAndOffset(format, comp) == Error::NONE)
655 components->push_back(comp);
656 }
657
658 if (in & PLANE_COMPONENT_META) {
659 comp.type = qtigralloc::PlaneLayoutComponentType_Meta;
660 components->push_back(comp);
661 }
662 }
663
getFormatLayout(private_handle_t * handle,std::vector<PlaneLayout> * out)664 static Error getFormatLayout(private_handle_t *handle, std::vector<PlaneLayout> *out) {
665 std::vector<PlaneLayout> plane_info;
666 int plane_count = 0;
667 BufferInfo info(handle->unaligned_width, handle->unaligned_height, handle->format, handle->usage);
668
669 gralloc::PlaneLayoutInfo plane_layout[8] = {};
670 if (gralloc::IsYuvFormat(handle->format)) {
671 gralloc::GetYUVPlaneInfo(info, handle->format, handle->width, handle->height, handle->flags,
672 &plane_count, plane_layout);
673 } else if (gralloc::IsUncompressedRGBFormat(handle->format) ||
674 gralloc::IsCompressedRGBFormat(handle->format)) {
675 gralloc::GetRGBPlaneInfo(info, handle->format, handle->width, handle->height, handle->flags,
676 &plane_count, plane_layout);
677 } else {
678 return Error::BAD_BUFFER;
679 }
680 plane_info.resize(plane_count);
681 for (int i = 0; i < plane_count; i++) {
682 std::vector<PlaneLayoutComponent> components;
683 grallocToStandardPlaneLayoutComponentType(plane_layout[i].component, &plane_info[i].components,
684 handle->format);
685 plane_info[i].horizontalSubsampling = (1ull << plane_layout[i].h_subsampling);
686 plane_info[i].verticalSubsampling = (1ull << plane_layout[i].v_subsampling);
687 plane_info[i].offsetInBytes = static_cast<int64_t>(plane_layout[i].offset);
688 plane_info[i].sampleIncrementInBits = static_cast<int64_t>(plane_layout[i].step * 8);
689 plane_info[i].strideInBytes = static_cast<int64_t>(plane_layout[i].stride_bytes);
690 plane_info[i].totalSizeInBytes = static_cast<int64_t>(plane_layout[i].size);
691 plane_info[i].widthInSamples = handle->unaligned_width >> plane_layout[i].h_subsampling;
692 plane_info[i].heightInSamples = handle->unaligned_height >> plane_layout[i].v_subsampling;
693 }
694 *out = plane_info;
695 return Error::NONE;
696 }
697
BufferManager()698 BufferManager::BufferManager() : next_id_(0) {
699 handles_map_.clear();
700 allocator_ = new Allocator();
701 allocator_->Init();
702 }
703
GetInstance()704 BufferManager *BufferManager::GetInstance() {
705 static BufferManager *instance = new BufferManager();
706 return instance;
707 }
708
~BufferManager()709 BufferManager::~BufferManager() {
710 if (allocator_) {
711 delete allocator_;
712 }
713 }
714
SetGrallocDebugProperties(gralloc::GrallocProperties props)715 void BufferManager::SetGrallocDebugProperties(gralloc::GrallocProperties props) {
716 allocator_->SetProperties(props);
717 AdrenoMemInfo::GetInstance()->AdrenoSetProperties(props);
718 }
719
FreeBuffer(std::shared_ptr<Buffer> buf)720 Error BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
721 auto hnd = buf->handle;
722 ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd);
723
724 if (private_handle_t::validate(hnd) != 0) {
725 ALOGE("FreeBuffer: Invalid handle: %p", hnd);
726 return Error::BAD_BUFFER;
727 }
728
729 auto meta_size = getMetaDataSize(buf->reserved_size);
730
731 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, hnd->fd,
732 buf->ion_handle_main) != 0) {
733 return Error::BAD_BUFFER;
734 }
735
736 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata),
737 static_cast<uint32_t>(meta_size), hnd->offset_metadata,
738 hnd->fd_metadata, buf->ion_handle_meta) != 0) {
739 return Error::BAD_BUFFER;
740 }
741
742 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
743 handle->fd = -1;
744 handle->fd_metadata = -1;
745 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
746 delete handle;
747 }
748 return Error::NONE;
749 }
750
ValidateBufferSize(private_handle_t const * hnd,BufferInfo info)751 Error BufferManager::ValidateBufferSize(private_handle_t const *hnd, BufferInfo info) {
752 unsigned int size, alignedw, alignedh;
753 info.format = GetImplDefinedFormat(info.usage, info.format);
754 int ret = GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
755 if (ret < 0) {
756 return Error::BAD_BUFFER;
757 }
758 auto ion_fd_size = static_cast<unsigned int>(lseek(hnd->fd, 0, SEEK_END));
759 if (size != ion_fd_size) {
760 return Error::BAD_VALUE;
761 }
762 return Error::NONE;
763 }
764
RegisterHandleLocked(const private_handle_t * hnd,int ion_handle,int ion_handle_meta)765 void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, int ion_handle,
766 int ion_handle_meta) {
767 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
768
769 if (hnd->base_metadata) {
770 auto metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
771 #ifdef METADATA_V2
772 buffer->reserved_size = metadata->reservedSize;
773 if (buffer->reserved_size > 0) {
774 buffer->reserved_region_ptr =
775 reinterpret_cast<void *>(hnd->base_metadata + sizeof(MetaData_t));
776 } else {
777 buffer->reserved_region_ptr = nullptr;
778 }
779 #else
780 buffer->reserved_region_ptr = reinterpret_cast<void *>(&(metadata->reservedRegion.data));
781 buffer->reserved_size = metadata->reservedRegion.size;
782 #endif
783 }
784
785 handles_map_.emplace(std::make_pair(hnd, buffer));
786 }
787
ImportHandleLocked(private_handle_t * hnd)788 Error BufferManager::ImportHandleLocked(private_handle_t *hnd) {
789 if (private_handle_t::validate(hnd) != 0) {
790 ALOGE("ImportHandleLocked: Invalid handle: %p", hnd);
791 return Error::BAD_BUFFER;
792 }
793 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
794 int ion_handle = allocator_->ImportBuffer(hnd->fd);
795 if (ion_handle < 0) {
796 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
797 return Error::BAD_BUFFER;
798 }
799 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
800 if (ion_handle_meta < 0) {
801 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd,
802 hnd->id);
803 return Error::BAD_BUFFER;
804 }
805 // Initialize members that aren't transported
806 hnd->size = static_cast<unsigned int>(lseek(hnd->fd, 0, SEEK_END));
807 hnd->offset = 0;
808 hnd->offset_metadata = 0;
809 hnd->base = 0;
810 hnd->base_metadata = 0;
811 hnd->gpuaddr = 0;
812
813 if (validateAndMap(hnd)) {
814 ALOGE("Failed to map metadata: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
815 return Error::BAD_BUFFER;
816 }
817
818 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
819 return Error::NONE;
820 }
821
GetBufferFromHandleLocked(const private_handle_t * hnd)822 std::shared_ptr<BufferManager::Buffer> BufferManager::GetBufferFromHandleLocked(
823 const private_handle_t *hnd) {
824 auto it = handles_map_.find(hnd);
825 if (it != handles_map_.end()) {
826 return it->second;
827 } else {
828 return nullptr;
829 }
830 }
831
MapBuffer(private_handle_t const * handle)832 Error BufferManager::MapBuffer(private_handle_t const *handle) {
833 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
834 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
835
836 hnd->base = 0;
837 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
838 hnd->fd) != 0) {
839 return Error::BAD_BUFFER;
840 }
841 return Error::NONE;
842 }
843
IsBufferImported(const private_handle_t * hnd)844 Error BufferManager::IsBufferImported(const private_handle_t *hnd) {
845 std::lock_guard<std::mutex> lock(buffer_lock_);
846 auto buf = GetBufferFromHandleLocked(hnd);
847 if (buf != nullptr) {
848 return Error::NONE;
849 }
850 return Error::BAD_BUFFER;
851 }
852
RetainBuffer(private_handle_t const * hnd)853 Error BufferManager::RetainBuffer(private_handle_t const *hnd) {
854 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
855 auto err = Error::NONE;
856 std::lock_guard<std::mutex> lock(buffer_lock_);
857 auto buf = GetBufferFromHandleLocked(hnd);
858 if (buf != nullptr) {
859 buf->IncRef();
860 } else {
861 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
862 err = ImportHandleLocked(handle);
863 }
864 return err;
865 }
866
ReleaseBuffer(private_handle_t const * hnd)867 Error BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
868 ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd);
869 std::lock_guard<std::mutex> lock(buffer_lock_);
870 auto buf = GetBufferFromHandleLocked(hnd);
871 if (buf == nullptr) {
872 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
873 return Error::BAD_BUFFER;
874 } else {
875 if (buf->DecRef()) {
876 handles_map_.erase(hnd);
877 // Unmap, close ion handle and close fd
878 FreeBuffer(buf);
879 }
880 }
881 return Error::NONE;
882 }
883
LockBuffer(const private_handle_t * hnd,uint64_t usage)884 Error BufferManager::LockBuffer(const private_handle_t *hnd, uint64_t usage) {
885 std::lock_guard<std::mutex> lock(buffer_lock_);
886 auto err = Error::NONE;
887 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
888
889 // If buffer is not meant for CPU return err
890 if (!CpuCanAccess(usage)) {
891 return Error::BAD_VALUE;
892 }
893
894 auto buf = GetBufferFromHandleLocked(hnd);
895 if (buf == nullptr) {
896 return Error::BAD_BUFFER;
897 }
898
899 if (hnd->base == 0) {
900 // we need to map for real
901 err = MapBuffer(hnd);
902 }
903
904 // Invalidate if CPU reads in software and there are non-CPU
905 // writers. No need to do this for the metadata buffer as it is
906 // only read/written in software.
907
908 // todo use handle here
909 if (err == Error::NONE && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
910 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
911 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
912 buf->ion_handle_main, CACHE_INVALIDATE, hnd->fd)) {
913 return Error::BAD_BUFFER;
914 }
915 }
916
917 // Mark the buffer to be flushed after CPU write.
918 if (err == Error::NONE && CpuCanWrite(usage)) {
919 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
920 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
921 }
922
923 return err;
924 }
925
FlushBuffer(const private_handle_t * handle)926 Error BufferManager::FlushBuffer(const private_handle_t *handle) {
927 std::lock_guard<std::mutex> lock(buffer_lock_);
928 auto status = Error::NONE;
929
930 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
931 auto buf = GetBufferFromHandleLocked(hnd);
932 if (buf == nullptr) {
933 return Error::BAD_BUFFER;
934 }
935
936 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
937 buf->ion_handle_main, CACHE_CLEAN, hnd->fd) != 0) {
938 status = Error::BAD_BUFFER;
939 }
940
941 return status;
942 }
943
RereadBuffer(const private_handle_t * handle)944 Error BufferManager::RereadBuffer(const private_handle_t *handle) {
945 std::lock_guard<std::mutex> lock(buffer_lock_);
946 auto status = Error::NONE;
947
948 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
949 auto buf = GetBufferFromHandleLocked(hnd);
950 if (buf == nullptr) {
951 return Error::BAD_BUFFER;
952 }
953
954 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
955 buf->ion_handle_main, CACHE_INVALIDATE, hnd->fd) != 0) {
956 status = Error::BAD_BUFFER;
957 }
958
959 return status;
960 }
961
UnlockBuffer(const private_handle_t * handle)962 Error BufferManager::UnlockBuffer(const private_handle_t *handle) {
963 std::lock_guard<std::mutex> lock(buffer_lock_);
964 auto status = Error::NONE;
965
966 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
967 auto buf = GetBufferFromHandleLocked(hnd);
968 if (buf == nullptr) {
969 return Error::BAD_BUFFER;
970 }
971
972 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
973 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
974 buf->ion_handle_main, CACHE_CLEAN, hnd->fd) != 0) {
975 status = Error::BAD_BUFFER;
976 }
977 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
978 } else {
979 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
980 buf->ion_handle_main, CACHE_READ_DONE, hnd->fd) != 0) {
981 status = Error::BAD_BUFFER;
982 }
983 }
984
985 return status;
986 }
987
AllocateBuffer(const BufferDescriptor & descriptor,buffer_handle_t * handle,unsigned int bufferSize,bool testAlloc)988 Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
989 unsigned int bufferSize, bool testAlloc) {
990 if (!handle)
991 return Error::BAD_BUFFER;
992 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
993
994 uint64_t usage = descriptor.GetUsage();
995 int format = GetImplDefinedFormat(usage, descriptor.GetFormat());
996 uint32_t layer_count = descriptor.GetLayerCount();
997
998 unsigned int size;
999 unsigned int alignedw, alignedh;
1000 int err = 0;
1001
1002 int buffer_type = GetBufferType(format);
1003 BufferInfo info = GetBufferInfo(descriptor);
1004 info.format = format;
1005 info.layer_count = layer_count;
1006
1007 GraphicsMetadata graphics_metadata = {};
1008 err = GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh, &graphics_metadata);
1009 if (err < 0) {
1010 return Error::BAD_DESCRIPTOR;
1011 }
1012
1013 if (testAlloc) {
1014 return Error::NONE;
1015 }
1016
1017 size = (bufferSize >= size) ? bufferSize : size;
1018 uint64_t flags = 0;
1019 auto page_size = UINT(getpagesize());
1020 AllocData data;
1021 data.align = GetDataAlignment(format, usage);
1022 data.size = size;
1023 data.handle = (uintptr_t)handle;
1024 data.uncached = UseUncached(format, usage);
1025
1026 // Allocate buffer memory
1027 err = allocator_->AllocateMem(&data, usage, format);
1028 if (err) {
1029 ALOGE("gralloc failed to allocate err=%s format %d size %d WxH %dx%d usage %" PRIu64,
1030 strerror(-err), format, size, alignedw, alignedh, usage);
1031 return Error::NO_RESOURCES;
1032 }
1033
1034 // Allocate memory for MetaData
1035 AllocData e_data;
1036 e_data.size = static_cast<unsigned int>(getMetaDataSize(descriptor.GetReservedSize()));
1037 e_data.handle = data.handle;
1038 e_data.align = page_size;
1039
1040 err = allocator_->AllocateMem(&e_data, 0, 0);
1041 if (err) {
1042 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
1043 return Error::NO_RESOURCES;
1044 }
1045
1046 flags = GetHandleFlags(format, usage);
1047 flags |= data.alloc_type;
1048
1049 // Create handle
1050 private_handle_t *hnd = new private_handle_t(
1051 data.fd, e_data.fd, INT(flags), INT(alignedw), INT(alignedh), descriptor.GetWidth(),
1052 descriptor.GetHeight(), format, buffer_type, data.size, usage);
1053
1054 hnd->id = ++next_id_;
1055 hnd->base = 0;
1056 hnd->base_metadata = 0;
1057 hnd->layer_count = layer_count;
1058
1059 bool use_adreno_for_size = CanUseAdrenoForSize(buffer_type, usage);
1060 if (use_adreno_for_size) {
1061 setMetaDataAndUnmap(hnd, SET_GRAPHICS_METADATA, reinterpret_cast<void *>(&graphics_metadata));
1062 }
1063
1064 #ifdef METADATA_V2
1065 auto error = validateAndMap(hnd, descriptor.GetReservedSize());
1066 #else
1067 auto error = validateAndMap(hnd);
1068 #endif
1069
1070 if (error != 0) {
1071 ALOGE("validateAndMap failed");
1072 return Error::BAD_BUFFER;
1073 }
1074 auto metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
1075 auto nameLength = std::min(descriptor.GetName().size(), size_t(MAX_NAME_LEN - 1));
1076 nameLength = descriptor.GetName().copy(metadata->name, nameLength);
1077 metadata->name[nameLength] = '\0';
1078
1079 #ifdef METADATA_V2
1080 metadata->reservedSize = descriptor.GetReservedSize();
1081 #else
1082 metadata->reservedRegion.size =
1083 static_cast<uint32_t>(std::min(descriptor.GetReservedSize(), (uint64_t)RESERVED_REGION_SIZE));
1084 #endif
1085 metadata->crop.top = 0;
1086 metadata->crop.left = 0;
1087 metadata->crop.right = hnd->width;
1088 metadata->crop.bottom = hnd->height;
1089
1090 unmapAndReset(hnd, descriptor.GetReservedSize());
1091
1092 *handle = hnd;
1093
1094 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
1095 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
1096 if (DEBUG) {
1097 private_handle_t::Dump(hnd);
1098 }
1099 return Error::NONE;
1100 }
1101
Dump(std::ostringstream * os)1102 Error BufferManager::Dump(std::ostringstream *os) {
1103 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
1104 for (auto it : handles_map_) {
1105 auto buf = it.second;
1106 auto hnd = buf->handle;
1107 *os << "handle id: " << std::setw(4) << hnd->id;
1108 *os << " fd: " << std::setw(3) << hnd->fd;
1109 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
1110 *os << " wxh: " << std::setw(4) << hnd->width << " x " << std::setw(4) << hnd->height;
1111 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
1112 *os << std::setw(4) << hnd->unaligned_height;
1113 *os << " size: " << std::setw(9) << hnd->size;
1114 *os << std::hex << std::setfill('0');
1115 *os << " priv_flags: "
1116 << "0x" << std::setw(8) << hnd->flags;
1117 *os << " usage: "
1118 << "0x" << std::setw(8) << hnd->usage;
1119 // TODO(user): get format string from qdutils
1120 *os << " format: "
1121 << "0x" << std::setw(8) << hnd->format;
1122 *os << std::dec << std::setfill(' ') << std::endl;
1123 }
1124 return Error::NONE;
1125 }
1126
1127 // Get list of private handles in handles_map_
GetAllHandles(std::vector<const private_handle_t * > * out_handle_list)1128 Error BufferManager::GetAllHandles(std::vector<const private_handle_t *> *out_handle_list) {
1129 std::lock_guard<std::mutex> lock(buffer_lock_);
1130 if (handles_map_.empty()) {
1131 return Error::NO_RESOURCES;
1132 }
1133 out_handle_list->reserve(handles_map_.size());
1134 for (auto handle : handles_map_) {
1135 out_handle_list->push_back(handle.first);
1136 }
1137 return Error::NONE;
1138 }
1139
GetReservedRegion(private_handle_t * handle,void ** reserved_region,uint64_t * reserved_region_size)1140 Error BufferManager::GetReservedRegion(private_handle_t *handle, void **reserved_region,
1141 uint64_t *reserved_region_size) {
1142 std::lock_guard<std::mutex> lock(buffer_lock_);
1143 if (!handle)
1144 return Error::BAD_BUFFER;
1145
1146 auto buf = GetBufferFromHandleLocked(handle);
1147 if (buf == nullptr)
1148 return Error::BAD_BUFFER;
1149 if (!handle->base_metadata) {
1150 return Error::BAD_BUFFER;
1151 }
1152
1153 *reserved_region = buf->reserved_region_ptr;
1154 *reserved_region_size = buf->reserved_size;
1155
1156 return Error::NONE;
1157 }
1158
GetMetadata(private_handle_t * handle,int64_t metadatatype_value,hidl_vec<uint8_t> * out)1159 Error BufferManager::GetMetadata(private_handle_t *handle, int64_t metadatatype_value,
1160 hidl_vec<uint8_t> *out) {
1161 std::lock_guard<std::mutex> lock(buffer_lock_);
1162 if (!handle)
1163 return Error::BAD_BUFFER;
1164 auto buf = GetBufferFromHandleLocked(handle);
1165 if (buf == nullptr)
1166 return Error::BAD_BUFFER;
1167
1168 if (!handle->base_metadata) {
1169 return Error::BAD_BUFFER;
1170 }
1171
1172 auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
1173
1174 Error error = Error::NONE;
1175 switch (metadatatype_value) {
1176 case (int64_t)StandardMetadataType::BUFFER_ID:
1177 android::gralloc4::encodeBufferId((uint64_t)handle->id, out);
1178 break;
1179 case (int64_t)StandardMetadataType::NAME: {
1180 std::string name(metadata->name);
1181 android::gralloc4::encodeName(name, out);
1182 break;
1183 }
1184 case (int64_t)StandardMetadataType::WIDTH:
1185 android::gralloc4::encodeWidth((uint64_t)handle->unaligned_width, out);
1186 break;
1187 case (int64_t)StandardMetadataType::HEIGHT:
1188 android::gralloc4::encodeHeight((uint64_t)handle->unaligned_height, out);
1189 break;
1190 case (int64_t)StandardMetadataType::LAYER_COUNT:
1191 android::gralloc4::encodeLayerCount((uint64_t)handle->layer_count, out);
1192 break;
1193 case (int64_t)StandardMetadataType::PIXEL_FORMAT_REQUESTED:
1194 // TODO(tbalacha): need to return IMPLEMENTATION_DEFINED,
1195 // which wouldn't be known from private_handle_t
1196 android::gralloc4::encodePixelFormatRequested((PixelFormat)handle->format, out);
1197 break;
1198 case (int64_t)StandardMetadataType::PIXEL_FORMAT_FOURCC: {
1199 uint32_t drm_format = 0;
1200 uint64_t drm_format_modifier = 0;
1201 GetDRMFormat(handle->format, handle->flags, &drm_format, &drm_format_modifier);
1202 android::gralloc4::encodePixelFormatFourCC(drm_format, out);
1203 break;
1204 }
1205 case (int64_t)StandardMetadataType::PIXEL_FORMAT_MODIFIER: {
1206 uint32_t drm_format = 0;
1207 uint64_t drm_format_modifier = 0;
1208 GetDRMFormat(handle->format, handle->flags, &drm_format, &drm_format_modifier);
1209 android::gralloc4::encodePixelFormatModifier(drm_format_modifier, out);
1210 break;
1211 }
1212 case (int64_t)StandardMetadataType::USAGE:
1213 android::gralloc4::encodeUsage((uint64_t)handle->usage, out);
1214 break;
1215 case (int64_t)StandardMetadataType::ALLOCATION_SIZE:
1216 android::gralloc4::encodeAllocationSize((uint64_t)handle->size, out);
1217 break;
1218 case (int64_t)StandardMetadataType::PROTECTED_CONTENT: {
1219 uint64_t protected_content = (handle->flags & qtigralloc::PRIV_FLAGS_SECURE_BUFFER) ? 1 : 0;
1220 android::gralloc4::encodeProtectedContent(protected_content, out);
1221 break;
1222 }
1223 case (int64_t)StandardMetadataType::CHROMA_SITING:
1224 android::gralloc4::encodeChromaSiting(android::gralloc4::ChromaSiting_None, out);
1225 break;
1226 case (int64_t)StandardMetadataType::DATASPACE:
1227 #ifdef METADATA_V2
1228 if (metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value)]) {
1229 #endif
1230 Dataspace dataspace;
1231 colorMetadataToDataspace(metadata->color, &dataspace);
1232 android::gralloc4::encodeDataspace(dataspace, out);
1233 #ifdef METADATA_V2
1234 } else {
1235 android::gralloc4::encodeDataspace(Dataspace::UNKNOWN, out);
1236 }
1237 #endif
1238 break;
1239 case (int64_t)StandardMetadataType::INTERLACED:
1240 if (metadata->interlaced > 0) {
1241 android::gralloc4::encodeInterlaced(qtigralloc::Interlaced_Qti, out);
1242 } else {
1243 android::gralloc4::encodeInterlaced(android::gralloc4::Interlaced_None, out);
1244 }
1245 break;
1246 case (int64_t)StandardMetadataType::COMPRESSION:
1247 if (handle->flags & qtigralloc::PRIV_FLAGS_UBWC_ALIGNED ||
1248 handle->flags & qtigralloc::PRIV_FLAGS_UBWC_ALIGNED_PI) {
1249 android::gralloc4::encodeCompression(qtigralloc::Compression_QtiUBWC, out);
1250 } else {
1251 android::gralloc4::encodeCompression(android::gralloc4::Compression_None, out);
1252 }
1253 break;
1254 case (int64_t)StandardMetadataType::PLANE_LAYOUTS: {
1255 std::vector<PlaneLayout> plane_layouts;
1256 getFormatLayout(handle, &plane_layouts);
1257 android::gralloc4::encodePlaneLayouts(plane_layouts, out);
1258 break;
1259 }
1260 case (int64_t)StandardMetadataType::BLEND_MODE:
1261 android::gralloc4::encodeBlendMode((BlendMode)metadata->blendMode, out);
1262 break;
1263 case (int64_t)StandardMetadataType::SMPTE2086: {
1264 if (metadata->color.masteringDisplayInfo.colorVolumeSEIEnabled) {
1265 Smpte2086 mastering_display_values;
1266 mastering_display_values.primaryRed = {
1267 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[0][0]) /
1268 50000.0f,
1269 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[0][1]) /
1270 50000.0f};
1271 mastering_display_values.primaryGreen = {
1272 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[1][0]) /
1273 50000.0f,
1274 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[1][1]) /
1275 50000.0f};
1276 mastering_display_values.primaryBlue = {
1277 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[2][0]) /
1278 50000.0f,
1279 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[2][1]) /
1280 50000.0f};
1281 mastering_display_values.whitePoint = {
1282 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.whitePoint[0]) /
1283 50000.0f,
1284 static_cast<float>(metadata->color.masteringDisplayInfo.primaries.whitePoint[1]) /
1285 50000.0f};
1286 mastering_display_values.maxLuminance =
1287 static_cast<float>(metadata->color.masteringDisplayInfo.maxDisplayLuminance);
1288 mastering_display_values.minLuminance =
1289 static_cast<float>(metadata->color.masteringDisplayInfo.minDisplayLuminance) / 10000.0f;
1290 android::gralloc4::encodeSmpte2086(mastering_display_values, out);
1291 } else {
1292 android::gralloc4::encodeSmpte2086(std::nullopt, out);
1293 }
1294 break;
1295 }
1296 case (int64_t)StandardMetadataType::CTA861_3: {
1297 if (metadata->color.contentLightLevel.lightLevelSEIEnabled) {
1298 Cta861_3 content_light_level;
1299 content_light_level.maxContentLightLevel =
1300 static_cast<float>(metadata->color.contentLightLevel.maxContentLightLevel);
1301 content_light_level.maxFrameAverageLightLevel =
1302 static_cast<float>(metadata->color.contentLightLevel.minPicAverageLightLevel) /
1303 10000.0f;
1304 android::gralloc4::encodeCta861_3(content_light_level, out);
1305 } else {
1306 android::gralloc4::encodeCta861_3(std::nullopt, out);
1307 }
1308 break;
1309 }
1310 case (int64_t)StandardMetadataType::SMPTE2094_40: {
1311 if (metadata->color.dynamicMetaDataValid &&
1312 metadata->color.dynamicMetaDataLen <= HDR_DYNAMIC_META_DATA_SZ) {
1313 std::vector<uint8_t> dynamic_metadata_payload;
1314 dynamic_metadata_payload.resize(metadata->color.dynamicMetaDataLen);
1315 dynamic_metadata_payload.assign(
1316 metadata->color.dynamicMetaDataPayload,
1317 metadata->color.dynamicMetaDataPayload + metadata->color.dynamicMetaDataLen);
1318 android::gralloc4::encodeSmpte2094_40(dynamic_metadata_payload, out);
1319 } else {
1320 android::gralloc4::encodeSmpte2094_40(std::nullopt, out);
1321 }
1322 break;
1323 }
1324 case (int64_t)StandardMetadataType::CROP: {
1325 // Crop is the same for all planes
1326 std::vector<Rect> out_crop = {{metadata->crop.left, metadata->crop.top, metadata->crop.right,
1327 metadata->crop.bottom}};
1328 android::gralloc4::encodeCrop(out_crop, out);
1329 break;
1330 }
1331 case QTI_VT_TIMESTAMP:
1332 android::gralloc4::encodeUint64(qtigralloc::MetadataType_VTTimestamp, metadata->vtTimeStamp,
1333 out);
1334 break;
1335 case QTI_COLOR_METADATA:
1336 qtigralloc::encodeColorMetadata(metadata->color, out);
1337 break;
1338 case QTI_PP_PARAM_INTERLACED:
1339 android::gralloc4::encodeInt32(qtigralloc::MetadataType_PPParamInterlaced,
1340 metadata->interlaced, out);
1341 break;
1342 case QTI_VIDEO_PERF_MODE:
1343 android::gralloc4::encodeUint32(qtigralloc::MetadataType_VideoPerfMode,
1344 metadata->isVideoPerfMode, out);
1345 break;
1346 case QTI_GRAPHICS_METADATA:
1347 qtigralloc::encodeGraphicsMetadata(metadata->graphics_metadata, out);
1348 break;
1349 case QTI_UBWC_CR_STATS_INFO:
1350 qtigralloc::encodeUBWCStats(metadata->ubwcCRStats, out);
1351 break;
1352 case QTI_REFRESH_RATE:
1353 android::gralloc4::encodeFloat(qtigralloc::MetadataType_RefreshRate, metadata->refreshrate,
1354 out);
1355 break;
1356 case QTI_MAP_SECURE_BUFFER:
1357 android::gralloc4::encodeInt32(qtigralloc::MetadataType_MapSecureBuffer,
1358 metadata->mapSecureBuffer, out);
1359 break;
1360 case QTI_LINEAR_FORMAT:
1361 android::gralloc4::encodeUint32(qtigralloc::MetadataType_LinearFormat, metadata->linearFormat,
1362 out);
1363 break;
1364 case QTI_SINGLE_BUFFER_MODE:
1365 android::gralloc4::encodeUint32(qtigralloc::MetadataType_SingleBufferMode,
1366 metadata->isSingleBufferMode, out);
1367 break;
1368 case QTI_CVP_METADATA:
1369 qtigralloc::encodeCVPMetadata(metadata->cvpMetadata, out);
1370 break;
1371 case QTI_VIDEO_HISTOGRAM_STATS:
1372 qtigralloc::encodeVideoHistogramMetadata(metadata->video_histogram_stats, out);
1373 break;
1374 case QTI_FD:
1375 android::gralloc4::encodeInt32(qtigralloc::MetadataType_FD, handle->fd, out);
1376 break;
1377 case QTI_PRIVATE_FLAGS:
1378 android::gralloc4::encodeInt32(qtigralloc::MetadataType_PrivateFlags, handle->flags, out);
1379 break;
1380 case QTI_ALIGNED_WIDTH_IN_PIXELS:
1381 android::gralloc4::encodeUint32(qtigralloc::MetadataType_AlignedWidthInPixels, handle->width,
1382 out);
1383 break;
1384 case QTI_ALIGNED_HEIGHT_IN_PIXELS:
1385 android::gralloc4::encodeUint32(qtigralloc::MetadataType_AlignedHeightInPixels,
1386 handle->height, out);
1387 break;
1388 #ifdef METADATA_V2
1389 case QTI_STANDARD_METADATA_STATUS:
1390 qtigralloc::encodeMetadataState(metadata->isStandardMetadataSet, out);
1391 break;
1392 case QTI_VENDOR_METADATA_STATUS:
1393 qtigralloc::encodeMetadataState(metadata->isVendorMetadataSet, out);
1394 break;
1395 #endif
1396 #ifdef QTI_BUFFER_TYPE
1397 case QTI_BUFFER_TYPE:
1398 android::gralloc4::encodeUint32(qtigralloc::MetadataType_BufferType, handle->buffer_type,
1399 out);
1400 break;
1401 #endif
1402 default:
1403 error = Error::UNSUPPORTED;
1404 }
1405
1406 return error;
1407 }
1408
SetMetadata(private_handle_t * handle,int64_t metadatatype_value,hidl_vec<uint8_t> in)1409 Error BufferManager::SetMetadata(private_handle_t *handle, int64_t metadatatype_value,
1410 hidl_vec<uint8_t> in) {
1411 std::lock_guard<std::mutex> lock(buffer_lock_);
1412 if (!handle)
1413 return Error::BAD_BUFFER;
1414
1415 auto buf = GetBufferFromHandleLocked(handle);
1416 if (buf == nullptr)
1417 return Error::BAD_BUFFER;
1418
1419 if (!handle->base_metadata) {
1420 return Error::BAD_BUFFER;
1421 }
1422 if (in.size() == 0) {
1423 return Error::UNSUPPORTED;
1424 }
1425
1426 auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
1427
1428 #ifdef METADATA_V2
1429 // By default, set these to true
1430 // Reset to false for special cases below
1431 if (IS_VENDOR_METADATA_TYPE(metadatatype_value)) {
1432 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(metadatatype_value)] = true;
1433 } else if (GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value) < METADATA_SET_SIZE) {
1434 metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value)] = true;
1435 }
1436 #endif
1437
1438 switch (metadatatype_value) {
1439 // These are constant (unchanged after allocation)
1440 case (int64_t)StandardMetadataType::BUFFER_ID:
1441 case (int64_t)StandardMetadataType::NAME:
1442 case (int64_t)StandardMetadataType::WIDTH:
1443 case (int64_t)StandardMetadataType::HEIGHT:
1444 case (int64_t)StandardMetadataType::LAYER_COUNT:
1445 case (int64_t)StandardMetadataType::PIXEL_FORMAT_REQUESTED:
1446 case (int64_t)StandardMetadataType::USAGE:
1447 return Error::BAD_VALUE;
1448 case (int64_t)StandardMetadataType::PIXEL_FORMAT_FOURCC:
1449 case (int64_t)StandardMetadataType::PIXEL_FORMAT_MODIFIER:
1450 case (int64_t)StandardMetadataType::PROTECTED_CONTENT:
1451 case (int64_t)StandardMetadataType::ALLOCATION_SIZE:
1452 case (int64_t)StandardMetadataType::PLANE_LAYOUTS:
1453 case (int64_t)StandardMetadataType::CHROMA_SITING:
1454 case (int64_t)StandardMetadataType::INTERLACED:
1455 case (int64_t)StandardMetadataType::COMPRESSION:
1456 case QTI_FD:
1457 case QTI_PRIVATE_FLAGS:
1458 case QTI_ALIGNED_WIDTH_IN_PIXELS:
1459 case QTI_ALIGNED_HEIGHT_IN_PIXELS:
1460 return Error::UNSUPPORTED;
1461 case (int64_t)StandardMetadataType::DATASPACE:
1462 Dataspace dataspace;
1463 android::gralloc4::decodeDataspace(in, &dataspace);
1464 dataspaceToColorMetadata(dataspace, &metadata->color);
1465 break;
1466 case (int64_t)StandardMetadataType::BLEND_MODE:
1467 BlendMode mode;
1468 android::gralloc4::decodeBlendMode(in, &mode);
1469 metadata->blendMode = (int32_t)mode;
1470 break;
1471 case (int64_t)StandardMetadataType::SMPTE2086: {
1472 std::optional<Smpte2086> mastering_display_values;
1473 android::gralloc4::decodeSmpte2086(in, &mastering_display_values);
1474 if (mastering_display_values != std::nullopt) {
1475 metadata->color.masteringDisplayInfo.colorVolumeSEIEnabled = true;
1476
1477 metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[0][0] =
1478 static_cast<uint32_t>(mastering_display_values->primaryRed.x * 50000.0f);
1479 metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[0][1] =
1480 static_cast<uint32_t>(mastering_display_values->primaryRed.y * 50000.0f);
1481
1482 metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[1][0] =
1483 static_cast<uint32_t>(mastering_display_values->primaryGreen.x * 50000.0f);
1484 metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[1][1] =
1485 static_cast<uint32_t>(mastering_display_values->primaryGreen.y * 50000.0f);
1486
1487 metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[2][0] =
1488 static_cast<uint32_t>(mastering_display_values->primaryBlue.x * 50000.0f);
1489 metadata->color.masteringDisplayInfo.primaries.rgbPrimaries[2][1] =
1490 static_cast<uint32_t>(mastering_display_values->primaryBlue.y * 50000.0f);
1491
1492 metadata->color.masteringDisplayInfo.primaries.whitePoint[0] =
1493 static_cast<uint32_t>(mastering_display_values->whitePoint.x * 50000.0f);
1494 metadata->color.masteringDisplayInfo.primaries.whitePoint[1] =
1495 static_cast<uint32_t>(mastering_display_values->whitePoint.y * 50000.0f);
1496
1497 metadata->color.masteringDisplayInfo.maxDisplayLuminance =
1498 static_cast<uint32_t>(mastering_display_values->maxLuminance);
1499 metadata->color.masteringDisplayInfo.minDisplayLuminance =
1500 static_cast<uint32_t>(mastering_display_values->minLuminance * 10000.0f);
1501 } else {
1502 #ifdef METADATA_V2
1503 metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value)] =
1504 false;
1505 #endif
1506 metadata->color.masteringDisplayInfo.colorVolumeSEIEnabled = false;
1507 }
1508 break;
1509 }
1510 case (int64_t)StandardMetadataType::CTA861_3: {
1511 std::optional<Cta861_3> content_light_level;
1512 android::gralloc4::decodeCta861_3(in, &content_light_level);
1513 if (content_light_level != std::nullopt) {
1514 metadata->color.contentLightLevel.lightLevelSEIEnabled = true;
1515 metadata->color.contentLightLevel.maxContentLightLevel =
1516 static_cast<uint32_t>(content_light_level->maxContentLightLevel);
1517 metadata->color.contentLightLevel.minPicAverageLightLevel =
1518 static_cast<uint32_t>(content_light_level->maxFrameAverageLightLevel * 10000.0f);
1519 } else {
1520 #ifdef METADATA_V2
1521 metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value)] =
1522 false;
1523 #endif
1524 metadata->color.contentLightLevel.lightLevelSEIEnabled = false;
1525 }
1526 break;
1527 }
1528 case (int64_t)StandardMetadataType::SMPTE2094_40: {
1529 std::optional<std::vector<uint8_t>> dynamic_metadata_payload;
1530 android::gralloc4::decodeSmpte2094_40(in, &dynamic_metadata_payload);
1531 if (dynamic_metadata_payload != std::nullopt) {
1532 if (dynamic_metadata_payload->size() > HDR_DYNAMIC_META_DATA_SZ)
1533 return Error::BAD_VALUE;
1534
1535 metadata->color.dynamicMetaDataLen = static_cast<uint32_t>(dynamic_metadata_payload->size());
1536 std::copy(dynamic_metadata_payload->begin(), dynamic_metadata_payload->end(),
1537 metadata->color.dynamicMetaDataPayload);
1538 metadata->color.dynamicMetaDataValid = true;
1539 } else {
1540 // Reset metadata by passing in std::nullopt
1541 #ifdef METADATA_V2
1542 metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value)] =
1543 false;
1544 #endif
1545 metadata->color.dynamicMetaDataValid = false;
1546 }
1547 break;
1548 }
1549 case (int64_t)StandardMetadataType::CROP: {
1550 std::vector<Rect> in_crop;
1551 android::gralloc4::decodeCrop(in, &in_crop);
1552 if (in_crop.size() != 1)
1553 return Error::UNSUPPORTED;
1554
1555 metadata->crop.left = in_crop[0].left;
1556 metadata->crop.top = in_crop[0].top;
1557 metadata->crop.right = in_crop[0].right;
1558 metadata->crop.bottom = in_crop[0].bottom;
1559 break;
1560 }
1561 case QTI_VT_TIMESTAMP:
1562 android::gralloc4::decodeUint64(qtigralloc::MetadataType_VTTimestamp, in,
1563 &metadata->vtTimeStamp);
1564 break;
1565 case QTI_COLOR_METADATA:
1566 ColorMetaData color;
1567 qtigralloc::decodeColorMetadata(in, &color);
1568 metadata->color = color;
1569 break;
1570 case QTI_PP_PARAM_INTERLACED:
1571 android::gralloc4::decodeInt32(qtigralloc::MetadataType_PPParamInterlaced, in,
1572 &metadata->interlaced);
1573 break;
1574 case QTI_VIDEO_PERF_MODE:
1575 android::gralloc4::decodeUint32(qtigralloc::MetadataType_VideoPerfMode, in,
1576 &metadata->isVideoPerfMode);
1577 break;
1578 case QTI_GRAPHICS_METADATA:
1579 qtigralloc::decodeGraphicsMetadata(in, &metadata->graphics_metadata);
1580 break;
1581 case QTI_UBWC_CR_STATS_INFO:
1582 qtigralloc::decodeUBWCStats(in, &metadata->ubwcCRStats[0]);
1583 break;
1584 case QTI_REFRESH_RATE:
1585 android::gralloc4::decodeFloat(qtigralloc::MetadataType_RefreshRate, in,
1586 &metadata->refreshrate);
1587 break;
1588 case QTI_MAP_SECURE_BUFFER:
1589 android::gralloc4::decodeInt32(qtigralloc::MetadataType_MapSecureBuffer, in,
1590 &metadata->mapSecureBuffer);
1591 break;
1592 case QTI_LINEAR_FORMAT:
1593 android::gralloc4::decodeUint32(qtigralloc::MetadataType_LinearFormat, in,
1594 &metadata->linearFormat);
1595 break;
1596 case QTI_SINGLE_BUFFER_MODE:
1597 android::gralloc4::decodeUint32(qtigralloc::MetadataType_SingleBufferMode, in,
1598 &metadata->isSingleBufferMode);
1599 break;
1600 case QTI_CVP_METADATA:
1601 qtigralloc::decodeCVPMetadata(in, &metadata->cvpMetadata);
1602 break;
1603 case QTI_VIDEO_HISTOGRAM_STATS:
1604 qtigralloc::decodeVideoHistogramMetadata(in, &metadata->video_histogram_stats);
1605 break;
1606 default:
1607 #ifdef METADATA_V2
1608 if (IS_VENDOR_METADATA_TYPE(metadatatype_value)) {
1609 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(metadatatype_value)] = false;
1610 } else if (GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value) < METADATA_SET_SIZE) {
1611 metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(metadatatype_value)] =
1612 false;
1613 }
1614 #endif
1615 return Error::BAD_VALUE;
1616 }
1617
1618 return Error::NONE;
1619 }
1620
1621 } // namespace gralloc
1622