Flutter Engine
The Flutter Engine
Functions
runtimeimagefilter.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPixelRef.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkRuntimeEffect.h"
#include "src/base/SkRandom.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include <string_view>

Go to the source code of this file.

Functions

static sk_sp< SkImageFiltermake_filter ()
 
 DEF_SIMPLE_GM_BG (rtif_distort, canvas, 500, 750, SK_ColorBLACK)
 
 DEF_SIMPLE_GM (rtif_unsharp, canvas, 512, 256)
 

Function Documentation

◆ DEF_SIMPLE_GM()

DEF_SIMPLE_GM ( rtif_unsharp  ,
canvas  ,
512  ,
256   
)

Definition at line 81 of file runtimeimagefilter.cpp.

81 {
82 // Similar to "unsharp_rt", which does the entire unsharp filter in a single shader. This uses
83 // the image filter DAG to compute the blurred version, then does the weighted subtraction.
85 uniform shader content;
86 uniform shader blurred;
87 vec4 main(vec2 coord) {
88 vec4 c = content.eval(coord);
89 vec4 b = blurred.eval(coord);
90 return c + (c - b) * 4;
91 }
92 )")).effect;
93 SkRuntimeShaderBuilder builder(std::move(effect));
94
95 auto image = ToolUtils::GetResourceAsImage("images/mandrill_256.png");
96 auto blurredSrc = SkImageFilters::Blur(1, 1, /*input=*/nullptr);
97
98 std::string_view childNames[] = { "content", "blurred" };
99 sk_sp<SkImageFilter> childNodes[] = { nullptr, blurredSrc };
100
101 auto sharpened = SkImageFilters::RuntimeShader(builder, childNames, childNodes, 2);
102
103 canvas->drawImage(image, 0, 0);
104 canvas->translate(256, 0);
105
107 paint.setImageFilter(sharpened);
108 canvas->saveLayer({ 0, 0, 256, 256 }, &paint);
109 canvas->drawImage(image, 0, 0);
110 canvas->restore();
111}
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > RuntimeShader(const SkRuntimeShaderBuilder &builder, std::string_view childShaderName, sk_sp< SkImageFilter > input)
static Result MakeForShader(SkString sksl, const Options &)
const Paint & paint
Definition: color_source.cc:38
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
sk_sp< SkRuntimeEffect > effect

◆ DEF_SIMPLE_GM_BG()

DEF_SIMPLE_GM_BG ( rtif_distort  ,
canvas  ,
500  ,
750  ,
SK_ColorBLACK   
)

Definition at line 44 of file runtimeimagefilter.cpp.

44 {
45 SkRect clip = SkRect::MakeWH(250, 250);
46 SkPaint filterPaint;
47 filterPaint.setImageFilter(make_filter());
48
49 auto draw_layer = [&](SkScalar tx, SkScalar ty, SkMatrix m) {
50 canvas->save();
51 canvas->translate(tx, ty);
52 canvas->clipRect(clip);
53 canvas->concat(m);
54 canvas->saveLayer(nullptr, &filterPaint);
55 const char* str = "The quick brown fox jumped over the lazy dog.";
56 SkRandom rand;
58 for (int i = 0; i < 25; ++i) {
59 int x = rand.nextULessThan(500);
60 int y = rand.nextULessThan(500);
62 paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
63 font.setSize(rand.nextRangeScalar(0, 300));
64 canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
65 }
66 canvas->restore();
67 canvas->restore();
68 };
69
70 draw_layer( 0, 0, SkMatrix::I());
71 draw_layer(250, 0, SkMatrix::Scale(0.5f, 0.5f));
72 draw_layer( 0, 250, SkMatrix::RotateDeg(45, {125, 125}));
73 draw_layer(250, 250, SkMatrix::Scale(0.5f, 0.5f) * SkMatrix::RotateDeg(45, {125, 125}));
74 draw_layer( 0, 500, SkMatrix::Skew(-0.5f, 0));
76 p.setPerspX(0.0015f);
77 p.setPerspY(-0.0015f);
78 draw_layer(250, 500, p);
79}
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define SkIntToScalar(x)
Definition: SkScalar.h:57
Definition: SkFont.h:35
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
static SkMatrix RotateDeg(SkScalar deg)
Definition: SkMatrix.h:104
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
static SkMatrix Skew(SkScalar kx, SkScalar ky)
Definition: SkMatrix.h:124
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
SkScalar nextRangeScalar(SkScalar min, SkScalar max)
Definition: SkRandom.h:106
uint32_t nextULessThan(uint32_t count)
Definition: SkRandom.h:93
uint32_t nextBits(unsigned bitCount)
Definition: SkRandom.h:72
float SkScalar
Definition: extension.cpp:12
double y
double x
SkFont DefaultPortableFont()
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
font
Font Metadata and Metrics.
static sk_sp< SkImageFilter > make_filter()
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ make_filter()

static sk_sp< SkImageFilter > make_filter ( )
static

Definition at line 29 of file runtimeimagefilter.cpp.

29 {
31 uniform shader child;
32 half4 main(float2 coord) {
33 coord.x += sin(coord.y / 3) * 4;
34 return child.eval(coord);
35 }
36 )")).effect;
37 SkRuntimeShaderBuilder builder(std::move(effect));
39 /*sampleRadius=*/4,
40 /*childShaderName=*/"",
41 /*input=*/nullptr);
42}