1d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/*
2d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Copyright 2016, The Android Open Source Project
3d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
4d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Licensed under the Apache License, Version 2.0 (the "License");
5d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * you may not use this file except in compliance with the License.
6d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * You may obtain a copy of the License at
7d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
8d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *      http://www.apache.org/licenses/LICENSE-2.0
9d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
10d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Unless required by applicable law or agreed to in writing, software
11d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * distributed under the License is distributed on an "AS IS" BASIS,
12d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * See the License for the specific language governing permissions and
14d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * limitations under the License.
15d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
16d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
17d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#ifndef ANDROID_HARDWARE_MEDIA_OMX_V1_0__CONVERSION_H
18d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#define ANDROID_HARDWARE_MEDIA_OMX_V1_0__CONVERSION_H
19d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
20d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <vector>
21d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <list>
22d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
23d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <unistd.h>
24d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
25d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <hidl/MQDescriptor.h>
26d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <hidl/Status.h>
27d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <hidlmemory/mapping.h>
28d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
29d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <binder/Binder.h>
30d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <binder/Status.h>
31d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <ui/FenceTime.h>
32d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <cutils/native_handle.h>
33d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
34d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <ui/GraphicBuffer.h>
35d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <media/OMXFenceParcelable.h>
36d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <media/OMXBuffer.h>
37d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <media/hardware/VideoAPI.h>
38d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
39d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hidl/memory/1.0/IMemory.h>
40d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hardware/media/omx/1.0/types.h>
41d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hardware/media/omx/1.0/IOmx.h>
42d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hardware/media/omx/1.0/IOmxNode.h>
43d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hardware/media/omx/1.0/IOmxBufferSource.h>
44d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hardware/media/omx/1.0/IOmxObserver.h>
45d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/hardware/media/omx/1.0/IGraphicBufferSource.h>
46d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
47d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/IGraphicBufferSource.h>
48d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#include <android/IOMXBufferSource.h>
49d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
50d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasanamespace android {
51d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasanamespace hardware {
52d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasanamespace media {
53d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasanamespace omx {
54d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasanamespace V1_0 {
55d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasanamespace utils {
56d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
57d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::hidl_array;
58d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::hidl_string;
59d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::hidl_vec;
60d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::hidl_handle;
61d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::Return;
62d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::Void;
63d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::sp;
64d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
65d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::String8;
66d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::OMXFenceParcelable;
67d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
68d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::omx::V1_0::Message;
69d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::omx_message;
70d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
71d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::omx::V1_0::ColorAspects;
72d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::V1_0::Rect;
73d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::V1_0::Region;
74d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
75d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::graphics::common::V1_0::Dataspace;
76d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
77d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::graphics::common::V1_0::PixelFormat;
78d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
79d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::OMXBuffer;
80d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
81d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::V1_0::AnwBuffer;
82d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::GraphicBuffer;
83d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
84d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::omx::V1_0::IOmx;
85d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::IOMX;
86d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
87d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::omx::V1_0::IOmxNode;
88d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::IOMXNode;
89d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
90d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::omx::V1_0::IOmxObserver;
91d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::IOMXObserver;
92d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
93d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::hardware::media::omx::V1_0::IOmxBufferSource;
94d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasausing ::android::IOMXBufferSource;
95d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
96d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// native_handle_t helper functions.
97d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
98d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
99d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Take an fd and create a native handle containing only the given fd.
100d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * The created handle will need to be deleted manually with
101d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `native_handle_delete()`.
102d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
103d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] fd The source file descriptor (of type `int`).
104d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The create `native_handle_t*` that contains the given \p fd. If the
105d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * supplied \p fd is negative, the created native handle will contain no file
106d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * descriptors.
107d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
108d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * If the native handle cannot be created, the return value will be
109d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `nullptr`.
110d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
111d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * This function does not duplicate the file descriptor.
112d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
113d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline native_handle_t* native_handle_create_from_fd(int fd) {
114d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (fd < 0) {
115d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        return native_handle_create(0, 0);
116d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
117d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    native_handle_t* nh = native_handle_create(1, 0);
118d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (nh == nullptr) {
119d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        return nullptr;
120d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
121d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    nh->data[0] = fd;
122d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return nh;
123d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
124d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
125d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
126d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Extract a file descriptor from a native handle.
127d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
128d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] nh The source `native_handle_t*`.
129d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] index The index of the file descriptor in \p nh to read from. This
130d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * input has the default value of `0`.
131d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The `index`-th file descriptor in \p nh. If \p nh does not have
132d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * enough file descriptors, the returned value will be `-1`.
133d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
134d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * This function does not duplicate the file descriptor.
135d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
136d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline int native_handle_read_fd(native_handle_t const* nh, int index = 0) {
137d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return ((nh == nullptr) || (nh->numFds == 0) ||
138d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            (nh->numFds <= index) || (index < 0)) ?
139d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            -1 : nh->data[index];
140d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
141d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
142d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
143d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Conversion functions
144d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * ====================
145d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
146d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * There are two main directions of conversion:
147d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * - `inTargetType(...)`: Create a wrapper whose lifetime depends on the
148d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   input. The wrapper has type `TargetType`.
149d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * - `toTargetType(...)`: Create a standalone object of type `TargetType` that
150d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   corresponds to the input. The lifetime of the output does not depend on the
151d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   lifetime of the input.
152d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * - `wrapIn(TargetType*, ...)`: Same as `inTargetType()`, but for `TargetType`
153d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   that cannot be copied and/or moved efficiently, or when there are multiple
154d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   output arguments.
155d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * - `convertTo(TargetType*, ...)`: Same as `toTargetType()`, but for
156d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   `TargetType` that cannot be copied and/or moved efficiently, or when there
157d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *   are multiple output arguments.
158d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
159d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `wrapIn()` and `convertTo()` functions will take output arguments before
160d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * input arguments. Some of these functions might return a value to indicate
161d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * success or error.
162d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
163d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * In converting or wrapping something as a Treble type that contains a
164d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `hidl_handle`, `native_handle_t*` will need to be created and returned as
165d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * an additional output argument, hence only `wrapIn()` or `convertTo()` would
166d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * be available. The caller must call `native_handle_delete()` to deallocate the
167d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * returned native handle when it is no longer needed.
168d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
169d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * For types that contain file descriptors, `inTargetType()` and `wrapAs()` do
170d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * not perform duplication of file descriptors, while `toTargetType()` and
171d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `convertTo()` do.
172d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
173d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
174d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
175d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `Return<void>` to `binder::Status`.
176d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
177d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Return<void>`.
178d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `binder::Status`.
179d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
180d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Return<void> -> ::android::binder::Status
181d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline ::android::binder::Status toBinderStatus(
182d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        Return<void> const& t) {
183d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return ::android::binder::Status::fromExceptionCode(
184d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t.isOk() ? OK : UNKNOWN_ERROR,
185d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t.description().c_str());
186d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
187d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
188d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
189d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `Return<Status>` to `binder::Status`.
190d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
191d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Return<Status>`.
192d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `binder::Status`.
193d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
194d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Return<Status> -> ::android::binder::Status
195d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline ::android::binder::Status toBinderStatus(
196d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        Return<Status> const& t) {
197d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return ::android::binder::Status::fromStatusT(
198d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t.isOk() ? static_cast<status_t>(static_cast<Status>(t)) : UNKNOWN_ERROR);
199d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
200d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
201d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
202d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `Return<Status>` to `status_t`. This is for legacy binder
203d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * calls.
204d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
205d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Return<Status>`.
206d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `status_t`.
207d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
208d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * This function first check if \p t has a transport error. If it does, then the
209d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * return value is the transport error code. Otherwise, the return value is
210d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * converted from `Status` contained inside \p t.
211d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
212d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Note:
213d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * - This `Status` is omx-specific. It is defined in `types.hal`.
214d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * - The name of this function is not `convert`.
215d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
216d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Status -> status_t
217d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline status_t toStatusT(Return<Status> const& t) {
218d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return t.isOk() ? static_cast<status_t>(static_cast<Status>(t)) : UNKNOWN_ERROR;
219d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
220d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
221d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
222d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `Return<void>` to `status_t`. This is for legacy binder calls.
223d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
224d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Return<void>`.
225d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `status_t`.
226d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
227d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Return<void> -> status_t
228d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline status_t toStatusT(Return<void> const& t) {
229d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return t.isOk() ? OK : UNKNOWN_ERROR;
230d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
231d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
232d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
233d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `Status` to `status_t`. This is for legacy binder calls.
234d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
235d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Status`.
236d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return the corresponding `status_t`.
237d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
238d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Status -> status_t
239d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline status_t toStatusT(Status const& t) {
240d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<status_t>(t);
241d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
242d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
243d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
244d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `status_t` to `Status`.
245d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
246d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `status_t`.
247d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `Status`.
248d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
249d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: status_t -> Status
250d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline Status toStatus(status_t l) {
251d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<Status>(l);
252d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
253d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
254d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
255d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap `native_handle_t*` in `hidl_handle`.
256d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
257d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] nh The source `native_handle_t*`.
258d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The `hidl_handle` that points to \p nh.
259d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
260d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: native_handle_t* -> hidl_handle
261d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline hidl_handle inHidlHandle(native_handle_t const* nh) {
262d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return hidl_handle(nh);
263d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
264d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
265d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
266d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap an `omx_message` and construct the corresponding `Message`.
267d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
268d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] t The wrapper of type `Message`.
269d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] nh The native_handle_t referred to by `t->fence`.
270d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `omx_message`.
271d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `true` if the wrapping is successful; `false` otherwise.
272d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
273d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Upon success, \p nh will be created to hold the file descriptor stored in
274d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `l.fenceFd`, and `t->fence` will point to \p nh. \p nh will need to be
275d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * destroyed manually by `native_handle_delete()` when \p t is no longer needed.
276d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
277d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * Upon failure, \p nh will not be created and will not need to be deleted. \p t
278d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * will be invalid.
279d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
280d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap, omx_message -> Message, native_handle_t*
281d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool wrapAs(Message* t, native_handle_t** nh, omx_message const& l) {
282d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    *nh = native_handle_create_from_fd(l.fenceFd);
283d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (!*nh) {
284d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        return false;
285d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
286d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->fence = *nh;
287d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    switch (l.type) {
288d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case omx_message::EVENT:
289d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = Message::Type::EVENT;
290d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.eventData.event = uint32_t(l.u.event_data.event);
291d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.eventData.data1 = l.u.event_data.data1;
292d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.eventData.data2 = l.u.event_data.data2;
293d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.eventData.data3 = l.u.event_data.data3;
294d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.eventData.data4 = l.u.event_data.data4;
295d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
296d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case omx_message::EMPTY_BUFFER_DONE:
297d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = Message::Type::EMPTY_BUFFER_DONE;
298d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.bufferData.buffer = l.u.buffer_data.buffer;
299d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
300d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case omx_message::FILL_BUFFER_DONE:
301d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = Message::Type::FILL_BUFFER_DONE;
302d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.extendedBufferData.buffer = l.u.extended_buffer_data.buffer;
303d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.extendedBufferData.rangeOffset =
304d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    l.u.extended_buffer_data.range_offset;
305d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.extendedBufferData.rangeLength =
306d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    l.u.extended_buffer_data.range_length;
307d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.extendedBufferData.flags = l.u.extended_buffer_data.flags;
308d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.extendedBufferData.timestampUs =
309d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    l.u.extended_buffer_data.timestamp;
310d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
311d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case omx_message::FRAME_RENDERED:
312d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = Message::Type::FRAME_RENDERED;
313d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.renderData.timestampUs = l.u.render_data.timestamp;
314d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->data.renderData.systemTimeNs = l.u.render_data.nanoTime;
315d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
316d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        default:
317d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            native_handle_delete(*nh);
318d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return false;
319d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
320d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return true;
321d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
322d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
323d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
324d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap a `Message` inside an `omx_message`.
325d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
326d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The wrapper of type `omx_message`.
327d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Message`.
328d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `true` if the wrapping is successful; `false` otherwise.
329d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
330d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: Message -> omx_message
331d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool wrapAs(omx_message* l, Message const& t) {
332d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    l->fenceFd = native_handle_read_fd(t.fence);
333d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    switch (t.type) {
334d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case Message::Type::EVENT:
335d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->type = omx_message::EVENT;
336d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.event_data.event = OMX_EVENTTYPE(t.data.eventData.event);
337d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.event_data.data1 = t.data.eventData.data1;
338d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.event_data.data2 = t.data.eventData.data2;
339d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.event_data.data3 = t.data.eventData.data3;
340d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.event_data.data4 = t.data.eventData.data4;
341d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
342d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case Message::Type::EMPTY_BUFFER_DONE:
343d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->type = omx_message::EMPTY_BUFFER_DONE;
344d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.buffer_data.buffer = t.data.bufferData.buffer;
345d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
346d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case Message::Type::FILL_BUFFER_DONE:
347d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->type = omx_message::FILL_BUFFER_DONE;
348d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.extended_buffer_data.buffer = t.data.extendedBufferData.buffer;
349d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.extended_buffer_data.range_offset =
350d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    t.data.extendedBufferData.rangeOffset;
351d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.extended_buffer_data.range_length =
352d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    t.data.extendedBufferData.rangeLength;
353d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.extended_buffer_data.flags = t.data.extendedBufferData.flags;
354d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.extended_buffer_data.timestamp =
355d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    t.data.extendedBufferData.timestampUs;
356d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
357d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case Message::Type::FRAME_RENDERED:
358d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->type = omx_message::FRAME_RENDERED;
359d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.render_data.timestamp = t.data.renderData.timestampUs;
360d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            l->u.render_data.nanoTime = t.data.renderData.systemTimeNs;
361d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            break;
362d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        default:
363d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return false;
364d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
365d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return true;
366d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
367d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
368d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
369d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Similar to `wrapTo(omx_message*, Message const&)`, but the output will
370d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * have an extended lifetime.
371d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
372d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The output `omx_message`.
373d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Message`.
374d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `true` if the conversion is successful; `false` otherwise.
375d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
376d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * This function calls `wrapto()`, then attempts to duplicate the file
377d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * descriptor for the fence if it is not `-1`. If duplication fails, `false`
378d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * will be returned.
379d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
380d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Message -> omx_message
381d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool convertTo(omx_message* l, Message const& t) {
382d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (!wrapAs(l, t)) {
383d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        return false;
384d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
385d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (l->fenceFd == -1) {
386d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        return true;
387d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
388d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    l->fenceFd = dup(l->fenceFd);
389d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return l->fenceFd != -1;
390d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
391d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
392d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
393d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap an `OMXFenceParcelable` inside a `hidl_handle`.
394d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
395d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] t The wrapper of type `hidl_handle`.
396d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] nh The native handle created to hold the file descriptor inside
397d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \p l.
398d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `OMXFenceParcelable`, which essentially contains one
399d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * file descriptor.
400d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `true` if \p t and \p nh are successfully created to wrap around \p
401d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * l; `false` otherwise.
402d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
403d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * On success, \p nh needs to be deleted by the caller with
404d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `native_handle_delete()` after \p t and \p nh are no longer needed.
405d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
406d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * On failure, \p nh will not need to be deleted, and \p t will hold an invalid
407d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * value.
408d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
409d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: OMXFenceParcelable -> hidl_handle, native_handle_t*
410d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool wrapAs(hidl_handle* t, native_handle_t** nh,
411d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        OMXFenceParcelable const& l) {
412d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    *nh = native_handle_create_from_fd(l.get());
413d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (!*nh) {
414d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        return false;
415d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
416d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    *t = *nh;
417d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return true;
418d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
419d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
420d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
421d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap a `hidl_handle` inside an `OMXFenceParcelable`.
422d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
423d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The wrapper of type `OMXFenceParcelable`.
424d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `hidl_handle`.
425d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
426d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: hidl_handle -> OMXFenceParcelable
427d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline void wrapAs(OMXFenceParcelable* l, hidl_handle const& t) {
428d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    l->mFenceFd = native_handle_read_fd(t);
429d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
430d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
431d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
432d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert a `hidl_handle` to `OMXFenceParcelable`. If `hidl_handle`
433d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * contains file descriptors, the first file descriptor will be duplicated and
434d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * stored in the output `OMXFenceParcelable`.
435d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
436d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The output `OMXFenceParcelable`.
437d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The input `hidl_handle`.
438d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `false` if \p t contains a valid file descriptor but duplication
439d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * fails; `true` otherwise.
440d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
441d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: hidl_handle -> OMXFenceParcelable
442d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool convertTo(OMXFenceParcelable* l, hidl_handle const& t) {
443d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    int fd = native_handle_read_fd(t);
444d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (fd != -1) {
445d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        fd = dup(fd);
446d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        if (fd == -1) {
447d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return false;
448d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
449d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
450d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    l->mFenceFd = fd;
451d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return true;
452d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
453d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
454d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
455d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `::android::ColorAspects` to `ColorAspects`.
456d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
457d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `::android::ColorAspects`.
458d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `ColorAspects`.
459d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
460d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: ::android::ColorAspects -> ColorAspects
461d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline ColorAspects toHardwareColorAspects(::android::ColorAspects const& l) {
462d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return ColorAspects{
463d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::Range>(l.mRange),
464d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::Primaries>(l.mPrimaries),
465d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::Transfer>(l.mTransfer),
466d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::MatrixCoeffs>(l.mMatrixCoeffs)};
467d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
468d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
469d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
470d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `int32_t` to `ColorAspects`.
471d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
472d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `int32_t`.
473d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `ColorAspects`.
474d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
475d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: int32_t -> ColorAspects
476d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline ColorAspects toHardwareColorAspects(int32_t l) {
477d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return ColorAspects{
478d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::Range>((l >> 24) & 0xFF),
479d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::Primaries>((l >> 16) & 0xFF),
480d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::Transfer>(l & 0xFF),
481d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<ColorAspects::MatrixCoeffs>((l >> 8) & 0xFF)};
482d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
483d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
484d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
485d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `ColorAspects` to `::android::ColorAspects`.
486d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
487d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `ColorAspects`.
488d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding `::android::ColorAspects`.
489d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
490d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: ColorAspects -> ::android::ColorAspects
491d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline int32_t toCompactColorAspects(ColorAspects const& t) {
492d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<int32_t>(
493d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            (static_cast<uint32_t>(t.range) << 24) |
494d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            (static_cast<uint32_t>(t.primaries) << 16) |
495d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            (static_cast<uint32_t>(t.transfer)) |
496d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            (static_cast<uint32_t>(t.matrixCoeffs) << 8));
497d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
498d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
499d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
500d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `int32_t` to `Dataspace`.
501d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
502d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `int32_t`.
503d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \result The corresponding `Dataspace`.
504d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
505d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: int32_t -> Dataspace
506d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline Dataspace toHardwareDataspace(int32_t l) {
507d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<Dataspace>(l);
508d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
509d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
510d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
511d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `Dataspace` to `int32_t`.
512d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
513d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `Dataspace`.
514d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \result The corresponding `int32_t`.
515d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
516d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: Dataspace -> int32_t
517d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline int32_t toRawDataspace(Dataspace const& t) {
518d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<int32_t>(t);
519d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
520d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
521d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
522d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap an opaque buffer inside a `hidl_vec<uint8_t>`.
523d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
524d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The pointer to the beginning of the opaque buffer.
525d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] size The size of the buffer.
526d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return A `hidl_vec<uint8_t>` that points to the buffer.
527d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
528d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: void*, size_t -> hidl_vec<uint8_t>
529d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline hidl_vec<uint8_t> inHidlBytes(void const* l, size_t size) {
530d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    hidl_vec<uint8_t> t;
531d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t.setToExternal(static_cast<uint8_t*>(const_cast<void*>(l)), size, false);
532d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return t;
533d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
534d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
535d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
536d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Create a `hidl_vec<uint8_t>` that is a copy of an opaque buffer.
537d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
538d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The pointer to the beginning of the opaque buffer.
539d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] size The size of the buffer.
540d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return A `hidl_vec<uint8_t>` that is a copy of the input buffer.
541d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
542d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: void*, size_t -> hidl_vec<uint8_t>
543d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline hidl_vec<uint8_t> toHidlBytes(void const* l, size_t size) {
544d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    hidl_vec<uint8_t> t;
545d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t.resize(size);
546d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    uint8_t const* src = static_cast<uint8_t const*>(l);
547d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    std::copy(src, src + size, t.data());
548d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return t;
549d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
550d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
551d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
552d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap `GraphicBuffer` in `AnwBuffer`.
553d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
554d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] t The wrapper of type `AnwBuffer`.
555d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `GraphicBuffer`.
556d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
557d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: GraphicBuffer -> AnwBuffer
558d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline void wrapAs(AnwBuffer* t, GraphicBuffer const& l) {
559d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.width = l.getWidth();
560d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.height = l.getHeight();
561d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.stride = l.getStride();
562d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.format = static_cast<PixelFormat>(l.getPixelFormat());
563d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.layerCount = l.getLayerCount();
564d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.usage = l.getUsage();
565d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.id = l.getId();
566d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->attr.generationNumber = l.getGenerationNumber();
567d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->nativeHandle = hidl_handle(l.handle);
568d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
569d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
570d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
571d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `AnwBuffer` to `GraphicBuffer`.
572d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
573d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The destination `GraphicBuffer`.
574d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `AnwBuffer`.
575d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
576d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * This function will duplicate all file descriptors in \p t.
577d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
578d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: AnwBuffer -> GraphicBuffer
579d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// Ref: frameworks/native/libs/ui/GraphicBuffer.cpp: GraphicBuffer::flatten
580d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool convertTo(GraphicBuffer* l, AnwBuffer const& t) {
581d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    native_handle_t* handle = t.nativeHandle == nullptr ?
582d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            nullptr : native_handle_clone(t.nativeHandle);
583d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
584d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    size_t const numInts = 12 + (handle ? handle->numInts : 0);
585d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    int32_t* ints = new int32_t[numInts];
586d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
587d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    size_t numFds = static_cast<size_t>(handle ? handle->numFds : 0);
588d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    int* fds = new int[numFds];
589d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
590d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[0] = 'GBFR';
591d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[1] = static_cast<int32_t>(t.attr.width);
592d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[2] = static_cast<int32_t>(t.attr.height);
593d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[3] = static_cast<int32_t>(t.attr.stride);
594d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[4] = static_cast<int32_t>(t.attr.format);
595d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[5] = static_cast<int32_t>(t.attr.layerCount);
596d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[6] = static_cast<int32_t>(t.attr.usage);
597d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[7] = static_cast<int32_t>(t.attr.id >> 32);
598d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[8] = static_cast<int32_t>(t.attr.id & 0xFFFFFFFF);
599d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[9] = static_cast<int32_t>(t.attr.generationNumber);
600d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[10] = 0;
601d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    ints[11] = 0;
602d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    if (handle) {
603d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        ints[10] = static_cast<int32_t>(handle->numFds);
604d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        ints[11] = static_cast<int32_t>(handle->numInts);
605d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        int* intsStart = handle->data + handle->numFds;
606d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        std::copy(handle->data, intsStart, fds);
607d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        std::copy(intsStart, intsStart + handle->numInts, &ints[12]);
608d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
609d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
610d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    void const* constBuffer = static_cast<void const*>(ints);
611d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    size_t size = numInts * sizeof(int32_t);
612d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    int const* constFds = static_cast<int const*>(fds);
613d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    status_t status = l->unflatten(constBuffer, size, constFds, numFds);
614d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
615d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    delete [] fds;
616d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    delete [] ints;
617d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    native_handle_delete(handle);
618d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return status == NO_ERROR;
619d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
620d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
621d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
622d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Wrap `OMXBuffer` in `CodecBuffer`.
623d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
624d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] t The wrapper of type `CodecBuffer`.
625d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `OMXBuffer`.
626d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `true` if the wrapping is successful; `false` otherwise.
627d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
628d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// wrap: OMXBuffer -> CodecBuffer
629d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool wrapAs(CodecBuffer* t, OMXBuffer const& l) {
630d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->sharedMemory = hidl_memory();
631d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->nativeHandle = hidl_handle();
632d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    switch (l.mBufferType) {
633d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case OMXBuffer::kBufferTypeInvalid: {
634d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = CodecBuffer::Type::INVALID;
635d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
636d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
637d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case OMXBuffer::kBufferTypePreset: {
638d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = CodecBuffer::Type::PRESET;
639d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.preset.rangeLength = static_cast<uint32_t>(l.mRangeLength);
640d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.preset.rangeOffset = static_cast<uint32_t>(l.mRangeOffset);
641d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
642d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
643d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case OMXBuffer::kBufferTypeHidlMemory: {
644d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = CodecBuffer::Type::SHARED_MEM;
645d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->sharedMemory = l.mHidlMemory;
646d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
647d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
648d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case OMXBuffer::kBufferTypeSharedMem: {
649d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            // This is not supported.
650d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return false;
651d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
652d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case OMXBuffer::kBufferTypeANWBuffer: {
653d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = CodecBuffer::Type::ANW_BUFFER;
654d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            if (l.mGraphicBuffer == nullptr) {
655d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                t->attr.anwBuffer.width = 0;
656d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                t->attr.anwBuffer.height = 0;
657d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                t->attr.anwBuffer.stride = 0;
658d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                t->attr.anwBuffer.format = static_cast<PixelFormat>(1);
659d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                t->attr.anwBuffer.layerCount = 0;
660d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                t->attr.anwBuffer.usage = 0;
661d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                return true;
662d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            }
663d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.anwBuffer.width = l.mGraphicBuffer->getWidth();
664d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.anwBuffer.height = l.mGraphicBuffer->getHeight();
665d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.anwBuffer.stride = l.mGraphicBuffer->getStride();
666d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.anwBuffer.format = static_cast<PixelFormat>(
667d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    l.mGraphicBuffer->getPixelFormat());
668d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.anwBuffer.layerCount = l.mGraphicBuffer->getLayerCount();
669d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->attr.anwBuffer.usage = l.mGraphicBuffer->getUsage();
670d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->nativeHandle = l.mGraphicBuffer->handle;
671d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
672d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
673d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case OMXBuffer::kBufferTypeNativeHandle: {
674d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->type = CodecBuffer::Type::NATIVE_HANDLE;
675d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            t->nativeHandle = l.mNativeHandle->handle();
676d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
677d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
678d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
679d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return false;
680d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
681d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
682d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
683d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `CodecBuffer` to `OMXBuffer`.
684d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
685d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The destination `OMXBuffer`.
686d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `CodecBuffer`.
687d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return `true` if successful; `false` otherwise.
688d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
689d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: CodecBuffer -> OMXBuffer
690d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool convertTo(OMXBuffer* l, CodecBuffer const& t) {
691d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    switch (t.type) {
692d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case CodecBuffer::Type::INVALID: {
693d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            *l = OMXBuffer();
694d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
695d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
696d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case CodecBuffer::Type::PRESET: {
697d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            *l = OMXBuffer(
698d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    t.attr.preset.rangeOffset,
699d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    t.attr.preset.rangeLength);
700d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
701d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
702d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case CodecBuffer::Type::SHARED_MEM: {
703d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            *l = OMXBuffer(t.sharedMemory);
704d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
705d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
706d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case CodecBuffer::Type::ANW_BUFFER: {
707d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            if (t.nativeHandle.getNativeHandle() == nullptr) {
708d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                *l = OMXBuffer(sp<GraphicBuffer>(nullptr));
709d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                return true;
710d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            }
711d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            AnwBuffer anwBuffer;
712d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            anwBuffer.nativeHandle = t.nativeHandle;
713d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            anwBuffer.attr = t.attr.anwBuffer;
714d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            sp<GraphicBuffer> graphicBuffer = new GraphicBuffer();
715d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            if (!convertTo(graphicBuffer.get(), anwBuffer)) {
716d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                return false;
717d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            }
718d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            *l = OMXBuffer(graphicBuffer);
719d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
720d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
721d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        case CodecBuffer::Type::NATIVE_HANDLE: {
722d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            *l = OMXBuffer(NativeHandle::create(
723d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa                    native_handle_clone(t.nativeHandle), true));
724d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            return true;
725d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        }
726d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
727d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return false;
728d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
729d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
730d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
731d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `IOMX::ComponentInfo` to `IOmx::ComponentInfo`.
732d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
733d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] t The destination `IOmx::ComponentInfo`.
734d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `IOMX::ComponentInfo`.
735d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
736d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: IOMX::ComponentInfo -> IOmx::ComponentInfo
737d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool convertTo(IOmx::ComponentInfo* t, IOMX::ComponentInfo const& l) {
738d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->mName = l.mName.string();
739d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    t->mRoles.resize(l.mRoles.size());
740d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    size_t i = 0;
741d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    for (auto& role : l.mRoles) {
742d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        t->mRoles[i++] = role.string();
743d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
744d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return true;
745d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
746d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
747d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
748d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `IOmx::ComponentInfo` to `IOMX::ComponentInfo`.
749d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
750d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[out] l The destination `IOMX::ComponentInfo`.
751d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `IOmx::ComponentInfo`.
752d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
753d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: IOmx::ComponentInfo -> IOMX::ComponentInfo
754d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool convertTo(IOMX::ComponentInfo* l, IOmx::ComponentInfo const& t) {
755d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    l->mName = t.mName.c_str();
756d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    l->mRoles.clear();
757d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    for (size_t i = 0; i < t.mRoles.size(); ++i) {
758d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa        l->mRoles.push_back(String8(t.mRoles[i].c_str()));
759d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    }
760d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return true;
761d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
762d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
763d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
764d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `OMX_BOOL` to `bool`.
765d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
766d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `OMX_BOOL`.
767d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The destination `bool`.
768d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
769d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: OMX_BOOL -> bool
770d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline bool toRawBool(OMX_BOOL l) {
771d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return l == OMX_FALSE ? false : true;
772d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
773d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
774d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
775d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `bool` to `OMX_BOOL`.
776d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
777d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `bool`.
778d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The destination `OMX_BOOL`.
779d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
780d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: bool -> OMX_BOOL
781d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline OMX_BOOL toEnumBool(bool t) {
782d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return t ? OMX_TRUE : OMX_FALSE;
783d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
784d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
785d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
786d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `OMX_COMMANDTYPE` to `uint32_t`.
787d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
788d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `OMX_COMMANDTYPE`.
789d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The underlying value of type `uint32_t`.
790d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
791d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `OMX_COMMANDTYPE` is an enum type whose underlying type is `uint32_t`.
792d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
793d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: OMX_COMMANDTYPE -> uint32_t
794d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline uint32_t toRawCommandType(OMX_COMMANDTYPE l) {
795d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<uint32_t>(l);
796d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
797d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
798d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
799d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `uint32_t` to `OMX_COMMANDTYPE`.
800d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
801d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `uint32_t`.
802d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding enum value of type `OMX_COMMANDTYPE`.
803d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
804d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `OMX_COMMANDTYPE` is an enum type whose underlying type is `uint32_t`.
805d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
806d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: uint32_t -> OMX_COMMANDTYPE
807d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline OMX_COMMANDTYPE toEnumCommandType(uint32_t t) {
808d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<OMX_COMMANDTYPE>(t);
809d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
810d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
811d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
812d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `OMX_INDEXTYPE` to `uint32_t`.
813d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
814d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `OMX_INDEXTYPE`.
815d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The underlying value of type `uint32_t`.
816d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
817d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `OMX_INDEXTYPE` is an enum type whose underlying type is `uint32_t`.
818d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
819d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: OMX_INDEXTYPE -> uint32_t
820d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline uint32_t toRawIndexType(OMX_INDEXTYPE l) {
821d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<uint32_t>(l);
822d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
823d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
824d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
825d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `uint32_t` to `OMX_INDEXTYPE`.
826d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
827d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `uint32_t`.
828d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The corresponding enum value of type `OMX_INDEXTYPE`.
829d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
830d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * `OMX_INDEXTYPE` is an enum type whose underlying type is `uint32_t`.
831d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
832d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: uint32_t -> OMX_INDEXTYPE
833d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline OMX_INDEXTYPE toEnumIndexType(uint32_t t) {
834d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<OMX_INDEXTYPE>(t);
835d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
836d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
837d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
838d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `IOMX::PortMode` to `PortMode`.
839d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
840d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `IOMX::PortMode`.
841d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The destination `PortMode`.
842d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
843d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: IOMX::PortMode -> PortMode
844d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline PortMode toHardwarePortMode(IOMX::PortMode l) {
845d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<PortMode>(l);
846d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
847d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
848d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
849d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `PortMode` to `IOMX::PortMode`.
850d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
851d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] t The source `PortMode`.
852d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The destination `IOMX::PortMode`.
853d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
854d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: PortMode -> IOMX::PortMode
855d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline IOMX::PortMode toIOMXPortMode(PortMode t) {
856d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<IOMX::PortMode>(t);
857d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
858d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
859d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
860d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `OMX_TICKS` to `uint64_t`.
861d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
862d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `OMX_TICKS`.
863d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The destination `uint64_t`.
864d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
865d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: OMX_TICKS -> uint64_t
866d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline uint64_t toRawTicks(OMX_TICKS l) {
867d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#ifndef OMX_SKIP64BIT
868d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<uint64_t>(l);
869d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#else
870d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<uint64_t>(l.nLowPart) |
871d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<uint64_t>(l.nHighPart << 32);
872d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#endif
873d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
874d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
875d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa/**
876d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \brief Convert `uint64_t` to `OMX_TICKS`.
877d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa *
878d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \param[in] l The source `uint64_t`.
879d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa * \return The destination `OMX_TICKS`.
880d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa */
881d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa// convert: uint64_t -> OMX_TICKS
882d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasainline OMX_TICKS toOMXTicks(uint64_t t) {
883d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#ifndef OMX_SKIP64BIT
884d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return static_cast<OMX_TICKS>(t);
885d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#else
886d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa    return OMX_TICKS{
887d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<uint32_t>(t & 0xFFFFFFFF),
888d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa            static_cast<uint32_t>(t >> 32)};
889d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#endif
890d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}
891d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
892d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}  // namespace utils
893d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}  // namespace V1_0
894d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}  // namespace omx
895d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}  // namespace media
896d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}  // namespace hardware
897d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa}  // namespace android
898d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa
899d2075e8b86dbf6d7c6af7de6f2f698b1acf4c6dfPawin Vongmasa#endif  // ANDROID_HARDWARE_MEDIA_OMX_V1_0__CONVERSION_H
900