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