| 1 |
/* |
| 2 |
* Copyright 2023 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 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkData.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/effects/SkGradient.h" |
| 17 |
#include "include/effects/SkRuntimeEffect.h" |
| 18 |
#include "src/core/SkColorFilterPriv.h" |
| 19 |
#include "src/shaders/SkWorkingColorSpaceShader.h" |
| 20 |
|
| 21 |
static sk_sp<SkShader> color_shader(SkColor4f color) { |
| 22 |
// Why not use SkShaders::Color? We want a shader that inhibits any paint optimization by any |
| 23 |
// backend. The CPU backend will ask the shader portion of the pipeline if it's constant. |
| 24 |
// If so, that portion of the pipeline is executed to get the color, and the color filter is |
| 25 |
// directly applied to the result. The only way to have the color filter run as part of the |
| 26 |
// full CPU pipeline is to have a shader that returns false for isConstant: |
| 27 |
SkBitmap bmp; |
| 28 |
bmp.allocPixels(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 29 |
bmp.eraseColor(color); |
| 30 |
return bmp.makeShader(SkFilterMode::kNearest); |
| 31 |
} |
| 32 |
|
| 33 |
static sk_sp<SkShader> paint_color_shader() { |
| 34 |
// This will return the paint color (unless it's a child of a runtime effect) |
| 35 |
SkBitmap bmp; |
| 36 |
bmp.allocPixels(SkImageInfo::MakeA8(1, 1)); |
| 37 |
bmp.eraseColor(SkColors::kWhite); |
| 38 |
return bmp.makeShader(SkFilterMode::kNearest); |
| 39 |
} |
| 40 |
|
| 41 |
static sk_sp<SkShader> raw_shader(SkColor4f color) { |
| 42 |
return SkRuntimeEffect::MakeForShader(SkString("uniform half4 c;" |
| 43 |
"half4 main(float2 xy) { return c; }")) |
| 44 |
.effect->makeShader(SkData::MakeWithCopy(&color, sizeof(SkColor4f)), {}); |
| 45 |
} |
| 46 |
|
| 47 |
static sk_sp<SkShader> managed_shader(SkColor4f color) { |
| 48 |
return SkRuntimeEffect::MakeForShader(SkString("layout(color) uniform half4 c;" |
| 49 |
"half4 main(float2 xy) {" |
| 50 |
"return half4(c.rgb*c.a, c.a);" |
| 51 |
"}")) |
| 52 |
.effect->makeShader(SkData::MakeWithCopy(&color, sizeof(SkColor4f)), {}); |
| 53 |
} |
| 54 |
|
| 55 |
static sk_sp<SkShader> gradient_shader() { |
| 56 |
const SkPoint pts[] = {{0, 0}, {40, 40}}; |
| 57 |
const SkColor4f colors[] = {SkColors::kRed, SkColors::kGreen}; |
| 58 |
return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}}); |
| 59 |
} |
| 60 |
|
| 61 |
static sk_sp<SkColorFilter> raw_cf(SkColor4f color) { |
| 62 |
return SkRuntimeEffect::MakeForColorFilter(SkString("uniform half4 c;" |
| 63 |
"half4 main(half4 color) { return c; }")) |
| 64 |
.effect->makeColorFilter(SkData::MakeWithCopy(&color, sizeof(SkColor4f))); |
| 65 |
} |
| 66 |
|
| 67 |
static sk_sp<SkColorFilter> managed_cf(SkColor4f color) { |
| 68 |
return SkRuntimeEffect::MakeForColorFilter(SkString("layout(color) uniform half4 c;" |
| 69 |
"half4 main(half4 color) { return c; }")) |
| 70 |
.effect->makeColorFilter(SkData::MakeWithCopy(&color, sizeof(SkColor4f))); |
| 71 |
} |
| 72 |
|
| 73 |
static sk_sp<SkColorFilter> indirect_cf(SkColor4f color) { |
| 74 |
SkRuntimeEffect::ChildPtr children[] = {color_shader(color)}; |
| 75 |
return SkRuntimeEffect::MakeForColorFilter( |
| 76 |
SkString("uniform shader s;" |
| 77 |
"half4 main(half4 color) { return s.eval(float2(0)); }")) |
| 78 |
.effect->makeColorFilter(nullptr, children); |
| 79 |
} |
| 80 |
|
| 81 |
static sk_sp<SkColorFilter> mode_cf(SkColor4f color) { |
| 82 |
return SkColorFilters::Blend(color, nullptr, SkBlendMode::kSrc); |
| 83 |
} |
| 84 |
|
| 85 |
static sk_sp<SkColorFilter> spin(sk_sp<SkColorFilter> cf) { |
| 86 |
return cf->makeWithWorkingColorSpace(SkColorSpace::MakeSRGB()->makeColorSpin()); |
| 87 |
} |
| 88 |
|
| 89 |
static sk_sp<SkShader> spin(sk_sp<SkShader> shader) { |
| 90 |
return shader->makeWithWorkingColorSpace(SkColorSpace::MakeSRGB()->makeColorSpin()); |
| 91 |
} |
| 92 |
|
| 93 |
static sk_sp<SkShader> linear(sk_sp<SkShader> shader) { |
| 94 |
return shader->makeWithWorkingColorSpace(SkColorSpace::MakeSRGBLinear()); |
| 95 |
} |
| 96 |
|
| 97 |
DEF_SIMPLE_GM_CAN_FAIL(workingspace, canvas, errorMsg, 200, 350) { |
| 98 |
if (!canvas->getSurface()) { |
| 99 |
// The only backend that really fails is DDL (because the color filter fails to evaluate on |
| 100 |
// the CPU when we do paint optimization). We can't identify DDL separate from other |
| 101 |
// recording backends, so skip this GM for all of them: |
| 102 |
*errorMsg = "Not supported in recording/DDL mode"; |
| 103 |
return skiagm::DrawResult::kSkip; |
| 104 |
} |
| 105 |
|
| 106 |
// This GM checks that changing the working color space of a color filter does the right thing. |
| 107 |
// We create a variety of color filters that are sensitive to the working space, and test them |
| 108 |
// with both fixed-input color (so that paint optimization can apply the filter on the CPU), |
| 109 |
// and with a shader input (so they're forced to evaluate in the drawing pipeline). |
| 110 |
// |
| 111 |
// In all cases, the tests are designed to draw green if implemented correctly. Any other color |
| 112 |
// (red or blue, most likely) is an error. |
| 113 |
// |
| 114 |
// The bottom row is the exception - it draws red-to-green gradients. The first two should be |
| 115 |
// "ugly" (via brown). The last one should be "nice" (via yellow). |
| 116 |
|
| 117 |
canvas->translate(5, 5); |
| 118 |
canvas->save(); |
| 119 |
|
| 120 |
auto cell = [&](sk_sp<SkShader> shader, |
| 121 |
sk_sp<SkColorFilter> colorFilter, |
| 122 |
SkColor4f paintColor = SkColors::kBlack) { |
| 123 |
SkPaint paint; |
| 124 |
paint.setColor(paintColor); |
| 125 |
paint.setShader(shader); |
| 126 |
paint.setColorFilter(colorFilter); |
| 127 |
canvas->drawRect({0, 0, 40, 40}, paint); |
| 128 |
canvas->translate(50, 0); |
| 129 |
}; |
| 130 |
|
| 131 |
auto nextRow = [&]() { |
| 132 |
canvas->restore(); |
| 133 |
canvas->translate(0, 50); |
| 134 |
canvas->save(); |
| 135 |
}; |
| 136 |
|
| 137 |
auto blackShader = color_shader(SkColors::kBlack); |
| 138 |
|
| 139 |
cell(nullptr, raw_cf(SkColors::kGreen)); |
| 140 |
cell(nullptr, managed_cf(SkColors::kGreen)); |
| 141 |
cell(nullptr, indirect_cf(SkColors::kGreen)); |
| 142 |
cell(nullptr, mode_cf(SkColors::kGreen)); |
| 143 |
|
| 144 |
nextRow(); |
| 145 |
|
| 146 |
cell(blackShader, raw_cf(SkColors::kGreen)); |
| 147 |
cell(blackShader, managed_cf(SkColors::kGreen)); |
| 148 |
cell(blackShader, indirect_cf(SkColors::kGreen)); |
| 149 |
cell(blackShader, mode_cf(SkColors::kGreen)); |
| 150 |
|
| 151 |
nextRow(); |
| 152 |
|
| 153 |
cell(nullptr, spin(raw_cf(SkColors::kRed))); // Un-managed red turns into green |
| 154 |
cell(nullptr, spin(managed_cf(SkColors::kGreen))); |
| 155 |
cell(nullptr, spin(indirect_cf(SkColors::kGreen))); |
| 156 |
cell(nullptr, spin(mode_cf(SkColors::kGreen))); |
| 157 |
|
| 158 |
nextRow(); |
| 159 |
|
| 160 |
cell(blackShader, spin(raw_cf(SkColors::kRed))); // Un-managed red turns into green |
| 161 |
cell(blackShader, spin(managed_cf(SkColors::kGreen))); |
| 162 |
cell(blackShader, spin(indirect_cf(SkColors::kGreen))); |
| 163 |
cell(blackShader, spin(mode_cf(SkColors::kGreen))); |
| 164 |
|
| 165 |
nextRow(); |
| 166 |
|
| 167 |
cell(raw_shader(SkColors::kGreen), nullptr); |
| 168 |
cell(managed_shader(SkColors::kGreen), nullptr); |
| 169 |
cell(color_shader(SkColors::kGreen), nullptr); |
| 170 |
cell(paint_color_shader(), nullptr, SkColors::kGreen); |
| 171 |
|
| 172 |
nextRow(); |
| 173 |
|
| 174 |
cell(spin(raw_shader(SkColors::kRed)), nullptr); // Un-managed red turns into green |
| 175 |
cell(spin(managed_shader(SkColors::kGreen)), nullptr); |
| 176 |
cell(spin(color_shader(SkColors::kGreen)), nullptr); |
| 177 |
cell(spin(paint_color_shader()), nullptr, SkColors::kGreen); |
| 178 |
|
| 179 |
nextRow(); |
| 180 |
|
| 181 |
cell(gradient_shader(), nullptr); // Red to green, via ugly brown |
| 182 |
cell(spin(gradient_shader()), nullptr); // Same (spin doesn't change anything) |
| 183 |
cell(linear(gradient_shader()), nullptr); // Red to green, via bright yellow |
| 184 |
|
| 185 |
return skiagm::DrawResult::kOk; |
| 186 |
} |
| 187 |
|
| 188 |
// When color conversion and alpha type is handled correctly from all input types (e.g. image, |
| 189 |
// color, and runtime effect), this should produce a 2x3 grid of squares in the same green color. |
| 190 |
// |
| 191 |
// * For CPU/Ganesh, the bottom row (all workInUnpremul cases) render incorrectly because all inputs |
| 192 |
// still produce premul values and the wrapped shader is assumed to produce a premul value. |
| 193 |
// * For Graphite, solid color and image inputs correctly convert to unpremul alpha but other shader |
| 194 |
// types produce premul values w/o any extra conversion to unpremul. Only the bottom-middle cell |
| 195 |
// renders incorrectly. |
| 196 |
DEF_SIMPLE_GM(workingspace_input_output, canvas, 256, 256) { |
| 197 |
// unpremul, sRGB input color to the runtime shader that is then wrapped in a workingspace |
| 198 |
SkColor4f inputColor = {0.2f, 0.4f, 0.7f, 0.5f}; |
| 199 |
|
| 200 |
// These should produce the same value, barring colortype encoding precision |
| 201 |
sk_sp<SkShader> childColor = SkShaders::Color(inputColor, nullptr); |
| 202 |
sk_sp<SkShader> childUniform = managed_shader(inputColor); |
| 203 |
sk_sp<SkShader> childImage = color_shader(inputColor); |
| 204 |
|
| 205 |
// The input working space will be the linear space with the current surface's gamut |
| 206 |
sk_sp<SkColorSpace> inputCS = canvas->imageInfo().refColorSpace(); |
| 207 |
if (inputCS) { |
| 208 |
inputCS = inputCS->makeLinearGamma(); |
| 209 |
} else { |
| 210 |
inputCS = SkColorSpace::MakeSRGBLinear(); |
| 211 |
} |
| 212 |
|
| 213 |
// The output space will be a colorspin of the input gamut with a 2.2 gamma |
| 214 |
sk_sp<SkColorSpace> outputCS = inputCS->makeColorSpin(); |
| 215 |
skcms_Matrix3x3 outputGamut; |
| 216 |
outputCS->toXYZD50(&outputGamut); |
| 217 |
outputCS = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, outputGamut); |
| 218 |
|
| 219 |
SkRect rect = {0.f, 0.f, 32.f, 32.f}; |
| 220 |
const float padding = 4.f; |
| 221 |
|
| 222 |
canvas->translate(padding, padding); |
| 223 |
for (bool workInUnpremul : {false, true}) { |
| 224 |
const char* manualUnpremul = workInUnpremul ? "false" : "true"; |
| 225 |
SkString sksl = SkStringPrintf( |
| 226 |
"uniform shader child;" |
| 227 |
"half4 main(float2 xy) {" |
| 228 |
"half4 inRGBA = child.eval(xy);" // This is in inputCS w/ inputAlpha type |
| 229 |
"if (%s) {" // do manual unpremul if not `workInUnpremul` |
| 230 |
"inRGBA.rgb /= inRGBA.a + 0.00001;" |
| 231 |
"}" |
| 232 |
// the "effect" to apply in the linear unpremul working space |
| 233 |
"half4 scaled = half4(saturate(inRGBA.rgb * 1.5), inRGBA.a);" |
| 234 |
// manual color spin and 2.2 gamma to inline the conversion from |
| 235 |
// inputCS to outputCS. |
| 236 |
"half4 outRGBA = half4(pow(scaled.bgr, half3(2.2)), scaled.a);" |
| 237 |
"if (%s) {" // do manual premul if not `workInUnpremul` |
| 238 |
"outRGBA.rgb *= outRGBA.a;" |
| 239 |
"}" |
| 240 |
"return outRGBA;" |
| 241 |
"}", |
| 242 |
manualUnpremul, manualUnpremul); |
| 243 |
|
| 244 |
auto rte = SkRuntimeEffect::MakeForShader(sksl); |
| 245 |
SkASSERTF(rte.effect, "Failed to create runtime effect: %s", rte.errorText.c_str()); |
| 246 |
|
| 247 |
canvas->save(); |
| 248 |
for (auto&& child : {childColor, childUniform, childImage}) { |
| 249 |
SkRuntimeShaderBuilder builder{rte.effect}; |
| 250 |
builder.child("child") = child; |
| 251 |
sk_sp<SkShader> rteShader = builder.makeShader(); |
| 252 |
|
| 253 |
SkPaint p; |
| 254 |
p.setAntiAlias(true); |
| 255 |
// The option that takes a workInUnpremul parameter isn't public yet |
| 256 |
p.setShader(SkWorkingColorSpaceShader::Make(std::move(rteShader), |
| 257 |
inputCS, |
| 258 |
outputCS, |
| 259 |
workInUnpremul)); |
| 260 |
canvas->drawRect(rect, p); |
| 261 |
canvas->translate(rect.width() + padding, 0.f); |
| 262 |
} |
| 263 |
canvas->restore(); |
| 264 |
canvas->translate(0.f, rect.height() + padding); |
| 265 |
} |
| 266 |
} |