| 1 |
/* |
| 2 |
* Copyright 2019 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/SkData.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkRRect.h" |
| 14 |
#include "include/core/SkSize.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/core/SkSurface.h" |
| 17 |
#include "include/effects/SkGradient.h" |
| 18 |
#include "include/effects/SkImageFilters.h" |
| 19 |
#include "include/effects/SkRuntimeEffect.h" |
| 20 |
#include "src/base/SkRandom.h" |
| 21 |
#include "src/core/SkColorSpacePriv.h" |
| 22 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 23 |
#include "tools/DecodeUtils.h" |
| 24 |
#include "tools/Resources.h" |
| 25 |
#include "tools/ToolUtils.h" |
| 26 |
|
| 27 |
enum RT_Flags { |
| 28 |
kAnimate_RTFlag = 0x1, |
| 29 |
kBench_RTFlag = 0x2, |
| 30 |
kColorFilter_RTFlag = 0x4, |
| 31 |
}; |
| 32 |
|
| 33 |
class RuntimeShaderGM : public skiagm::GM { |
| 34 |
public: |
| 35 |
RuntimeShaderGM(const char* name, SkISize size, const char* sksl, uint32_t flags = 0) |
| 36 |
: fName(name), fSize(size), fFlags(flags), fSkSL(sksl) {} |
| 37 |
|
| 38 |
void onOnceBeforeDraw() override { |
| 39 |
auto [effect, error] = (fFlags & kColorFilter_RTFlag) |
| 40 |
? SkRuntimeEffect::MakeForColorFilter(fSkSL) |
| 41 |
: SkRuntimeEffect::MakeForShader(fSkSL); |
| 42 |
if (!effect) { |
| 43 |
SkDebugf("RuntimeShader error: %s\n", error.c_str()); |
| 44 |
} |
| 45 |
fEffect = std::move(effect); |
| 46 |
} |
| 47 |
|
| 48 |
bool runAsBench() const override { return SkToBool(fFlags & kBench_RTFlag); } |
| 49 |
SkString getName() const override { return fName; } |
| 50 |
SkISize getISize() override { return fSize; } |
| 51 |
|
| 52 |
bool onAnimate(double nanos) override { |
| 53 |
fSecs = nanos / (1000 * 1000 * 1000); |
| 54 |
return SkToBool(fFlags & kAnimate_RTFlag); |
| 55 |
} |
| 56 |
|
| 57 |
protected: |
| 58 |
SkString fName; |
| 59 |
SkISize fSize; |
| 60 |
uint32_t fFlags; |
| 61 |
float fSecs = 0.0f; |
| 62 |
|
| 63 |
SkString fSkSL; |
| 64 |
sk_sp<SkRuntimeEffect> fEffect; |
| 65 |
}; |
| 66 |
|
| 67 |
class SimpleRT : public RuntimeShaderGM { |
| 68 |
public: |
| 69 |
SimpleRT() : RuntimeShaderGM("runtime_shader", {512, 256}, R"( |
| 70 |
uniform half4 gColor; |
| 71 |
|
| 72 |
half4 main(float2 p) { |
| 73 |
return half4(p*(1.0/255), gColor.b, 1); |
| 74 |
} |
| 75 |
)", kBench_RTFlag) {} |
| 76 |
|
| 77 |
void onDraw(SkCanvas* canvas) override { |
| 78 |
SkRuntimeShaderBuilder builder(fEffect); |
| 79 |
|
| 80 |
SkMatrix localM; |
| 81 |
localM.setRotate(90, 128, 128); |
| 82 |
builder.uniform("gColor") = SkColor4f{1, 0, 0, 1}; |
| 83 |
|
| 84 |
SkPaint p; |
| 85 |
p.setShader(builder.makeShader(&localM)); |
| 86 |
canvas->drawRect({0, 0, 256, 256}, p); |
| 87 |
} |
| 88 |
}; |
| 89 |
DEF_GM(return new SimpleRT;) |
| 90 |
|
| 91 |
static sk_sp<SkShader> make_shader(sk_sp<SkImage> img, SkISize size) { |
| 92 |
SkMatrix scale = SkMatrix::Scale(size.width() / (float)img->width(), |
| 93 |
size.height() / (float)img->height()); |
| 94 |
return img->makeShader(SkSamplingOptions(), scale); |
| 95 |
} |
| 96 |
|
| 97 |
static sk_sp<SkShader> make_threshold(SkISize size) { |
| 98 |
auto info = SkImageInfo::Make(size.width(), size.height(), kAlpha_8_SkColorType, |
| 99 |
kPremul_SkAlphaType); |
| 100 |
auto surf = SkSurfaces::Raster(info); |
| 101 |
auto canvas = surf->getCanvas(); |
| 102 |
|
| 103 |
const SkScalar rad = 50; |
| 104 |
const SkColor4f colors[] = {SkColors::kBlack, {0,0,0,0}}; |
| 105 |
SkPaint paint; |
| 106 |
paint.setAntiAlias(true); |
| 107 |
paint.setShader(SkShaders::RadialGradient({0,0}, rad, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 108 |
|
| 109 |
SkPaint layerPaint; |
| 110 |
const SkScalar sigma = 16.0f; |
| 111 |
layerPaint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr)); |
| 112 |
canvas->saveLayer(nullptr, &layerPaint); |
| 113 |
|
| 114 |
SkRandom rand; |
| 115 |
for (int i = 0; i < 25; ++i) { |
| 116 |
SkScalar x = rand.nextF() * size.width(); |
| 117 |
SkScalar y = rand.nextF() * size.height(); |
| 118 |
canvas->save(); |
| 119 |
canvas->translate(x, y); |
| 120 |
canvas->drawCircle(0, 0, rad, paint); |
| 121 |
canvas->restore(); |
| 122 |
} |
| 123 |
|
| 124 |
canvas->restore(); // apply the blur |
| 125 |
|
| 126 |
return surf->makeImageSnapshot()->makeShader(SkSamplingOptions()); |
| 127 |
} |
| 128 |
|
| 129 |
class ThresholdRT : public RuntimeShaderGM { |
| 130 |
public: |
| 131 |
ThresholdRT() : RuntimeShaderGM("threshold_rt", {256, 256}, R"( |
| 132 |
uniform shader before_map; |
| 133 |
uniform shader after_map; |
| 134 |
uniform shader threshold_map; |
| 135 |
|
| 136 |
uniform float cutoff; |
| 137 |
uniform float slope; |
| 138 |
|
| 139 |
float smooth_cutoff(float x) { |
| 140 |
x = x * slope + (0.5 - slope * cutoff); |
| 141 |
return clamp(x, 0, 1); |
| 142 |
} |
| 143 |
|
| 144 |
half4 main(float2 xy) { |
| 145 |
half4 before = before_map.eval(xy); |
| 146 |
half4 after = after_map.eval(xy); |
| 147 |
|
| 148 |
float m = smooth_cutoff(threshold_map.eval(xy).a); |
| 149 |
return mix(before, after, m); |
| 150 |
} |
| 151 |
)", kAnimate_RTFlag | kBench_RTFlag) {} |
| 152 |
|
| 153 |
sk_sp<SkShader> fBefore, fAfter, fThreshold; |
| 154 |
|
| 155 |
void onOnceBeforeDraw() override { |
| 156 |
const SkISize size = {256, 256}; |
| 157 |
fThreshold = make_threshold(size); |
| 158 |
fBefore = make_shader(ToolUtils::GetResourceAsImage("images/mandrill_256.png"), size); |
| 159 |
fAfter = make_shader(ToolUtils::GetResourceAsImage("images/dog.jpg"), size); |
| 160 |
|
| 161 |
this->RuntimeShaderGM::onOnceBeforeDraw(); |
| 162 |
} |
| 163 |
|
| 164 |
void onDraw(SkCanvas* canvas) override { |
| 165 |
SkRuntimeShaderBuilder builder(fEffect); |
| 166 |
|
| 167 |
builder.uniform("cutoff") = sinf(fSecs) * 0.55f + 0.5f; |
| 168 |
builder.uniform("slope") = 10.0f; |
| 169 |
|
| 170 |
builder.child("before_map") = fBefore; |
| 171 |
builder.child("after_map") = fAfter; |
| 172 |
builder.child("threshold_map") = fThreshold; |
| 173 |
|
| 174 |
SkPaint paint; |
| 175 |
paint.setShader(builder.makeShader()); |
| 176 |
canvas->drawRect({0, 0, 256, 256}, paint); |
| 177 |
|
| 178 |
auto draw = [&](SkScalar x, SkScalar y, sk_sp<SkShader> shader) { |
| 179 |
paint.setShader(shader); |
| 180 |
canvas->save(); |
| 181 |
canvas->translate(x, y); |
| 182 |
canvas->drawRect({0, 0, 256, 256}, paint); |
| 183 |
canvas->restore(); |
| 184 |
}; |
| 185 |
draw(256, 0, fThreshold); |
| 186 |
draw( 0, 256, fBefore); |
| 187 |
draw(256, 256, fAfter); |
| 188 |
} |
| 189 |
}; |
| 190 |
DEF_GM(return new ThresholdRT;) |
| 191 |
|
| 192 |
class SpiralRT : public RuntimeShaderGM { |
| 193 |
public: |
| 194 |
SpiralRT() : RuntimeShaderGM("spiral_rt", {512, 512}, R"( |
| 195 |
uniform float rad_scale; |
| 196 |
uniform float2 in_center; |
| 197 |
layout(color) uniform float4 in_colors0; |
| 198 |
layout(color) uniform float4 in_colors1; |
| 199 |
|
| 200 |
half4 main(float2 p) { |
| 201 |
float2 pp = p - in_center; |
| 202 |
float radius = length(pp); |
| 203 |
radius = sqrt(radius); |
| 204 |
float angle = atan(pp.y / pp.x); |
| 205 |
float t = (angle + 3.1415926/2) / (3.1415926); |
| 206 |
t += radius * rad_scale; |
| 207 |
t = fract(t); |
| 208 |
return in_colors0 * (1-t) + in_colors1 * t; |
| 209 |
} |
| 210 |
)", kAnimate_RTFlag | kBench_RTFlag) {} |
| 211 |
|
| 212 |
void onDraw(SkCanvas* canvas) override { |
| 213 |
SkRuntimeShaderBuilder builder(fEffect); |
| 214 |
|
| 215 |
builder.uniform("rad_scale") = std::sin(fSecs * 0.5f + 2.0f) / 5; |
| 216 |
builder.uniform("in_center") = SkV2{256, 256}; |
| 217 |
builder.uniform("in_colors0") = SkColors::kRed; |
| 218 |
builder.uniform("in_colors1") = SkColors::kGreen; |
| 219 |
|
| 220 |
SkPaint paint; |
| 221 |
paint.setShader(builder.makeShader()); |
| 222 |
canvas->drawRect({0, 0, 512, 512}, paint); |
| 223 |
} |
| 224 |
}; |
| 225 |
DEF_GM(return new SpiralRT;) |
| 226 |
|
| 227 |
// Test case for sampling with both unmodified input coordinates, and explicit coordinates. |
| 228 |
// The first version of skbug.com/40042955 suffered a bug where all samples of a child were treated |
| 229 |
// as pass-through if *at least one* used the unmodified coordinates. This was detected & tracked |
| 230 |
// in b/181092919. This GM is similar, and demonstrates the bug before the fix was applied. |
| 231 |
class UnsharpRT : public RuntimeShaderGM { |
| 232 |
public: |
| 233 |
UnsharpRT() : RuntimeShaderGM("unsharp_rt", {512, 256}, R"( |
| 234 |
uniform shader child; |
| 235 |
half4 main(float2 xy) { |
| 236 |
half4 c = child.eval(xy) * 5; |
| 237 |
c -= child.eval(xy + float2( 1, 0)); |
| 238 |
c -= child.eval(xy + float2(-1, 0)); |
| 239 |
c -= child.eval(xy + float2( 0, 1)); |
| 240 |
c -= child.eval(xy + float2( 0, -1)); |
| 241 |
return c; |
| 242 |
} |
| 243 |
)") {} |
| 244 |
|
| 245 |
sk_sp<SkImage> fMandrill; |
| 246 |
|
| 247 |
void onOnceBeforeDraw() override { |
| 248 |
fMandrill = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 249 |
this->RuntimeShaderGM::onOnceBeforeDraw(); |
| 250 |
} |
| 251 |
|
| 252 |
void onDraw(SkCanvas* canvas) override { |
| 253 |
// First we draw the unmodified image |
| 254 |
canvas->drawImage(fMandrill, 0, 0); |
| 255 |
|
| 256 |
// Now draw the image with our unsharp mask applied |
| 257 |
SkRuntimeShaderBuilder builder(fEffect); |
| 258 |
const SkSamplingOptions sampling(SkFilterMode::kNearest); |
| 259 |
builder.child("child") = fMandrill->makeShader(sampling); |
| 260 |
|
| 261 |
SkPaint paint; |
| 262 |
paint.setShader(builder.makeShader()); |
| 263 |
canvas->translate(256, 0); |
| 264 |
canvas->drawRect({ 0, 0, 256, 256 }, paint); |
| 265 |
} |
| 266 |
}; |
| 267 |
DEF_GM(return new UnsharpRT;) |
| 268 |
|
| 269 |
class ColorCubeRT : public RuntimeShaderGM { |
| 270 |
public: |
| 271 |
ColorCubeRT() : RuntimeShaderGM("color_cube_rt", {512, 512}, R"( |
| 272 |
uniform shader child; |
| 273 |
uniform shader color_cube; |
| 274 |
|
| 275 |
uniform float rg_scale; |
| 276 |
uniform float rg_bias; |
| 277 |
uniform float b_scale; |
| 278 |
uniform float inv_size; |
| 279 |
|
| 280 |
half4 main(float2 xy) { |
| 281 |
float4 c = unpremul(child.eval(xy)); |
| 282 |
|
| 283 |
// Map to cube coords: |
| 284 |
float3 cubeCoords = float3(c.rg * rg_scale + rg_bias, c.b * b_scale); |
| 285 |
|
| 286 |
// Compute slice coordinate |
| 287 |
float2 coords1 = float2((floor(cubeCoords.b) + cubeCoords.r) * inv_size, cubeCoords.g); |
| 288 |
float2 coords2 = float2(( ceil(cubeCoords.b) + cubeCoords.r) * inv_size, cubeCoords.g); |
| 289 |
|
| 290 |
// Two bilinear fetches, plus a manual lerp for the third axis: |
| 291 |
half4 color = mix(color_cube.eval(coords1), color_cube.eval(coords2), |
| 292 |
fract(cubeCoords.b)); |
| 293 |
|
| 294 |
// Premul again |
| 295 |
color.rgb *= color.a; |
| 296 |
|
| 297 |
return color; |
| 298 |
} |
| 299 |
)") {} |
| 300 |
|
| 301 |
sk_sp<SkImage> fMandrill, fMandrillSepia, fIdentityCube, fSepiaCube; |
| 302 |
|
| 303 |
void onOnceBeforeDraw() override { |
| 304 |
fMandrill = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 305 |
fMandrillSepia = ToolUtils::GetResourceAsImage("images/mandrill_sepia.png"); |
| 306 |
fIdentityCube = ToolUtils::GetResourceAsImage("images/lut_identity.png"); |
| 307 |
fSepiaCube = ToolUtils::GetResourceAsImage("images/lut_sepia.png"); |
| 308 |
|
| 309 |
this->RuntimeShaderGM::onOnceBeforeDraw(); |
| 310 |
} |
| 311 |
|
| 312 |
void onDraw(SkCanvas* canvas) override { |
| 313 |
SkRuntimeShaderBuilder builder(fEffect); |
| 314 |
|
| 315 |
// First we draw the unmodified image, and a copy that was sepia-toned in Photoshop: |
| 316 |
canvas->drawImage(fMandrill, 0, 0); |
| 317 |
canvas->drawImage(fMandrillSepia, 0, 256); |
| 318 |
|
| 319 |
// LUT dimensions should be (kSize^2, kSize) |
| 320 |
constexpr float kSize = 16.0f; |
| 321 |
|
| 322 |
const SkSamplingOptions sampling(SkFilterMode::kLinear); |
| 323 |
|
| 324 |
builder.uniform("rg_scale") = (kSize - 1) / kSize; |
| 325 |
builder.uniform("rg_bias") = 0.5f / kSize; |
| 326 |
builder.uniform("b_scale") = kSize - 1; |
| 327 |
builder.uniform("inv_size") = 1.0f / kSize; |
| 328 |
|
| 329 |
builder.child("child") = fMandrill->makeShader(sampling); |
| 330 |
|
| 331 |
SkPaint paint; |
| 332 |
|
| 333 |
// TODO: Should we add SkImage::makeNormalizedShader() to handle this automatically? |
| 334 |
SkMatrix normalize = SkMatrix::Scale(1.0f / (kSize * kSize), 1.0f / kSize); |
| 335 |
|
| 336 |
// Now draw the image with an identity color cube - it should look like the original |
| 337 |
builder.child("color_cube") = fIdentityCube->makeShader(sampling, normalize); |
| 338 |
paint.setShader(builder.makeShader()); |
| 339 |
canvas->translate(256, 0); |
| 340 |
canvas->drawRect({ 0, 0, 256, 256 }, paint); |
| 341 |
|
| 342 |
// ... and with a sepia-tone color cube. This should match the sepia-toned image. |
| 343 |
builder.child("color_cube") = fSepiaCube->makeShader(sampling, normalize); |
| 344 |
paint.setShader(builder.makeShader()); |
| 345 |
canvas->translate(0, 256); |
| 346 |
canvas->drawRect({ 0, 0, 256, 256 }, paint); |
| 347 |
} |
| 348 |
}; |
| 349 |
DEF_GM(return new ColorCubeRT;) |
| 350 |
|
| 351 |
// Same as above, but demonstrating how to implement this as a runtime color filter (that samples |
| 352 |
// a shader child for the LUT). |
| 353 |
class ColorCubeColorFilterRT : public RuntimeShaderGM { |
| 354 |
public: |
| 355 |
ColorCubeColorFilterRT() : RuntimeShaderGM("color_cube_cf_rt", {512, 512}, R"( |
| 356 |
uniform shader color_cube; |
| 357 |
|
| 358 |
uniform float rg_scale; |
| 359 |
uniform float rg_bias; |
| 360 |
uniform float b_scale; |
| 361 |
uniform float inv_size; |
| 362 |
|
| 363 |
half4 main(half4 inColor) { |
| 364 |
float4 c = unpremul(inColor); |
| 365 |
|
| 366 |
// Map to cube coords: |
| 367 |
float3 cubeCoords = float3(c.rg * rg_scale + rg_bias, c.b * b_scale); |
| 368 |
|
| 369 |
// Compute slice coordinate |
| 370 |
float2 coords1 = float2((floor(cubeCoords.b) + cubeCoords.r) * inv_size, cubeCoords.g); |
| 371 |
float2 coords2 = float2(( ceil(cubeCoords.b) + cubeCoords.r) * inv_size, cubeCoords.g); |
| 372 |
|
| 373 |
// Two bilinear fetches, plus a manual lerp for the third axis: |
| 374 |
half4 color = mix(color_cube.eval(coords1), color_cube.eval(coords2), |
| 375 |
fract(cubeCoords.b)); |
| 376 |
|
| 377 |
// Premul again |
| 378 |
color.rgb *= color.a; |
| 379 |
|
| 380 |
return color; |
| 381 |
} |
| 382 |
)", kColorFilter_RTFlag) {} |
| 383 |
|
| 384 |
sk_sp<SkImage> fMandrill, fMandrillSepia, fIdentityCube, fSepiaCube; |
| 385 |
|
| 386 |
void onOnceBeforeDraw() override { |
| 387 |
fMandrill = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 388 |
fMandrillSepia = ToolUtils::GetResourceAsImage("images/mandrill_sepia.png"); |
| 389 |
fIdentityCube = ToolUtils::GetResourceAsImage("images/lut_identity.png"); |
| 390 |
fSepiaCube = ToolUtils::GetResourceAsImage("images/lut_sepia.png"); |
| 391 |
|
| 392 |
this->RuntimeShaderGM::onOnceBeforeDraw(); |
| 393 |
} |
| 394 |
|
| 395 |
void onDraw(SkCanvas* canvas) override { |
| 396 |
SkRuntimeColorFilterBuilder builder(fEffect); |
| 397 |
|
| 398 |
// First we draw the unmodified image, and a copy that was sepia-toned in Photoshop: |
| 399 |
canvas->drawImage(fMandrill, 0, 0); |
| 400 |
canvas->drawImage(fMandrillSepia, 0, 256); |
| 401 |
|
| 402 |
// LUT dimensions should be (kSize^2, kSize) |
| 403 |
constexpr float kSize = 16.0f; |
| 404 |
|
| 405 |
const SkSamplingOptions sampling(SkFilterMode::kLinear); |
| 406 |
|
| 407 |
builder.uniform("rg_scale") = (kSize - 1) / kSize; |
| 408 |
builder.uniform("rg_bias") = 0.5f / kSize; |
| 409 |
builder.uniform("b_scale") = kSize - 1; |
| 410 |
builder.uniform("inv_size") = 1.0f / kSize; |
| 411 |
|
| 412 |
SkPaint paint; |
| 413 |
|
| 414 |
// TODO: Should we add SkImage::makeNormalizedShader() to handle this automatically? |
| 415 |
SkMatrix normalize = SkMatrix::Scale(1.0f / (kSize * kSize), 1.0f / kSize); |
| 416 |
|
| 417 |
// Now draw the image with an identity color cube - it should look like the original |
| 418 |
builder.child("color_cube") = fIdentityCube->makeShader(sampling, normalize); |
| 419 |
|
| 420 |
paint.setColorFilter(builder.makeColorFilter()); |
| 421 |
canvas->drawImage(fMandrill, 256, 0, sampling, &paint); |
| 422 |
|
| 423 |
// ... and with a sepia-tone color cube. This should match the sepia-toned image. |
| 424 |
builder.child("color_cube") = fSepiaCube->makeShader(sampling, normalize); |
| 425 |
|
| 426 |
paint.setColorFilter(builder.makeColorFilter()); |
| 427 |
canvas->drawImage(fMandrill, 256, 256, sampling, &paint); |
| 428 |
} |
| 429 |
}; |
| 430 |
DEF_GM(return new ColorCubeColorFilterRT;) |
| 431 |
|
| 432 |
// Emits coverage for a rounded rectangle whose corners are superellipses defined by the boundary: |
| 433 |
// |
| 434 |
// x^n + y^n == 1 |
| 435 |
// |
| 436 |
// Where x and y are normalized, clamped coordinates ranging from 0..1 inside the nearest corner's |
| 437 |
// bounding box. |
| 438 |
// |
| 439 |
// See: https://en.wikipedia.org/wiki/Superellipse |
| 440 |
class ClipSuperRRect : public RuntimeShaderGM { |
| 441 |
public: |
| 442 |
ClipSuperRRect(const char* name, float power) : RuntimeShaderGM(name, {500, 500}, R"( |
| 443 |
uniform float power_minus1; |
| 444 |
uniform float2 stretch_factor; |
| 445 |
uniform float2x2 derivatives; |
| 446 |
half4 main(float2 xy) { |
| 447 |
xy = max(abs(xy) + stretch_factor, 0); |
| 448 |
float2 exp_minus1 = pow(xy, power_minus1.xx); // If power == 3.5: xy * xy * sqrt(xy) |
| 449 |
float f = dot(exp_minus1, xy) - 1; // f = x^n + y^n - 1 |
| 450 |
float2 grad = exp_minus1 * derivatives; |
| 451 |
float fwidth = abs(grad.x) + abs(grad.y) + 1e-12; // 1e-12 to avoid a divide by zero. |
| 452 |
return half4(saturate(.5 - f/fwidth)); // Approx coverage by riding the gradient to f=0. |
| 453 |
} |
| 454 |
)"), fPower(power) {} |
| 455 |
|
| 456 |
void drawSuperRRect(SkCanvas* canvas, const SkRect& superRRect, float radX, float radY, |
| 457 |
SkColor color) { |
| 458 |
SkPaint paint; |
| 459 |
paint.setColor(color); |
| 460 |
|
| 461 |
if (fPower == 2) { |
| 462 |
// Draw a normal round rect for the sake of testing. |
| 463 |
SkRRect rrect = SkRRect::MakeRectXY(superRRect, radX, radY); |
| 464 |
paint.setAntiAlias(true); |
| 465 |
canvas->drawRRect(rrect, paint); |
| 466 |
return; |
| 467 |
} |
| 468 |
|
| 469 |
SkRuntimeShaderBuilder builder(fEffect); |
| 470 |
builder.uniform("power_minus1") = fPower - 1; |
| 471 |
|
| 472 |
// Size the corners such that the "apex" of our "super" rounded corner is in the same |
| 473 |
// location that the apex of a circular rounded corner would be with the given radii. We |
| 474 |
// define the apex as the point on the rounded corner that is 45 degrees between the |
| 475 |
// horizontal and vertical edges. |
| 476 |
float scale = (1 - SK_ScalarRoot2Over2) / (1 - exp2f(-1/fPower)); |
| 477 |
float cornerWidth = radX * scale; |
| 478 |
float cornerHeight = radY * scale; |
| 479 |
cornerWidth = std::min(cornerWidth, superRRect.width() * .5f); |
| 480 |
cornerHeight = std::min(cornerHeight, superRRect.height() * .5f); |
| 481 |
// The stretch factor controls how long the flat edge should be between rounded corners. |
| 482 |
builder.uniform("stretch_factor") = SkV2{1 - superRRect.width()*.5f / cornerWidth, |
| 483 |
1 - superRRect.height()*.5f / cornerHeight}; |
| 484 |
|
| 485 |
// Calculate a 2x2 "derivatives" matrix that the shader will use to find the gradient. |
| 486 |
// |
| 487 |
// f = s^n + t^n - 1 [s,t are "super" rounded corner coords in normalized 0..1 space] |
| 488 |
// |
| 489 |
// gradient = [df/dx df/dy] = [ns^(n-1) nt^(n-1)] * |ds/dx ds/dy| |
| 490 |
// |dt/dx dt/dy| |
| 491 |
// |
| 492 |
// = [s^(n-1) t^(n-1)] * |n 0| * |ds/dx ds/dy| |
| 493 |
// |0 n| |dt/dx dt/dy| |
| 494 |
// |
| 495 |
// = [s^(n-1) t^(n-1)] * |2n/cornerWidth 0| * mat2x2(canvasMatrix)^-1 |
| 496 |
// |0 2n/cornerHeight| |
| 497 |
// |
| 498 |
// = [s^(n-1) t^(n-1)] * "derivatives" |
| 499 |
// |
| 500 |
const SkMatrix& M = canvas->getTotalMatrix(); |
| 501 |
float a=M.getScaleX(), b=M.getSkewX(), c=M.getSkewY(), d=M.getScaleY(); |
| 502 |
float determinant = a*d - b*c; |
| 503 |
float dx = fPower / (cornerWidth * determinant); |
| 504 |
float dy = fPower / (cornerHeight * determinant); |
| 505 |
builder.uniform("derivatives") = SkV4{d*dx, -c*dy, -b*dx, a*dy}; |
| 506 |
|
| 507 |
// This matrix will be inverted by the effect system, giving a matrix that converts local |
| 508 |
// coordinates to (almost) coner coordinates. To get the rest of the way to the nearest |
| 509 |
// corner's space, the shader will have to take the absolute value, add the stretch_factor, |
| 510 |
// then clamp above zero. |
| 511 |
SkMatrix cornerToLocal; |
| 512 |
cornerToLocal.setScaleTranslate(cornerWidth, cornerHeight, superRRect.centerX(), |
| 513 |
superRRect.centerY()); |
| 514 |
canvas->clipShader(builder.makeShader(&cornerToLocal)); |
| 515 |
|
| 516 |
// Bloat the outer edges of the rect we will draw so it contains all the antialiased pixels. |
| 517 |
// Bloat by a full pixel instead of half in case Skia is in a mode that draws this rect with |
| 518 |
// unexpected AA of its own. |
| 519 |
float inverseDet = 1 / fabsf(determinant); |
| 520 |
float bloatX = (fabsf(d) + fabsf(c)) * inverseDet; |
| 521 |
float bloatY = (fabsf(b) + fabsf(a)) * inverseDet; |
| 522 |
canvas->drawRect(superRRect.makeOutset(bloatX, bloatY), paint); |
| 523 |
} |
| 524 |
|
| 525 |
void onDraw(SkCanvas* canvas) override { |
| 526 |
SkRandom rand(2); |
| 527 |
|
| 528 |
canvas->save(); |
| 529 |
canvas->translate(canvas->imageInfo().width() / 2.f, canvas->imageInfo().height() / 2.f); |
| 530 |
|
| 531 |
canvas->save(); |
| 532 |
canvas->rotate(21); |
| 533 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(-5, 25, 175, 100), 50, 30, |
| 534 |
rand.nextU() | 0xff808080); |
| 535 |
canvas->restore(); |
| 536 |
|
| 537 |
canvas->save(); |
| 538 |
canvas->rotate(94); |
| 539 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(95, 75, 125, 100), 30, 30, |
| 540 |
rand.nextU() | 0xff808080); |
| 541 |
canvas->restore(); |
| 542 |
|
| 543 |
canvas->save(); |
| 544 |
canvas->rotate(132); |
| 545 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(0, 75, 150, 100), 40, 30, |
| 546 |
rand.nextU() | 0xff808080); |
| 547 |
canvas->restore(); |
| 548 |
|
| 549 |
canvas->save(); |
| 550 |
canvas->rotate(282); |
| 551 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(15, -20, 100, 100), 20, 20, |
| 552 |
rand.nextU() | 0xff808080); |
| 553 |
canvas->restore(); |
| 554 |
|
| 555 |
canvas->save(); |
| 556 |
canvas->rotate(0); |
| 557 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(140, -50, 90, 110), 25, 25, |
| 558 |
rand.nextU() | 0xff808080); |
| 559 |
canvas->restore(); |
| 560 |
|
| 561 |
canvas->save(); |
| 562 |
canvas->rotate(-35); |
| 563 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(160, -60, 60, 90), 18, 18, |
| 564 |
rand.nextU() | 0xff808080); |
| 565 |
canvas->restore(); |
| 566 |
|
| 567 |
canvas->save(); |
| 568 |
canvas->rotate(65); |
| 569 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(220, -120, 60, 90), 18, 18, |
| 570 |
rand.nextU() | 0xff808080); |
| 571 |
canvas->restore(); |
| 572 |
|
| 573 |
canvas->save(); |
| 574 |
canvas->rotate(265); |
| 575 |
this->drawSuperRRect(canvas, SkRect::MakeXYWH(150, -129, 80, 160), 24, 39, |
| 576 |
rand.nextU() | 0xff808080); |
| 577 |
canvas->restore(); |
| 578 |
|
| 579 |
canvas->restore(); |
| 580 |
} |
| 581 |
|
| 582 |
private: |
| 583 |
const float fPower; |
| 584 |
}; |
| 585 |
DEF_GM(return new ClipSuperRRect("clip_super_rrect_pow2", 2);) |
| 586 |
// DEF_GM(return new ClipSuperRRect("clip_super_rrect_pow3", 3);) |
| 587 |
DEF_GM(return new ClipSuperRRect("clip_super_rrect_pow3.5", 3.5);) |
| 588 |
// DEF_GM(return new ClipSuperRRect("clip_super_rrect_pow4", 4);) |
| 589 |
// DEF_GM(return new ClipSuperRRect("clip_super_rrect_pow4.5", 4.5);) |
| 590 |
// DEF_GM(return new ClipSuperRRect("clip_super_rrect_pow5", 5);) |
| 591 |
|
| 592 |
class LinearGradientRT : public RuntimeShaderGM { |
| 593 |
public: |
| 594 |
LinearGradientRT() : RuntimeShaderGM("linear_gradient_rt", {256 + 10, 128 + 15}, R"( |
| 595 |
layout(color) uniform vec4 in_colors0; |
| 596 |
layout(color) uniform vec4 in_colors1; |
| 597 |
|
| 598 |
vec4 main(vec2 p) { |
| 599 |
float t = p.x / 256; |
| 600 |
if (p.y < 32) { |
| 601 |
return mix(in_colors0, in_colors1, t); |
| 602 |
} else { |
| 603 |
vec3 linColor0 = toLinearSrgb(in_colors0.rgb); |
| 604 |
vec3 linColor1 = toLinearSrgb(in_colors1.rgb); |
| 605 |
vec3 linColor = mix(linColor0, linColor1, t); |
| 606 |
return fromLinearSrgb(linColor).rgb1; |
| 607 |
} |
| 608 |
} |
| 609 |
)") {} |
| 610 |
|
| 611 |
void onDraw(SkCanvas* canvas) override { |
| 612 |
// Colors chosen to use values other than 0 and 1 - so that it's obvious if the conversion |
| 613 |
// intrinsics are doing anything. (Most transfer functions map 0 -> 0 and 1 -> 1). |
| 614 |
SkRuntimeShaderBuilder builder(fEffect); |
| 615 |
builder.uniform("in_colors0") = SkColor4f{0.75f, 0.25f, 0.0f, 1.0f}; |
| 616 |
builder.uniform("in_colors1") = SkColor4f{0.0f, 0.75f, 0.25f, 1.0f}; |
| 617 |
SkPaint paint; |
| 618 |
paint.setShader(builder.makeShader()); |
| 619 |
|
| 620 |
canvas->save(); |
| 621 |
canvas->clear(SK_ColorWHITE); |
| 622 |
canvas->translate(5, 5); |
| 623 |
|
| 624 |
// We draw everything twice. First to a surface with no color management, where the |
| 625 |
// intrinsics should do nothing (eg, the top bar should look the same in the top and bottom |
| 626 |
// halves). Then to an sRGB surface, where they should produce linearly interpolated |
| 627 |
// gradients (the bottom half of the second bar should be brighter than the top half). |
| 628 |
for (auto cs : {static_cast<SkColorSpace*>(nullptr), sk_srgb_singleton()}) { |
| 629 |
SkImageInfo info = SkImageInfo::Make( |
| 630 |
256, 64, kN32_SkColorType, kPremul_SkAlphaType, sk_ref_sp(cs)); |
| 631 |
auto surface = canvas->makeSurface(info); |
| 632 |
if (!surface) { |
| 633 |
surface = SkSurfaces::Raster(info); |
| 634 |
} |
| 635 |
|
| 636 |
surface->getCanvas()->drawRect({0, 0, 256, 64}, paint); |
| 637 |
canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 638 |
canvas->translate(0, 64 + 5); |
| 639 |
} |
| 640 |
|
| 641 |
canvas->restore(); |
| 642 |
} |
| 643 |
}; |
| 644 |
DEF_GM(return new LinearGradientRT;) |
| 645 |
|
| 646 |
DEF_SIMPLE_GM(child_sampling_rt, canvas, 256,256) { |
| 647 |
static constexpr char scale[] = |
| 648 |
"uniform shader child;" |
| 649 |
"half4 main(float2 xy) {" |
| 650 |
" return child.eval(xy*0.1);" |
| 651 |
"}"; |
| 652 |
|
| 653 |
SkPaint p; |
| 654 |
p.setColor(SK_ColorRED); |
| 655 |
p.setAntiAlias(true); |
| 656 |
p.setStyle(SkPaint::kStroke_Style); |
| 657 |
p.setStrokeWidth(1); |
| 658 |
|
| 659 |
auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100)); |
| 660 |
surf->getCanvas()->drawLine(0, 0, 100, 100, p); |
| 661 |
auto shader = surf->makeImageSnapshot()->makeShader(SkSamplingOptions(SkFilterMode::kLinear)); |
| 662 |
|
| 663 |
SkRuntimeShaderBuilder builder(SkRuntimeEffect::MakeForShader(SkString(scale)).effect); |
| 664 |
builder.child("child") = shader; |
| 665 |
p.setShader(builder.makeShader()); |
| 666 |
|
| 667 |
canvas->drawPaint(p); |
| 668 |
} |
| 669 |
|
| 670 |
static sk_sp<SkShader> normal_map_shader() { |
| 671 |
// Produces a hemispherical normal: |
| 672 |
static const char* kSrc = R"( |
| 673 |
half4 main(vec2 p) { |
| 674 |
p = (p / 256) * 2 - 1; |
| 675 |
float p2 = dot(p, p); |
| 676 |
vec3 v = (p2 > 1) ? vec3(0, 0, 1) : vec3(p, sqrt(1 - p2)); |
| 677 |
return (v * 0.5 + 0.5).xyz1; |
| 678 |
} |
| 679 |
)"; |
| 680 |
auto effect = SkRuntimeEffect::MakeForShader(SkString(kSrc)).effect; |
| 681 |
return effect->makeShader(nullptr, {}); |
| 682 |
} |
| 683 |
|
| 684 |
static sk_sp<SkImage> normal_map_image() { |
| 685 |
// Above, baked into an image: |
| 686 |
auto info = SkImageInfo::Make(256, 256, kN32_SkColorType, kPremul_SkAlphaType); |
| 687 |
auto surface = SkSurfaces::Raster(info); |
| 688 |
SkPaint p; |
| 689 |
p.setShader(normal_map_shader()); |
| 690 |
surface->getCanvas()->drawPaint(p); |
| 691 |
return surface->makeImageSnapshot(); |
| 692 |
} |
| 693 |
|
| 694 |
static sk_sp<SkShader> normal_map_image_shader() { |
| 695 |
return normal_map_image()->makeShader(SkFilterMode::kNearest); |
| 696 |
} |
| 697 |
|
| 698 |
static sk_sp<SkShader> normal_map_raw_image_shader() { |
| 699 |
return normal_map_image()->makeRawShader(SkFilterMode::kNearest); |
| 700 |
} |
| 701 |
|
| 702 |
static sk_sp<SkImage> normal_map_unpremul_image() { |
| 703 |
auto image = normal_map_image(); |
| 704 |
SkPixmap pm; |
| 705 |
SkAssertResult(image->peekPixels(&pm)); |
| 706 |
SkBitmap bmp; |
| 707 |
bmp.allocPixels(image->imageInfo().makeAlphaType(kUnpremul_SkAlphaType)); |
| 708 |
// Copy all pixels over, but set alpha to 0 |
| 709 |
for (int y = 0; y < pm.height(); y++) { |
| 710 |
for (int x = 0; x < pm.width(); x++) { |
| 711 |
*bmp.getAddr32(x, y) = *pm.addr32(x, y) & 0x00FFFFFF; |
| 712 |
} |
| 713 |
} |
| 714 |
return bmp.asImage(); |
| 715 |
} |
| 716 |
|
| 717 |
static sk_sp<SkShader> normal_map_unpremul_image_shader() { |
| 718 |
return normal_map_unpremul_image()->makeShader(SkFilterMode::kNearest); |
| 719 |
} |
| 720 |
|
| 721 |
static sk_sp<SkShader> normal_map_raw_unpremul_image_shader() { |
| 722 |
return normal_map_unpremul_image()->makeRawShader(SkFilterMode::kNearest); |
| 723 |
} |
| 724 |
|
| 725 |
static sk_sp<SkShader> lit_shader(sk_sp<SkShader> normals) { |
| 726 |
// Simple N-dot-L against a fixed, directional light: |
| 727 |
static const char* kSrc = R"( |
| 728 |
uniform shader normals; |
| 729 |
half4 main(vec2 p) { |
| 730 |
vec3 n = normalize(normals.eval(p).xyz * 2 - 1); |
| 731 |
vec3 l = normalize(vec3(1, -1, 1)); |
| 732 |
return saturate(dot(n, l)).xxx1; |
| 733 |
} |
| 734 |
)"; |
| 735 |
auto effect = SkRuntimeEffect::MakeForShader(SkString(kSrc)).effect; |
| 736 |
return effect->makeShader(/* uniforms= */ nullptr, &normals, /* childCount= */ 1); |
| 737 |
} |
| 738 |
|
| 739 |
static sk_sp<SkShader> lit_shader_linear(sk_sp<SkShader> normals) { |
| 740 |
// Simple N-dot-L against a fixed, directional light, done in linear space: |
| 741 |
static const char* kSrc = R"( |
| 742 |
uniform shader normals; |
| 743 |
half4 main(vec2 p) { |
| 744 |
vec3 n = normalize(normals.eval(p).xyz * 2 - 1); |
| 745 |
vec3 l = normalize(vec3(1, -1, 1)); |
| 746 |
return fromLinearSrgb(saturate(dot(n, l)).xxx).xxx1; |
| 747 |
} |
| 748 |
)"; |
| 749 |
auto effect = SkRuntimeEffect::MakeForShader(SkString(kSrc)).effect; |
| 750 |
return effect->makeShader(/* uniforms= */ nullptr, &normals, /* childCount= */ 1); |
| 751 |
} |
| 752 |
|
| 753 |
DEF_SIMPLE_GM(paint_alpha_normals_rt, canvas, 512,512) { |
| 754 |
// Various draws, with non-opaque paint alpha. This demonstrates several issues around how |
| 755 |
// paint alpha is applied differently on CPU (globally, after all shaders) and GPU (per shader, |
| 756 |
// inconsistently). See: skbug.com/40043035 |
| 757 |
// |
| 758 |
// When this works, it will be a demo of applying paint alpha to fade out a complex effect. |
| 759 |
auto draw_shader = [=](int x, int y, sk_sp<SkShader> shader) { |
| 760 |
SkPaint p; |
| 761 |
p.setAlpha(164); |
| 762 |
p.setShader(shader); |
| 763 |
|
| 764 |
canvas->save(); |
| 765 |
canvas->translate(x, y); |
| 766 |
canvas->clipRect({0, 0, 256, 256}); |
| 767 |
canvas->drawPaint(p); |
| 768 |
canvas->restore(); |
| 769 |
}; |
| 770 |
|
| 771 |
draw_shader(0, 0, normal_map_shader()); |
| 772 |
draw_shader(0, 256, normal_map_image_shader()); |
| 773 |
|
| 774 |
draw_shader(256, 0, lit_shader(normal_map_shader())); |
| 775 |
draw_shader(256, 256, lit_shader(normal_map_image_shader())); |
| 776 |
} |
| 777 |
|
| 778 |
DEF_SIMPLE_GM(raw_image_shader_normals_rt, canvas, 768, 512) { |
| 779 |
// Demonstrates the utility of SkImage::makeRawShader, for non-color child shaders. |
| 780 |
|
| 781 |
// First, make an offscreen surface, so we can control the destination color space: |
| 782 |
auto surfInfo = SkImageInfo::Make(512, 512, |
| 783 |
kN32_SkColorType, |
| 784 |
kPremul_SkAlphaType, |
| 785 |
SkColorSpace::MakeSRGB()->makeColorSpin()); |
| 786 |
auto surface = canvas->makeSurface(surfInfo); |
| 787 |
if (!surface) { |
| 788 |
surface = SkSurfaces::Raster(surfInfo); |
| 789 |
} |
| 790 |
|
| 791 |
auto draw_shader = [](int x, int y, sk_sp<SkShader> shader, SkCanvas* canvas) { |
| 792 |
SkPaint p; |
| 793 |
p.setShader(shader); |
| 794 |
|
| 795 |
canvas->save(); |
| 796 |
canvas->translate(x, y); |
| 797 |
canvas->clipRect({0, 0, 256, 256}); |
| 798 |
canvas->drawPaint(p); |
| 799 |
canvas->restore(); |
| 800 |
}; |
| 801 |
|
| 802 |
sk_sp<SkShader> colorNormals = normal_map_image_shader(), |
| 803 |
rawNormals = normal_map_raw_image_shader(); |
| 804 |
|
| 805 |
// Draw our normal map as colors (will be color-rotated), and raw (untransformed) |
| 806 |
draw_shader(0, 0, colorNormals, surface->getCanvas()); |
| 807 |
draw_shader(0, 256, rawNormals, surface->getCanvas()); |
| 808 |
|
| 809 |
// Now draw our lighting shader using the normal and raw versions of the normals as children. |
| 810 |
// The top image will have the normals rotated (incorrectly), so the lighting is very dark. |
| 811 |
draw_shader(256, 0, lit_shader(colorNormals), surface->getCanvas()); |
| 812 |
draw_shader(256, 256, lit_shader(rawNormals), surface->getCanvas()); |
| 813 |
|
| 814 |
// Now draw the offscreen surface back to our original canvas. If we do this naively, the image |
| 815 |
// will be un-transformed back to the canvas' color space. That will have the effect of undoing |
| 816 |
// the color spin on the upper-left, and APPLYING a color-spin on the bottom left. To preserve |
| 817 |
// the intent of this GM (and make it draw consistently whether or not the original surface has |
| 818 |
// a color space attached), we reinterpret the offscreen image as being in sRGB: |
| 819 |
canvas->drawImage( |
| 820 |
surface->makeImageSnapshot()->reinterpretColorSpace(SkColorSpace::MakeSRGB()), 0, 0); |
| 821 |
|
| 822 |
// Finally, to demonstrate that raw unpremul image shaders don't premul, draw lighting two more |
| 823 |
// times, with an unpremul normal map (containing ZERO in the alpha channel). THe top will |
| 824 |
// premultiply the normals, resulting in totally dark lighting. The bottom will retain the RGB |
| 825 |
// encoded normals, even with zero alpha: |
| 826 |
draw_shader(512, 0, lit_shader(normal_map_unpremul_image_shader()), canvas); |
| 827 |
draw_shader(512, 256, lit_shader(normal_map_raw_unpremul_image_shader()), canvas); |
| 828 |
} |
| 829 |
|
| 830 |
DEF_SIMPLE_GM(lit_shader_linear_rt, canvas, 512, 256) { |
| 831 |
// First, make an offscreen surface, so we can control the destination color space: |
| 832 |
auto surfInfo = SkImageInfo::Make(512, 256, |
| 833 |
kN32_SkColorType, |
| 834 |
kPremul_SkAlphaType, |
| 835 |
SkColorSpace::MakeSRGB()); |
| 836 |
auto surface = canvas->makeSurface(surfInfo); |
| 837 |
if (!surface) { |
| 838 |
surface = SkSurfaces::Raster(surfInfo); |
| 839 |
} |
| 840 |
|
| 841 |
auto draw_shader = [](int x, int y, sk_sp<SkShader> shader, SkCanvas* canvas) { |
| 842 |
SkPaint p; |
| 843 |
p.setShader(shader); |
| 844 |
|
| 845 |
canvas->save(); |
| 846 |
canvas->translate(x, y); |
| 847 |
canvas->clipRect({0, 0, 256, 256}); |
| 848 |
canvas->drawPaint(p); |
| 849 |
canvas->restore(); |
| 850 |
}; |
| 851 |
|
| 852 |
// We draw two lit spheres - one does math in the working space (so gamma-encoded). The second |
| 853 |
// works in linear space, then converts to sRGB. This produces (more accurate) sharp falloff: |
| 854 |
draw_shader(0, 0, lit_shader(normal_map_shader()), surface->getCanvas()); |
| 855 |
draw_shader(256, 0, lit_shader_linear(normal_map_shader()), surface->getCanvas()); |
| 856 |
|
| 857 |
// Now draw the offscreen surface back to our original canvas: |
| 858 |
canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 859 |
} |
| 860 |
|
| 861 |
// skbug.com/40044685 GPU was double applying the local matrix. |
| 862 |
DEF_SIMPLE_GM(local_matrix_shader_rt, canvas, 256, 256) { |
| 863 |
SkString passthrough(R"( |
| 864 |
uniform shader s; |
| 865 |
half4 main(float2 p) { return s.eval(p); } |
| 866 |
)"); |
| 867 |
auto [rte, error] = SkRuntimeEffect::MakeForShader(passthrough, {}); |
| 868 |
if (!rte) { |
| 869 |
SkDebugf("%s\n", error.c_str()); |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png"); |
| 874 |
auto imgShader = image->makeShader(SkFilterMode::kNearest); |
| 875 |
|
| 876 |
auto r = SkRect::MakeWH(image->width(), image->height()); |
| 877 |
|
| 878 |
auto lm = SkMatrix::RotateDeg(90.f, {image->width()/2.f, image->height()/2.f}); |
| 879 |
|
| 880 |
SkPaint paint; |
| 881 |
|
| 882 |
// image |
| 883 |
paint.setShader(imgShader); |
| 884 |
canvas->drawRect(r, paint); |
| 885 |
|
| 886 |
// passthrough(image) |
| 887 |
canvas->save(); |
| 888 |
canvas->translate(image->width(), 0); |
| 889 |
paint.setShader(rte->makeShader(/* uniforms= */ nullptr, &imgShader, /* childCount= */ 1)); |
| 890 |
canvas->drawRect(r, paint); |
| 891 |
canvas->restore(); |
| 892 |
|
| 893 |
// localmatrix(image) |
| 894 |
canvas->save(); |
| 895 |
canvas->translate(0, image->height()); |
| 896 |
paint.setShader(imgShader->makeWithLocalMatrix(lm)); |
| 897 |
canvas->drawRect(r, paint); |
| 898 |
canvas->restore(); |
| 899 |
|
| 900 |
// localmatrix(passthrough(image)) This was the bug. |
| 901 |
canvas->save(); |
| 902 |
canvas->translate(image->width(), image->height()); |
| 903 |
paint.setShader(rte->makeShader(/* uniforms= */ nullptr, &imgShader, /* childCount= */ 1) |
| 904 |
->makeWithLocalMatrix(lm)); |
| 905 |
canvas->drawRect(r, paint); |
| 906 |
canvas->restore(); |
| 907 |
} |
| 908 |
|
| 909 |
DEF_SIMPLE_GM(null_child_rt, canvas, 150, 100) { |
| 910 |
using ChildPtr = SkRuntimeEffect::ChildPtr; |
| 911 |
|
| 912 |
// Every swatch should evaluate to the same shade of purple. |
| 913 |
// Paint with a shader evaluating a null shader. |
| 914 |
// Point passed to eval() is ignored; transparent black is returned. |
| 915 |
{ |
| 916 |
const SkString kEvalShader{R"( |
| 917 |
uniform shader s; |
| 918 |
half4 main(float2 p) { return s.eval(p) + half4(0.5, 0, 0.5, 1); } |
| 919 |
)"}; |
| 920 |
auto [rtShader, error] = SkRuntimeEffect::MakeForShader(kEvalShader); |
| 921 |
SkASSERT(rtShader); |
| 922 |
|
| 923 |
SkPaint paint; |
| 924 |
ChildPtr children[1] = {ChildPtr{sk_sp<SkShader>{nullptr}}}; |
| 925 |
paint.setShader(rtShader->makeShader(/*uniforms=*/nullptr, children)); |
| 926 |
paint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)); // green (ignored) |
| 927 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 928 |
canvas->translate(50, 0); |
| 929 |
} |
| 930 |
// Paint with a shader evaluating a null color filter. |
| 931 |
// Color passed to eval() is returned; paint color is ignored. |
| 932 |
{ |
| 933 |
const SkString kEvalColorFilter{R"( |
| 934 |
uniform colorFilter cf; |
| 935 |
half4 main(float2 p) { return cf.eval(half4(0.5, 0, 0.5, 1)); } |
| 936 |
)"}; |
| 937 |
auto [rtShader, error] = SkRuntimeEffect::MakeForShader(kEvalColorFilter); |
| 938 |
SkASSERT(rtShader); |
| 939 |
|
| 940 |
SkPaint paint; |
| 941 |
ChildPtr children[1] = {ChildPtr{sk_sp<SkColorFilter>{nullptr}}}; |
| 942 |
paint.setShader(rtShader->makeShader(/*uniforms=*/nullptr, children)); |
| 943 |
paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute) |
| 944 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 945 |
canvas->translate(50, 0); |
| 946 |
} |
| 947 |
// Paint with a shader evaluating a null blender. |
| 948 |
// Colors passed to eval() are blended via src-over; paint color is ignored. |
| 949 |
{ |
| 950 |
const SkString kEvalBlender{R"( |
| 951 |
uniform blender b; |
| 952 |
half4 main(float2 p) { return b.eval(half4(0.5, 0, 0, 0.5), half4(0, 0, 1, 1)); } |
| 953 |
)"}; |
| 954 |
auto [rtShader, error] = SkRuntimeEffect::MakeForShader(kEvalBlender); |
| 955 |
SkASSERT(rtShader); |
| 956 |
|
| 957 |
SkPaint paint; |
| 958 |
ChildPtr children[1] = {ChildPtr{sk_sp<SkBlender>{nullptr}}}; |
| 959 |
paint.setShader(rtShader->makeShader(/*uniforms=*/nullptr, children)); |
| 960 |
paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute) |
| 961 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 962 |
canvas->translate(50, 0); |
| 963 |
} |
| 964 |
|
| 965 |
canvas->translate(-150, 50); |
| 966 |
|
| 967 |
// Paint with a color filter evaluating a null shader. |
| 968 |
// Point passed to eval() is ignored; transparent black is returned. |
| 969 |
{ |
| 970 |
const SkString kEvalShader{R"( |
| 971 |
uniform shader s; |
| 972 |
half4 main(half4 c) { return s.eval(float2(0)) + half4(0.5, 0, 0.5, 1); } |
| 973 |
)"}; |
| 974 |
auto [rtFilter, error] = SkRuntimeEffect::MakeForColorFilter(kEvalShader); |
| 975 |
SkASSERT(rtFilter); |
| 976 |
|
| 977 |
SkPaint paint; |
| 978 |
ChildPtr children[1] = {ChildPtr{sk_sp<SkShader>{nullptr}}}; |
| 979 |
paint.setColorFilter(rtFilter->makeColorFilter(/*uniforms=*/nullptr, children)); |
| 980 |
paint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)); // green (ignored) |
| 981 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 982 |
canvas->translate(50, 0); |
| 983 |
} |
| 984 |
// Paint with a color filter evaluating a null color filter. |
| 985 |
// Color passed to eval() is returned; paint color is ignored. |
| 986 |
{ |
| 987 |
const SkString kEvalColorFilter{R"( |
| 988 |
uniform colorFilter cf; |
| 989 |
half4 main(half4 c) { return cf.eval(half4(0.5, 0, 0.5, 1)); } |
| 990 |
)"}; |
| 991 |
auto [rtFilter, error] = SkRuntimeEffect::MakeForColorFilter(kEvalColorFilter); |
| 992 |
SkASSERT(rtFilter); |
| 993 |
|
| 994 |
SkPaint paint; |
| 995 |
ChildPtr children[1] = {ChildPtr{sk_sp<SkColorFilter>{nullptr}}}; |
| 996 |
paint.setColorFilter(rtFilter->makeColorFilter(/*uniforms=*/nullptr, children)); |
| 997 |
paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute) |
| 998 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 999 |
canvas->translate(50, 0); |
| 1000 |
} |
| 1001 |
// Paint with a color filter evaluating a null blender. |
| 1002 |
// Colors passed to eval() are blended via src-over; paint color is ignored. |
| 1003 |
{ |
| 1004 |
const SkString kEvalBlender{R"( |
| 1005 |
uniform blender b; |
| 1006 |
half4 main(half4 c) { return b.eval(half4(0.5, 0, 0, 0.5), half4(0, 0, 1, 1)); } |
| 1007 |
)"}; |
| 1008 |
auto [rtFilter, error] = SkRuntimeEffect::MakeForColorFilter(kEvalBlender); |
| 1009 |
SkASSERT(rtFilter); |
| 1010 |
|
| 1011 |
SkPaint paint; |
| 1012 |
ChildPtr children[1] = {ChildPtr{sk_sp<SkBlender>{nullptr}}}; |
| 1013 |
paint.setColorFilter(rtFilter->makeColorFilter(/*uniforms=*/nullptr, children)); |
| 1014 |
paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute) |
| 1015 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 1016 |
canvas->translate(50, 0); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
DEF_SIMPLE_GM_CAN_FAIL(deferred_shader_rt, canvas, errorMsg, 150, 50) { |
| 1021 |
// Skip this GM on recording devices. It actually works okay on serialize-8888, but pic-8888 |
| 1022 |
// does not. Ultimately, behavior on CPU is potentially strange (especially with SkRP), because |
| 1023 |
// SkRP will build the shader more than once per draw. |
| 1024 |
if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 1025 |
return skiagm::DrawResult::kSkip; |
| 1026 |
} |
| 1027 |
|
| 1028 |
const SkString kShader{R"( |
| 1029 |
uniform half4 color; |
| 1030 |
half4 main(float2 p) { return color; } |
| 1031 |
)"}; |
| 1032 |
auto [effect, error] = SkRuntimeEffect::MakeForShader(kShader); |
| 1033 |
SkASSERT(effect); |
| 1034 |
|
| 1035 |
SkColor4f color = SkColors::kRed; |
| 1036 |
auto makeUniforms = [color](const SkRuntimeEffectPriv::UniformsCallbackContext&) mutable { |
| 1037 |
auto result = SkData::MakeWithCopy(&color, sizeof(color)); |
| 1038 |
color = {color.fB, color.fR, color.fG, color.fA}; |
| 1039 |
return result; |
| 1040 |
}; |
| 1041 |
|
| 1042 |
auto shader = |
| 1043 |
SkRuntimeEffectPriv::MakeDeferredShader(effect.get(), makeUniforms, /*children=*/{}); |
| 1044 |
SkASSERT(shader); |
| 1045 |
|
| 1046 |
SkPaint paint; |
| 1047 |
paint.setShader(shader); |
| 1048 |
|
| 1049 |
for (int i = 0; i < 3; ++i) { |
| 1050 |
canvas->drawRect({0, 0, 50, 50}, paint); |
| 1051 |
canvas->translate(50, 0); |
| 1052 |
} |
| 1053 |
|
| 1054 |
return skiagm::DrawResult::kOk; |
| 1055 |
} |
| 1056 |
|
| 1057 |
sk_sp<SkShader> paint_color_shader() { |
| 1058 |
SkBitmap bmp; |
| 1059 |
bmp.allocPixels(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType)); |
| 1060 |
bmp.eraseColor(SK_ColorWHITE); |
| 1061 |
return bmp.makeShader(SkFilterMode::kNearest); |
| 1062 |
} |
| 1063 |
|
| 1064 |
DEF_SIMPLE_GM_CAN_FAIL(alpha_image_shader_rt, canvas, errorMsg, 350, 50) { |
| 1065 |
if (!canvas->getSurface()) { |
| 1066 |
// The only backend that really fails is DDL (because the color filter fails to evaluate on |
| 1067 |
// the CPU when we do paint optimization). We can't identify DDL separate from other |
| 1068 |
// recording backends, so skip this GM for all of them: |
| 1069 |
*errorMsg = "Not supported in recording/DDL mode"; |
| 1070 |
return skiagm::DrawResult::kSkip; |
| 1071 |
} |
| 1072 |
|
| 1073 |
// Skia typically applies the paint color (or input color, for more complex GPU-FP trees) |
| 1074 |
// to alpha-only images. This is useful in trivial cases, but surprising and inconsistent in |
| 1075 |
// more complex situations, especially when using SkSL. |
| 1076 |
// |
| 1077 |
// This GM checks that we suppress the paint-color tinting from SkSL, and always get {0,0,0,a}. |
| 1078 |
auto checkerboard = ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4); |
| 1079 |
auto paint_shader = paint_color_shader(); |
| 1080 |
SkRuntimeEffect::ChildPtr children[1] = { paint_shader }; |
| 1081 |
|
| 1082 |
SkPaint paint; |
| 1083 |
paint.setColor({0.5f, 0, 0.5f, 1.0f}); |
| 1084 |
|
| 1085 |
auto rect = [&]() { |
| 1086 |
canvas->drawRect({0, 0, 48, 48}, paint); |
| 1087 |
canvas->translate(50, 0); |
| 1088 |
}; |
| 1089 |
|
| 1090 |
// Two simple cases: just paint color, then the "paint color" shader. |
| 1091 |
// These should both be PURPLE |
| 1092 |
rect(); |
| 1093 |
|
| 1094 |
paint.setShader(paint_shader); |
| 1095 |
rect(); |
| 1096 |
|
| 1097 |
// All remaining cases should be BLACK |
| 1098 |
|
| 1099 |
// Shader that evaluates the "paint color" shader. |
| 1100 |
// For color-filter and blender, we test them with and without an actual SkShader on the paint. |
| 1101 |
// These should all be BLACK |
| 1102 |
paint.setShader( |
| 1103 |
SkRuntimeEffect::MakeForShader(SkString("uniform shader s;" |
| 1104 |
"half4 main(float2 p) { return s.eval(p); }")) |
| 1105 |
.effect->makeShader(/* uniforms= */ nullptr, children)); |
| 1106 |
rect(); |
| 1107 |
|
| 1108 |
// Color-filter that evaluates the "paint color" shader, with and without a shader on the paint |
| 1109 |
paint.setShader(nullptr); |
| 1110 |
paint.setColorFilter(SkRuntimeEffect::MakeForColorFilter( |
| 1111 |
SkString("uniform shader s;" |
| 1112 |
"half4 main(half4 color) { return s.eval(float2(0)); }")) |
| 1113 |
.effect->makeColorFilter(nullptr, children)); |
| 1114 |
rect(); |
| 1115 |
|
| 1116 |
paint.setShader(checkerboard); |
| 1117 |
rect(); |
| 1118 |
|
| 1119 |
// Blender that evaluates the "paint color" shader, with and without a shader on the paint |
| 1120 |
paint.setShader(nullptr); |
| 1121 |
paint.setColorFilter(nullptr); |
| 1122 |
paint.setBlender( |
| 1123 |
SkRuntimeEffect::MakeForBlender( |
| 1124 |
SkString("uniform shader s;" |
| 1125 |
"half4 main(half4 src, half4 dst) { return s.eval(float2(0)); }")) |
| 1126 |
.effect->makeBlender(nullptr, children)); |
| 1127 |
rect(); |
| 1128 |
|
| 1129 |
paint.setShader(checkerboard); |
| 1130 |
rect(); |
| 1131 |
|
| 1132 |
return skiagm::DrawResult::kOk; |
| 1133 |
} |