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