1 /*
2 * Copyright 2021 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 "ColorSpaces.h"
18
19 namespace android {
20 namespace renderengine {
21 namespace skia {
22
23 // please keep in sync with hwui/utils/Color.cpp
24 // TODO: Scale by the dimming ratio here instead of in a generic 3x3 transform
25 // Otherwise there may be luminance shift for e.g., HLG.
toSkColorSpace(ui::Dataspace dataspace)26 sk_sp<SkColorSpace> toSkColorSpace(ui::Dataspace dataspace) {
27 skcms_Matrix3x3 gamut;
28 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
29 case HAL_DATASPACE_STANDARD_BT709:
30 gamut = SkNamedGamut::kSRGB;
31 break;
32 case HAL_DATASPACE_STANDARD_BT2020:
33 gamut = SkNamedGamut::kRec2020;
34 break;
35 case HAL_DATASPACE_STANDARD_DCI_P3:
36 gamut = SkNamedGamut::kDisplayP3;
37 break;
38 case HAL_DATASPACE_STANDARD_ADOBE_RGB:
39 gamut = SkNamedGamut::kAdobeRGB;
40 break;
41 case HAL_DATASPACE_STANDARD_BT601_625:
42 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
43 case HAL_DATASPACE_STANDARD_BT601_525:
44 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
45 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
46 case HAL_DATASPACE_STANDARD_BT470M:
47 case HAL_DATASPACE_STANDARD_FILM:
48 case HAL_DATASPACE_STANDARD_UNSPECIFIED:
49 default:
50 gamut = SkNamedGamut::kSRGB;
51 break;
52 }
53
54 switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) {
55 case HAL_DATASPACE_TRANSFER_LINEAR:
56 return SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, gamut);
57 case HAL_DATASPACE_TRANSFER_SRGB:
58 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
59 case HAL_DATASPACE_TRANSFER_GAMMA2_2:
60 return SkColorSpace::MakeRGB({2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
61 case HAL_DATASPACE_TRANSFER_GAMMA2_6:
62 return SkColorSpace::MakeRGB({2.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
63 case HAL_DATASPACE_TRANSFER_GAMMA2_8:
64 return SkColorSpace::MakeRGB({2.8f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, gamut);
65 case HAL_DATASPACE_TRANSFER_ST2084:
66 return SkColorSpace::MakeRGB({-2.f, -1.55522297832f, 1.86045365631f, 32 / 2523.0f,
67 2413 / 128.0f, -2392 / 128.0f, 8192 / 1305.0f},
68 gamut);
69 case HAL_DATASPACE_TRANSFER_SMPTE_170M:
70 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, gamut);
71 case HAL_DATASPACE_TRANSFER_HLG:
72 skcms_TransferFunction hlgFn;
73 if (skcms_TransferFunction_makeScaledHLGish(&hlgFn, 0.314509843, 2.f, 2.f,
74 1.f / 0.17883277f, 0.28466892f,
75 0.55991073f)) {
76 return SkColorSpace::MakeRGB(hlgFn, gamut);
77 } else {
78 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
79 }
80 case HAL_DATASPACE_TRANSFER_UNSPECIFIED:
81 default:
82 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
83 }
84 }
85
86 } // namespace skia
87 } // namespace renderengine
88 } // namespace android