Flutter Engine
The Flutter Engine
imagefilterscropexpand.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
18#include "include/core/SkRect.h"
26
27#include <utility>
28
29namespace {
30
34
35} // namespace
36
37///////////////////////////////////////////////////////////////////////////////
38
39DEF_SIMPLE_GM(imagefilterscropexpand, canvas, 730, 650) {
40 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
41
42 sk_sp<SkImage> gradientCircle(make_gradient_circle(64, 64));
44
45 sk_sp<SkImageFilter> gradientCircleSource(SkImageFilters::Image(std::move(gradientCircle),
47 sk_sp<SkImageFilter> noopCropped(SkImageFilters::Offset(0, 0, nullptr, &cropRect));
48 // This color matrix saturates the green component but only partly increases the opacity.
49 // For the opaque checkerboard, the opacity boost doesn't matter but it does impact the
50 // area outside the checkerboard.
51 float matrix[20] = { 1, 0, 0, 0, 0,
52 0, 1, 0, 0, 1,
53 0, 0, 1, 0, 0,
54 0, 0, 0, 1, 32.0f/255 };
56
59
60 SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
61 SkScalar kd = SkIntToScalar(2);
62 SkScalar surfaceScale = SkIntToScalar(1);
64 r.roundOut(&bounds);
65
67 canvas->translate(MARGIN, MARGIN);
68 for (int outset = -15; outset <= 20; outset += 5) {
69 canvas->save();
70 SkIRect bigRect = cropRect;
71 bigRect.outset(outset, outset);
72
73 draw(canvas, checkerboard, bigRect,
74 SkImageFilters::ColorFilter(cfAlphaTrans, noopCropped, &bigRect));
75
76 draw(canvas, checkerboard, bigRect,
77 SkImageFilters::Blur(0.3f, 0.3f, noopCropped, &bigRect));
78
79 draw(canvas, checkerboard, bigRect,
80 SkImageFilters::Blur(8.0f, 8.0f, noopCropped, &bigRect));
81
82 draw(canvas, checkerboard, bigRect,
83 SkImageFilters::Dilate(2, 2, noopCropped, &bigRect));
84
85 draw(canvas, checkerboard, bigRect,
86 SkImageFilters::Erode(2, 2, noopCropped, &bigRect));
87
88 draw(canvas, checkerboard, bigRect,
89 SkImageFilters::DropShadow(10, 10, 3, 3, SK_ColorBLUE, noopCropped, &bigRect));
90
91 draw(canvas, checkerboard, bigRect,
93 gradientCircleSource, noopCropped, &bigRect));
94
95 draw(canvas, checkerboard, bigRect,
96 SkImageFilters::Offset(SkIntToScalar(-8), SkIntToScalar(16), noopCropped, &bigRect));
97
98 draw(canvas, checkerboard, bigRect,
99 SkImageFilters::PointLitDiffuse(pointLocation, SK_ColorWHITE, surfaceScale, kd,
100 noopCropped, &bigRect));
101
102 canvas->restore();
103 canvas->translate(0, SkIntToScalar(80));
104 }
105}
106
107namespace {
110 auto canvas = surf->getCanvas();
111 canvas->clear(0xFFFF0000);
112 SkPaint darkPaint;
113 darkPaint.setColor(0xFF404040);
114 SkPaint lightPaint;
115 lightPaint.setColor(0xFFA0A0A0);
116 for (int y = 8; y < 48; y += 16) {
117 for (int x = 8; x < 48; x += 16) {
118 canvas->save();
119 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
120 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
121 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
122 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
123 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
124 canvas->restore();
125 }
126 }
127 return surf->makeImageSnapshot();
128 }
129
133 SkScalar radius = std::min(x, y) * 0.8f;
135 SkCanvas* canvas = surface->getCanvas();
136 canvas->clear(0x00000000);
137 SkColor colors[2];
141 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr,
143 canvas->drawCircle(x, y, radius, paint);
144
145 return surface->makeImageSnapshot();
146 }
147
148 void draw(SkCanvas* canvas, sk_sp<SkImage> image, const SkIRect& layerRect,
149 sk_sp<SkImageFilter> filter) {
151 paint.setImageFilter(std::move(filter));
152
153 // We don't pass 'layerRect' in as the saveLayer bounds hint because that is not the
154 // pre-filtering local bounds (which would be image->dimensions()). We could clip before
155 // the saveLayer but every 'filter' should have been constructed with a crop rect equal
156 // to 'layerRect', so do an unclipped saveLayer to ensure that the crop rect itself is
157 // what restricts the output of the filter.
158 canvas->saveLayer(nullptr, &paint);
159 canvas->drawImage(image, 0, 0);
160 canvas->restore();
161
162 SkPaint strokePaint;
163 strokePaint.setColor(0xFFFF0000);
164 strokePaint.setStyle(SkPaint::kStroke_Style);
165 canvas->drawRect(SkRect::Make(layerRect), strokePaint);
166
167 canvas->translate(SkIntToScalar(80), 0);
168 }
169} // namespace
static sk_sp< SkImage > make_checkerboard(int width, int height)
static const int outset
Definition: BlurTest.cpp:58
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
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 restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > PointLitDiffuse(const SkPoint3 &location, SkColor lightColor, SkScalar surfaceScale, SkScalar kd, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > DropShadow(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > DisplacementMap(SkColorChannel xChannelSelector, SkColorChannel yChannelSelector, SkScalar scale, sk_sp< SkImageFilter > displacement, sk_sp< SkImageFilter > color, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
const Paint & paint
Definition: color_source.cc:38
#define MARGIN
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
static float min(float r, float g, float b)
Definition: hsl.cpp:48
static sk_sp< SkImage > make_gradient_circle(int width, int height)
DEF_SIMPLE_GM(imagefilterscropexpand, canvas, 730, 650)
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
int32_t height
int32_t width
Definition: SkRect.h:32
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
void outset(int32_t dx, int32_t dy)
Definition: SkRect.h:428
static SkImageInfo MakeN32Premul(int width, int height)
static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z)
Definition: SkPoint3.h:18
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
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 MakeWH(float w, float h)
Definition: SkRect.h:609
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)