1 /*
2 * Copyright (C) 2015 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 #ifndef COLOR_H
17 #define COLOR_H
18
19 #include <SkColor.h>
20 #include <SkColorSpace.h>
21 #include <SkImageInfo.h>
22 #include <cutils/compiler.h>
23 #include <math.h>
24 #include <system/graphics.h>
25
26 #include <optional>
27
28 struct ANativeWindow_Buffer;
29 struct AHardwareBuffer_Desc;
30
31 namespace android {
32 namespace uirenderer {
33 namespace Color {
34 enum Color {
35 Red_500 = 0xFFF44336,
36 Pink_500 = 0xFFE91E63,
37 Purple_500 = 0xFF9C27B0,
38 DeepPurple_500 = 0xFF673AB7,
39 Indigo_500 = 0xFF3F51B5,
40 Blue_500 = 0xFF2196F3,
41 LightBlue_300 = 0xFF4FC3F7,
42 LightBlue_500 = 0xFF03A9F4,
43 Cyan_500 = 0xFF00BCD4,
44 Teal_500 = 0xFF008577,
45 Teal_700 = 0xFF00796B,
46 Green_500 = 0xFF4CAF50,
47 Green_700 = 0xFF388E3C,
48 LightGreen_500 = 0xFF8BC34A,
49 LightGreen_700 = 0xFF689F38,
50 Lime_500 = 0xFFCDDC39,
51 Yellow_500 = 0xFFFFEB3B,
52 Amber_500 = 0xFFFFC107,
53 Orange_500 = 0xFFFF9800,
54 DeepOrange_500 = 0xFFFF5722,
55 Brown_500 = 0xFF795548,
56 Grey_200 = 0xFFEEEEEE,
57 Grey_500 = 0xFF9E9E9E,
58 Grey_700 = 0xFF616161,
59 BlueGrey_500 = 0xFF607D8B,
60 Transparent = 0x00000000,
61 Black = 0xFF000000,
62 White = 0xFFFFFFFF,
63 };
64 }
65
66 static_assert(Color::White == SK_ColorWHITE, "color format has changed");
67 static_assert(Color::Black == SK_ColorBLACK, "color format has changed");
68
69 // Array of bright (500 intensity) colors for synthetic content
70 static const Color::Color BrightColors[] = {
71 Color::Red_500, Color::Pink_500, Color::Purple_500, Color::DeepPurple_500,
72 Color::Indigo_500, Color::Blue_500, Color::LightBlue_500, Color::Cyan_500,
73 Color::Teal_500, Color::Green_500, Color::LightGreen_500, Color::Lime_500,
74 Color::Yellow_500, Color::Amber_500, Color::Orange_500, Color::DeepOrange_500,
75 Color::Brown_500, Color::Grey_500, Color::BlueGrey_500,
76 };
77 static constexpr int BrightColorsCount = sizeof(BrightColors) / sizeof(Color::Color);
78
79 enum class TransferFunctionType : int8_t { None = 0, Full, Limited, Gamma };
80
81 // Opto-electronic conversion function for the sRGB color space
82 // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value
OECF_sRGB(float linear)83 static constexpr float OECF_sRGB(float linear) {
84 // IEC 61966-2-1:1999
85 return linear <= 0.0031308f ? linear * 12.92f : (powf(linear, 1.0f / 2.4f) * 1.055f) - 0.055f;
86 }
87
88 // Electro-optical conversion function for the sRGB color space
89 // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value
EOCF_sRGB(float srgb)90 static constexpr float EOCF_sRGB(float srgb) {
91 // IEC 61966-2-1:1999
92 return srgb <= 0.04045f ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
93 }
94
95 SkImageInfo ANativeWindowToImageInfo(const ANativeWindow_Buffer& buffer,
96 sk_sp<SkColorSpace> colorSpace);
97
98 SkImageInfo BufferDescriptionToImageInfo(const AHardwareBuffer_Desc& bufferDesc,
99 sk_sp<SkColorSpace> colorSpace);
100
101 uint32_t ColorTypeToBufferFormat(SkColorType colorType);
102 SkColorType BufferFormatToColorType(uint32_t bufferFormat);
103
104 ANDROID_API sk_sp<SkColorSpace> DataSpaceToColorSpace(android_dataspace dataspace);
105
106 /**
107 * Return the android_dataspace corresponding to colorSpace.
108 *
109 * Note: This currently only returns android_dataspaces with corresponding
110 * ADataSpaces. The NDK relies on this, so if you need to update it to return
111 * an android_dataspace *without* an ADataSpace, the NDK methods need to be
112 * updated.
113 *
114 * @param colorSpace May be null, in which case this will return
115 * HAL_DATASPACE_UNKNOWN.
116 * @param colorType Some SkColorSpaces are associated with more than one
117 * android_dataspace. In that case, the SkColorType is used to
118 * determine which one to return.
119 */
120 ANDROID_API android_dataspace ColorSpaceToADataSpace(SkColorSpace*, SkColorType);
121
122 struct Lab {
123 float L;
124 float a;
125 float b;
126 };
127
128 Lab sRGBToLab(SkColor color);
129 SkColor LabToSRGB(const Lab& lab, SkAlpha alpha);
130 skcms_TransferFunction GetPQSkTransferFunction(float sdr_white_level = 0.f);
131 skcms_TransferFunction GetExtendedTransferFunction(float sdrHdrRatio);
132 std::optional<skcms_TransferFunction> GetHLGScaleTransferFunction();
133
134 } /* namespace uirenderer */
135 } /* namespace android */
136
137 #endif /* COLOR_H */
138