Flutter Engine
The Flutter Engine
SkNDKConversions.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
10namespace {
11static const struct {
13 AndroidBitmapFormat format;
14} gColorTypeTable[] = {
15 { kRGBA_8888_SkColorType, ANDROID_BITMAP_FORMAT_RGBA_8888 },
16 { kRGBA_F16_SkColorType, ANDROID_BITMAP_FORMAT_RGBA_F16 },
17 { kRGB_565_SkColorType, ANDROID_BITMAP_FORMAT_RGB_565 },
18 // Android allows using its alpha 8 format to get 8 bit gray pixels.
19 { kGray_8_SkColorType, ANDROID_BITMAP_FORMAT_A_8 },
20};
21
22} // anonymous namespace
23
26 for (const auto& entry : gColorTypeTable) {
27 if (entry.colorType == colorType) {
28 return entry.format;
29 }
30 }
31 return ANDROID_BITMAP_FORMAT_NONE;
32 }
33
34 SkColorType toColorType(AndroidBitmapFormat format) {
35 for (const auto& entry : gColorTypeTable) {
36 if (entry.format == format) {
37 return entry.colorType;
38 }
39 }
41 }
42
43} // SkNDKConversions
44
45static constexpr skcms_TransferFunction k2Dot6 = {2.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
46
47static constexpr skcms_Matrix3x3 kDCIP3 = {{
48 {0.486143, 0.323835, 0.154234},
49 {0.226676, 0.710327, 0.0629966},
50 {0.000800549, 0.0432385, 0.78275},
51}};
52
53namespace {
54static const struct {
55 ADataSpace dataSpace;
56 skcms_TransferFunction transferFunction;
57 skcms_Matrix3x3 gamut;
58} gColorSpaceTable[] = {
60 { ADATASPACE_SCRGB, SkNamedTransferFn::kSRGB, SkNamedGamut::kSRGB },
61 { ADATASPACE_SCRGB_LINEAR, SkNamedTransferFn::kLinear, SkNamedGamut::kSRGB },
62 { ADATASPACE_SRGB_LINEAR, SkNamedTransferFn::kLinear, SkNamedGamut::kSRGB },
63 { ADATASPACE_ADOBE_RGB, SkNamedTransferFn::k2Dot2, SkNamedGamut::kAdobeRGB },
64 { ADATASPACE_DISPLAY_P3, SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3 },
67 { ADATASPACE_DCI_P3, k2Dot6, kDCIP3 },
68};
69
70} // anonymous namespace
71
72static bool nearly_equal(float a, float b) {
73 return fabs(a - b) < .002f;
74}
75
77 return nearly_equal(x.g, y.g)
78 && nearly_equal(x.a, y.a)
79 && nearly_equal(x.b, y.b)
80 && nearly_equal(x.c, y.c)
81 && nearly_equal(x.d, y.d)
82 && nearly_equal(x.e, y.e)
83 && nearly_equal(x.f, y.f);
84}
85
86static bool nearly_equal(const skcms_Matrix3x3& a, const skcms_Matrix3x3& b) {
87 for (int i = 0; i < 3; i++)
88 for (int j = 0; j < 3; j++) {
89 if (!nearly_equal(a.vals[i][j], b.vals[i][j])) return false;
90 }
91 return true;
92}
93
94namespace SkNDKConversions {
95 ADataSpace toDataSpace(SkColorSpace* cs) {
96 if (!cs) return ADATASPACE_SRGB;
97
99 skcms_Matrix3x3 gamut;
100 if (cs->isNumericalTransferFn(&fn) && cs->toXYZD50(&gamut)) {
101 for (const auto& entry : gColorSpaceTable) {
102 if (nearly_equal(gamut, entry.gamut) && nearly_equal(fn, entry.transferFunction)) {
103 return entry.dataSpace;
104 }
105 }
106 }
107 return ADATASPACE_UNKNOWN;
108 }
109
110 sk_sp<SkColorSpace> toColorSpace(ADataSpace dataSpace) {
111 for (const auto& entry : gColorSpaceTable) {
112 if (entry.dataSpace == dataSpace) {
113 return SkColorSpace::MakeRGB(entry.transferFunction, entry.gamut);
114 }
115 }
116 return nullptr;
117 }
118}
119
SkColorType
Definition: SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static bool nearly_equal(float a, float b)
static constexpr skcms_TransferFunction k2Dot6
static constexpr skcms_Matrix3x3 kDCIP3
bool isNumericalTransferFn(skcms_TransferFunction *fn) const
bool toXYZD50(skcms_Matrix3x3 *toXYZD50) const
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
static bool b
struct MyStruct a[10]
uint32_t uint32_t * format
double y
double x
AndroidBitmapFormat toAndroidBitmapFormat(SkColorType colorType)
ADataSpace toDataSpace(SkColorSpace *cs)
SkColorType toColorType(AndroidBitmapFormat format)
sk_sp< SkColorSpace > toColorSpace(ADataSpace dataSpace)
static constexpr skcms_Matrix3x3 kSRGB
Definition: SkColorSpace.h:67
static constexpr skcms_Matrix3x3 kAdobeRGB
Definition: SkColorSpace.h:77
static constexpr skcms_Matrix3x3 kRec2020
Definition: SkColorSpace.h:93
static constexpr skcms_Matrix3x3 kDisplayP3
Definition: SkColorSpace.h:87
static constexpr skcms_TransferFunction kRec2020
Definition: SkColorSpace.h:54
static constexpr skcms_TransferFunction k2Dot2
Definition: SkColorSpace.h:48
static constexpr skcms_TransferFunction kSRGB
Definition: SkColorSpace.h:45
static constexpr skcms_TransferFunction kLinear
Definition: SkColorSpace.h:51