Flutter Engine
The Flutter Engine
Typedefs | Functions
backdrop_imagefilter_croprect.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkPaint.h"
#include "include/effects/SkImageFilters.h"

Go to the source code of this file.

Typedefs

typedef sk_sp< SkImageFilter >(* FilterFactory) (const SkIRect *crop)
 

Functions

static void draw_backdrop_filter_gm (SkCanvas *canvas, float outsetX, float outsetY, FilterFactory factory)
 
static sk_sp< SkImageFiltermake_invert_filter (const SkIRect *crop)
 
static sk_sp< SkImageFiltermake_blur_filter (const SkIRect *crop)
 
 DEF_SIMPLE_GM (backdrop_imagefilter_croprect, canvas, 600, 500)
 
 DEF_SIMPLE_GM (backdrop_imagefilter_croprect_rotated, canvas, 600, 500)
 
 DEF_SIMPLE_GM (backdrop_imagefilter_croprect_persp, canvas, 600, 500)
 
 DEF_SIMPLE_GM (backdrop_imagefilter_croprect_nested, canvas, 600, 500)
 

Typedef Documentation

◆ FilterFactory

typedef sk_sp< SkImageFilter >(* FilterFactory) (const SkIRect *crop)

Definition at line 14 of file backdrop_imagefilter_croprect.cpp.

Function Documentation

◆ DEF_SIMPLE_GM() [1/4]

DEF_SIMPLE_GM ( backdrop_imagefilter_croprect  ,
canvas  ,
600  ,
500   
)

Definition at line 86 of file backdrop_imagefilter_croprect.cpp.

86 {
88}
static sk_sp< SkImageFilter > make_invert_filter(const SkIRect *crop)
static void draw_backdrop_filter_gm(SkCanvas *canvas, float outsetX, float outsetY, FilterFactory factory)

◆ DEF_SIMPLE_GM() [2/4]

DEF_SIMPLE_GM ( backdrop_imagefilter_croprect_nested  ,
canvas  ,
600  ,
500   
)

Definition at line 114 of file backdrop_imagefilter_croprect.cpp.

114 {
115 SkPaint p;
116 p.setAlphaf(0.5f);
117 // This ensures there is a non-root device on the stack with a non-zero origin.
118 canvas->translate(15.f, 10.f);
119 canvas->clipRect(SkRect::MakeWH(600.f, 500.f));
120
121 canvas->saveLayer(nullptr, &p);
123 canvas->restore();
124}
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ DEF_SIMPLE_GM() [3/4]

DEF_SIMPLE_GM ( backdrop_imagefilter_croprect_persp  ,
canvas  ,
600  ,
500   
)

Definition at line 103 of file backdrop_imagefilter_croprect.cpp.

103 {
104 SkMatrix persp = SkMatrix::I();
105 persp.setPerspY(0.001f);
106 persp.setSkewX(8.f / 25.f);
107 canvas->concat(persp);
108 draw_backdrop_filter_gm(canvas, 32.f, 32.f, make_blur_filter);
109}
static sk_sp< SkImageFilter > make_blur_filter(const SkIRect *crop)
SkMatrix & setSkewX(SkScalar v)
Definition: SkMatrix.h:518
SkMatrix & setPerspY(SkScalar v)
Definition: SkMatrix.h:544
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544

◆ DEF_SIMPLE_GM() [4/4]

DEF_SIMPLE_GM ( backdrop_imagefilter_croprect_rotated  ,
canvas  ,
600  ,
500   
)

Definition at line 93 of file backdrop_imagefilter_croprect.cpp.

93 {
94 canvas->translate(140.f, -180.f);
95 canvas->rotate(30.f);
96 draw_backdrop_filter_gm(canvas, 32.f, 32.f, make_blur_filter);
97}

◆ draw_backdrop_filter_gm()

static void draw_backdrop_filter_gm ( SkCanvas canvas,
float  outsetX,
float  outsetY,
FilterFactory  factory 
)
static

