Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
simpleaaclip.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"
13#include "include/core/SkFont.h"
15#include "include/core/SkPath.h"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
23#include "src/core/SkAAClip.h"
24#include "src/core/SkMask.h"
25#include "tools/ToolUtils.h"
27
28namespace skiagm {
29
30static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
31 const SkPaint& paint) {
32 SkMaskBuilder mask;
33 SkBitmap bm;
34
35 clip.copyToMask(&mask);
36
37 SkAutoMaskFreeImage amfi(mask.image());
38
39 bm.installMaskPixels(mask);
40
41 // need to copy for deferred drawing test to work
42 SkBitmap bm2;
43
44 ToolUtils::copy_to(&bm2, bm.colorType(), bm);
45
46 canvas->drawImage(bm2.asImage(),
48 SK_Scalar1 * mask.fBounds.fTop,
50 &paint);
51}
52
53//////////////////////////////////////////////////////////////////////////////
54/*
55 * This GM tests anti aliased single operation booleans with SkAAClips,
56 * SkRect and SkPaths.
57 */
58class SimpleClipGM : public GM {
59public:
65
67 : fGeomType(geomType) {
68 }
69
70protected:
71 void onOnceBeforeDraw() override {
72 // offset the rects a bit so we get anti-aliasing in the rect case
73 fBase.setLTRB(100.65f,
74 100.65f,
75 150.65f,
76 150.65f);
77 fRect = fBase;
78 fRect.inset(5, 5);
79 fRect.offset(25, 25);
80
81 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
82 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
83 INHERITED::setBGColor(0xFFDDDDDD);
84 }
85
87 clip->setPath(fBasePath, fBasePath.getBounds().roundOut(), true);
88
89 SkAAClip clip2;
90 clip2.setPath(fRectPath, fRectPath.getBounds().roundOut(), true);
91 clip->op(clip2, op);
92 }
93
94 void drawOrig(SkCanvas* canvas) {
96
98 paint.setColor(SK_ColorBLACK);
99
100 canvas->drawRect(fBase, paint);
101 canvas->drawRect(fRect, paint);
102 }
103
105
107
108 this->buildRgn(&clip, op);
109 this->drawOrig(canvas);
110
112 paint.setColor(color);
113 paint_rgn(canvas, clip, paint);
114 }
115
117
118 this->drawOrig(canvas);
119
120 canvas->save();
121
122 // create the clip mask with the supplied boolean op
123 if (kPath_GeomType == fGeomType) {
124 // path-based case
125 canvas->clipPath(fBasePath, true);
126 canvas->clipPath(fRectPath, op, true);
127 } else {
128 // rect-based case
129 canvas->clipRect(fBase, true);
130 canvas->clipRect(fRect, op, true);
131 }
132
133 // draw a rect that will entirely cover the clip mask area
135 paint.setColor(color);
136
138 SkIntToScalar(180), SkIntToScalar(180));
139
140 canvas->drawRect(r, paint);
141
142 canvas->restore();
143 }
144
145 SkString getName() const override {
146 SkString str;
147 str.printf("simpleaaclip_%s",
148 kRect_GeomType == fGeomType ? "rect" :
149 (kPath_GeomType == fGeomType ? "path" :
150 "aaclip"));
151 return str;
152 }
153
154 SkISize getISize() override { return SkISize::Make(500, 240); }
155
156 void onDraw(SkCanvas* canvas) override {
157
158 const struct {
159 SkColor fColor;
160 const char* fName;
161 SkClipOp fOp;
162 } gOps[] = {
163 {SK_ColorBLACK, "Difference", SkClipOp::kDifference},
164 {SK_ColorRED, "Intersect", SkClipOp::kIntersect},
165 };
166
167 SkPaint textPaint;
169 int xOff = 0;
170
171 for (size_t op = 0; op < std::size(gOps); op++) {
172 canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint);
173
174 if (kAAClip_GeomType == fGeomType) {
175 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
176 } else {
177 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
178 }
179
180 if (xOff >= 400) {
181 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
182 xOff = 0;
183 } else {
184 canvas->translate(SkIntToScalar(200), 0);
185 xOff += 200;
186 }
187 }
188 }
189private:
190
191 SkGeomTypes fGeomType;
192
193 SkRect fBase;
194 SkRect fRect;
195
196 SkPath fBasePath; // fBase as a round rect
197 SkPath fRectPath; // fRect as a round rect
198
199 using INHERITED = GM;
200};
201
202//////////////////////////////////////////////////////////////////////////////
203
204// rects
205DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
206DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
207DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
208
209} // namespace skiagm
SkColor4f color
SkClipOp
Definition SkClipOp.h:13
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
std::unique_ptr< uint8_t, SkFunctionObject< SkMaskBuilder::FreeImage > > SkAutoMaskFreeImage
Definition SkMask.h:316
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
bool setPath(const SkPath &, const SkIRect &bounds, bool doAA=true)
bool installMaskPixels(SkMaskBuilder &mask)
Definition SkBitmap.cpp:349
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
SkColorType colorType() const
Definition SkBitmap.h:160
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 clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
int save()
Definition SkCanvas.cpp:451
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
SkPath & addRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, SkPathDirection dir=SkPathDirection::kCW)
Definition SkPath.cpp:1088
const SkRect & getBounds() const
Definition SkPath.cpp:420
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
void setBGColor(SkColor)
Definition gm.cpp:159
void drawRgnOped(SkCanvas *canvas, SkClipOp op, SkColor color)
void drawOrig(SkCanvas *canvas)
SimpleClipGM(SkGeomTypes geomType)
void drawPathsOped(SkCanvas *canvas, SkClipOp op, SkColor color)
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
void onOnceBeforeDraw() override
SkString getName() const override
void buildRgn(SkAAClip *clip, SkClipOp op)
const Paint & paint
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkTypeface > DefaultPortableTypeface()
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
static void paint_rgn(SkCanvas *canvas, const SkAAClip &clip, const SkPaint &paint)
const char * fName
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
uint8_t *& image()
Definition SkMask.h:236
const SkIRect fBounds
Definition SkMask.h:42
void inset(float dx, float dy)
Definition SkRect.h:1060
void roundOut(SkIRect *dst) const
Definition SkRect.h:1241
void offset(float dx, float dy)
Definition SkRect.h:1016
void setLTRB(float left, float top, float right, float bottom)
Definition SkRect.h:865
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646