Flutter Engine
The Flutter Engine
SkSL_EvaluatingImageShader.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_EvaluatingImageShader, 128, 128, false, 5) {
5void draw(SkCanvas* canvas) {
6 // Turn `image` into an SkShader:
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}} // END FIDDLE
REG_FIDDLE(SkSL_EvaluatingImageShader, 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))