Definition at line 16 of file backdrop_imagefilter_croprect.cpp.

17 {
18 // CTM translates to (150, 150)
19 SkPoint origin = SkPoint::Make(150.f, 150.f);
20 // The save layer specified after the CTM has negative coordinates, but
21 // means that (100, 100) to (500, 250) in device-space will be saved
22 SkRect clip = SkRect::MakeXYWH(-50.f, -50.f, 400.f, 150.f);
23 // The image-filter crop relative to the CTM, which will map to
24 // (200, 160) to (400, 190) in device space, or (100, 600) to (300, 90) in
25 // the layer's image space.
26 SkRect cropInLocal = SkRect::MakeLTRB(50.f, 10.f, 250.f, 40.f);
27
28 SkIRect cropRect = cropInLocal.makeOutset(outsetX, outsetY).roundOut();
29 sk_sp<SkImageFilter> imageFilter = factory(&cropRect);
30
31 SkPaint p;
32 for (int i = 0; i < 2; ++i) {
33 canvas->save();
34 canvas->translate(origin.fX, origin.fY);
35
36 canvas->clipRect(clip);
37
38 if (i == 0) {
39 // Primary save layer mode, so save layer before drawing the content
40 SkPaint imfPaint;
41 imfPaint.setImageFilter(imageFilter);
42 canvas->saveLayer(nullptr, &imfPaint);
43 } // else backdrop mode, so the content is drawn first
44
45 // Fill the clip with one color (cyan for i == 0 (inverse = red), and
46 // magenta for i == 1 (inverse = green))
47 p.setColor(i == 0 ? SK_ColorCYAN : SK_ColorMAGENTA);
48 canvas->drawPaint(p);
49
50 // Then an inner rectangle with a color meant to be inverted by the image filter
51 p.setColor(i == 0 ? SK_ColorRED : SK_ColorGREEN);
52 canvas->drawRect(cropInLocal, p);
53
54 if (i == 1) {
55 // Backdrop mode, so save a layer using the image filter as the backdrop to filter
56 // content on initialization.
57 canvas->saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, imageFilter.get(),
59 }
60
61 // Restore the saved layer (either a main layer that was just drawn into and needs to be
62 // filtered, or an "empty" layer initialized with the previously filtered backdrop)
63 canvas->restore();
64
65 // Move down
66 canvas->restore();
67 origin.fY += 150.f;
68 }
69}
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
@ kInitWithPrevious_SaveLayerFlag
initializes with previous contents
Definition: SkCanvas.h:672
int save()
Definition: SkCanvas.cpp:447
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
T * get() const
Definition: SkRefCnt.h:303
Definition: SkRect.h:32
float fX
x-axis value
Definition: SkPoint_impl.h:164
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
float fY
y-axis value
Definition: SkPoint_impl.h:165
SkRect makeOutset(float dx, float dy) const
Definition: SkRect.h:1002
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
void roundOut(SkIRect *dst) const
Definition: SkRect.h:1241
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646

◆ make_blur_filter()

static sk_sp< SkImageFilter > make_blur_filter ( const SkIRect crop)
static

Definition at line 79 of file backdrop_imagefilter_croprect.cpp.

79 {
80 // Use different sigmas for x and y so rotated CTM is apparent
81 return SkImageFilters::Blur(16.f, 4.f, nullptr, crop);
82}
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})

◆ make_invert_filter()

static sk_sp< SkImageFilter > make_invert_filter ( const SkIRect crop)
static

Definition at line 71 of file backdrop_imagefilter_croprect.cpp.

71 {
72 static const float matrix[20] = {-1.f, 0.f, 0.f, 0.f, 1.f,
73 0.f, -1.f, 0.f, 0.f, 1.f,
74 0.f, 0.f, -1.f, 0.f, 1.f,
75 0.f, 0.f, 0.f, 1.f, 0.f};
77}
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258