Flutter Engine
The Flutter Engine
imagefiltersgraph.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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"
21#include "include/core/SkSize.h"
24#include "tools/ToolUtils.h"
26
27#include <utility>
28
30public:
32
33protected:
34 SkString getName() const override { return SkString("imagefiltersgraph"); }
35
36 SkISize getISize() override { return SkISize::Make(600, 150); }
37
38 void onOnceBeforeDraw() override {
39 fImage = ToolUtils::CreateStringImage(100, 100, SK_ColorWHITE, 20, 70, 96, "e");
40 }
41
42 void onDraw(SkCanvas* canvas) override {
43 canvas->clear(SK_ColorBLACK);
44 {
48 sk_sp<SkImageFilter> blur(SkImageFilters::Blur(4.0f, 4.0f, std::move(bitmapSource)));
51 std::move(erode)));
53
55 paint.setImageFilter(std::move(merge));
56 canvas->drawPaint(paint);
57 canvas->translate(SkIntToScalar(100), 0);
58 }
59 {
60 sk_sp<SkImageFilter> morph(SkImageFilters::Dilate(5, 5, nullptr));
61
62 float matrix[20] = { 1, 0, 0, 0, 0,
63 0, 1, 0, 0, 0,
64 0, 0, 1, 0, 0,
65 0, 0, 0, 0.5f, 0 };
66
68 sk_sp<SkImageFilter> colorMorph(SkImageFilters::ColorFilter(std::move(matrixFilter),
69 std::move(morph)));
72 std::move(colorMorph)));
73
74 DrawClippedImage(canvas, fImage.get(), paint);
75 canvas->translate(SkIntToScalar(100), 0);
76 }
77 {
78 float matrix[20] = { 1, 0, 0, 0, 0,
79 0, 1, 0, 0, 0,
80 0, 0, 1, 0, 0,
81 0, 0, 0, 0.5f, 0 };
83 sk_sp<SkImageFilter> matrixFilter(SkImageFilters::ColorFilter(std::move(matrixCF),
84 nullptr));
85 sk_sp<SkImageFilter> offsetFilter(SkImageFilters::Offset(10.0f, 10.f, matrixFilter));
86
88 paint.setImageFilter(SkImageFilters::Arithmetic(
89 0, 1, 1, 0, true, std::move(matrixFilter), std::move(offsetFilter), nullptr));
90
91 DrawClippedImage(canvas, fImage.get(), paint);
92 canvas->translate(SkIntToScalar(100), 0);
93 }
94 {
95 sk_sp<SkImageFilter> blur(SkImageFilters::Blur(10, 10, nullptr));
96
97 SkIRect cropRect = SkIRect::MakeWH(95, 100);
99 paint.setImageFilter(
100 SkImageFilters::Blend(SkBlendMode::kSrcIn, std::move(blur), nullptr, &cropRect));
101 DrawClippedImage(canvas, fImage.get(), paint);
102 canvas->translate(SkIntToScalar(100), 0);
103 }
104 {
105 // Dilate -> matrix convolution.
106 // This tests that a filter using asFragmentProcessor (matrix
107 // convolution) correctly handles a non-zero source offset
108 // (supplied by the dilate).
109 sk_sp<SkImageFilter> dilate(SkImageFilters::Dilate(5, 5, nullptr));
110
111 SkScalar kernel[9] = { -1, -1, -1,
112 -1, 7, -1,
113 -1, -1, -1 };
114 SkISize kernelSize = SkISize::Make(3, 3);
115 SkScalar gain = 1.0f, bias = 0;
116 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
117 bool convolveAlpha = false;
119 kernelSize, kernel, gain, bias, kernelOffset, SkTileMode::kClamp, convolveAlpha,
120 std::move(dilate)));
121
123 paint.setImageFilter(std::move(convolve));
124 DrawClippedImage(canvas, fImage.get(), paint);
125 canvas->translate(SkIntToScalar(100), 0);
126 }
127 {
128 // Test that crop offsets are absolute, not relative to the parent's crop rect.
131 SkIRect outerRect = SkIRect::MakeXYWH(10, 10, 80, 80);
132 SkIRect innerRect= SkIRect::MakeXYWH(20, 20, 60, 60);
134 std::move(cf1), nullptr, &outerRect));
136 std::move(cf2), std::move(color1), &innerRect));
137
139 paint.setImageFilter(std::move(color2));
140 paint.setColor(SK_ColorRED);
141 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
142 canvas->translate(SkIntToScalar(100), 0);
143 }
144 }
145
146private:
147 static void DrawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint) {
148 canvas->save();
149 canvas->clipIRect(image->bounds());
150 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
151 canvas->restore();
152 }
153
154 sk_sp<SkImage> fImage;
155
156 using INHERITED = GM;
157};
158
159///////////////////////////////////////////////////////////////////////////////
160
161DEF_GM(return new ImageFiltersGraphGM;)
static void merge(const uint8_t *SK_RESTRICT row, int rowN, const SkAlpha *SK_RESTRICT srcAA, const int16_t *SK_RESTRICT srcRuns, SkAlpha *SK_RESTRICT dstAA, int16_t *SK_RESTRICT dstRuns, int width)
Definition: SkAAClip.cpp:1691
@ kSrcOver
r = s + (1-sa)*d
@ kSrcIn
r = s * da
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
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 SkIntToScalar(x)
Definition: SkScalar.h:57
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
void onOnceBeforeDraw() override
SkString getName() const override
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 clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:991
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
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 > ColorFilter(sk_sp< SkColorFilter > cf, 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 > Merge(sk_sp< SkImageFilter > *const filters, int count, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Arithmetic(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4, bool enforcePMColor, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground, 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 > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > Blend(SkBlendMode mode, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground=nullptr, const CropRect &cropRect={})
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={})
SkIRect bounds() const
Definition: SkImage.h:303
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, const char *str)
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr SkIPoint Make(int32_t x, int32_t y)
Definition: SkPoint_impl.h:38
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659