Flutter Engine
The Flutter Engine
SkSL_CoordinateSpaces.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_CoordinateSpaces, 128, 128, false, 5) {
5void draw(SkCanvas* canvas) {
6 const char* sksl =
7 "uniform shader image;"
8 "half4 main(float2 coord) {"
9 " coord.x += sin(coord.y / 3) * 4;" // Displace each row by up to 4 pixels
10 " return image.eval(coord);"
11 "}";
12
13 // Draw the SkSL shader, with an image shader bound to `image`: // SK_FOLD_START
14
15 // Turn `image` into an SkShader:
17
18 // Parse the SkSL, and create an SkRuntimeEffect object:
19 auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl));
20
21 // SkRuntimeEffect::makeShader expects an SkSpan<ChildPtr>, one per `uniform shader`:
22 SkRuntimeEffect::ChildPtr children[] = { imageShader };
23
24 // Create an SkShader from our SkSL, with `imageShader` bound to `image`:
25 sk_sp<SkShader> myShader = effect->makeShader(/*uniforms=*/ nullptr,
26 /*children=*/ { children, 1 });
27
28 // Fill the surface with `myShader`:
29 SkPaint p;
30 p.setShader(myShader);
31 canvas->drawPaint(p);
32 // SK_FOLD_END
33}} // END FIDDLE
REG_FIDDLE(SkSL_CoordinateSpaces, 128, 128, false, 5)
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
static Result MakeForShader(SkString sksl, const Options &)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SkSamplingOptions(SkFilterMode::kLinear))