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