Flutter Engine
The Flutter Engine
Classes | Namespaces | Functions
imageblurrepeatmode.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/effects/SkImageFilters.h"
#include "tools/ToolUtils.h"
#include <initializer_list>
#include <utility>

Go to the source code of this file.

Classes

class  skiagm::ImageBlurRepeatModeGM
 

Namespaces

namespace  skiagm
 

Functions

static sk_sp< SkImagemake_image (SkCanvas *canvas, int direction)
 
static void draw_image (SkCanvas *canvas, const sk_sp< SkImage > image, sk_sp< SkImageFilter > filter)
 
 DEF_SIMPLE_GM (imageblurrepeatunclipped, canvas, 256, 128)
 

Function Documentation

◆ DEF_SIMPLE_GM()

DEF_SIMPLE_GM ( imageblurrepeatunclipped  ,
canvas  ,
256  ,
128   
)

Definition at line 138 of file imageblurrepeatmode.cpp.

138 {
139 // To show translucency
141 SK_ColorGRAY, 8);
142 canvas->drawImage(checkerboard, 0, 0);
143
144 // Make an image with one red and one blue band
145 SkBitmap bmp;
146 bmp.allocN32Pixels(100, 20);
148 bmp.eraseArea(SkIRect::MakeXYWH(0, 10, 100, 10), SK_ColorBLUE);
149
150 auto img = bmp.asImage();
151 // The blur filter uses a repeat crop applied to the image bounds to define the tiling geometry,
152 // but the crop IF is created directly since the tilemode factory for ::Blur also adds a kDecal
153 // post-crop that is undesired for this GM.
154 auto filter = SkImageFilters::Blur(0, 10,
155 SkImageFilters::Crop(SkRect::Make(img->bounds()), SkTileMode::kRepeat, nullptr));
157 paint.setImageFilter(std::move(filter));
158
159 // Draw the blurred image once with a clip that shows the repeat is tiled several times.
160 // 3xsigma is used to match the historic, but underspecified behavior for when kRepeat was used
161 // with no crop rect (which must now be provided or kRepeat is ignored).
162 canvas->translate(0, 50);
163 canvas->save();
164 canvas->clipIRect(img->bounds().makeOutset(0, 30));
165 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
166 canvas->restore();
167
168 // Draw the blurred image with a clip positioned such that the draw would be excluded except
169 // that the image filter causes it to intersect with the clip. It should look like the
170 // left image, but clipped to the debug-black rectangle.
171 canvas->translate(110, 0);
172 canvas->save();
173 canvas->clipIRect(SkIRect::MakeXYWH(0, -30, 100, 10));
174 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
175 canvas->restore();
176
177 // Visualize the clip
180 canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
181}
constexpr SkColor SK_ColorLTGRAY
Definition: SkColor.h:118
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void eraseArea(const SkIRect &area, SkColor c) const
Definition: SkBitmap.h:854
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Crop(const SkRect &rect, SkTileMode tileMode, sk_sp< SkImageFilter > input)
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
const Paint & paint
Definition: color_source.cc:38
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:168
SkSamplingOptions(SkFilterMode::kLinear))
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
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
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)

◆ draw_image()

static void draw_image ( SkCanvas canvas,
const sk_sp< SkImage image,
sk_sp< SkImageFilter filter 
)
static

Definition at line 64 of file imageblurrepeatmode.cpp.

64 {
65 SkAutoCanvasRestore acr(canvas, true);
67 paint.setImageFilter(std::move(filter));
68
69 canvas->translate(SkIntToScalar(30), 0);
70 canvas->clipIRect(image->bounds());
71 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
72}
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:991
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
SkIRect bounds() const
Definition: SkImage.h:303
sk_sp< const SkImage > image
Definition: SkRecords.h:269

◆ make_image()

static sk_sp< SkImage > make_image ( SkCanvas canvas,
int  direction 
)
static

Definition at line 28 of file imageblurrepeatmode.cpp.

28 {
30 auto surface = ToolUtils::makeSurface(canvas, info);
31 SkCanvas* c = surface->getCanvas();
33 paint.setAntiAlias(true);
34
35 const SkColor colors[] = {
37 };
38
39 int width = 25;
40 bool xDirection = (direction & 0x1) == 1;
41 bool yDirection = (direction & 0x2) == 2;
42 if (xDirection) {
43 for (int x = 0; x < info.width(); x += width) {
44 paint.setColor(colors[x/width % 5]);
45 if (yDirection) {
46 paint.setAlphaf(0.5f);
47 }
48 c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint);
49 }
50 }
51
52 if (yDirection) {
53 for (int y = 0; y < info.height(); y += width) {
54 paint.setColor(colors[y/width % 5]);
55 if (xDirection) {
56 paint.setAlphaf(0.5f);
57 }
58 c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
59 }
60 }
61 return surface->makeImageSnapshot();
62}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
VkSurfaceKHR surface
Definition: main.cc:49
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
int32_t width
static SkImageInfo MakeN32Premul(int width, int height)