• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "Color.h"
18 
19 #include <ui/ColorSpace.h>
20 #include <utils/Log.h>
21 
22 #ifdef __ANDROID__ // Layoutlib does not support hardware buffers or native windows
23 #include <android/hardware_buffer.h>
24 #include <android/native_window.h>
25 #endif
26 
27 #include <algorithm>
28 #include <cmath>
29 #include <Properties.h>
30 
31 namespace android {
32 namespace uirenderer {
33 
34 #ifdef __ANDROID__ // Layoutlib does not support hardware buffers or native windows
createImageInfo(int32_t width,int32_t height,int32_t format,sk_sp<SkColorSpace> colorSpace)35 static inline SkImageInfo createImageInfo(int32_t width, int32_t height, int32_t format,
36                                           sk_sp<SkColorSpace> colorSpace) {
37     SkColorType colorType = kUnknown_SkColorType;
38     SkAlphaType alphaType = kOpaque_SkAlphaType;
39     switch (format) {
40         case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
41             colorType = kN32_SkColorType;
42             alphaType = kPremul_SkAlphaType;
43             break;
44         case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
45             colorType = kN32_SkColorType;
46             alphaType = kOpaque_SkAlphaType;
47             break;
48         case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
49             colorType = kRGB_565_SkColorType;
50             alphaType = kOpaque_SkAlphaType;
51             break;
52         case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
53             colorType = kRGBA_1010102_SkColorType;
54             alphaType = kPremul_SkAlphaType;
55             break;
56         case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
57             colorType = kRGBA_F16_SkColorType;
58             alphaType = kPremul_SkAlphaType;
59             break;
60         default:
61             ALOGV("Unsupported format: %d, return unknown by default", format);
62             break;
63     }
64     return SkImageInfo::Make(width, height, colorType, alphaType, colorSpace);
65 }
66 
ANativeWindowToImageInfo(const ANativeWindow_Buffer & buffer,sk_sp<SkColorSpace> colorSpace)67 SkImageInfo ANativeWindowToImageInfo(const ANativeWindow_Buffer& buffer,
68                                      sk_sp<SkColorSpace> colorSpace) {
69     return createImageInfo(buffer.width, buffer.height, buffer.format, colorSpace);
70 }
71 
BufferDescriptionToImageInfo(const AHardwareBuffer_Desc & bufferDesc,sk_sp<SkColorSpace> colorSpace)72 SkImageInfo BufferDescriptionToImageInfo(const AHardwareBuffer_Desc& bufferDesc,
73                                          sk_sp<SkColorSpace> colorSpace) {
74     return createImageInfo(bufferDesc.width, bufferDesc.height, bufferDesc.format, colorSpace);
75 }
76 
ColorTypeToBufferFormat(SkColorType colorType)77 uint32_t ColorTypeToBufferFormat(SkColorType colorType) {
78     switch (colorType) {
79         case kRGBA_8888_SkColorType:
80             return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
81         case kRGBA_F16_SkColorType:
82             return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
83         case kRGB_565_SkColorType:
84             return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
85         case kRGB_888x_SkColorType:
86             return AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM;
87         case kRGBA_1010102_SkColorType:
88             return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
89         case kARGB_4444_SkColorType:
90             // Hardcoding the value from android::PixelFormat
91             static constexpr uint64_t kRGBA4444 = 7;
92             return kRGBA4444;
93         default:
94             ALOGV("Unsupported colorType: %d, return RGBA_8888 by default", (int)colorType);
95             return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
96     }
97 }
98 #endif
99 
100 namespace {
101 static constexpr skcms_TransferFunction k2Dot6 = {2.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
102 
103 // Skia's SkNamedGamut::kDisplayP3 is based on a white point of D65. This gamut
104 // matches the white point used by ColorSpace.Named.DCIP3.
105 static constexpr skcms_Matrix3x3 kDCIP3 = {{
106         {0.486143, 0.323835, 0.154234},
107         {0.226676, 0.710327, 0.0629966},
108         {0.000800549, 0.0432385, 0.78275},
109 }};
110 
nearlyEqual(float a,float b)111 static bool nearlyEqual(float a, float b) {
112     // By trial and error, this is close enough to match for the ADataSpaces we
113     // compare for.
114     return ::fabs(a - b) < .002f;
115 }
116 
nearlyEqual(const skcms_TransferFunction & x,const skcms_TransferFunction & y)117 static bool nearlyEqual(const skcms_TransferFunction& x, const skcms_TransferFunction& y) {
118     return nearlyEqual(x.g, y.g)
119         && nearlyEqual(x.a, y.a)
120         && nearlyEqual(x.b, y.b)
121         && nearlyEqual(x.c, y.c)
122         && nearlyEqual(x.d, y.d)
123         && nearlyEqual(x.e, y.e)
124         && nearlyEqual(x.f, y.f);
125 }
126 
nearlyEqual(const skcms_Matrix3x3 & x,const skcms_Matrix3x3 & y)127 static bool nearlyEqual(const skcms_Matrix3x3& x, const skcms_Matrix3x3& y) {
128     for (int i = 0; i < 3; i++) {
129         for (int j = 0; j < 3; j++) {
130             if (!nearlyEqual(x.vals[i][j], y.vals[i][j])) return false;
131         }
132     }
133     return true;
134 }
135 
136 } // anonymous namespace
137 
ColorSpaceToADataSpace(SkColorSpace * colorSpace,SkColorType colorType)138 android_dataspace ColorSpaceToADataSpace(SkColorSpace* colorSpace, SkColorType colorType) {
139     if (!colorSpace) {
140         return HAL_DATASPACE_UNKNOWN;
141     }
142 
143     if (colorSpace->isSRGB()) {
144         if (colorType == kRGBA_F16_SkColorType) {
145             return HAL_DATASPACE_V0_SCRGB;
146         }
147         return HAL_DATASPACE_V0_SRGB;
148     }
149 
150     skcms_TransferFunction fn;
151     if (!colorSpace->isNumericalTransferFn(&fn)) {
152         // pq with the default white point
153         auto rec2020PQ = SkColorSpace::MakeRGB(GetPQSkTransferFunction(), SkNamedGamut::kRec2020);
154         if (SkColorSpace::Equals(colorSpace, rec2020PQ.get())) {
155             return HAL_DATASPACE_BT2020_PQ;
156         }
157         // standard PQ
158         rec2020PQ = SkColorSpace::MakeRGB(SkNamedTransferFn::kPQ, SkNamedGamut::kRec2020);
159         if (SkColorSpace::Equals(colorSpace, rec2020PQ.get())) {
160             return HAL_DATASPACE_BT2020_PQ;
161         }
162         LOG_ALWAYS_FATAL("Only select non-numerical transfer functions are supported");
163     }
164 
165     skcms_Matrix3x3 gamut;
166     LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&gamut));
167 
168     if (nearlyEqual(gamut, SkNamedGamut::kSRGB)) {
169         if (nearlyEqual(fn, SkNamedTransferFn::kLinear)) {
170             // Skia doesn't differentiate amongst the RANGES. In Java, we associate
171             // LINEAR_EXTENDED_SRGB with F16, and LINEAR_SRGB with other Configs.
172             // Make the same association here.
173             if (colorType == kRGBA_F16_SkColorType) {
174                 return HAL_DATASPACE_V0_SCRGB_LINEAR;
175             }
176             return HAL_DATASPACE_V0_SRGB_LINEAR;
177         }
178 
179         if (nearlyEqual(fn, SkNamedTransferFn::kRec2020)) {
180             return HAL_DATASPACE_V0_BT709;
181         }
182     }
183 
184     if (nearlyEqual(fn, SkNamedTransferFn::kSRGB) && nearlyEqual(gamut, SkNamedGamut::kDisplayP3)) {
185         return HAL_DATASPACE_DISPLAY_P3;
186     }
187 
188     if (nearlyEqual(fn, SkNamedTransferFn::k2Dot2) && nearlyEqual(gamut, SkNamedGamut::kAdobeRGB)) {
189         return HAL_DATASPACE_ADOBE_RGB;
190     }
191 
192     if (nearlyEqual(fn, SkNamedTransferFn::kRec2020) &&
193         nearlyEqual(gamut, SkNamedGamut::kRec2020)) {
194         return HAL_DATASPACE_BT2020;
195     }
196 
197     if (nearlyEqual(fn, k2Dot6) && nearlyEqual(gamut, kDCIP3)) {
198         return HAL_DATASPACE_DCI_P3;
199     }
200 
201     return HAL_DATASPACE_UNKNOWN;
202 }
203 
DataSpaceToColorSpace(android_dataspace dataspace)204 sk_sp<SkColorSpace> DataSpaceToColorSpace(android_dataspace dataspace) {
205     if (dataspace == HAL_DATASPACE_UNKNOWN) {
206         return SkColorSpace::MakeSRGB();
207     }
208     if (dataspace == HAL_DATASPACE_DCI_P3) {
209         // This cannot be handled by the switch statements below because it
210         // needs to use the locally-defined kDCIP3 gamut, rather than the one in
211         // Skia (SkNamedGamut), which is used for other data spaces with
212         // HAL_DATASPACE_STANDARD_DCI_P3 (e.g. HAL_DATASPACE_DISPLAY_P3).
213         return SkColorSpace::MakeRGB(k2Dot6, kDCIP3);
214     }
215 
216     skcms_Matrix3x3 gamut;
217     switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
218         case HAL_DATASPACE_STANDARD_BT709:
219             gamut = SkNamedGamut::kSRGB;
220             break;
221         case HAL_DATASPACE_STANDARD_BT2020:
222             gamut = SkNamedGamut::kRec2020;
223             break;
224         case HAL_DATASPACE_STANDARD_DCI_P3:
225             gamut = SkNamedGamut::kDisplayP3;
226             break;
227         case HAL_DATASPACE_STANDARD_ADOBE_RGB:
228             gamut = SkNamedGamut::kAdobeRGB;
229             break;
230         case HAL_DATASPACE_STANDARD_UNSPECIFIED:
231             return nullptr;
232         case HAL_DATASPACE_STANDARD_BT601_625:
233         case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
234         case HAL_DATASPACE_STANDARD_BT601_525:
235         case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
236         case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
237         case HAL_DATASPACE_STANDARD_BT470M:
238         case HAL_DATASPACE_STANDARD_FILM:
239         default:
240             ALOGV("Unsupported Gamut: %d", dataspace);
241             return nullptr;
242     }
243 
244     switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) {
245         case HAL_DATASPACE_TRANSFER_LINEAR:
246             return SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, gamut);
247         case HAL_DATASPACE_TRANSFER_SRGB:
248             return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
249         case HAL_DATASPACE_TRANSFER_GAMMA2_2:
250             return SkColorSpace::MakeRGB({2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
251         case HAL_DATASPACE_TRANSFER_GAMMA2_6:
252             return SkColorSpace::MakeRGB(k2Dot6, gamut);
253         case HAL_DATASPACE_TRANSFER_GAMMA2_8:
254             return SkColorSpace::MakeRGB({2.8f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
255         case HAL_DATASPACE_TRANSFER_ST2084:
256             return SkColorSpace::MakeRGB(SkNamedTransferFn::kPQ, gamut);
257         case HAL_DATASPACE_TRANSFER_SMPTE_170M:
258             return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, gamut);
259         case HAL_DATASPACE_TRANSFER_UNSPECIFIED:
260             return nullptr;
261         case HAL_DATASPACE_TRANSFER_HLG:
262         default:
263             ALOGV("Unsupported Gamma: %d", dataspace);
264             return nullptr;
265     }
266 }
267 
268 template<typename T>
clamp(T x,T min,T max)269 static constexpr T clamp(T x, T min, T max) {
270     return x < min ? min : x > max ? max : x;
271 }
272 
273 //static const float2 ILLUMINANT_D50_XY = {0.34567f, 0.35850f};
274 static const float3 ILLUMINANT_D50_XYZ = {0.964212f, 1.0f, 0.825188f};
275 static const mat3 BRADFORD = mat3{
276         float3{ 0.8951f, -0.7502f,  0.0389f},
277         float3{ 0.2664f,  1.7135f, -0.0685f},
278         float3{-0.1614f,  0.0367f,  1.0296f}
279 };
280 
adaptation(const mat3 & matrix,const float3 & srcWhitePoint,const float3 & dstWhitePoint)281 static mat3 adaptation(const mat3& matrix, const float3& srcWhitePoint, const float3& dstWhitePoint) {
282     float3 srcLMS = matrix * srcWhitePoint;
283     float3 dstLMS = matrix * dstWhitePoint;
284     return inverse(matrix) * mat3{dstLMS / srcLMS} * matrix;
285 }
286 
287 namespace LabColorSpace {
288 
289 static constexpr float A = 216.0f / 24389.0f;
290 static constexpr float B = 841.0f / 108.0f;
291 static constexpr float C = 4.0f / 29.0f;
292 static constexpr float D = 6.0f / 29.0f;
293 
toXyz(const Lab & lab)294 float3 toXyz(const Lab& lab) {
295     float3 v { lab.L, lab.a, lab.b };
296     v[0] = clamp(v[0], 0.0f, 100.0f);
297     v[1] = clamp(v[1], -128.0f, 128.0f);
298     v[2] = clamp(v[2], -128.0f, 128.0f);
299 
300     float fy = (v[0] + 16.0f) / 116.0f;
301     float fx = fy + (v[1] * 0.002f);
302     float fz = fy - (v[2] * 0.005f);
303     float X = fx > D ? fx * fx * fx : (1.0f / B) * (fx - C);
304     float Y = fy > D ? fy * fy * fy : (1.0f / B) * (fy - C);
305     float Z = fz > D ? fz * fz * fz : (1.0f / B) * (fz - C);
306 
307     v[0] = X * ILLUMINANT_D50_XYZ[0];
308     v[1] = Y * ILLUMINANT_D50_XYZ[1];
309     v[2] = Z * ILLUMINANT_D50_XYZ[2];
310 
311     return v;
312 }
313 
fromXyz(const float3 & v)314 Lab fromXyz(const float3& v) {
315     float X = v[0] / ILLUMINANT_D50_XYZ[0];
316     float Y = v[1] / ILLUMINANT_D50_XYZ[1];
317     float Z = v[2] / ILLUMINANT_D50_XYZ[2];
318 
319     float fx = X > A ? pow(X, 1.0f / 3.0f) : B * X + C;
320     float fy = Y > A ? pow(Y, 1.0f / 3.0f) : B * Y + C;
321     float fz = Z > A ? pow(Z, 1.0f / 3.0f) : B * Z + C;
322 
323     float L = 116.0f * fy - 16.0f;
324     float a = 500.0f * (fx - fy);
325     float b = 200.0f * (fy - fz);
326 
327     return Lab {
328             clamp(L, 0.0f, 100.0f),
329             clamp(a, -128.0f, 128.0f),
330             clamp(b, -128.0f, 128.0f)
331     };
332 }
333 
334 };
335 
sRGBToLab(SkColor color)336 Lab sRGBToLab(SkColor color) {
337     auto colorSpace = ColorSpace::sRGB();
338     float3 rgb;
339     rgb.r = SkColorGetR(color) / 255.0f;
340     rgb.g = SkColorGetG(color) / 255.0f;
341     rgb.b = SkColorGetB(color) / 255.0f;
342     float3 xyz = colorSpace.rgbToXYZ(rgb);
343     float3 srcXYZ = ColorSpace::XYZ(float3{colorSpace.getWhitePoint(), 1});
344     xyz = adaptation(BRADFORD, srcXYZ, ILLUMINANT_D50_XYZ) * xyz;
345     return LabColorSpace::fromXyz(xyz);
346 }
347 
LabToSRGB(const Lab & lab,SkAlpha alpha)348 SkColor LabToSRGB(const Lab& lab, SkAlpha alpha) {
349     auto colorSpace = ColorSpace::sRGB();
350     float3 xyz = LabColorSpace::toXyz(lab);
351     float3 dstXYZ = ColorSpace::XYZ(float3{colorSpace.getWhitePoint(), 1});
352     xyz = adaptation(BRADFORD, ILLUMINANT_D50_XYZ, dstXYZ) * xyz;
353     float3 rgb = colorSpace.xyzToRGB(xyz);
354     return SkColorSetARGB(alpha,
355             static_cast<uint8_t>(rgb.r * 255),
356             static_cast<uint8_t>(rgb.g * 255),
357             static_cast<uint8_t>(rgb.b * 255));
358 }
359 
GetPQSkTransferFunction(float sdr_white_level)360 skcms_TransferFunction GetPQSkTransferFunction(float sdr_white_level) {
361     if (sdr_white_level <= 0.f) {
362         sdr_white_level = Properties::defaultSdrWhitePoint;
363     }
364     // The generic PQ transfer function produces normalized luminance values i.e.
365     // the range 0-1 represents 0-10000 nits for the reference display, but we
366     // want to map 1.0 to |sdr_white_level| nits so we need to scale accordingly.
367     const double w = 10000. / sdr_white_level;
368     // Distribute scaling factor W by scaling A and B with X ^ (1/F):
369     // ((A + Bx^C) / (D + Ex^C))^F * W = ((A + Bx^C) / (D + Ex^C) * W^(1/F))^F
370     // See https://crbug.com/1058580#c32 for discussion.
371     skcms_TransferFunction fn = SkNamedTransferFn::kPQ;
372     const double ws = pow(w, 1. / fn.f);
373     fn.a = ws * fn.a;
374     fn.b = ws * fn.b;
375     return fn;
376 }
377 
378 }  // namespace uirenderer
379 }  // namespace android
380