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