1 /*
2  * Copyright (C) 2018 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 "CanvasTransform.h"
18 
19 #include <SkAndroidFrameworkUtils.h>
20 #include <SkBlendMode.h>
21 #include <SkColorFilter.h>
22 #include <SkGradientShader.h>
23 #include <SkHighContrastFilter.h>
24 #include <SkPaint.h>
25 #include <SkShader.h>
26 #include <log/log.h>
27 
28 #include <algorithm>
29 #include <cmath>
30 
31 #include "Properties.h"
32 #include "utils/Color.h"
33 
34 namespace android::uirenderer {
35 
makeLight(SkColor color)36 SkColor makeLight(SkColor color) {
37     Lab lab = sRGBToLab(color);
38     float invertedL = std::min(110 - lab.L, 100.0f);
39     if (invertedL > lab.L) {
40         lab.L = invertedL;
41         return LabToSRGB(lab, SkColorGetA(color));
42     } else {
43         return color;
44     }
45 }
46 
makeDark(SkColor color)47 SkColor makeDark(SkColor color) {
48     Lab lab = sRGBToLab(color);
49     float invertedL = std::min(110 - lab.L, 100.0f);
50     if (invertedL < lab.L) {
51         lab.L = invertedL;
52         return LabToSRGB(lab, SkColorGetA(color));
53     } else {
54         return color;
55     }
56 }
57 
transformColor(ColorTransform transform,SkColor color)58 SkColor transformColor(ColorTransform transform, SkColor color) {
59     switch (transform) {
60         case ColorTransform::Light:
61             return makeLight(color);
62         case ColorTransform::Dark:
63             return makeDark(color);
64         default:
65             return color;
66     }
67 }
68 
transformColorInverse(ColorTransform transform,SkColor color)69 SkColor transformColorInverse(ColorTransform transform, SkColor color) {
70     switch (transform) {
71         case ColorTransform::Dark:
72             return makeLight(color);
73         case ColorTransform::Light:
74             return makeDark(color);
75         default:
76             return color;
77     }
78 }
79 
applyColorTransform(ColorTransform transform,SkPaint & paint)80 static void applyColorTransform(ColorTransform transform, SkPaint& paint) {
81     if (transform == ColorTransform::None) return;
82 
83     if (transform == ColorTransform::Invert) {
84         auto filter = SkHighContrastFilter::Make(
85                 {/* grayscale= */ false, SkHighContrastConfig::InvertStyle::kInvertLightness,
86                  /* contrast= */ 0.0f});
87 
88         if (paint.getColorFilter()) {
89             paint.setColorFilter(SkColorFilters::Compose(filter, paint.refColorFilter()));
90         } else {
91             paint.setColorFilter(filter);
92         }
93         return;
94     }
95 
96     SkColor newColor = transformColor(transform, paint.getColor());
97     paint.setColor(newColor);
98 
99     if (paint.getShader()) {
100         SkAndroidFrameworkUtils::LinearGradientInfo info;
101         std::array<SkColor, 10> _colorStorage;
102         std::array<SkScalar, _colorStorage.size()> _offsetStorage;
103         info.fColorCount = _colorStorage.size();
104         info.fColors = _colorStorage.data();
105         info.fColorOffsets = _offsetStorage.data();
106 
107         if (SkAndroidFrameworkUtils::ShaderAsALinearGradient(paint.getShader(), &info) &&
108             info.fColorCount <= _colorStorage.size()) {
109             for (int i = 0; i < info.fColorCount; i++) {
110                 info.fColors[i] = transformColor(transform, info.fColors[i]);
111             }
112             paint.setShader(SkGradientShader::MakeLinear(
113                     info.fPoints, info.fColors, info.fColorOffsets, info.fColorCount,
114                     info.fTileMode, info.fGradientFlags, nullptr));
115         }
116     }
117 
118     if (paint.getColorFilter()) {
119         SkBlendMode mode;
120         SkColor color;
121         // TODO: LRU this or something to avoid spamming new color mode filters
122         if (paint.getColorFilter()->asAColorMode(&color, &mode)) {
123             color = transformColor(transform, color);
124             paint.setColorFilter(SkColorFilters::Blend(color, mode));
125         }
126     }
127 }
128 
paletteForColorHSV(SkColor color)129 static BitmapPalette paletteForColorHSV(SkColor color) {
130     float hsv[3];
131     SkColorToHSV(color, hsv);
132     return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark;
133 }
134 
filterPalette(const SkPaint * paint,BitmapPalette palette)135 static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) {
136     if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) {
137         return palette;
138     }
139 
140     SkColor4f color = palette == BitmapPalette::Light ? SkColors::kWhite : SkColors::kBlack;
141     sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
142     color = paint->getColorFilter()->filterColor4f(color, srgb.get(), srgb.get());
143     return paletteForColorHSV(color.toSkColor());
144 }
145 
transformPaint(ColorTransform transform,SkPaint * paint)146 bool transformPaint(ColorTransform transform, SkPaint* paint) {
147     // TODO
148     applyColorTransform(transform, *paint);
149     return true;
150 }
151 
transformPaint(ColorTransform transform,SkPaint * paint,BitmapPalette palette)152 bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) {
153     palette = filterPalette(paint, palette);
154     bool shouldInvert = false;
155     if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) {
156         shouldInvert = true;
157     }
158     if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) {
159         shouldInvert = true;
160     }
161     if (shouldInvert) {
162         SkHighContrastConfig config;
163         config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
164         paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter()));
165     }
166     return shouldInvert;
167 }
168 
169 }  // namespace android::uirenderer
170