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