blob: cb698c162b86ccbf718cdc3f43eb77ef3de77f14 [file] [log] [blame]
commit-bot@chromium.orga343c842014-01-31 14:48:58 +00001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "gm.h"
10
11#include "SkBitmap.h"
12#include "SkGradientShader.h"
13#include "SkTLList.h"
14
15static SkBitmap make_bmp(int w, int h) {
16 SkBitmap bmp;
17 bmp.allocN32Pixels(w, h, true);
18
19 SkCanvas canvas(bmp);
20 SkScalar wScalar = SkIntToScalar(w);
21 SkScalar hScalar = SkIntToScalar(h);
22
23 SkPoint pt = { wScalar / 2, hScalar / 2 };
24
25 SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
26
27 SkColor colors[] = { SK_ColorDKGRAY, 0xFF222255,
28 0xFF331133, 0xFF884422,
29 0xFF000022, SK_ColorWHITE,
30 0xFFAABBCC};
31
32 SkScalar pos[] = {0,
33 SK_Scalar1 / 6,
34 2 * SK_Scalar1 / 6,
35 3 * SK_Scalar1 / 6,
36 4 * SK_Scalar1 / 6,
37 5 * SK_Scalar1 / 6,
38 SK_Scalar1};
39
40 SkPaint paint;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000041 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
42 SkMatrix mat = SkMatrix::I();
43 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000044 paint.setShader(SkGradientShader::CreateRadial(
45 pt, radius,
46 colors, pos,
47 SK_ARRAY_COUNT(colors),
48 SkShader::kRepeat_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000049 0, &mat))->unref();
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000050 canvas.drawRect(rect, paint);
51 rect.inset(wScalar / 8, hScalar / 8);
52 mat.preTranslate(6 * wScalar, 6 * hScalar);
53 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
54 }
55
56 paint.setAntiAlias(true);
caryclark5fb6bd42014-06-23 11:25:00 -070057 sk_tool_utils::set_portable_typeface(&paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000058 paint.setTextSize(wScalar / 2.2f);
59 paint.setShader(0);
60 paint.setColor(SK_ColorLTGRAY);
61 static const char kTxt[] = "Skia";
62 SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
63 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
64 paint.setColor(SK_ColorBLACK);
65 paint.setStyle(SkPaint::kStroke_Style);
66 paint.setStrokeWidth(SK_Scalar1);
67 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
68 return bmp;
69}
70
71namespace skiagm {
72/**
73 * This GM tests convex polygon clips.
74 */
75class ConvexPolyClip : public GM {
76public:
77 ConvexPolyClip() {
78 this->setBGColor(0xFFFFFFFF);
79 }
80
81protected:
82 virtual SkString onShortName() SK_OVERRIDE {
83 return SkString("convex_poly_clip");
84 }
85
86 virtual SkISize onISize() SK_OVERRIDE {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000087 // When benchmarking the saveLayer set of draws is skipped.
88 int w = 435;
89 if (kBench_Mode != this->getMode()) {
90 w *= 2;
91 }
tfarinaf5393182014-06-09 23:59:03 -070092 return SkISize::Make(w, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000093 }
94
95 virtual void onOnceBeforeDraw() SK_OVERRIDE {
96 SkPath tri;
97 tri.moveTo(5.f, 5.f);
98 tri.lineTo(100.f, 20.f);
99 tri.lineTo(15.f, 100.f);
100
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000101 fClips.addToTail()->setPath(tri);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000102
103 SkPath hexagon;
104 static const SkScalar kRadius = 45.f;
105 const SkPoint center = { kRadius, kRadius };
106 for (int i = 0; i < 6; ++i) {
107 SkScalar angle = 2 * SK_ScalarPI * i / 6;
108 SkPoint point;
109 point.fY = SkScalarSinCos(angle, &point.fX);
110 point.scale(kRadius);
111 point = center + point;
112 if (0 == i) {
113 hexagon.moveTo(point);
114 } else {
115 hexagon.lineTo(point);
116 }
117 }
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000118 fClips.addToTail()->setPath(hexagon);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000119
120 SkMatrix scaleM;
121 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
122 hexagon.transform(scaleM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000123 fClips.addToTail()->setPath(hexagon);
124
125 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000126
127 SkPath rotRect;
128 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
129 rotRect.addRect(rect);
130 SkMatrix rotM;
131 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
132 rotRect.transform(rotM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000133 fClips.addToTail()->setPath(rotRect);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000134
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000135 fBmp = make_bmp(100, 100);
136 }
137
138 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000139 SkScalar y = 0;
140 static const SkScalar kMargin = 10.f;
141
142 SkPaint bgPaint;
143 bgPaint.setAlpha(0x15);
144 SkISize size = canvas->getDeviceSize();
145 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(size.fWidth),
146 SkIntToScalar(size.fHeight));
147 canvas->drawBitmapRectToRect(fBmp, NULL, dstRect, &bgPaint);
148
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000149 static const char kTxt[] = "Clip Me!";
150 SkPaint txtPaint;
151 txtPaint.setTextSize(23.f);
152 txtPaint.setAntiAlias(true);
153 txtPaint.setColor(SK_ColorDKGRAY);
154 SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1);
155
156 SkScalar startX = 0;
157 int testLayers = kBench_Mode != this->getMode();
158 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
159 for (SkTLList<Clip>::Iter iter(fClips, SkTLList<Clip>::Iter::kHead_IterStart);
160 NULL != iter.get();
161 iter.next()) {
162 const Clip* clip = iter.get();
163 SkScalar x = startX;
164 for (int aa = 0; aa < 2; ++aa) {
165 if (doLayer) {
166 SkRect bounds;
167 clip->getBounds(&bounds);
168 bounds.outset(2, 2);
169 bounds.offset(x, y);
170 canvas->saveLayer(&bounds, NULL);
171 } else {
172 canvas->save();
173 }
174 canvas->translate(x, y);
175 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
176 canvas->drawBitmap(fBmp, 0, 0);
177 canvas->restore();
178 x += fBmp.width() + kMargin;
179 }
180 for (int aa = 0; aa < 2; ++aa) {
181
182 SkPaint clipOutlinePaint;
183 clipOutlinePaint.setAntiAlias(true);
184 clipOutlinePaint.setColor(0x50505050);
185 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
186 clipOutlinePaint.setStrokeWidth(0);
187
188 if (doLayer) {
189 SkRect bounds;
190 clip->getBounds(&bounds);
191 bounds.outset(2, 2);
192 bounds.offset(x, y);
193 canvas->saveLayer(&bounds, NULL);
194 } else {
195 canvas->save();
196 }
197 canvas->translate(x, y);
198 SkPath closedClipPath;
199 clip->asClosedPath(&closedClipPath);
200 canvas->drawPath(closedClipPath, clipOutlinePaint);
201 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
202 canvas->scale(1.f, 1.8f);
203 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
204 0, 1.5f * txtPaint.getTextSize(),
205 txtPaint);
206 canvas->restore();
207 x += textW + 2 * kMargin;
208 }
209 y += fBmp.height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000210 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000211 y = 0;
212 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000213 }
214 }
215
commit-bot@chromium.org6adce672014-02-03 14:48:17 +0000216 virtual uint32_t onGetFlags() const {
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000217 return kAsBench_Flag | kSkipTiled_Flag;
commit-bot@chromium.org6adce672014-02-03 14:48:17 +0000218 }
219
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000220private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000221 class Clip {
222 public:
223 enum ClipType {
224 kNone_ClipType,
225 kPath_ClipType,
226 kRect_ClipType
227 };
228
229 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000230
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000231 void setOnCanvas(SkCanvas* canvas, SkRegion::Op op, bool aa) const {
232 switch (fClipType) {
233 case kPath_ClipType:
234 canvas->clipPath(fPath, op, aa);
235 break;
236 case kRect_ClipType:
237 canvas->clipRect(fRect, op, aa);
238 break;
239 case kNone_ClipType:
240 SkDEBUGFAIL("Uninitialized Clip.");
241 break;
242 }
243 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000244
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000245 void asClosedPath(SkPath* path) const {
246 switch (fClipType) {
247 case kPath_ClipType:
248 *path = fPath;
249 path->close();
250 break;
251 case kRect_ClipType:
252 path->reset();
253 path->addRect(fRect);
254 break;
255 case kNone_ClipType:
256 SkDEBUGFAIL("Uninitialized Clip.");
257 break;
258 }
259 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000260
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000261 void setPath(const SkPath& path) {
262 fClipType = kPath_ClipType;
263 fPath = path;
264 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000265
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000266 void setRect(const SkRect& rect) {
267 fClipType = kRect_ClipType;
268 fRect = rect;
269 fPath.reset();
270 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000271
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000272 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000273
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000274 void getBounds(SkRect* bounds) const {
275 switch (fClipType) {
276 case kPath_ClipType:
277 *bounds = fPath.getBounds();
278 break;
279 case kRect_ClipType:
280 *bounds = fRect;
281 break;
282 case kNone_ClipType:
283 SkDEBUGFAIL("Uninitialized Clip.");
284 break;
285 }
286 }
287
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000288 private:
289 ClipType fClipType;
290 SkPath fPath;
291 SkRect fRect;
292 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000293
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000294 SkTLList<Clip> fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000295 SkBitmap fBmp;
296
297 typedef GM INHERITED;
298};
299
300DEF_GM( return SkNEW(ConvexPolyClip); )
301
302}