Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
imagefiltersclipped.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"
22#include "include/core/SkSize.h"
30#include "tools/DecodeUtils.h"
31#include "tools/ToolUtils.h"
32
33#include <utility>
34
35#define RESIZE_FACTOR_X SkIntToScalar(2)
36#define RESIZE_FACTOR_Y SkIntToScalar(5)
37
41 SkScalar radius = std::min(x, y) * 0.8f;
43 SkCanvas* canvas = surface->getCanvas();
44 canvas->clear(0x00000000);
45 SkColor colors[2];
46 colors[0] = SK_ColorWHITE;
47 colors[1] = SK_ColorBLACK;
49 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr,
51 canvas->drawCircle(x, y, radius, paint);
52 return surface->makeImageSnapshot();
53}
54
55static void draw_clipped_filter(SkCanvas* canvas, sk_sp<SkImageFilter> filter, size_t i,
56 const SkRect& primBounds, const SkRect& clipBounds) {
58 paint.setColor(SK_ColorWHITE);
59 paint.setImageFilter(std::move(filter));
60 paint.setAntiAlias(true);
61 canvas->save();
62 canvas->clipRect(clipBounds);
63 if (5 == i) {
64 canvas->translate(SkIntToScalar(16), SkIntToScalar(-32));
65 } else if (6 == i) {
67 }
68 canvas->drawCircle(primBounds.centerX(), primBounds.centerY(),
69 primBounds.width() * 2 / 5, paint);
70 canvas->restore();
71}
72
73namespace skiagm {
74
75class ImageFiltersClippedGM : public GM {
76public:
78 this->setBGColor(0x00000000);
79 }
80
81protected:
82 SkString getName() const override { return SkString("imagefiltersclipped"); }
83
84 SkISize getISize() override { return SkISize::Make(860, 500); }
85
86 void onOnceBeforeDraw() override {
87 fCheckerboard =
88 ToolUtils::create_checkerboard_image(64, 64, 0xFFA0A0A0, 0xFF404040, 8);
89 fGradientCircle = make_gradient_circle(64, 64);
90 }
91
92 void onDraw(SkCanvas* canvas) override {
93 canvas->clear(SK_ColorBLACK);
94
95 sk_sp<SkImageFilter> gradient(SkImageFilters::Image(fGradientCircle,
99 SkMatrix resizeMatrix;
101 SkPoint3 pointLocation = SkPoint3::Make(32, 32, SkIntToScalar(10));
102
104 sk_sp<SkImageFilter> filters[] = {
107 SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, nullptr),
109 SkIntToScalar(12), std::move(gradient), checkerboard),
113 SkImageFilters::MatrixTransform(resizeMatrix, SkSamplingOptions(), nullptr),
114 // Crop output of lighting to the checkerboard
117 };
118
119 SkScalar margin = SkIntToScalar(16);
120 SkRect bounds = r;
121 bounds.outset(margin, margin);
122
123 canvas->save();
124 for (int xOffset = 0; xOffset < 80; xOffset += 16) {
125 canvas->save();
126 bounds.fLeft = SkIntToScalar(xOffset);
127 for (size_t i = 0; i < std::size(filters); ++i) {
128 draw_clipped_filter(canvas, filters[i], i, r, bounds);
129 canvas->translate(r.width() + margin, 0);
130 }
131 canvas->restore();
132 canvas->translate(0, r.height() + margin);
133 }
134 canvas->restore();
135
136 sk_sp<SkImageFilter> rectFilter(
138 canvas->translate(std::size(filters)*(r.width() + margin), 0);
139 for (int xOffset = 0; xOffset < 80; xOffset += 16) {
140 bounds.fLeft = SkIntToScalar(xOffset);
141 draw_clipped_filter(canvas, rectFilter, 0, r, bounds);
142 canvas->translate(0, r.height() + margin);
143 }
144 }
145
146private:
147 sk_sp<SkImage> fCheckerboard, fGradientCircle;
148
149 using INHERITED = GM;
150};
151
152//////////////////////////////////////////////////////////////////////////////
153
154DEF_GM(return new ImageFiltersClippedGM;)
155} // namespace skiagm
156
157DEF_SIMPLE_GM(imagefilter_convolve_subset, canvas, 160, 180) {
158 sk_sp<SkImage> reference = ToolUtils::GetResourceAsImage("images/filter_reference.png");
159 SkRect crop = SkRect::Make(reference->dimensions()).makeInset(10, 10);
160 auto drawFilteredImage = [&](sk_sp<SkImageFilter> filter) {
161 SkPaint p;
162 p.setImageFilter(std::move(filter));
163 canvas->drawImage(reference, 0, 0, {}, &p);
164 canvas->translate(0, reference->height());
165 };
166
167 {
168 float kernel[] = {1.f, 1.f, 1.f,
169 1.f, -7.f, 1.f,
170 1.f, 1.f, 1.f};
171 drawFilteredImage(SkImageFilters::MatrixConvolution(
172 {3, 3}, kernel, 1.f, 0.3f, {1, 1}, SkTileMode::kClamp, true, nullptr, crop));
173 }
174
175 drawFilteredImage(SkImageFilters::Blur(10.f, 10.f, SkTileMode::kMirror, nullptr, crop));
176}
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkScalarInvert(x)
Definition SkScalar.h:73
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void clear(SkColor color)
Definition SkCanvas.h:1199
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
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 > MatrixConvolution(const SkISize &kernelSize, const SkScalar kernel[], SkScalar gain, SkScalar bias, const SkIPoint &kernelOffset, SkTileMode tileMode, bool convolveAlpha, 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 > MatrixTransform(const SkMatrix &matrix, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
static sk_sp< SkImageFilter > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Shader(sk_sp< SkShader > shader, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition SkMatrix.cpp:296
void setBGColor(SkColor)
Definition gm.cpp:159
void onDraw(SkCanvas *canvas) override
SkString getName() const override
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
#define RESIZE_FACTOR_Y
static sk_sp< SkImage > make_gradient_circle(int width, int height)
#define RESIZE_FACTOR_X
static void draw_clipped_filter(SkCanvas *canvas, sk_sp< SkImageFilter > filter, size_t i, const SkRect &primBounds, const SkRect &clipBounds)
double y
double x
SK_API sk_sp< SkShader > MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, int numOctaves, SkScalar seed, const SkISize *tileSize=nullptr)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
static sk_sp< SkImage > make_gradient_circle(int width, int height)
int32_t height
int32_t width
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
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)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
SkRect makeInset(float dx, float dy) const
Definition SkRect.h:987
constexpr float centerX() const
Definition SkRect.h:776
constexpr float height() const
Definition SkRect.h:769
constexpr float centerY() const
Definition SkRect.h:785
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)