blob: 201f9bc603540723483feb5092df118136c1150b [file] [log] [blame]
robertphillips@google.com67febd92012-05-22 12:14:50 +00001/*
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
9#include "gm.h"
10#include "SkCanvas.h"
11#include "SkAAClip.h"
12
13namespace skiagm {
14
15static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
16 const SkPaint& paint) {
17 SkMask mask;
18 SkBitmap bm;
19
20 clip.copyToMask(&mask);
21
22 SkAutoMaskFreeImage amfi(mask.fImage);
23
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000024 bm.installMaskPixels(mask);
robertphillips@google.com67febd92012-05-22 12:14:50 +000025
26 // need to copy for deferred drawing test to work
27 SkBitmap bm2;
28
commit-bot@chromium.org6285f4f2014-02-20 19:08:07 +000029 bm.deepCopyTo(&bm2);
robertphillips@google.com67febd92012-05-22 12:14:50 +000030
31 canvas->drawBitmap(bm2,
32 SK_Scalar1 * mask.fBounds.fLeft,
33 SK_Scalar1 * mask.fBounds.fTop,
34 &paint);
35}
36
37//////////////////////////////////////////////////////////////////////////////
38/*
rmistry@google.comd6176b02012-08-23 18:14:13 +000039 * This GM tests anti aliased single operation booleans with SkAAClips,
robertphillips@google.com67febd92012-05-22 12:14:50 +000040 * SkRect and SkPaths.
41 */
42class SimpleClipGM : public GM {
43public:
44 enum SkGeomTypes {
45 kRect_GeomType,
46 kPath_GeomType,
47 kAAClip_GeomType
48 };
49
50 SimpleClipGM(SkGeomTypes geomType)
51 : fGeomType(geomType) {
52
53 // offset the rects a bit so we get anti-aliasing in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000054 fBase.set(100.65f,
55 100.65f,
56 150.65f,
57 150.65f);
robertphillips@google.com67febd92012-05-22 12:14:50 +000058 fRect = fBase;
59 fRect.inset(5, 5);
60 fRect.offset(25, 25);
61
62 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
63 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
64 INHERITED::setBGColor(0xFFDDDDDD);
65 }
66
67protected:
68 void buildRgn(SkAAClip* clip, SkRegion::Op op) {
69 clip->setPath(fBasePath, NULL, true);
70
71 SkAAClip clip2;
72 clip2.setPath(fRectPath, NULL, true);
73 clip->op(clip2, op);
74 }
75
76 void drawOrig(SkCanvas* canvas) {
77 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +000078
robertphillips@google.com67febd92012-05-22 12:14:50 +000079 paint.setStyle(SkPaint::kStroke_Style);
80 paint.setColor(SK_ColorBLACK);
rmistry@google.comd6176b02012-08-23 18:14:13 +000081
robertphillips@google.com67febd92012-05-22 12:14:50 +000082 canvas->drawRect(fBase, paint);
83 canvas->drawRect(fRect, paint);
84 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000085
robertphillips@google.com67febd92012-05-22 12:14:50 +000086 void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
87
88 SkAAClip clip;
89
90 this->buildRgn(&clip, op);
91 this->drawOrig(canvas);
92
93 SkPaint paint;
94 paint.setColor(color);
95 paint_rgn(canvas, clip, paint);
96 }
97
98 void drawPathsOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
99
100 this->drawOrig(canvas);
101
102 canvas->save();
103
104 // create the clip mask with the supplied boolean op
105 if (kPath_GeomType == fGeomType) {
106 // path-based case
107 canvas->clipPath(fBasePath, SkRegion::kReplace_Op, true);
108 canvas->clipPath(fRectPath, op, true);
109 } else {
110 // rect-based case
111 canvas->clipRect(fBase, SkRegion::kReplace_Op, true);
112 canvas->clipRect(fRect, op, true);
113 }
114
115 // draw a rect that will entirely cover the clip mask area
116 SkPaint paint;
117 paint.setColor(color);
118
119 SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
120 SkIntToScalar(180), SkIntToScalar(180));
121
122 canvas->drawRect(r, paint);
123
124 canvas->restore();
125 }
126
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000127 virtual uint32_t onGetFlags() const SK_OVERRIDE {
128 return kPath_GeomType == fGeomType ? kSkipTiled_Flag : 0;
129 }
130
robertphillips@google.com67febd92012-05-22 12:14:50 +0000131 virtual SkString onShortName() {
132 SkString str;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133 str.printf("simpleaaclip_%s",
robertphillips@google.com67febd92012-05-22 12:14:50 +0000134 kRect_GeomType == fGeomType ? "rect" :
rmistry@google.comd6176b02012-08-23 18:14:13 +0000135 (kPath_GeomType == fGeomType ? "path" :
robertphillips@google.com67febd92012-05-22 12:14:50 +0000136 "aaclip"));
137 return str;
138 }
139
140 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -0700141 return SkISize::Make(640, 480);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000142 }
143
144 virtual void onDraw(SkCanvas* canvas) {
145
146 static const struct {
147 SkColor fColor;
148 const char* fName;
149 SkRegion::Op fOp;
150 } gOps[] = {
151 { SK_ColorBLACK, "Difference", SkRegion::kDifference_Op },
152 { SK_ColorRED, "Intersect", SkRegion::kIntersect_Op },
153 { 0xFF008800, "Union", SkRegion::kUnion_Op },
robertphillips@google.com67febd92012-05-22 12:14:50 +0000154 { SK_ColorGREEN, "Rev Diff", SkRegion::kReverseDifference_Op },
robertphillips@google.com2a021292012-07-17 15:37:15 +0000155 { SK_ColorYELLOW, "Replace", SkRegion::kReplace_Op },
156 { SK_ColorBLUE, "XOR", SkRegion::kXOR_Op },
robertphillips@google.com67febd92012-05-22 12:14:50 +0000157 };
158
159 SkPaint textPaint;
160 textPaint.setAntiAlias(true);
161 textPaint.setTextSize(SK_Scalar1*24);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000162 int xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000163
164 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
165 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166 SkIntToScalar(75), SkIntToScalar(50),
robertphillips@google.com67febd92012-05-22 12:14:50 +0000167 textPaint);
168
169 if (kAAClip_GeomType == fGeomType) {
170 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
171 } else {
172 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
173 }
174
robertphillips@google.com2a021292012-07-17 15:37:15 +0000175 if (xOff >= 400) {
176 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
177 xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000178 } else {
179 canvas->translate(SkIntToScalar(200), 0);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000180 xOff += 200;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000181 }
182 }
183 }
184private:
185
186 SkGeomTypes fGeomType;
187
188 SkRect fBase;
189 SkRect fRect;
190
191 SkPath fBasePath; // fBase as a round rect
192 SkPath fRectPath; // fRect as a round rect
193
194 typedef GM INHERITED;
195};
196
197//////////////////////////////////////////////////////////////////////////////
198
199// rects
reed@google.comde1fc472012-12-14 12:59:07 +0000200DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
201DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
202DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000203
robertphillips@google.com67febd92012-05-22 12:14:50 +0000204}