Flutter Engine
The Flutter Engine
SkSL_PremultipliedAlpha.cpp
Go to the documentation of this file.
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.
4REG_FIDDLE(SkSL_PremultipliedAlpha, 200, 200, false, 0) {
5// Interactive version of this demo available at:
6// https://shaders.skia.org/?id=8a80c2a7bcd9fb6b39460fd6acc1e828e5725d0210b72719ef696b02e1557435
7void 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}} // END FIDDLE
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
REG_FIDDLE(SkSL_PremultipliedAlpha, 200, 200, false, 0)
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
static Result MakeForShader(SkString sksl, const Options &)