• 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 <Properties.h>
20 #include <android/hardware_buffer.h>
21 #include <android/native_window.h>
22 #include <ui/ColorSpace.h>
23 #include <utils/Log.h>
24 
25 #include <algorithm>
26 #include <cmath>
27 
28 namespace android {
29 namespace uirenderer {
30 
createImageInfo(int32_t width,int32_t height,int32_t format,sk_sp<SkColorSpace> colorSpace)31 static inline SkImageInfo createImageInfo(int32_t width, int32_t height, int32_t format,
32                                           sk_sp<SkColorSpace> colorSpace) {
33     SkColorType colorType = kUnknown_SkColorType;
34     SkAlphaType alphaType = kOpaque_SkAlphaType;
35     switch (format) {
36         case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
37             colorType = kN32_SkColorType;
38             alphaType = kPremul_SkAlphaType;
39             break;
40         case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
41             colorType = kN32_SkColorType;
42             alphaType = kOpaque_SkAlphaType;
43             break;
44         case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
45             colorType = kRGB_565_SkColorType;
46             alphaType = kOpaque_SkAlphaType;
47             break;
48         case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
49             colorType = kRGBA_1010102_SkColorType;
50             alphaType = kPremul_SkAlphaType;
51             break;
52         case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
53             colorType = kRGBA_F16_SkColorType;
54             alphaType = kPremul_SkAlphaType;
55             break;
56         case AHARDWAREBUFFER_FORMAT_R8_UNORM:
57             colorType = kAlpha_8_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         case kAlpha_8_SkColorType:
94               return AHARDWAREBUFFER_FORMAT_R8_UNORM;
95         default:
96             ALOGV("Unsupported colorType: %d, return RGBA_8888 by default", (int)colorType);
97             return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
98     }
99 }
100 
BufferFormatToColorType(uint32_t format)101 SkColorType BufferFormatToColorType(uint32_t format) {
102     switch (format) {
103         case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
104             return kN32_SkColorType;
105         case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
106             return kN32_SkColorType;
107         case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
108             return kRGB_565_SkColorType;
109         case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
110             return kRGBA_1010102_SkColorType;
111         case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
112             return kRGBA_F16_SkColorType;
113         case AHARDWAREBUFFER_FORMAT_R8_UNORM:
114             return kAlpha_8_SkColorType;
115         default:
116             ALOGV("Unsupported format: %d, return unknown by default", format);
117             return kUnknown_SkColorType;
118     }
119 }
120 
121 namespace {
122 static constexpr skcms_TransferFunction k2Dot6 = {2.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
123 
124 // Skia's SkNamedGamut::kDisplayP3 is based on a white point of D65. This gamut
125 // matches the white point used by ColorSpace.Named.DCIP3.
126 static constexpr skcms_Matrix3x3 kDCIP3 = {{
127         {0.486143, 0.323835, 0.154234},
128         {0.226676, 0.710327, 0.0629966},
129         {0.000800549, 0.0432385, 0.78275},
130 }};
131 
nearlyEqual(float a,float b)132 static bool nearlyEqual(float a, float b) {
133     // By trial and error, this is close enough to match for the ADataSpaces we
134     // compare for.
135     return ::fabs(a - b) < .002f;
136 }
137 
nearlyEqual(const skcms_TransferFunction & x,const skcms_TransferFunction & y)138 static bool nearlyEqual(const skcms_TransferFunction& x, const skcms_TransferFunction& y) {
139     return nearlyEqual(x.g, y.g)
140         && nearlyEqual(x.a, y.a)
141         && nearlyEqual(x.b, y.b)
142         && nearlyEqual(x.c, y.c)
143         && nearlyEqual(x.d, y.d)
144         && nearlyEqual(x.e, y.e)
145         && nearlyEqual(x.f, y.f);
146 }
147 
nearlyEqual(const skcms_Matrix3x3 & x,const skcms_Matrix3x3 & y)148 static bool nearlyEqual(const skcms_Matrix3x3& x, const skcms_Matrix3x3& y) {
149     for (int i = 0; i < 3; i++) {
150         for (int j = 0; j < 3; j++) {
151             if (!nearlyEqual(x.vals[i][j], y.vals[i][j])) return false;
152         }
153     }
154     return true;
155 }
156 
157 } // anonymous namespace
158 
ColorSpaceToADataSpace(SkColorSpace * colorSpace,SkColorType colorType)159 android_dataspace ColorSpaceToADataSpace(SkColorSpace* colorSpace, SkColorType colorType) {
160     if (!colorSpace) {
161         return HAL_DATASPACE_UNKNOWN;
162     }
163 
164     if (colorSpace->isSRGB()) {
165         if (colorType == kRGBA_F16_SkColorType) {
166             return HAL_DATASPACE_V0_SCRGB;
167         }
168         return HAL_DATASPACE_V0_SRGB;
169     }
170 
171     skcms_TransferFunction fn;
172     if (!colorSpace->isNumericalTransferFn(&fn)) {
173         auto res = skcms_TransferFunction_getType(&fn);
174         if (res == skcms_TFType_PQish) {
175             return HAL_DATASPACE_BT2020_PQ;
176         }
177         if (res == skcms_TFType_HLGish) {
178             return static_cast<android_dataspace>(HAL_DATASPACE_BT2020_HLG);
179         }
180         LOG_ALWAYS_FATAL("Only select non-numerical transfer functions are supported");
181     }
182 
183     skcms_Matrix3x3 gamut;
184     LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&gamut));
185 
186     if (nearlyEqual(gamut, SkNamedGamut::kSRGB)) {
187         if (nearlyEqual(fn, SkNamedTransferFn::kLinear)) {
188             // Skia doesn't differentiate amongst the RANGES. In Java, we associate
189             // LINEAR_EXTENDED_SRGB with F16, and LINEAR_SRGB with other Configs.
190             // Make the same association here.
191             if (colorType == kRGBA_F16_SkColorType) {
192                 return HAL_DATASPACE_V0_SCRGB_LINEAR;
193             }
194             return HAL_DATASPACE_V0_SRGB_LINEAR;
195         }
196 
197         if (nearlyEqual(fn, SkNamedTransferFn::kRec2020)) {
198             return HAL_DATASPACE_V0_BT709;
199         }
200     }
201 
202     if (nearlyEqual(fn, SkNamedTransferFn::kSRGB) && nearlyEqual(gamut, SkNamedGamut::kDisplayP3)) {
203         return HAL_DATASPACE_DISPLAY_P3;
204     }
205 
206     if (nearlyEqual(fn, SkNamedTransferFn::k2Dot2) && nearlyEqual(gamut, SkNamedGamut::kAdobeRGB)) {
207         return HAL_DATASPACE_ADOBE_RGB;
208     }
209 
210     if (nearlyEqual(fn, SkNamedTransferFn::kRec2020) &&
211         nearlyEqual(gamut, SkNamedGamut::kRec2020)) {
212         return HAL_DATASPACE_BT2020;
213     }
214 
215     if (nearlyEqual(fn, k2Dot6) && nearlyEqual(gamut, kDCIP3)) {
216         return HAL_DATASPACE_DCI_P3;
217     }
218 
219     return HAL_DATASPACE_UNKNOWN;
220 }
221 
DataSpaceToColorSpace(android_dataspace dataspace)222 sk_sp<SkColorSpace> DataSpaceToColorSpace(android_dataspace dataspace) {
223     if (dataspace == HAL_DATASPACE_UNKNOWN) {
224         return SkColorSpace::MakeSRGB();
225     }
226     if (dataspace == HAL_DATASPACE_DCI_P3) {
227         // This cannot be handled by the switch statements below because it
228         // needs to use the locally-defined kDCIP3 gamut, rather than the one in
229         // Skia (SkNamedGamut), which is used for other data spaces with
230         // HAL_DATASPACE_STANDARD_DCI_P3 (e.g. HAL_DATASPACE_DISPLAY_P3).
231         return SkColorSpace::MakeRGB(k2Dot6, kDCIP3);
232     }
233 
234     skcms_Matrix3x3 gamut;
235     switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
236         case HAL_DATASPACE_STANDARD_BT709:
237             gamut = SkNamedGamut::kSRGB;
238             break;
239         case HAL_DATASPACE_STANDARD_BT2020:
240         case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
241             gamut = SkNamedGamut::kRec2020;
242             break;
243         case HAL_DATASPACE_STANDARD_DCI_P3:
244             gamut = SkNamedGamut::kDisplayP3;
245             break;
246         case HAL_DATASPACE_STANDARD_ADOBE_RGB:
247             gamut = SkNamedGamut::kAdobeRGB;
248             break;
249         case HAL_DATASPACE_STANDARD_UNSPECIFIED:
250             return nullptr;
251         case HAL_DATASPACE_STANDARD_BT601_625:
252         case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
253         case HAL_DATASPACE_STANDARD_BT601_525:
254         case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
255         case HAL_DATASPACE_STANDARD_BT470M:
256         case HAL_DATASPACE_STANDARD_FILM:
257         default:
258             ALOGV("Unsupported Gamut: %d", dataspace);
259             return nullptr;
260     }
261 
262     // HLG
263     if ((dataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_HLG) {
264         const auto hlgFn = GetHLGScaleTransferFunction();
265         if (hlgFn.has_value()) {
266             return SkColorSpace::MakeRGB(hlgFn.value(), gamut);
267         }
268     }
269 
270     switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) {
271         case HAL_DATASPACE_TRANSFER_LINEAR:
272             return SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, gamut);
273         case HAL_DATASPACE_TRANSFER_SRGB:
274             return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
275         case HAL_DATASPACE_TRANSFER_GAMMA2_2:
276             return SkColorSpace::MakeRGB({2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
277         case HAL_DATASPACE_TRANSFER_GAMMA2_6:
278             return SkColorSpace::MakeRGB(k2Dot6, gamut);
279         case HAL_DATASPACE_TRANSFER_GAMMA2_8:
280             return SkColorSpace::MakeRGB({2.8f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
281         case HAL_DATASPACE_TRANSFER_ST2084:
282             return SkColorSpace::MakeRGB({-2.0, -1.555223, 1.860454, 32 / 2523.0, 2413 / 128.0,
283                                           -2392 / 128.0, 8192 / 1305.0},
284                                          gamut);
285         case HAL_DATASPACE_TRANSFER_SMPTE_170M:
286             return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, gamut);
287         case HAL_DATASPACE_TRANSFER_UNSPECIFIED:
288             return nullptr;
289         default:
290             ALOGV("Unsupported Gamma: %d", dataspace);
291             return nullptr;
292     }
293 }
294 
295 template<typename T>
clamp(T x,T min,T max)296 static constexpr T clamp(T x, T min, T max) {
297     return x < min ? min : x > max ? max : x;
298 }
299 
300 //static const float2 ILLUMINANT_D50_XY = {0.34567f, 0.35850f};
301 static const float3 ILLUMINANT_D50_XYZ = {0.964212f, 1.0f, 0.825188f};
302 static const mat3 BRADFORD = mat3{
303         float3{ 0.8951f, -0.7502f,  0.0389f},
304         float3{ 0.2664f,  1.7135f, -0.0685f},
305         float3{-0.1614f,  0.0367f,  1.0296f}
306 };
307 
adaptation(const mat3 & matrix,const float3 & srcWhitePoint,const float3 & dstWhitePoint)308 static mat3 adaptation(const mat3& matrix, const float3& srcWhitePoint, const float3& dstWhitePoint) {
309     float3 srcLMS = matrix * srcWhitePoint;
310     float3 dstLMS = matrix * dstWhitePoint;
311     return inverse(matrix) * mat3{dstLMS / srcLMS} * matrix;
312 }
313 
314 namespace LabColorSpace {
315 
316 static constexpr float A = 216.0f / 24389.0f;
317 static constexpr float B = 841.0f / 108.0f;
318 static constexpr float C = 4.0f / 29.0f;
319 static constexpr float D = 6.0f / 29.0f;
320 
toXyz(const Lab & lab)321 float3 toXyz(const Lab& lab) {
322     float3 v { lab.L, lab.a, lab.b };
323     v[0] = clamp(v[0], 0.0f, 100.0f);
324     v[1] = clamp(v[1], -128.0f, 128.0f);
325     v[2] = clamp(v[2], -128.0f, 128.0f);
326 
327     float fy = (v[0] + 16.0f) / 116.0f;
328     float fx = fy + (v[1] * 0.002f);
329     float fz = fy - (v[2] * 0.005f);
330     float X = fx > D ? fx * fx * fx : (1.0f / B) * (fx - C);
331     float Y = fy > D ? fy * fy * fy : (1.0f / B) * (fy - C);
332     float Z = fz > D ? fz * fz * fz : (1.0f / B) * (fz - C);
333 
334     v[0] = X * ILLUMINANT_D50_XYZ[0];
335     v[1] = Y * ILLUMINANT_D50_XYZ[1];
336     v[2] = Z * ILLUMINANT_D50_XYZ[2];
337 
338     return v;
339 }
340 
fromXyz(const float3 & v)341 Lab fromXyz(const float3& v) {
342     float X = v[0] / ILLUMINANT_D50_XYZ[0];
343     float Y = v[1] / ILLUMINANT_D50_XYZ[1];
344     float Z = v[2] / ILLUMINANT_D50_XYZ[2];
345 
346     float fx = X > A ? pow(X, 1.0f / 3.0f) : B * X + C;
347     float fy = Y > A ? pow(Y, 1.0f / 3.0f) : B * Y + C;
348     float fz = Z > A ? pow(Z, 1.0f / 3.0f) : B * Z + C;
349 
350     float L = 116.0f * fy - 16.0f;
351     float a = 500.0f * (fx - fy);
352     float b = 200.0f * (fy - fz);
353 
354     return Lab {
355             clamp(L, 0.0f, 100.0f),
356             clamp(a, -128.0f, 128.0f),
357             clamp(b, -128.0f, 128.0f)
358     };
359 }
360 
361 };
362 
sRGBToLab(SkColor color)363 Lab sRGBToLab(SkColor color) {
364     auto colorSpace = ColorSpace::sRGB();
365     float3 rgb;
366     rgb.r = SkColorGetR(color) / 255.0f;
367     rgb.g = SkColorGetG(color) / 255.0f;
368     rgb.b = SkColorGetB(color) / 255.0f;
369     float3 xyz = colorSpace.rgbToXYZ(rgb);
370     float3 srcXYZ = ColorSpace::XYZ(float3{colorSpace.getWhitePoint(), 1});
371     xyz = adaptation(BRADFORD, srcXYZ, ILLUMINANT_D50_XYZ) * xyz;
372     return LabColorSpace::fromXyz(xyz);
373 }
374 
LabToSRGB(const Lab & lab,SkAlpha alpha)375 SkColor LabToSRGB(const Lab& lab, SkAlpha alpha) {
376     auto colorSpace = ColorSpace::sRGB();
377     float3 xyz = LabColorSpace::toXyz(lab);
378     float3 dstXYZ = ColorSpace::XYZ(float3{colorSpace.getWhitePoint(), 1});
379     xyz = adaptation(BRADFORD, ILLUMINANT_D50_XYZ, dstXYZ) * xyz;
380     float3 rgb = colorSpace.xyzToRGB(xyz);
381     return SkColorSetARGB(alpha,
382             static_cast<uint8_t>(rgb.r * 255),
383             static_cast<uint8_t>(rgb.g * 255),
384             static_cast<uint8_t>(rgb.b * 255));
385 }
386 
GetPQSkTransferFunction(float sdr_white_level)387 skcms_TransferFunction GetPQSkTransferFunction(float sdr_white_level) {
388     if (sdr_white_level <= 0.f) {
389         sdr_white_level = Properties::defaultSdrWhitePoint;
390     }
391     // The generic PQ transfer function produces normalized luminance values i.e.
392     // the range 0-1 represents 0-10000 nits for the reference display, but we
393     // want to map 1.0 to |sdr_white_level| nits so we need to scale accordingly.
394     const double w = 10000. / sdr_white_level;
395     // Distribute scaling factor W by scaling A and B with X ^ (1/F):
396     // ((A + Bx^C) / (D + Ex^C))^F * W = ((A + Bx^C) / (D + Ex^C) * W^(1/F))^F
397     // See https://crbug.com/1058580#c32 for discussion.
398     skcms_TransferFunction fn = SkNamedTransferFn::kPQ;
399     const double ws = pow(w, 1. / fn.f);
400     fn.a = ws * fn.a;
401     fn.b = ws * fn.b;
402     return fn;
403 }
404 
trfn_apply_gain(const skcms_TransferFunction trfn,float gain)405 static skcms_TransferFunction trfn_apply_gain(const skcms_TransferFunction trfn, float gain) {
406     float pow_gain_ginv = std::pow(gain, 1 / trfn.g);
407     skcms_TransferFunction result;
408     result.g = trfn.g;
409     result.a = trfn.a * pow_gain_ginv;
410     result.b = trfn.b * pow_gain_ginv;
411     result.c = trfn.c * gain;
412     result.d = trfn.d;
413     result.e = trfn.e * gain;
414     result.f = trfn.f * gain;
415     return result;
416 }
417 
GetExtendedTransferFunction(float sdrHdrRatio)418 skcms_TransferFunction GetExtendedTransferFunction(float sdrHdrRatio) {
419     if (sdrHdrRatio <= 1.f) {
420         return SkNamedTransferFn::kSRGB;
421     }
422     // Scale the transfer by the sdrHdrRatio
423     return trfn_apply_gain(SkNamedTransferFn::kSRGB, sdrHdrRatio);
424 }
425 
426 // Skia skcms' default HLG maps encoded [0, 1] to linear [1, 12] in order to follow ARIB
427 // but LinearEffect expects to map 1.0 == 203 nits
GetHLGScaleTransferFunction()428 std::optional<skcms_TransferFunction> GetHLGScaleTransferFunction() {
429     skcms_TransferFunction hlgFn;
430     if (skcms_TransferFunction_makeScaledHLGish(&hlgFn, 0.314509843, 2.f, 2.f, 1.f / 0.17883277f,
431                                                 0.28466892f, 0.55991073f)) {
432         return std::make_optional<skcms_TransferFunction>(hlgFn);
433     }
434     return {};
435 }
436 
437 }  // namespace uirenderer
438 }  // namespace android
439