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/DeviceProductInfo.h>
19 #include <ui/PixelFormat.h>
20 #include <ui/Rect.h>
21 
22 #include <android-base/stringprintf.h>
23 #include <string>
24 
25 using android::base::StringAppendF;
26 using android::base::StringPrintf;
27 using android::ui::ColorMode;
28 using android::ui::RenderIntent;
29 
decodeStandard(android_dataspace dataspace)30 std::string decodeStandard(android_dataspace dataspace) {
31     const uint32_t dataspaceSelect = (dataspace & HAL_DATASPACE_STANDARD_MASK);
32     switch (dataspaceSelect) {
33         case HAL_DATASPACE_STANDARD_BT709:
34             return std::string("BT709");
35 
36         case HAL_DATASPACE_STANDARD_BT601_625:
37             return std::string("BT601_625");
38 
39         case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
40             return std::string("BT601_625_UNADJUSTED");
41 
42         case HAL_DATASPACE_STANDARD_BT601_525:
43             return std::string("BT601_525");
44 
45         case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
46             return std::string("BT601_525_UNADJUSTED");
47 
48         case HAL_DATASPACE_STANDARD_BT2020:
49             return std::string("BT2020");
50 
51         case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
52             return std::string("BT2020 (constant luminance)");
53 
54         case HAL_DATASPACE_STANDARD_BT470M:
55             return std::string("BT470M");
56 
57         case HAL_DATASPACE_STANDARD_FILM:
58             return std::string("FILM");
59 
60         case HAL_DATASPACE_STANDARD_DCI_P3:
61             return std::string("DCI-P3");
62 
63         case HAL_DATASPACE_STANDARD_ADOBE_RGB:
64             return std::string("AdobeRGB");
65 
66         case 0:
67             switch (dataspace & 0xffff) {
68                 case HAL_DATASPACE_JFIF:
69                     return std::string("(deprecated) JFIF (BT601_625)");
70 
71                 case HAL_DATASPACE_BT601_625:
72                     return std::string("(deprecated) BT601_625");
73 
74                 case HAL_DATASPACE_BT601_525:
75                     return std::string("(deprecated) BT601_525");
76 
77                 case HAL_DATASPACE_SRGB_LINEAR:
78                 case HAL_DATASPACE_SRGB:
79                     return std::string("(deprecated) sRGB");
80 
81                 case HAL_DATASPACE_BT709:
82                     return std::string("(deprecated) BT709");
83 
84                 case HAL_DATASPACE_ARBITRARY:
85                     return std::string("ARBITRARY");
86 
87                 case HAL_DATASPACE_UNKNOWN:
88                 // Fallthrough
89                 default:
90                     return StringPrintf("Unknown deprecated dataspace code %d", dataspace);
91             }
92     }
93 
94     return StringPrintf("Unknown dataspace code %d", dataspaceSelect);
95 }
96 
decodeTransfer(android_dataspace dataspace)97 std::string decodeTransfer(android_dataspace dataspace) {
98     const uint32_t dataspaceSelect = (dataspace & HAL_DATASPACE_STANDARD_MASK);
99     if (dataspaceSelect == 0) {
100         switch (dataspace & 0xffff) {
101             case HAL_DATASPACE_JFIF:
102             case HAL_DATASPACE_BT601_625:
103             case HAL_DATASPACE_BT601_525:
104             case HAL_DATASPACE_BT709:
105                 return std::string("SMPTE_170M");
106 
107             case HAL_DATASPACE_SRGB_LINEAR:
108             case HAL_DATASPACE_ARBITRARY:
109                 return std::string("Linear");
110 
111             case HAL_DATASPACE_SRGB:
112                 return std::string("sRGB");
113 
114             case HAL_DATASPACE_UNKNOWN:
115             // Fallthrough
116             default:
117                 return std::string("");
118         }
119     }
120 
121     const uint32_t dataspaceTransfer = (dataspace & HAL_DATASPACE_TRANSFER_MASK);
122     switch (dataspaceTransfer) {
123         case HAL_DATASPACE_TRANSFER_UNSPECIFIED:
124             return std::string("Unspecified");
125 
126         case HAL_DATASPACE_TRANSFER_LINEAR:
127             return std::string("Linear");
128 
129         case HAL_DATASPACE_TRANSFER_SRGB:
130             return std::string("sRGB");
131 
132         case HAL_DATASPACE_TRANSFER_SMPTE_170M:
133             return std::string("SMPTE_170M");
134 
135         case HAL_DATASPACE_TRANSFER_GAMMA2_2:
136             return std::string("gamma 2.2");
137 
138         case HAL_DATASPACE_TRANSFER_GAMMA2_6:
139             return std::string("gamma 2.6");
140 
141         case HAL_DATASPACE_TRANSFER_GAMMA2_8:
142             return std::string("gamma 2.8");
143 
144         case HAL_DATASPACE_TRANSFER_ST2084:
145             return std::string("SMPTE 2084");
146 
147         case HAL_DATASPACE_TRANSFER_HLG:
148             return std::string("STD-B67");
149     }
150 
151     return StringPrintf("Unknown dataspace transfer %d", dataspaceTransfer);
152 }
153 
decodeRange(android_dataspace dataspace)154 std::string decodeRange(android_dataspace dataspace) {
155     const uint32_t dataspaceSelect = (dataspace & HAL_DATASPACE_STANDARD_MASK);
156     if (dataspaceSelect == 0) {
157         switch (dataspace & 0xffff) {
158             case HAL_DATASPACE_JFIF:
159             case HAL_DATASPACE_SRGB_LINEAR:
160             case HAL_DATASPACE_SRGB:
161                 return std::string("Full range");
162 
163             case HAL_DATASPACE_BT601_625:
164             case HAL_DATASPACE_BT601_525:
165             case HAL_DATASPACE_BT709:
166                 return std::string("Limited range");
167 
168             case HAL_DATASPACE_ARBITRARY:
169             case HAL_DATASPACE_UNKNOWN:
170             // Fallthrough
171             default:
172                 return std::string("unspecified range");
173         }
174     }
175 
176     const uint32_t dataspaceRange = (dataspace & HAL_DATASPACE_RANGE_MASK);
177     switch (dataspaceRange) {
178         case HAL_DATASPACE_RANGE_UNSPECIFIED:
179             return std::string("Range Unspecified");
180 
181         case HAL_DATASPACE_RANGE_FULL:
182             return std::string("Full range");
183 
184         case HAL_DATASPACE_RANGE_LIMITED:
185             return std::string("Limited range");
186 
187         case HAL_DATASPACE_RANGE_EXTENDED:
188             return std::string("Extended range");
189     }
190 
191     return StringPrintf("Unknown dataspace range %d", dataspaceRange);
192 }
193 
dataspaceDetails(android_dataspace dataspace)194 std::string dataspaceDetails(android_dataspace dataspace) {
195     if (dataspace == 0) {
196         return "Default";
197     }
198     return StringPrintf("%s %s %s", decodeStandard(dataspace).c_str(),
199                         decodeTransfer(dataspace).c_str(), 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 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 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 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 
toString(const android::DeviceProductInfo::ManufactureOrModelDate & date)328 std::string toString(const android::DeviceProductInfo::ManufactureOrModelDate& date) {
329     using ModelYear = android::DeviceProductInfo::ModelYear;
330     using ManufactureYear = android::DeviceProductInfo::ManufactureYear;
331     using ManufactureWeekAndYear = android::DeviceProductInfo::ManufactureWeekAndYear;
332 
333     if (const auto* model = std::get_if<ModelYear>(&date)) {
334         return StringPrintf("ModelYear{%d}", model->year);
335     } else if (const auto* manufacture = std::get_if<ManufactureYear>(&date)) {
336         return StringPrintf("ManufactureDate{year=%d}", manufacture->year);
337     } else if (const auto* manufacture = std::get_if<ManufactureWeekAndYear>(&date)) {
338         return StringPrintf("ManufactureDate{week=%d, year=%d}", manufacture->week,
339                             manufacture->year);
340     } else {
341         LOG_FATAL("Unknown alternative for variant DeviceProductInfo::ManufactureOrModelDate");
342         return {};
343     }
344 }
345 
toString(const android::DeviceProductInfo & info)346 std::string toString(const android::DeviceProductInfo& info) {
347     return StringPrintf("DeviceProductInfo{name=%s, productId=%s, manufacturerPnpId=%s, "
348                         "manufactureOrModelDate=%s}",
349                         info.name.data(), info.productId.data(), info.manufacturerPnpId.data(),
350                         toString(info.manufactureOrModelDate).c_str());
351 }
352