1 // Copyright 2024 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(SkSL_PremultipliedAlpha, 200, 200, false, 0) {
5 // Interactive version of this demo available at:
6 // https://shaders.skia.org/?id=8a80c2a7bcd9fb6b39460fd6acc1e828e5725d0210b72719ef696b02e1557435
draw(SkCanvas * canvas)7 void draw(SkCanvas* canvas) {
8 const char* sksl =
9
10 "const half3 iColor = half3(0, 0.5, 0.75);"
11 "half4 main(float2 coord) {"
12 " float alpha = 1 - (coord.y / 150);"
13 " if (coord.x < 100) {"
14 " /* Correctly premultiplied version of color */"
15 " return iColor.rgb1 * alpha;"
16 " } else {"
17 " /* Returning an unpremultiplied color (just setting alpha) leads to over-bright colors. */"
18 " return half4(iColor, alpha);"
19 " }"
20 "}";
21
22 auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl));
23 sk_sp<SkShader> myShader = effect->makeShader(/*uniforms=*/ nullptr,
24 /*children=*/ {});
25
26 // Fill canvas with gray, first:
27 canvas->drawColor(SK_ColorGRAY);
28
29 // Blend our test shader on top of that:
30 SkPaint p;
31 p.setShader(myShader);
32 canvas->drawPaint(p);
33 }
34 } // END FIDDLE
35