1 /*
2 * Copyright (C) 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 #include "aidl/android/hardware/graphics/common/Dataspace.h"
18 #include <ui/PublicFormat.h>
19
20
21 // ----------------------------------------------------------------------------
22 namespace android {
23 // ----------------------------------------------------------------------------
24
25 using ::aidl::android::hardware::graphics::common::Dataspace;
26
mapPublicFormatToHalFormat(PublicFormat f)27 int mapPublicFormatToHalFormat(PublicFormat f) {
28 switch (f) {
29 case PublicFormat::JPEG:
30 case PublicFormat::DEPTH_POINT_CLOUD:
31 case PublicFormat::DEPTH_JPEG:
32 case PublicFormat::HEIC:
33 case PublicFormat::JPEG_R:
34 return HAL_PIXEL_FORMAT_BLOB;
35 case PublicFormat::DEPTH16:
36 return HAL_PIXEL_FORMAT_Y16;
37 case PublicFormat::RAW_SENSOR:
38 case PublicFormat::RAW_DEPTH:
39 return HAL_PIXEL_FORMAT_RAW16;
40 case PublicFormat::RAW_DEPTH10:
41 return HAL_PIXEL_FORMAT_RAW10;
42 default:
43 // Most formats map 1:1
44 return static_cast<int>(f);
45 }
46 }
47
mapPublicFormatToHalDataspace(PublicFormat f)48 android_dataspace mapPublicFormatToHalDataspace(PublicFormat f) {
49 Dataspace dataspace;
50 switch (f) {
51 case PublicFormat::JPEG:
52 dataspace = Dataspace::JFIF;
53 break;
54 case PublicFormat::DEPTH_POINT_CLOUD:
55 case PublicFormat::DEPTH16:
56 case PublicFormat::RAW_DEPTH:
57 case PublicFormat::RAW_DEPTH10:
58 dataspace = Dataspace::DEPTH;
59 break;
60 case PublicFormat::RAW_SENSOR:
61 case PublicFormat::RAW_PRIVATE:
62 case PublicFormat::RAW10:
63 case PublicFormat::RAW12:
64 dataspace = Dataspace::ARBITRARY;
65 break;
66 case PublicFormat::YUV_420_888:
67 case PublicFormat::NV21:
68 case PublicFormat::YV12:
69 dataspace = Dataspace::JFIF;
70 break;
71 case PublicFormat::DEPTH_JPEG:
72 dataspace = Dataspace::DYNAMIC_DEPTH;
73 break;
74 case PublicFormat::HEIC:
75 dataspace = Dataspace::HEIF;
76 break;
77 case PublicFormat::JPEG_R:
78 dataspace = Dataspace::JPEG_R;
79 break;
80 default:
81 // Most formats map to UNKNOWN
82 dataspace = Dataspace::UNKNOWN;
83 break;
84 }
85 return static_cast<android_dataspace>(dataspace);
86 }
87
mapHalFormatDataspaceToPublicFormat(int format,android_dataspace dataSpace)88 PublicFormat mapHalFormatDataspaceToPublicFormat(int format, android_dataspace dataSpace) {
89 Dataspace ds = static_cast<Dataspace>(dataSpace);
90 switch (format) {
91 case HAL_PIXEL_FORMAT_RAW10:
92 switch (ds) {
93 case Dataspace::DEPTH:
94 return PublicFormat::RAW_DEPTH10;
95 default:
96 return PublicFormat::RAW10;
97 }
98 case HAL_PIXEL_FORMAT_RGBA_8888:
99 case HAL_PIXEL_FORMAT_RGBX_8888:
100 case HAL_PIXEL_FORMAT_RGBA_FP16:
101 case HAL_PIXEL_FORMAT_RGBA_1010102:
102 case HAL_PIXEL_FORMAT_RGB_888:
103 case HAL_PIXEL_FORMAT_RGB_565:
104 case HAL_PIXEL_FORMAT_Y8:
105 case HAL_PIXEL_FORMAT_RAW12:
106 case HAL_PIXEL_FORMAT_YCbCr_420_888:
107 case HAL_PIXEL_FORMAT_YV12:
108 case HAL_PIXEL_FORMAT_YCBCR_P010:
109 // Enums overlap in both name and value
110 return static_cast<PublicFormat>(format);
111 case HAL_PIXEL_FORMAT_RAW16:
112 switch (ds) {
113 case Dataspace::DEPTH:
114 return PublicFormat::RAW_DEPTH;
115 default:
116 return PublicFormat::RAW_SENSOR;
117 }
118 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
119 // Name differs, though value is the same
120 return PublicFormat::RAW_PRIVATE;
121 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
122 // Name differs, though the value is the same
123 return PublicFormat::NV16;
124 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
125 // Name differs, though the value is the same
126 return PublicFormat::NV21;
127 case HAL_PIXEL_FORMAT_YCbCr_422_I:
128 // Name differs, though the value is the same
129 return PublicFormat::YUY2;
130 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
131 // Name differs, though the value is the same
132 return PublicFormat::PRIVATE;
133 case HAL_PIXEL_FORMAT_Y16:
134 // Dataspace-dependent
135 switch (ds) {
136 case Dataspace::DEPTH:
137 return PublicFormat::DEPTH16;
138 default:
139 // Assume non-depth Y16 is just Y16.
140 return PublicFormat::Y16;
141 }
142 case HAL_PIXEL_FORMAT_BLOB:
143 // Dataspace-dependent
144 switch (ds) {
145 case Dataspace::DEPTH:
146 return PublicFormat::DEPTH_POINT_CLOUD;
147 case Dataspace::JFIF:
148 return PublicFormat::JPEG;
149 case Dataspace::HEIF:
150 return PublicFormat::HEIC;
151 default:
152 if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_DYNAMIC_DEPTH)) {
153 return PublicFormat::DEPTH_JPEG;
154 } else if (dataSpace == static_cast<android_dataspace>(Dataspace::JPEG_R)) {
155 return PublicFormat::JPEG_R;
156 }else {
157 // Assume otherwise-marked blobs are also JPEG
158 return PublicFormat::JPEG;
159 }
160 }
161 case HAL_PIXEL_FORMAT_BGRA_8888:
162 // Not defined in public API
163 return PublicFormat::UNKNOWN;
164
165 default:
166 return PublicFormat::UNKNOWN;
167 }
168 }
169
170 // ----------------------------------------------------------------------------
171 }; // namespace android
172 // ----------------------------------------------------------------------------
173