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_EvaluatingImageShader, 128, 128, false, 5) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6 // Turn `image` into an SkShader:
7 sk_sp<SkShader> imageShader = image->makeShader(SkSamplingOptions(SkFilterMode::kLinear));
8
9 const char* sksl =
10 "uniform shader image;"
11 "half4 main(float2 coord) {"
12 " return image.eval(coord).bgra;" // Sample 'image', then swap red and blue
13 "}";
14
15 // Parse the SkSL, and create an SkRuntimeEffect object:
16 auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl));
17
18 // SkRuntimeEffect::makeShader expects an SkSpan<ChildPtr>, one per `uniform shader`:
19 SkRuntimeEffect::ChildPtr children[] = { imageShader };
20
21 // Create an SkShader from our SkSL, with `imageShader` bound to `image`:
22 sk_sp<SkShader> myShader = effect->makeShader(/*uniforms=*/ nullptr,
23 /*children=*/ { children, 1 });
24
25 // Fill the surface with `myShader`:
26 SkPaint p;
27 p.setShader(myShader);
28 canvas->drawPaint(p);
29 }
30 } // END FIDDLE
31