Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
pictureimagefilter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm/gm.h"
12#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
25#include "tools/ToolUtils.h"
27
28// This GM exercises the SkPictureImageFilter ImageFilter class.
29
30static void fill_rect_filtered(SkCanvas* canvas,
31 const SkRect& clipRect,
32 sk_sp<SkImageFilter> filter) {
34 paint.setImageFilter(filter);
35 canvas->save();
36 canvas->clipRect(clipRect);
37 canvas->drawPaint(paint);
38 canvas->restore();
39}
40
42 SkPictureRecorder recorder;
43 SkCanvas* canvas = recorder.beginRecording(100, 100);
45 paint.setColor(0xFFFFFFFF);
47 canvas->drawString("e", 20.0f, 70.0f, font, paint);
48 return recorder.finishRecordingAsPicture();
49}
50
51// Create a picture that will draw LCD text
53 SkPictureRecorder recorder;
54 SkCanvas* canvas = recorder.beginRecording(100, 100);
57 paint.setColor(0xFFFFFFFF);
58 // this has to be small enough that it doesn't become a path
61 canvas->drawString("e", 20.0f, 70.0f, font, paint);
62 return recorder.finishRecordingAsPicture();
63}
64
66public:
68
69protected:
70 SkString getName() const override { return SkString("pictureimagefilter"); }
71
72 SkISize getISize() override { return SkISize::Make(600, 300); }
73
74 void onOnceBeforeDraw() override {
75 fPicture = make_picture();
76 fLCDPicture = make_LCD_picture();
77 }
78
82 pic, dim, nullptr, nullptr, SkImages::BitDepth::kU8, SkColorSpace::MakeSRGB());
83 return SkImageFilters::Image(img, r, r, sampling);
84 }
86 return make(fPicture, fPicture->cullRect(), sampling);
87 }
88
89 void onDraw(SkCanvas* canvas) override {
90 canvas->clear(SK_ColorGRAY);
91 {
92 SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
93 SkRect emptyRect = SkRect::MakeXYWH(20, 20, 0, 0);
94 SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100);
95 sk_sp<SkImageFilter> pictureSource(SkImageFilters::Picture(fPicture));
96 sk_sp<SkImageFilter> pictureSourceSrcRect(SkImageFilters::Picture(fPicture, srcRect));
97 sk_sp<SkImageFilter> pictureSourceEmptyRect(SkImageFilters::Picture(fPicture,
98 emptyRect));
100 sk_sp<SkImageFilter> pictureSourcePixelated = make(SkSamplingOptions());
101
102 canvas->save();
103 // Draw the picture unscaled.
104 fill_rect_filtered(canvas, bounds, pictureSource);
105 canvas->translate(SkIntToScalar(100), 0);
106
107 // Draw an unscaled subset of the source picture.
108 fill_rect_filtered(canvas, bounds, pictureSourceSrcRect);
109 canvas->translate(SkIntToScalar(100), 0);
110
111 // Draw the picture to an empty rect (should draw nothing).
112 fill_rect_filtered(canvas, bounds, pictureSourceEmptyRect);
113 canvas->translate(SkIntToScalar(100), 0);
114
115 // Draw the LCD picture to a layer
116 {
117 SkPaint stroke;
119
120 canvas->drawRect(bounds, stroke);
121
123 paint.setImageFilter(make(fLCDPicture, fPicture->cullRect(), SkSamplingOptions()));
124
125 canvas->scale(4, 4);
126 canvas->translate(-0.9f*srcRect.fLeft, -2.45f*srcRect.fTop);
127
128 canvas->saveLayer(&bounds, &paint);
129 canvas->restore();
130 }
131
132 canvas->restore();
133
134 // Draw the picture scaled
135 canvas->translate(0, SkIntToScalar(100));
136 canvas->scale(200 / srcRect.width(), 200 / srcRect.height());
137 canvas->translate(-srcRect.fLeft, -srcRect.fTop);
138 fill_rect_filtered(canvas, srcRect, pictureSource);
139
140 // Draw the picture scaled, but rasterized at original resolution
141 canvas->translate(srcRect.width(), 0);
142 fill_rect_filtered(canvas, srcRect, pictureSourceResampled);
143
144 // Draw the picture scaled, pixelated
145 canvas->translate(srcRect.width(), 0);
146 fill_rect_filtered(canvas, srcRect, pictureSourcePixelated);
147 }
148 }
149
150private:
151 sk_sp<SkPicture> fPicture;
152 sk_sp<SkPicture> fLCDPicture;
153
154 using INHERITED = GM;
155};
156
157///////////////////////////////////////////////////////////////////////////////
158
159DEF_GM( return new PictureImageFilterGM; )
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SkIntToScalar(x)
Definition SkScalar.h:57
void onOnceBeforeDraw() override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
sk_sp< SkImageFilter > make(sk_sp< SkPicture > pic, SkRect r, const SkSamplingOptions &sampling)
SkString getName() const override
sk_sp< SkImageFilter > make(const SkSamplingOptions &sampling)
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawPaint(const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
static sk_sp< SkColorSpace > MakeSRGB()
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > Picture(sk_sp< SkPicture > pic, const SkRect &targetRect)
void setStyle(Style style)
Definition SkPaint.cpp:105
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
virtual SkRect cullRect() const =0
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
const Paint & paint
#define DEF_GM(CODE)
Definition gm.h:40
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
@ kU8
uses 8-bit unsigned int per color component
sk_sp< SkTypeface > DefaultPortableTypeface()
static void fill_rect_filtered(SkCanvas *canvas, const SkRect &clipRect, sk_sp< SkImageFilter > filter)
static sk_sp< SkPicture > make_picture()
static sk_sp< SkPicture > make_LCD_picture()
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15