xref: /aosp_15_r20/external/skia/docs/examples/SkSL_CoordinateSpaces.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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_CoordinateSpaces, 128, 128, false, 5) {
draw(SkCanvas * canvas)5 void 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:
16   sk_sp<SkShader> imageShader = image->makeShader(SkSamplingOptions(SkFilterMode::kLinear));
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 }
34 }  // END FIDDLE
35