1 /*
2  * Copyright 2017 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/DebugUtils.h>
18 #include <ui/PixelFormat.h>
19 #include <ui/Rect.h>
20 
21 #include <android-base/stringprintf.h>
22 #include <string>
23 
24 using android::base::StringPrintf;
25 using android::ui::ColorMode;
26 using android::ui::RenderIntent;
27 
decodeStandard(android_dataspace dataspace)28 std::string decodeStandard(android_dataspace dataspace) {
29     const uint32_t dataspaceSelect = (dataspace & HAL_DATASPACE_STANDARD_MASK);
30     switch (dataspaceSelect) {
31         case HAL_DATASPACE_STANDARD_BT709:
32             return std::string("BT709");
33 
34         case HAL_DATASPACE_STANDARD_BT601_625:
35             return std::string("BT601_625");
36 
37         case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
38             return std::string("BT601_625_UNADJUSTED");
39 
40         case HAL_DATASPACE_STANDARD_BT601_525:
41             return std::string("BT601_525");
42 
43         case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
44             return std::string("BT601_525_UNADJUSTED");
45 
46         case HAL_DATASPACE_STANDARD_BT2020:
47             return std::string("BT2020");
48 
49         case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
50             return std::string("BT2020 (constant luminance)");
51 
52         case HAL_DATASPACE_STANDARD_BT470M:
53             return std::string("BT470M");
54 
55         case HAL_DATASPACE_STANDARD_FILM:
56             return std::string("FILM");
57 
58         case HAL_DATASPACE_STANDARD_DCI_P3:
59             return std::string("DCI-P3");
60 
61         case HAL_DATASPACE_STANDARD_ADOBE_RGB:
62             return std::string("AdobeRGB");
63 
64         case 0:
65             switch (dataspace & 0xffff) {
66                 case HAL_DATASPACE_JFIF:
67                     return std::string("(deprecated) JFIF (BT601_625)");
68 
69                 case HAL_DATASPACE_BT601_625:
70                     return std::string("(deprecated) BT601_625");
71 
72                 case HAL_DATASPACE_BT601_525:
73                     return std::string("(deprecated) BT601_525");
74 
75                 case HAL_DATASPACE_SRGB_LINEAR:
76                 case HAL_DATASPACE_SRGB:
77                     return std::string("(deprecated) sRGB");
78 
79                 case HAL_DATASPACE_BT709:
80                     return std::string("(deprecated) BT709");
81 
82                 case HAL_DATASPACE_ARBITRARY:
83                     return std::string("ARBITRARY");
84 
85                 case HAL_DATASPACE_UNKNOWN:
86                 // Fallthrough
87                 default:
88                     return android::base::StringPrintf("Unknown deprecated dataspace code %d",
89                                                        dataspace);
90             }
91     }
92 
93     return android::base::StringPrintf("Unknown dataspace code %d", dataspaceSelect);
94 }
95 
decodeTransfer(android_dataspace dataspace)96 std::string decodeTransfer(android_dataspace dataspace) {
97     const uint32_t dataspaceSelect = (dataspace & HAL_DATASPACE_STANDARD_MASK);
98     if (dataspaceSelect == 0) {
99         switch (dataspace & 0xffff) {
100             case HAL_DATASPACE_JFIF:
101             case HAL_DATASPACE_BT601_625:
102             case HAL_DATASPACE_BT601_525:
103             case HAL_DATASPACE_BT709:
104                 return std::string("SMPTE_170M");
105 
106             case HAL_DATASPACE_SRGB_LINEAR:
107             case HAL_DATASPACE_ARBITRARY:
108                 return std::string("Linear");
109 
110             case HAL_DATASPACE_SRGB:
111                 return std::string("sRGB");
112 
113             case HAL_DATASPACE_UNKNOWN:
114             // Fallthrough
115             default:
116                 return std::string("");
117         }
118     }
119 
120     const uint32_t dataspaceTransfer = (dataspace & HAL_DATASPACE_TRANSFER_MASK);
121     switch (dataspaceTransfer) {
122         case HAL_DATASPACE_TRANSFER_UNSPECIFIED:
123             return std::string("Unspecified");
124 
125         case HAL_DATASPACE_TRANSFER_LINEAR:
126             return std::string("Linear");
127 
128         case HAL_DATASPACE_TRANSFER_SRGB:
129             return std::string("sRGB");
130 
131         case HAL_DATASPACE_TRANSFER_SMPTE_170M:
132             return std::string("SMPTE_170M");
133 
134         case HAL_DATASPACE_TRANSFER_GAMMA2_2:
135             return std::string("gamma 2.2");
136 
137         case HAL_DATASPACE_TRANSFER_GAMMA2_6:
138             return std::string("gamma 2.6");
139 
140         case HAL_DATASPACE_TRANSFER_GAMMA2_8:
141             return std::string("gamma 2.8");
142 
143         case HAL_DATASPACE_TRANSFER_ST2084:
144             return std::string("SMPTE 2084");
145 
146         case HAL_DATASPACE_TRANSFER_HLG:
147             return std::string("STD-B67");
148     }
149 
150     return android::base::StringPrintf("Unknown dataspace transfer %d", dataspaceTransfer);
151 }
152 
decodeRange(android_dataspace dataspace)153 std::string decodeRange(android_dataspace dataspace) {
154     const uint32_t dataspaceSelect = (dataspace & HAL_DATASPACE_STANDARD_MASK);
155     if (dataspaceSelect == 0) {
156         switch (dataspace & 0xffff) {
157             case HAL_DATASPACE_JFIF:
158             case HAL_DATASPACE_SRGB_LINEAR:
159             case HAL_DATASPACE_SRGB:
160                 return std::string("Full range");
161 
162             case HAL_DATASPACE_BT601_625:
163             case HAL_DATASPACE_BT601_525:
164             case HAL_DATASPACE_BT709:
165                 return std::string("Limited range");
166 
167             case HAL_DATASPACE_ARBITRARY:
168             case HAL_DATASPACE_UNKNOWN:
169             // Fallthrough
170             default:
171                 return std::string("unspecified range");
172         }
173     }
174 
175     const uint32_t dataspaceRange = (dataspace & HAL_DATASPACE_RANGE_MASK);
176     switch (dataspaceRange) {
177         case HAL_DATASPACE_RANGE_UNSPECIFIED:
178             return std::string("Range Unspecified");
179 
180         case HAL_DATASPACE_RANGE_FULL:
181             return std::string("Full range");
182 
183         case HAL_DATASPACE_RANGE_LIMITED:
184             return std::string("Limited range");
185 
186         case HAL_DATASPACE_RANGE_EXTENDED:
187             return std::string("Extended range");
188     }
189 
190     return android::base::StringPrintf("Unknown dataspace range %d", dataspaceRange);
191 }
192 
dataspaceDetails(android_dataspace dataspace)193 std::string dataspaceDetails(android_dataspace dataspace) {
194     if (dataspace == 0) {
195         return "Default";
196     }
197     return android::base::StringPrintf("%s %s %s", decodeStandard(dataspace).c_str(),
198                                        decodeTransfer(dataspace).c_str(),
199                                        decodeRange(dataspace).c_str());
200 }
201 
decodeColorMode(ColorMode colorMode)202 std::string decodeColorMode(ColorMode colorMode) {
203     switch (colorMode) {
204         case ColorMode::NATIVE:
205             return std::string("ColorMode::NATIVE");
206 
207         case ColorMode::STANDARD_BT601_625:
208             return std::string("ColorMode::BT601_625");
209 
210         case ColorMode::STANDARD_BT601_625_UNADJUSTED:
211             return std::string("ColorMode::BT601_625_UNADJUSTED");
212 
213         case ColorMode::STANDARD_BT601_525:
214             return std::string("ColorMode::BT601_525");
215 
216         case ColorMode::STANDARD_BT601_525_UNADJUSTED:
217             return std::string("ColorMode::BT601_525_UNADJUSTED");
218 
219         case ColorMode::STANDARD_BT709:
220             return std::string("ColorMode::BT709");
221 
222         case ColorMode::DCI_P3:
223             return std::string("ColorMode::DCI_P3");
224 
225         case ColorMode::SRGB:
226             return std::string("ColorMode::SRGB");
227 
228         case ColorMode::ADOBE_RGB:
229             return std::string("ColorMode::ADOBE_RGB");
230 
231         case ColorMode::DISPLAY_P3:
232             return std::string("ColorMode::DISPLAY_P3");
233 
234         case ColorMode::BT2020:
235             return std::string("ColorMode::BT2020");
236 
237         case ColorMode::DISPLAY_BT2020:
238             return std::string("ColorMode::DISPLAY_BT2020");
239 
240         case ColorMode::BT2100_PQ:
241             return std::string("ColorMode::BT2100_PQ");
242 
243         case ColorMode::BT2100_HLG:
244             return std::string("ColorMode::BT2100_HLG");
245     }
246 
247     return android::base::StringPrintf("Unknown color mode %d", colorMode);
248 }
249 
decodeColorTransform(android_color_transform colorTransform)250 std::string decodeColorTransform(android_color_transform colorTransform) {
251     switch (colorTransform) {
252         case HAL_COLOR_TRANSFORM_IDENTITY:
253             return std::string("Identity");
254 
255         case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
256             return std::string("Arbitrary matrix");
257 
258         case HAL_COLOR_TRANSFORM_VALUE_INVERSE:
259             return std::string("Inverse value");
260 
261         case HAL_COLOR_TRANSFORM_GRAYSCALE:
262             return std::string("Grayscale");
263 
264         case HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA:
265             return std::string("Correct protanopia");
266 
267         case HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA:
268             return std::string("Correct deuteranopia");
269 
270         case HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA:
271             return std::string("Correct tritanopia");
272     }
273 
274     return android::base::StringPrintf("Unknown color transform %d", colorTransform);
275 }
276 
277 // Converts a PixelFormat to a human-readable string.  Max 11 chars.
278 // (Could use a table of prefab String8 objects.)
decodePixelFormat(android::PixelFormat format)279 std::string decodePixelFormat(android::PixelFormat format) {
280     switch (format) {
281         case android::PIXEL_FORMAT_UNKNOWN:
282             return std::string("Unknown/None");
283         case android::PIXEL_FORMAT_CUSTOM:
284             return std::string("Custom");
285         case android::PIXEL_FORMAT_TRANSLUCENT:
286             return std::string("Translucent");
287         case android::PIXEL_FORMAT_TRANSPARENT:
288             return std::string("Transparent");
289         case android::PIXEL_FORMAT_OPAQUE:
290             return std::string("Opaque");
291         case android::PIXEL_FORMAT_RGBA_8888:
292             return std::string("RGBA_8888");
293         case android::PIXEL_FORMAT_RGBX_8888:
294             return std::string("RGBx_8888");
295         case android::PIXEL_FORMAT_RGBA_FP16:
296             return std::string("RGBA_FP16");
297         case android::PIXEL_FORMAT_RGBA_1010102:
298             return std::string("RGBA_1010102");
299         case android::PIXEL_FORMAT_RGB_888:
300             return std::string("RGB_888");
301         case android::PIXEL_FORMAT_RGB_565:
302             return std::string("RGB_565");
303         case android::PIXEL_FORMAT_BGRA_8888:
304             return std::string("BGRA_8888");
305         default:
306             return android::base::StringPrintf("Unknown %#08x", format);
307     }
308 }
309 
decodeRenderIntent(RenderIntent renderIntent)310 std::string decodeRenderIntent(RenderIntent renderIntent) {
311     switch(renderIntent) {
312       case RenderIntent::COLORIMETRIC:
313           return std::string("RenderIntent::COLORIMETRIC");
314       case RenderIntent::ENHANCE:
315           return std::string("RenderIntent::ENHANCE");
316       case RenderIntent::TONE_MAP_COLORIMETRIC:
317           return std::string("RenderIntent::TONE_MAP_COLORIMETRIC");
318       case RenderIntent::TONE_MAP_ENHANCE:
319           return std::string("RenderIntent::TONE_MAP_ENHANCE");
320     }
321     return std::string("Unknown RenderIntent");
322 }
323 
to_string(const android::Rect & rect)324 std::string to_string(const android::Rect& rect) {
325     return StringPrintf("(%4d,%4d,%4d,%4d)", rect.left, rect.top, rect.right, rect.bottom);
326 }
327