1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "libgralloctypes"
18
19 #include <cstring>
20 #include <cinttypes>
21 #include <limits>
22
23 #include <hidl/HidlSupport.h>
24 #include <log/log.h>
25
26 #include "gralloctypes/Gralloc4.h"
27
28 using android::hardware::hidl_vec;
29
30 using aidl::android::hardware::graphics::common::BlendMode;
31 using aidl::android::hardware::graphics::common::ChromaSiting;
32 using aidl::android::hardware::graphics::common::Compression;
33 using aidl::android::hardware::graphics::common::Cta861_3;
34 using aidl::android::hardware::graphics::common::Dataspace;
35 using aidl::android::hardware::graphics::common::ExtendableType;
36 using aidl::android::hardware::graphics::common::Interlaced;
37 using aidl::android::hardware::graphics::common::PlaneLayout;
38 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
39 using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
40 using aidl::android::hardware::graphics::common::Rect;
41 using aidl::android::hardware::graphics::common::Smpte2086;
42 using aidl::android::hardware::graphics::common::StandardMetadataType;
43 using aidl::android::hardware::graphics::common::XyColor;
44
45 using BufferDescriptorInfo = android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
46 using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
47
48 namespace android {
49
50 namespace gralloc4 {
51
hasAdditionOverflow(size_t a,size_t b)52 static inline bool hasAdditionOverflow(size_t a, size_t b) {
53 return a > SIZE_MAX - b;
54 }
55
56 /**
57 * OutputHidlVec represents the hidl_vec that is outputed when a type is encoded into a byte stream.
58 * This class is used to track the current state of a hidl_vec as it is filled with the encoded
59 * byte stream.
60 *
61 * This type is needed because hidl_vec's resize() allocates a new backing array every time.
62 * This type does not need an copies and only needs one resize operation.
63 */
64 class OutputHidlVec {
65 public:
OutputHidlVec(hidl_vec<uint8_t> * vec)66 OutputHidlVec(hidl_vec<uint8_t>* vec)
67 : mVec(vec) {}
68
resize()69 status_t resize() {
70 if (!mVec) {
71 return BAD_VALUE;
72 }
73 mVec->resize(mNeededResize);
74 mResized = true;
75 return NO_ERROR;
76 }
77
encode(const uint8_t * data,size_t size)78 status_t encode(const uint8_t* data, size_t size) {
79 if (!mVec) {
80 return BAD_VALUE;
81 }
82 if (!mResized) {
83 if (hasAdditionOverflow(mNeededResize, size)) {
84 clear();
85 return BAD_VALUE;
86 }
87 /**
88 * Update mNeededResize and return NO_ERROR here because if (!mResized), the
89 * caller hasn't called resize(). No data will be written into the mVec until
90 * the caller resizes. We can't resize here for the caller because hidl_vec::resize()
91 * allocates a new backing array every time.
92 */
93 mNeededResize += size;
94 return NO_ERROR;
95 }
96
97 if (hasAdditionOverflow(mOffset, size) || (mVec->size() < size + mOffset)) {
98 clear();
99 return BAD_VALUE;
100 }
101
102 std::copy(data, data + size, mVec->data() + mOffset);
103
104 mOffset += size;
105 return NO_ERROR;
106 }
107
clear()108 void clear() {
109 if (mVec) {
110 mVec->resize(0);
111 }
112 mNeededResize = 0;
113 mResized = false;
114 mOffset = 0;
115 }
116
117 private:
118 hidl_vec<uint8_t>* mVec;
119 size_t mNeededResize = 0;
120 size_t mResized = false;
121 size_t mOffset = 0;
122 };
123
124 /**
125 * InputHidlVec represents the hidl_vec byte stream that is inputed when a type is decoded.
126 * This class is used to track the current index of the byte stream of the hidl_vec as it is
127 * decoded.
128 */
129 class InputHidlVec {
130 public:
InputHidlVec(const hidl_vec<uint8_t> * vec)131 InputHidlVec(const hidl_vec<uint8_t>* vec)
132 : mVec(vec) {}
133
decode(uint8_t * data,size_t size)134 status_t decode(uint8_t* data, size_t size) {
135 if (!mVec || hasAdditionOverflow(mOffset, size) || mOffset + size > mVec->size()) {
136 return BAD_VALUE;
137 }
138
139 std::copy(mVec->data() + mOffset, mVec->data() + mOffset + size, data);
140
141 mOffset += size;
142 return NO_ERROR;
143 }
144
decode(std::string * string,size_t size)145 status_t decode(std::string* string, size_t size) {
146 if (!mVec || hasAdditionOverflow(mOffset, size) || mOffset + size > mVec->size()) {
147 return BAD_VALUE;
148 }
149
150 string->assign(mVec->data() + mOffset, mVec->data() + mOffset + size);
151
152 mOffset += size;
153 return NO_ERROR;
154 }
155
hasRemainingData()156 bool hasRemainingData() {
157 if (!mVec) {
158 return false;
159 }
160 return mVec->size() > mOffset;
161 }
162
getRemainingSize()163 size_t getRemainingSize() {
164 if (!mVec) {
165 return 0;
166 }
167 return mVec->size() - mOffset;
168 }
169
170 private:
171 const hidl_vec<uint8_t>* mVec;
172 size_t mOffset = 0;
173 };
174
175 /**
176 * EncodeHelper is a function type that encodes T into the OutputHidlVec.
177 */
178 template<class T>
179 using EncodeHelper = status_t(*)(const T&, OutputHidlVec*);
180
181 /**
182 * DecodeHelper is a function type that decodes InputHidlVec into T.
183 */
184 template<class T>
185 using DecodeHelper = status_t(*)(InputHidlVec*, T*);
186
187 /**
188 * ErrorHandler is a function type that is called when the corresponding DecodeHelper function
189 * fails. ErrorHandler cleans up the object T so the caller doesn't receive a partially created
190 * T.
191 */
192 template<class T>
193 using ErrorHandler = void(*)(T*);
194
195 status_t encodeMetadataType(const MetadataType& input, OutputHidlVec* output);
196 status_t validateMetadataType(InputHidlVec* input, const MetadataType& expectedMetadataType);
197
198 /**
199 * encode/encodeMetadata are the main encoding functions. They take in T and uses the encodeHelper
200 * function to turn T into the hidl_vec byte stream.
201 *
202 * These functions first call the encodeHelper function to determine how large the hidl_vec
203 * needs to be. They resize the hidl_vec. Finally, it reruns the encodeHelper function which
204 * encodes T into the hidl_vec byte stream.
205 */
206 template <class T>
encode(const T & input,hidl_vec<uint8_t> * output,EncodeHelper<T> encodeHelper)207 status_t encode(const T& input, hidl_vec<uint8_t>* output, EncodeHelper<T> encodeHelper) {
208 OutputHidlVec outputHidlVec{output};
209
210 status_t err = encodeHelper(input, &outputHidlVec);
211 if (err) {
212 return err;
213 }
214
215 err = outputHidlVec.resize();
216 if (err) {
217 return err;
218 }
219
220 return encodeHelper(input, &outputHidlVec);
221 }
222
223 template <class T>
encodeMetadata(const MetadataType & metadataType,const T & input,hidl_vec<uint8_t> * output,EncodeHelper<T> encodeHelper)224 status_t encodeMetadata(const MetadataType& metadataType, const T& input, hidl_vec<uint8_t>* output,
225 EncodeHelper<T> encodeHelper) {
226 OutputHidlVec outputHidlVec{output};
227
228 status_t err = encodeMetadataType(metadataType, &outputHidlVec);
229 if (err) {
230 return err;
231 }
232
233 err = encodeHelper(input, &outputHidlVec);
234 if (err) {
235 return err;
236 }
237
238 err = outputHidlVec.resize();
239 if (err) {
240 return err;
241 }
242
243 err = encodeMetadataType(metadataType, &outputHidlVec);
244 if (err) {
245 return err;
246 }
247
248 return encodeHelper(input, &outputHidlVec);
249 }
250
251 template <class T>
encodeOptionalMetadata(const MetadataType & metadataType,const std::optional<T> & input,hidl_vec<uint8_t> * output,EncodeHelper<T> encodeHelper)252 status_t encodeOptionalMetadata(const MetadataType& metadataType, const std::optional<T>& input,
253 hidl_vec<uint8_t>* output, EncodeHelper<T> encodeHelper) {
254 if (!input) {
255 return NO_ERROR;
256 }
257 return encodeMetadata(metadataType, *input, output, encodeHelper);
258 }
259
260 /**
261 * decode/decodeMetadata are the main decoding functions. They take in a hidl_vec and use the
262 * decodeHelper function to turn the hidl_vec byte stream into T. If an error occurs, the
263 * errorHandler function cleans up T.
264 */
265 template <class T>
decode(const hidl_vec<uint8_t> & input,T * output,DecodeHelper<T> decodeHelper,ErrorHandler<T> errorHandler=nullptr)266 status_t decode(const hidl_vec<uint8_t>& input, T* output, DecodeHelper<T> decodeHelper,
267 ErrorHandler<T> errorHandler = nullptr) {
268 InputHidlVec inputHidlVec{&input};
269
270 status_t err = decodeHelper(&inputHidlVec, output);
271 if (err) {
272 return err;
273 }
274
275 err = inputHidlVec.hasRemainingData();
276 if (err) {
277 if (errorHandler) {
278 errorHandler(output);
279 }
280 return BAD_VALUE;
281 }
282
283 return NO_ERROR;
284 }
285
286 template <class T>
decodeMetadata(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,T * output,DecodeHelper<T> decodeHelper,ErrorHandler<T> errorHandler=nullptr)287 status_t decodeMetadata(const MetadataType& metadataType, const hidl_vec<uint8_t>& input, T* output,
288 DecodeHelper<T> decodeHelper, ErrorHandler<T> errorHandler = nullptr) {
289 InputHidlVec inputHidlVec{&input};
290
291 status_t err = validateMetadataType(&inputHidlVec, metadataType);
292 if (err) {
293 return err;
294 }
295
296 err = decodeHelper(&inputHidlVec, output);
297 if (err) {
298 return err;
299 }
300
301 err = inputHidlVec.hasRemainingData();
302 if (err) {
303 if (errorHandler) {
304 errorHandler(output);
305 }
306 return BAD_VALUE;
307 }
308
309 return NO_ERROR;
310 }
311
312 template <class T>
decodeOptionalMetadata(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,std::optional<T> * output,DecodeHelper<T> decodeHelper)313 status_t decodeOptionalMetadata(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
314 std::optional<T>* output, DecodeHelper<T> decodeHelper) {
315 if (!output) {
316 return BAD_VALUE;
317 }
318 if (input.size() <= 0) {
319 output->reset();
320 return NO_ERROR;
321 }
322 T tmp;
323 status_t err = decodeMetadata(metadataType, input, &tmp, decodeHelper);
324 if (!err) {
325 *output = tmp;
326 }
327 return err;
328 }
329
330 /**
331 * Private helper functions
332 */
333 template <class T>
encodeInteger(const T & input,OutputHidlVec * output)334 status_t encodeInteger(const T& input, OutputHidlVec* output) {
335 static_assert(std::is_same<T, uint32_t>::value || std::is_same<T, int32_t>::value ||
336 std::is_same<T, uint64_t>::value || std::is_same<T, int64_t>::value ||
337 std::is_same<T, float>::value || std::is_same<T, double>::value);
338 if (!output) {
339 return BAD_VALUE;
340 }
341
342 const uint8_t* tmp = reinterpret_cast<const uint8_t*>(&input);
343 return output->encode(tmp, sizeof(input));
344 }
345
346 template <class T>
decodeInteger(InputHidlVec * input,T * output)347 status_t decodeInteger(InputHidlVec* input, T* output) {
348 static_assert(std::is_same<T, uint32_t>::value || std::is_same<T, int32_t>::value ||
349 std::is_same<T, uint64_t>::value || std::is_same<T, int64_t>::value ||
350 std::is_same<T, float>::value || std::is_same<T, double>::value);
351 if (!output) {
352 return BAD_VALUE;
353 }
354
355 uint8_t* tmp = reinterpret_cast<uint8_t*>(output);
356 return input->decode(tmp, sizeof(*output));
357 }
358
encodeString(const std::string & input,OutputHidlVec * output)359 status_t encodeString(const std::string& input, OutputHidlVec* output) {
360 if (!output) {
361 return BAD_VALUE;
362 }
363
364 status_t err = encodeInteger<int64_t>(input.size(), output);
365 if (err) {
366 return err;
367 }
368
369 return output->encode(reinterpret_cast<const uint8_t*>(input.data()), input.size());
370 }
371
decodeString(InputHidlVec * input,std::string * output)372 status_t decodeString(InputHidlVec* input, std::string* output) {
373 if (!output) {
374 return BAD_VALUE;
375 }
376
377 int64_t size = 0;
378 status_t err = decodeInteger<int64_t>(input, &size);
379 if (err) {
380 return err;
381 }
382 if (size < 0) {
383 return BAD_VALUE;
384 }
385
386 return input->decode(output, size);
387 }
388
encodeByteVector(const std::vector<uint8_t> & input,OutputHidlVec * output)389 status_t encodeByteVector(const std::vector<uint8_t>& input, OutputHidlVec* output) {
390 if (!output) {
391 return BAD_VALUE;
392 }
393
394 status_t err = encodeInteger<int64_t>(input.size(), output);
395 if (err) {
396 return err;
397 }
398
399 return output->encode(input.data(), input.size());
400 }
401
decodeByteVector(InputHidlVec * input,std::vector<uint8_t> * output)402 status_t decodeByteVector(InputHidlVec* input, std::vector<uint8_t>* output) {
403 if (!output) {
404 return BAD_VALUE;
405 }
406
407 int64_t size = 0;
408 status_t err = decodeInteger<int64_t>(input, &size);
409 if (err || size < 0) {
410 return err;
411 }
412
413 if (size > input->getRemainingSize()) {
414 return BAD_VALUE;
415 }
416 output->resize(size);
417
418 return input->decode(output->data(), size);
419 }
420
encodeExtendableType(const ExtendableType & input,OutputHidlVec * output)421 status_t encodeExtendableType(const ExtendableType& input, OutputHidlVec* output) {
422 status_t err = encodeString(input.name, output);
423 if (err) {
424 return err;
425 }
426
427 err = encodeInteger<int64_t>(input.value, output);
428 if (err) {
429 return err;
430 }
431
432 return NO_ERROR;
433 }
434
decodeExtendableType(InputHidlVec * input,ExtendableType * output)435 status_t decodeExtendableType(InputHidlVec* input, ExtendableType* output) {
436 status_t err = decodeString(input, &output->name);
437 if (err) {
438 return err;
439 }
440
441 err = decodeInteger<int64_t>(input, &output->value);
442 if (err) {
443 return err;
444 }
445
446 return NO_ERROR;
447 }
448
clearExtendableType(ExtendableType * output)449 void clearExtendableType(ExtendableType* output) {
450 if (!output) {
451 return;
452 }
453 output->name.clear();
454 output->value = 0;
455 }
456
encodeMetadataType(const MetadataType & input,OutputHidlVec * output)457 status_t encodeMetadataType(const MetadataType& input, OutputHidlVec* output) {
458 status_t err = encodeString(input.name, output);
459 if (err) {
460 return err;
461 }
462
463 err = encodeInteger<int64_t>(input.value, output);
464 if (err) {
465 return err;
466 }
467
468 return NO_ERROR;
469 }
470
decodeMetadataType(InputHidlVec * input,MetadataType * output)471 status_t decodeMetadataType(InputHidlVec* input, MetadataType* output) {
472 std::string name;
473 status_t err = decodeString(input, &name);
474 if (err) {
475 return err;
476 }
477 output->name = name;
478
479 err = decodeInteger<int64_t>(input, &output->value);
480 if (err) {
481 return err;
482 }
483
484 return NO_ERROR;
485 }
486
validateMetadataType(InputHidlVec * input,const MetadataType & expectedMetadataType)487 status_t validateMetadataType(InputHidlVec* input, const MetadataType& expectedMetadataType) {
488 MetadataType receivedMetadataType;
489
490 status_t err = decodeMetadataType(input, &receivedMetadataType);
491 if (err) {
492 return err;
493 }
494
495 if (expectedMetadataType.name != receivedMetadataType.name) {
496 return BAD_VALUE;
497 }
498
499 if (receivedMetadataType.value != expectedMetadataType.value) {
500 return BAD_VALUE;
501 }
502
503 return NO_ERROR;
504 }
505
encodeXyColor(const XyColor & input,OutputHidlVec * output)506 status_t encodeXyColor(const XyColor& input, OutputHidlVec* output) {
507 status_t err = encodeInteger<float>(input.x, output);
508 if (err) {
509 return err;
510 }
511 return encodeInteger<float>(input.y, output);
512 }
513
decodeXyColor(InputHidlVec * input,XyColor * output)514 status_t decodeXyColor(InputHidlVec* input, XyColor* output) {
515 status_t err = decodeInteger<float>(input, &output->x);
516 if (err) {
517 return err;
518 }
519 return decodeInteger<float>(input, &output->y);
520 }
521
clearXyColor(XyColor * output)522 void clearXyColor(XyColor* output) {
523 if (!output) {
524 return;
525 }
526 output->x = 0;
527 output->y = 0;
528 }
529
encodeRect(const Rect & input,OutputHidlVec * output)530 status_t encodeRect(const Rect& input, OutputHidlVec* output) {
531 status_t err = encodeInteger<int32_t>(static_cast<int32_t>(input.left), output);
532 if (err) {
533 return err;
534 }
535 err = encodeInteger<int32_t>(static_cast<int32_t>(input.top), output);
536 if (err) {
537 return err;
538 }
539 err = encodeInteger<int32_t>(static_cast<int32_t>(input.right), output);
540 if (err) {
541 return err;
542 }
543 return encodeInteger<int32_t>(static_cast<int32_t>(input.bottom), output);
544 }
545
decodeRect(InputHidlVec * input,Rect * output)546 status_t decodeRect(InputHidlVec* input, Rect* output) {
547 status_t err = decodeInteger<int32_t>(input, &output->left);
548 if (err) {
549 return err;
550 }
551 err = decodeInteger<int32_t>(input, &output->top);
552 if (err) {
553 return err;
554 }
555 err = decodeInteger<int32_t>(input, &output->right);
556 if (err) {
557 return err;
558 }
559 return decodeInteger<int32_t>(input, &output->bottom);
560 }
561
encodeBufferDescriptorInfoHelper(const BufferDescriptorInfo & input,OutputHidlVec * output)562 status_t encodeBufferDescriptorInfoHelper(const BufferDescriptorInfo& input,
563 OutputHidlVec* output) {
564 status_t err = encodeString(input.name, output);
565 if (err) {
566 return err;
567 }
568 err = encodeInteger<uint32_t>(input.width, output);
569 if (err) {
570 return err;
571 }
572 err = encodeInteger<uint32_t>(input.height, output);
573 if (err) {
574 return err;
575 }
576 err = encodeInteger<uint32_t>(input.layerCount, output);
577 if (err) {
578 return err;
579 }
580 err = encodeInteger<int32_t>(static_cast<int32_t>(input.format), output);
581 if (err) {
582 return err;
583 }
584 err = encodeInteger<uint64_t>(input.usage, output);
585 if (err) {
586 return err;
587 }
588 return encodeInteger<uint64_t>(input.reservedSize, output);
589 }
590
decodeBufferDescriptorInfoHelper(InputHidlVec * input,BufferDescriptorInfo * output)591 status_t decodeBufferDescriptorInfoHelper(InputHidlVec* input, BufferDescriptorInfo* output) {
592 std::string name;
593 status_t err = decodeString(input, &name);
594 if (err) {
595 return err;
596 }
597 output->name = name;
598
599 err = decodeInteger<uint32_t>(input, &output->width);
600 if (err) {
601 return err;
602 }
603 err = decodeInteger<uint32_t>(input, &output->height);
604 if (err) {
605 return err;
606 }
607 err = decodeInteger<uint32_t>(input, &output->layerCount);
608 if (err) {
609 return err;
610 }
611 err = decodeInteger<int32_t>(input, reinterpret_cast<int32_t*>(&output->format));
612 if (err) {
613 return err;
614 }
615 err = decodeInteger<uint64_t>(input, &output->usage);
616 if (err) {
617 return err;
618 }
619 return decodeInteger<uint64_t>(input, &output->reservedSize);
620 }
621
encodePlaneLayoutComponent(const PlaneLayoutComponent & input,OutputHidlVec * output)622 status_t encodePlaneLayoutComponent(const PlaneLayoutComponent& input, OutputHidlVec* output) {
623 if (!output) {
624 return BAD_VALUE;
625 }
626
627 status_t err = encodeExtendableType(input.type, output);
628 if (err) {
629 return err;
630 }
631 err = encodeInteger<int64_t>(static_cast<int64_t>(input.offsetInBits), output);
632 if (err) {
633 return err;
634 }
635 return encodeInteger<int64_t>(static_cast<int64_t>(input.sizeInBits), output);
636 }
637
decodePlaneLayoutComponent(InputHidlVec * input,PlaneLayoutComponent * output)638 status_t decodePlaneLayoutComponent(InputHidlVec* input, PlaneLayoutComponent* output) {
639 if (!output) {
640 return BAD_VALUE;
641 }
642
643 status_t err = decodeExtendableType(input, &output->type);
644 if (err) {
645 return err;
646 }
647 err = decodeInteger<int64_t>(input, &output->offsetInBits);
648 if (err) {
649 return err;
650 }
651 return decodeInteger<int64_t>(input, &output->sizeInBits);
652 }
653
encodePlaneLayoutComponents(const std::vector<PlaneLayoutComponent> & input,OutputHidlVec * output)654 status_t encodePlaneLayoutComponents(const std::vector<PlaneLayoutComponent>& input, OutputHidlVec* output) {
655 if (!output) {
656 return BAD_VALUE;
657 }
658
659 status_t err = encodeInteger<int64_t>(static_cast<int64_t>(input.size()), output);
660 if (err) {
661 return err;
662 }
663
664 for (const auto& planeLayoutComponent: input) {
665 err = encodePlaneLayoutComponent(planeLayoutComponent, output);
666 if (err) {
667 return err;
668 }
669 }
670
671 return NO_ERROR;
672 }
673
decodePlaneLayoutComponents(InputHidlVec * input,std::vector<PlaneLayoutComponent> * output)674 status_t decodePlaneLayoutComponents(InputHidlVec* input, std::vector<PlaneLayoutComponent>* output) {
675 if (!output) {
676 return BAD_VALUE;
677 }
678
679 int64_t size = 0;
680 status_t err = decodeInteger<int64_t>(input, &size);
681 if (err) {
682 return err;
683 }
684 if (size < 0 || size > 10000) {
685 return BAD_VALUE;
686 }
687
688 output->resize(size);
689
690 for (auto& planeLayoutComponent : *output) {
691 err = decodePlaneLayoutComponent(input, &planeLayoutComponent);
692 if (err) {
693 return err;
694 }
695 }
696 return NO_ERROR;
697 }
698
encodePlaneLayout(const PlaneLayout & input,OutputHidlVec * output)699 status_t encodePlaneLayout(const PlaneLayout& input, OutputHidlVec* output) {
700 if (!output) {
701 return BAD_VALUE;
702 }
703
704 status_t err = encodePlaneLayoutComponents(input.components, output);
705 if (err) {
706 return err;
707 }
708
709 err = encodeInteger<int64_t>(static_cast<int64_t>(input.offsetInBytes), output);
710 if (err) {
711 return err;
712 }
713 err = encodeInteger<int64_t>(static_cast<int64_t>(input.sampleIncrementInBits), output);
714 if (err) {
715 return err;
716 }
717 err = encodeInteger<int64_t>(static_cast<int64_t>(input.strideInBytes), output);
718 if (err) {
719 return err;
720 }
721 err = encodeInteger<int64_t>(static_cast<int64_t>(input.widthInSamples), output);
722 if (err) {
723 return err;
724 }
725 err = encodeInteger<int64_t>(static_cast<int64_t>(input.heightInSamples), output);
726 if (err) {
727 return err;
728 }
729 err = encodeInteger<int64_t>(static_cast<int64_t>(input.totalSizeInBytes), output);
730 if (err) {
731 return err;
732 }
733 err = encodeInteger<int64_t>(static_cast<int64_t>(input.horizontalSubsampling), output);
734 if (err) {
735 return err;
736 }
737 return encodeInteger<int64_t>(static_cast<int64_t>(input.verticalSubsampling), output);
738 }
739
decodePlaneLayout(InputHidlVec * input,PlaneLayout * output)740 status_t decodePlaneLayout(InputHidlVec* input, PlaneLayout* output) {
741 if (!output) {
742 return BAD_VALUE;
743 }
744
745 status_t err = decodePlaneLayoutComponents(input, &output->components);
746 if (err) {
747 return err;
748 }
749
750 err = decodeInteger<int64_t>(input, &output->offsetInBytes);
751 if (err) {
752 return err;
753 }
754 err = decodeInteger<int64_t>(input, &output->sampleIncrementInBits);
755 if (err) {
756 return err;
757 }
758 err = decodeInteger<int64_t>(input, &output->strideInBytes);
759 if (err) {
760 return err;
761 }
762 err = decodeInteger<int64_t>(input, &output->widthInSamples);
763 if (err) {
764 return err;
765 }
766 err = decodeInteger<int64_t>(input, &output->heightInSamples);
767 if (err) {
768 return err;
769 }
770 err = decodeInteger<int64_t>(input, &output->totalSizeInBytes);
771 if (err) {
772 return err;
773 }
774 err = decodeInteger<int64_t>(input, &output->horizontalSubsampling);
775 if (err) {
776 return err;
777 }
778 return decodeInteger<int64_t>(input, &output->verticalSubsampling);
779 }
780
encodePlaneLayoutsHelper(const std::vector<PlaneLayout> & planeLayouts,OutputHidlVec * outOutputHidlVec)781 status_t encodePlaneLayoutsHelper(const std::vector<PlaneLayout>& planeLayouts, OutputHidlVec* outOutputHidlVec) {
782 status_t err = encodeInteger<int64_t>(static_cast<int64_t>(planeLayouts.size()), outOutputHidlVec);
783 if (err) {
784 return err;
785 }
786
787 for (const auto& planeLayout : planeLayouts) {
788 err = encodePlaneLayout(planeLayout, outOutputHidlVec);
789 if (err) {
790 return err;
791 }
792 }
793
794 return NO_ERROR;
795 }
796
decodePlaneLayoutsHelper(InputHidlVec * inputHidlVec,std::vector<PlaneLayout> * outPlaneLayouts)797 status_t decodePlaneLayoutsHelper(InputHidlVec* inputHidlVec, std::vector<PlaneLayout>* outPlaneLayouts) {
798 int64_t size = 0;
799 status_t err = decodeInteger<int64_t>(inputHidlVec, &size);
800 if (err) {
801 return err;
802 }
803 if (size < 0) {
804 return BAD_VALUE;
805 }
806
807 for (size_t i = 0; i < size; i++) {
808 outPlaneLayouts->emplace_back();
809 err = decodePlaneLayout(inputHidlVec, &outPlaneLayouts->back());
810 if (err) {
811 return err;
812 }
813 }
814 return NO_ERROR;
815 }
816
clearPlaneLayouts(std::vector<PlaneLayout> * output)817 void clearPlaneLayouts(std::vector<PlaneLayout>* output) {
818 if (!output) {
819 return;
820 }
821 output->clear();
822 }
823
encodeCropHelper(const std::vector<Rect> & crops,OutputHidlVec * outOutputHidlVec)824 status_t encodeCropHelper(const std::vector<Rect>& crops, OutputHidlVec* outOutputHidlVec) {
825 status_t err = encodeInteger<int64_t>(static_cast<int64_t>(crops.size()), outOutputHidlVec);
826 if (err) {
827 return err;
828 }
829
830 for (const auto& crop : crops) {
831 err = encodeRect(crop, outOutputHidlVec);
832 if (err) {
833 return err;
834 }
835 }
836
837 return NO_ERROR;
838 }
839
decodeCropHelper(InputHidlVec * inputHidlVec,std::vector<Rect> * outCrops)840 status_t decodeCropHelper(InputHidlVec* inputHidlVec, std::vector<Rect>* outCrops) {
841 int64_t size = 0;
842 status_t err = decodeInteger<int64_t>(inputHidlVec, &size);
843 if (err) {
844 return err;
845 }
846 if (size < 0) {
847 return BAD_VALUE;
848 }
849
850 for (size_t i = 0; i < size; i++) {
851 outCrops->emplace_back();
852 err = decodeRect(inputHidlVec, &outCrops->back());
853 if (err) {
854 return err;
855 }
856 }
857 return NO_ERROR;
858 }
859
clearCrop(std::vector<Rect> * output)860 void clearCrop(std::vector<Rect>* output) {
861 if (!output) {
862 return;
863 }
864 output->clear();
865 }
866
encodeSmpte2086Helper(const Smpte2086 & smpte2086,OutputHidlVec * outOutputHidlVec)867 status_t encodeSmpte2086Helper(const Smpte2086& smpte2086, OutputHidlVec* outOutputHidlVec) {
868 status_t err = encodeXyColor(smpte2086.primaryRed, outOutputHidlVec);
869 if (err) {
870 return err;
871 }
872 err = encodeXyColor(smpte2086.primaryGreen, outOutputHidlVec);
873 if (err) {
874 return err;
875 }
876 err = encodeXyColor(smpte2086.primaryBlue, outOutputHidlVec);
877 if (err) {
878 return err;
879 }
880 err = encodeXyColor(smpte2086.whitePoint, outOutputHidlVec);
881 if (err) {
882 return err;
883 }
884 err = encodeInteger<float>(smpte2086.maxLuminance, outOutputHidlVec);
885 if (err) {
886 return err;
887 }
888 return encodeInteger<float>(smpte2086.minLuminance, outOutputHidlVec);
889 }
890
decodeSmpte2086Helper(InputHidlVec * inputHidlVec,Smpte2086 * outSmpte2086)891 status_t decodeSmpte2086Helper(InputHidlVec* inputHidlVec, Smpte2086* outSmpte2086) {
892 status_t err = decodeXyColor(inputHidlVec, &outSmpte2086->primaryRed);
893 if (err) {
894 return err;
895 }
896 err = decodeXyColor(inputHidlVec, &outSmpte2086->primaryGreen);
897 if (err) {
898 return err;
899 }
900 err = decodeXyColor(inputHidlVec, &outSmpte2086->primaryBlue);
901 if (err) {
902 return err;
903 }
904 err = decodeXyColor(inputHidlVec, &outSmpte2086->whitePoint);
905 if (err) {
906 return err;
907 }
908 err = decodeInteger<float>(inputHidlVec, &outSmpte2086->maxLuminance);
909 if (err) {
910 return err;
911 }
912 return decodeInteger<float>(inputHidlVec, &outSmpte2086->minLuminance);
913 }
914
encodeCta861_3Helper(const Cta861_3 & cta861_3,OutputHidlVec * outOutputHidlVec)915 status_t encodeCta861_3Helper(const Cta861_3& cta861_3, OutputHidlVec* outOutputHidlVec) {
916 status_t err = encodeInteger<float>(cta861_3.maxContentLightLevel, outOutputHidlVec);
917 if (err) {
918 return err;
919 }
920 return encodeInteger<float>(cta861_3.maxFrameAverageLightLevel, outOutputHidlVec);
921 }
922
decodeCta861_3Helper(InputHidlVec * inputHidlVec,Cta861_3 * outCta861_3)923 status_t decodeCta861_3Helper(InputHidlVec* inputHidlVec, Cta861_3* outCta861_3) {
924 status_t err = decodeInteger<float>(inputHidlVec, &outCta861_3->maxContentLightLevel);
925 if (err) {
926 return err;
927 }
928 return decodeInteger<float>(inputHidlVec, &outCta861_3->maxFrameAverageLightLevel);
929 }
930
931 /**
932 * Public API functions
933 */
encodeBufferDescriptorInfo(const BufferDescriptorInfo & bufferDescriptorInfo,hidl_vec<uint8_t> * outBufferDescriptorInfo)934 status_t encodeBufferDescriptorInfo(const BufferDescriptorInfo& bufferDescriptorInfo,
935 hidl_vec<uint8_t>* outBufferDescriptorInfo) {
936 return encode(bufferDescriptorInfo, outBufferDescriptorInfo, encodeBufferDescriptorInfoHelper);
937 }
938
decodeBufferDescriptorInfo(const hidl_vec<uint8_t> & bufferDescriptorInfo,BufferDescriptorInfo * outBufferDescriptorInfo)939 status_t decodeBufferDescriptorInfo(const hidl_vec<uint8_t>& bufferDescriptorInfo,
940 BufferDescriptorInfo* outBufferDescriptorInfo) {
941 return decode(bufferDescriptorInfo, outBufferDescriptorInfo, decodeBufferDescriptorInfoHelper);
942 }
943
encodeBufferId(uint64_t bufferId,hidl_vec<uint8_t> * outBufferId)944 status_t encodeBufferId(uint64_t bufferId, hidl_vec<uint8_t>* outBufferId) {
945 return encodeMetadata(MetadataType_BufferId, bufferId, outBufferId, encodeInteger);
946 }
947
decodeBufferId(const hidl_vec<uint8_t> & bufferId,uint64_t * outBufferId)948 status_t decodeBufferId(const hidl_vec<uint8_t>& bufferId, uint64_t* outBufferId) {
949 return decodeMetadata(MetadataType_BufferId, bufferId, outBufferId, decodeInteger);
950 }
951
encodeName(const std::string & name,hidl_vec<uint8_t> * outName)952 status_t encodeName(const std::string& name, hidl_vec<uint8_t>* outName) {
953 return encodeMetadata(MetadataType_Name, name, outName, encodeString);
954 }
955
decodeName(const hidl_vec<uint8_t> & name,std::string * outName)956 status_t decodeName(const hidl_vec<uint8_t>& name, std::string* outName) {
957 return decodeMetadata(MetadataType_Name, name, outName, decodeString);
958 }
959
encodeWidth(uint64_t width,hidl_vec<uint8_t> * outWidth)960 status_t encodeWidth(uint64_t width, hidl_vec<uint8_t>* outWidth) {
961 return encodeMetadata(MetadataType_Width, width, outWidth, encodeInteger);
962 }
963
decodeWidth(const hidl_vec<uint8_t> & width,uint64_t * outWidth)964 status_t decodeWidth(const hidl_vec<uint8_t>& width, uint64_t* outWidth) {
965 return decodeMetadata(MetadataType_Width, width, outWidth, decodeInteger);
966 }
967
encodeHeight(uint64_t height,hidl_vec<uint8_t> * outHeight)968 status_t encodeHeight(uint64_t height, hidl_vec<uint8_t>* outHeight) {
969 return encodeMetadata(MetadataType_Height, height, outHeight, encodeInteger);
970 }
971
decodeHeight(const hidl_vec<uint8_t> & height,uint64_t * outHeight)972 status_t decodeHeight(const hidl_vec<uint8_t>& height, uint64_t* outHeight) {
973 return decodeMetadata(MetadataType_Height, height, outHeight, decodeInteger);
974 }
975
encodeLayerCount(uint64_t layerCount,hidl_vec<uint8_t> * outLayerCount)976 status_t encodeLayerCount(uint64_t layerCount, hidl_vec<uint8_t>* outLayerCount) {
977 return encodeMetadata(MetadataType_LayerCount, layerCount, outLayerCount, encodeInteger);
978 }
979
decodeLayerCount(const hidl_vec<uint8_t> & layerCount,uint64_t * outLayerCount)980 status_t decodeLayerCount(const hidl_vec<uint8_t>& layerCount, uint64_t* outLayerCount) {
981 return decodeMetadata(MetadataType_LayerCount, layerCount, outLayerCount, decodeInteger);
982 }
983
encodePixelFormatRequested(const hardware::graphics::common::V1_2::PixelFormat & pixelFormatRequested,hidl_vec<uint8_t> * outPixelFormatRequested)984 status_t encodePixelFormatRequested(const hardware::graphics::common::V1_2::PixelFormat& pixelFormatRequested,
985 hidl_vec<uint8_t>* outPixelFormatRequested) {
986 return encodeMetadata(MetadataType_PixelFormatRequested, static_cast<int32_t>(pixelFormatRequested),
987 outPixelFormatRequested, encodeInteger);
988 }
989
decodePixelFormatRequested(const hidl_vec<uint8_t> & pixelFormatRequested,hardware::graphics::common::V1_2::PixelFormat * outPixelFormatRequested)990 status_t decodePixelFormatRequested(const hidl_vec<uint8_t>& pixelFormatRequested,
991 hardware::graphics::common::V1_2::PixelFormat* outPixelFormatRequested) {
992 return decodeMetadata(MetadataType_PixelFormatRequested, pixelFormatRequested,
993 reinterpret_cast<int32_t*>(outPixelFormatRequested), decodeInteger);
994 }
995
encodePixelFormatFourCC(uint32_t pixelFormatFourCC,hidl_vec<uint8_t> * outPixelFormatFourCC)996 status_t encodePixelFormatFourCC(uint32_t pixelFormatFourCC, hidl_vec<uint8_t>* outPixelFormatFourCC) {
997 return encodeMetadata(MetadataType_PixelFormatFourCC, pixelFormatFourCC, outPixelFormatFourCC,
998 encodeInteger);
999 }
1000
decodePixelFormatFourCC(const hidl_vec<uint8_t> & pixelFormatFourCC,uint32_t * outPixelFormatFourCC)1001 status_t decodePixelFormatFourCC(const hidl_vec<uint8_t>& pixelFormatFourCC, uint32_t* outPixelFormatFourCC) {
1002 return decodeMetadata(MetadataType_PixelFormatFourCC, pixelFormatFourCC, outPixelFormatFourCC,
1003 decodeInteger);
1004 }
1005
encodePixelFormatModifier(uint64_t pixelFormatModifier,hidl_vec<uint8_t> * outPixelFormatModifier)1006 status_t encodePixelFormatModifier(uint64_t pixelFormatModifier, hidl_vec<uint8_t>* outPixelFormatModifier) {
1007 return encodeMetadata(MetadataType_PixelFormatModifier, pixelFormatModifier, outPixelFormatModifier,
1008 encodeInteger);
1009 }
1010
decodePixelFormatModifier(const hidl_vec<uint8_t> & pixelFormatModifier,uint64_t * outPixelFormatModifier)1011 status_t decodePixelFormatModifier(const hidl_vec<uint8_t>& pixelFormatModifier, uint64_t* outPixelFormatModifier) {
1012 return decodeMetadata(MetadataType_PixelFormatModifier, pixelFormatModifier, outPixelFormatModifier,
1013 decodeInteger);
1014 }
1015
encodeUsage(uint64_t usage,hidl_vec<uint8_t> * outUsage)1016 status_t encodeUsage(uint64_t usage, hidl_vec<uint8_t>* outUsage) {
1017 return encodeMetadata(MetadataType_Usage, usage, outUsage, encodeInteger);
1018 }
1019
decodeUsage(const hidl_vec<uint8_t> & usage,uint64_t * outUsage)1020 status_t decodeUsage(const hidl_vec<uint8_t>& usage, uint64_t* outUsage) {
1021 return decodeMetadata(MetadataType_Usage, usage, outUsage, decodeInteger);
1022 }
1023
encodeAllocationSize(uint64_t allocationSize,hidl_vec<uint8_t> * outAllocationSize)1024 status_t encodeAllocationSize(uint64_t allocationSize, hidl_vec<uint8_t>* outAllocationSize) {
1025 return encodeMetadata(MetadataType_AllocationSize, allocationSize, outAllocationSize, encodeInteger);
1026 }
1027
decodeAllocationSize(const hidl_vec<uint8_t> & allocationSize,uint64_t * outAllocationSize)1028 status_t decodeAllocationSize(const hidl_vec<uint8_t>& allocationSize, uint64_t* outAllocationSize) {
1029 return decodeMetadata(MetadataType_AllocationSize, allocationSize, outAllocationSize, decodeInteger);
1030 }
1031
encodeProtectedContent(uint64_t protectedContent,hidl_vec<uint8_t> * outProtectedContent)1032 status_t encodeProtectedContent(uint64_t protectedContent, hidl_vec<uint8_t>* outProtectedContent) {
1033 return encodeMetadata(MetadataType_ProtectedContent, protectedContent, outProtectedContent,
1034 encodeInteger);
1035 }
1036
decodeProtectedContent(const hidl_vec<uint8_t> & protectedContent,uint64_t * outProtectedContent)1037 status_t decodeProtectedContent(const hidl_vec<uint8_t>& protectedContent, uint64_t* outProtectedContent) {
1038 return decodeMetadata(MetadataType_ProtectedContent, protectedContent, outProtectedContent,
1039 decodeInteger);
1040 }
1041
encodeCompression(const ExtendableType & compression,hidl_vec<uint8_t> * outCompression)1042 status_t encodeCompression(const ExtendableType& compression, hidl_vec<uint8_t>* outCompression) {
1043 return encodeMetadata(MetadataType_Compression, compression, outCompression, encodeExtendableType);
1044 }
1045
decodeCompression(const hidl_vec<uint8_t> & compression,ExtendableType * outCompression)1046 status_t decodeCompression(const hidl_vec<uint8_t>& compression, ExtendableType* outCompression) {
1047 return decodeMetadata(MetadataType_Compression, compression, outCompression, decodeExtendableType,
1048 clearExtendableType);
1049 }
1050
encodeInterlaced(const ExtendableType & interlaced,hidl_vec<uint8_t> * outInterlaced)1051 status_t encodeInterlaced(const ExtendableType& interlaced, hidl_vec<uint8_t>* outInterlaced) {
1052 return encodeMetadata(MetadataType_Interlaced, interlaced, outInterlaced, encodeExtendableType);
1053 }
1054
decodeInterlaced(const hidl_vec<uint8_t> & interlaced,ExtendableType * outInterlaced)1055 status_t decodeInterlaced(const hidl_vec<uint8_t>& interlaced, ExtendableType* outInterlaced) {
1056 return decodeMetadata(MetadataType_Interlaced, interlaced, outInterlaced, decodeExtendableType,
1057 clearExtendableType);
1058 }
1059
encodeChromaSiting(const ExtendableType & chromaSiting,hidl_vec<uint8_t> * outChromaSiting)1060 status_t encodeChromaSiting(const ExtendableType& chromaSiting, hidl_vec<uint8_t>* outChromaSiting) {
1061 return encodeMetadata(MetadataType_ChromaSiting, chromaSiting, outChromaSiting, encodeExtendableType);
1062 }
1063
decodeChromaSiting(const hidl_vec<uint8_t> & chromaSiting,ExtendableType * outChromaSiting)1064 status_t decodeChromaSiting(const hidl_vec<uint8_t>& chromaSiting, ExtendableType* outChromaSiting) {
1065 return decodeMetadata(MetadataType_ChromaSiting, chromaSiting, outChromaSiting, decodeExtendableType,
1066 clearExtendableType);
1067 }
1068
encodePlaneLayouts(const std::vector<PlaneLayout> & planeLayouts,hidl_vec<uint8_t> * outPlaneLayouts)1069 status_t encodePlaneLayouts(const std::vector<PlaneLayout>& planeLayouts, hidl_vec<uint8_t>* outPlaneLayouts) {
1070 return encodeMetadata(MetadataType_PlaneLayouts, planeLayouts, outPlaneLayouts,
1071 encodePlaneLayoutsHelper);
1072 }
1073
decodePlaneLayouts(const hidl_vec<uint8_t> & planeLayouts,std::vector<PlaneLayout> * outPlaneLayouts)1074 status_t decodePlaneLayouts(const hidl_vec<uint8_t>& planeLayouts, std::vector<PlaneLayout>* outPlaneLayouts) {
1075 return decodeMetadata(MetadataType_PlaneLayouts, planeLayouts, outPlaneLayouts,
1076 decodePlaneLayoutsHelper, clearPlaneLayouts);
1077 }
1078
encodeCrop(const std::vector<Rect> & crop,hidl_vec<uint8_t> * outCrop)1079 status_t encodeCrop(const std::vector<Rect>& crop, hidl_vec<uint8_t>* outCrop) {
1080 return encodeMetadata(MetadataType_Crop, crop, outCrop, encodeCropHelper);
1081 }
1082
decodeCrop(const hidl_vec<uint8_t> & crop,std::vector<Rect> * outCrop)1083 status_t decodeCrop(const hidl_vec<uint8_t>& crop, std::vector<Rect>* outCrop) {
1084 return decodeMetadata(MetadataType_Crop, crop, outCrop, decodeCropHelper, clearCrop);
1085 }
1086
encodeDataspace(const Dataspace & dataspace,hidl_vec<uint8_t> * outDataspace)1087 status_t encodeDataspace(const Dataspace& dataspace, hidl_vec<uint8_t>* outDataspace) {
1088 return encodeMetadata(MetadataType_Dataspace, static_cast<int32_t>(dataspace), outDataspace,
1089 encodeInteger);
1090 }
1091
decodeDataspace(const hidl_vec<uint8_t> & dataspace,Dataspace * outDataspace)1092 status_t decodeDataspace(const hidl_vec<uint8_t>& dataspace, Dataspace* outDataspace) {
1093 return decodeMetadata(MetadataType_Dataspace, dataspace, reinterpret_cast<int32_t*>(outDataspace),
1094 decodeInteger);
1095 }
1096
encodeBlendMode(const BlendMode & blendMode,hidl_vec<uint8_t> * outBlendMode)1097 status_t encodeBlendMode(const BlendMode& blendMode, hidl_vec<uint8_t>* outBlendMode) {
1098 return encodeMetadata(MetadataType_BlendMode, static_cast<int32_t>(blendMode), outBlendMode,
1099 encodeInteger);
1100 }
1101
decodeBlendMode(const hidl_vec<uint8_t> & blendMode,BlendMode * outBlendMode)1102 status_t decodeBlendMode(const hidl_vec<uint8_t>& blendMode, BlendMode* outBlendMode) {
1103 return decodeMetadata(MetadataType_BlendMode, blendMode, reinterpret_cast<int32_t*>(outBlendMode),
1104 decodeInteger);
1105 }
1106
encodeSmpte2086(const std::optional<Smpte2086> & smpte2086,hidl_vec<uint8_t> * outSmpte2086)1107 status_t encodeSmpte2086(const std::optional<Smpte2086>& smpte2086,
1108 hidl_vec<uint8_t>* outSmpte2086) {
1109 return encodeOptionalMetadata(MetadataType_Smpte2086, smpte2086, outSmpte2086, encodeSmpte2086Helper);
1110 }
1111
decodeSmpte2086(const hidl_vec<uint8_t> & smpte2086,std::optional<Smpte2086> * outSmpte2086)1112 status_t decodeSmpte2086(const hidl_vec<uint8_t>& smpte2086,
1113 std::optional<Smpte2086>* outSmpte2086) {
1114 return decodeOptionalMetadata(MetadataType_Smpte2086, smpte2086, outSmpte2086, decodeSmpte2086Helper);
1115 }
1116
encodeCta861_3(const std::optional<Cta861_3> & cta861_3,hidl_vec<uint8_t> * outCta861_3)1117 status_t encodeCta861_3(const std::optional<Cta861_3>& cta861_3, hidl_vec<uint8_t>* outCta861_3) {
1118 return encodeOptionalMetadata(MetadataType_Cta861_3, cta861_3, outCta861_3, encodeCta861_3Helper);
1119 }
1120
decodeCta861_3(const hidl_vec<uint8_t> & cta861_3,std::optional<Cta861_3> * outCta861_3)1121 status_t decodeCta861_3(const hidl_vec<uint8_t>& cta861_3, std::optional<Cta861_3>* outCta861_3) {
1122 return decodeOptionalMetadata(MetadataType_Cta861_3, cta861_3, outCta861_3, decodeCta861_3Helper);
1123 }
1124
encodeSmpte2094_40(const std::optional<std::vector<uint8_t>> & smpte2094_40,hidl_vec<uint8_t> * outSmpte2094_40)1125 status_t encodeSmpte2094_40(const std::optional<std::vector<uint8_t>>& smpte2094_40,
1126 hidl_vec<uint8_t>* outSmpte2094_40) {
1127 return encodeOptionalMetadata(MetadataType_Smpte2094_40, smpte2094_40, outSmpte2094_40,
1128 encodeByteVector);
1129 }
1130
decodeSmpte2094_40(const hidl_vec<uint8_t> & smpte2094_40,std::optional<std::vector<uint8_t>> * outSmpte2094_40)1131 status_t decodeSmpte2094_40(const hidl_vec<uint8_t>& smpte2094_40,
1132 std::optional<std::vector<uint8_t>>* outSmpte2094_40) {
1133 return decodeOptionalMetadata(MetadataType_Smpte2094_40, smpte2094_40, outSmpte2094_40,
1134 decodeByteVector);
1135 }
1136
encodeUint32(const MetadataType & metadataType,uint32_t input,hidl_vec<uint8_t> * output)1137 status_t encodeUint32(const MetadataType& metadataType, uint32_t input,
1138 hidl_vec<uint8_t>* output) {
1139 return encodeMetadata(metadataType, input, output, encodeInteger);
1140 }
1141
decodeUint32(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,uint32_t * output)1142 status_t decodeUint32(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1143 uint32_t* output) {
1144 return decodeMetadata(metadataType, input, output, decodeInteger);
1145 }
1146
encodeInt32(const MetadataType & metadataType,int32_t input,hidl_vec<uint8_t> * output)1147 status_t encodeInt32(const MetadataType& metadataType, int32_t input,
1148 hidl_vec<uint8_t>* output) {
1149 return encodeMetadata(metadataType, input, output, encodeInteger);
1150 }
1151
decodeInt32(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,int32_t * output)1152 status_t decodeInt32(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1153 int32_t* output) {
1154 return decodeMetadata(metadataType, input, output, decodeInteger);
1155 }
1156
encodeUint64(const MetadataType & metadataType,uint64_t input,hidl_vec<uint8_t> * output)1157 status_t encodeUint64(const MetadataType& metadataType, uint64_t input,
1158 hidl_vec<uint8_t>* output) {
1159 return encodeMetadata(metadataType, input, output, encodeInteger);
1160 }
1161
decodeUint64(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,uint64_t * output)1162 status_t decodeUint64(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1163 uint64_t* output) {
1164 return decodeMetadata(metadataType, input, output, decodeInteger);
1165 }
1166
encodeInt64(const MetadataType & metadataType,int64_t input,hidl_vec<uint8_t> * output)1167 status_t encodeInt64(const MetadataType& metadataType, int64_t input,
1168 hidl_vec<uint8_t>* output) {
1169 return encodeMetadata(metadataType, input, output, encodeInteger);
1170 }
1171
decodeInt64(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,int64_t * output)1172 status_t decodeInt64(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1173 int64_t* output) {
1174 return decodeMetadata(metadataType, input, output, decodeInteger);
1175 }
1176
encodeFloat(const MetadataType & metadataType,float input,hidl_vec<uint8_t> * output)1177 status_t encodeFloat(const MetadataType& metadataType, float input,
1178 hidl_vec<uint8_t>* output) {
1179 return encodeMetadata(metadataType, input, output, encodeInteger);
1180 }
1181
decodeFloat(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,float * output)1182 status_t decodeFloat(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1183 float* output) {
1184 return decodeMetadata(metadataType, input, output, decodeInteger);
1185 }
1186
encodeDouble(const MetadataType & metadataType,double input,hidl_vec<uint8_t> * output)1187 status_t encodeDouble(const MetadataType& metadataType, double input,
1188 hidl_vec<uint8_t>* output) {
1189 return encodeMetadata(metadataType, input, output, encodeInteger);
1190 }
1191
decodeDouble(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,double * output)1192 status_t decodeDouble(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1193 double* output) {
1194 return decodeMetadata(metadataType, input, output, decodeInteger);
1195 }
1196
encodeString(const MetadataType & metadataType,const std::string & input,hidl_vec<uint8_t> * output)1197 status_t encodeString(const MetadataType& metadataType, const std::string& input,
1198 hidl_vec<uint8_t>* output) {
1199 return encodeMetadata(metadataType, input, output, encodeString);
1200 }
1201
decodeString(const MetadataType & metadataType,const hidl_vec<uint8_t> & input,std::string * output)1202 status_t decodeString(const MetadataType& metadataType, const hidl_vec<uint8_t>& input,
1203 std::string* output) {
1204 return decodeMetadata(metadataType, input, output, decodeString);
1205 }
1206
isStandardMetadataType(const MetadataType & metadataType)1207 bool isStandardMetadataType(const MetadataType& metadataType) {
1208 return !std::strncmp(metadataType.name.c_str(), GRALLOC4_STANDARD_METADATA_TYPE,
1209 metadataType.name.size());
1210 }
1211
isStandardCompression(const ExtendableType & compression)1212 bool isStandardCompression(const ExtendableType& compression) {
1213 return !std::strncmp(compression.name.c_str(), GRALLOC4_STANDARD_COMPRESSION,
1214 compression.name.size());
1215 }
1216
isStandardInterlaced(const ExtendableType & interlaced)1217 bool isStandardInterlaced(const ExtendableType& interlaced) {
1218 return !std::strncmp(interlaced.name.c_str(), GRALLOC4_STANDARD_INTERLACED,
1219 interlaced.name.size());
1220 }
1221
isStandardChromaSiting(const ExtendableType & chromaSiting)1222 bool isStandardChromaSiting(const ExtendableType& chromaSiting) {
1223 return !std::strncmp(chromaSiting.name.c_str(), GRALLOC4_STANDARD_CHROMA_SITING,
1224 chromaSiting.name.size());
1225 }
1226
isStandardPlaneLayoutComponentType(const ExtendableType & planeLayoutComponentType)1227 bool isStandardPlaneLayoutComponentType(const ExtendableType& planeLayoutComponentType) {
1228 return !std::strncmp(planeLayoutComponentType.name.c_str(), GRALLOC4_STANDARD_PLANE_LAYOUT_COMPONENT_TYPE,
1229 planeLayoutComponentType.name.size());
1230 }
1231
getStandardMetadataTypeValue(const MetadataType & metadataType)1232 StandardMetadataType getStandardMetadataTypeValue(const MetadataType& metadataType) {
1233 return static_cast<StandardMetadataType>(metadataType.value);
1234 }
1235
getStandardCompressionValue(const ExtendableType & compression)1236 Compression getStandardCompressionValue(const ExtendableType& compression) {
1237 return static_cast<Compression>(compression.value);
1238 }
1239
getStandardInterlacedValue(const ExtendableType & interlaced)1240 Interlaced getStandardInterlacedValue(const ExtendableType& interlaced) {
1241 return static_cast<Interlaced>(interlaced.value);
1242 }
1243
getStandardChromaSitingValue(const ExtendableType & chromaSiting)1244 ChromaSiting getStandardChromaSitingValue(const ExtendableType& chromaSiting) {
1245 return static_cast<ChromaSiting>(chromaSiting.value);
1246 }
1247
getStandardPlaneLayoutComponentTypeValue(const ExtendableType & planeLayoutComponentType)1248 PlaneLayoutComponentType getStandardPlaneLayoutComponentTypeValue(
1249 const ExtendableType& planeLayoutComponentType) {
1250 return static_cast<PlaneLayoutComponentType>(planeLayoutComponentType.value);
1251 }
1252
getCompressionName(const ExtendableType & compression)1253 std::string getCompressionName(const ExtendableType& compression) {
1254 if (!isStandardCompression(compression)) {
1255 std::ostringstream stream;
1256 stream << compression.name << "#" << compression.value;
1257 return stream.str();
1258 }
1259 switch (getStandardCompressionValue(compression)) {
1260 case Compression::NONE:
1261 return "None";
1262 case Compression::DISPLAY_STREAM_COMPRESSION:
1263 return "DisplayStreamCompression";
1264 }
1265 }
1266
getInterlacedName(const ExtendableType & interlaced)1267 std::string getInterlacedName(const ExtendableType& interlaced) {
1268 if (!isStandardInterlaced(interlaced)) {
1269 std::ostringstream stream;
1270 stream << interlaced.name << "#" << interlaced.value;
1271 return stream.str();
1272 }
1273 switch (getStandardInterlacedValue(interlaced)) {
1274 case Interlaced::NONE:
1275 return "None";
1276 case Interlaced::TOP_BOTTOM:
1277 return "TopBottom";
1278 case Interlaced::RIGHT_LEFT:
1279 return "RightLeft";
1280 }
1281 }
1282
getChromaSitingName(const ExtendableType & chromaSiting)1283 std::string getChromaSitingName(const ExtendableType& chromaSiting) {
1284 if (!isStandardChromaSiting(chromaSiting)) {
1285 std::ostringstream stream;
1286 stream << chromaSiting.name << "#" << chromaSiting.value;
1287 return stream.str();
1288 }
1289 switch (getStandardChromaSitingValue(chromaSiting)) {
1290 case ChromaSiting::NONE:
1291 return "None";
1292 case ChromaSiting::UNKNOWN:
1293 return "Unknown";
1294 case ChromaSiting::SITED_INTERSTITIAL:
1295 return "SitedInterstitial";
1296 case ChromaSiting::COSITED_HORIZONTAL:
1297 return "CositedHorizontal";
1298 }
1299 }
1300
getPlaneLayoutComponentTypeName(const ExtendableType & planeLayoutComponentType)1301 std::string getPlaneLayoutComponentTypeName(const ExtendableType& planeLayoutComponentType) {
1302 if (!isStandardPlaneLayoutComponentType(planeLayoutComponentType)) {
1303 std::ostringstream stream;
1304 stream << planeLayoutComponentType.name << "#" << planeLayoutComponentType.value;
1305 return stream.str();
1306 }
1307 switch (getStandardPlaneLayoutComponentTypeValue(planeLayoutComponentType)) {
1308 case PlaneLayoutComponentType::Y:
1309 return "Y";
1310 case PlaneLayoutComponentType::CB:
1311 return "Cb";
1312 case PlaneLayoutComponentType::CR:
1313 return "Cr";
1314 case PlaneLayoutComponentType::R:
1315 return "R";
1316 case PlaneLayoutComponentType::G:
1317 return "G";
1318 case PlaneLayoutComponentType::B:
1319 return "B";
1320 case PlaneLayoutComponentType::RAW:
1321 return "RAW";
1322 case PlaneLayoutComponentType::A:
1323 return "A";
1324 }
1325 }
1326
1327 } // namespace gralloc4
1328
1329 } // namespace android
1330