blob: 8d54b00cd0a57cb81fa27e62486520ec7adc568f [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:
mtklein72c9faa2015-01-09 10:06:39 -080082 SkString onShortName() SK_OVERRIDE {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000083 return SkString("convex_poly_clip");
84 }
85
mtklein72c9faa2015-01-09 10:06:39 -080086 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
mtklein72c9faa2015-01-09 10:06:39 -080095 void onOnceBeforeDraw() SK_OVERRIDE {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000096 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
mtklein72c9faa2015-01-09 10:06:39 -0800138 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);
Cary Clark992c7b02014-07-31 08:58:44 -0400153 sk_tool_utils::set_portable_typeface(&txtPaint);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000154 txtPaint.setColor(SK_ColorDKGRAY);
155 SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1);
156
157 SkScalar startX = 0;
158 int testLayers = kBench_Mode != this->getMode();
159 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
160 for (SkTLList<Clip>::Iter iter(fClips, SkTLList<Clip>::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700161 iter.get();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000162 iter.next()) {
163 const Clip* clip = iter.get();
164 SkScalar x = startX;
165 for (int aa = 0; aa < 2; ++aa) {
166 if (doLayer) {
167 SkRect bounds;
168 clip->getBounds(&bounds);
169 bounds.outset(2, 2);
170 bounds.offset(x, y);
171 canvas->saveLayer(&bounds, NULL);
172 } else {
173 canvas->save();
174 }
175 canvas->translate(x, y);
176 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
177 canvas->drawBitmap(fBmp, 0, 0);
178 canvas->restore();
179 x += fBmp.width() + kMargin;
180 }
181 for (int aa = 0; aa < 2; ++aa) {
182
183 SkPaint clipOutlinePaint;
184 clipOutlinePaint.setAntiAlias(true);
185 clipOutlinePaint.setColor(0x50505050);
186 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
187 clipOutlinePaint.setStrokeWidth(0);
188
189 if (doLayer) {
190 SkRect bounds;
191 clip->getBounds(&bounds);
192 bounds.outset(2, 2);
193 bounds.offset(x, y);
194 canvas->saveLayer(&bounds, NULL);
195 } else {
196 canvas->save();
197 }
198 canvas->translate(x, y);
199 SkPath closedClipPath;
200 clip->asClosedPath(&closedClipPath);
201 canvas->drawPath(closedClipPath, clipOutlinePaint);
202 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
203 canvas->scale(1.f, 1.8f);
204 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
205 0, 1.5f * txtPaint.getTextSize(),
206 txtPaint);
207 canvas->restore();
208 x += textW + 2 * kMargin;
209 }
210 y += fBmp.height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000211 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000212 y = 0;
213 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000214 }
215 }
216
mtkleincf5d9c92015-01-23 10:31:45 -0800217 bool runAsBench() const SK_OVERRIDE { return true; }
218
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000219private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000220 class Clip {
221 public:
222 enum ClipType {
223 kNone_ClipType,
224 kPath_ClipType,
225 kRect_ClipType
226 };
227
228 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000229
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000230 void setOnCanvas(SkCanvas* canvas, SkRegion::Op op, bool aa) const {
231 switch (fClipType) {
232 case kPath_ClipType:
233 canvas->clipPath(fPath, op, aa);
234 break;
235 case kRect_ClipType:
236 canvas->clipRect(fRect, op, aa);
237 break;
238 case kNone_ClipType:
239 SkDEBUGFAIL("Uninitialized Clip.");
240 break;
241 }
242 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000243
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000244 void asClosedPath(SkPath* path) const {
245 switch (fClipType) {
246 case kPath_ClipType:
247 *path = fPath;
248 path->close();
249 break;
250 case kRect_ClipType:
251 path->reset();
252 path->addRect(fRect);
253 break;
254 case kNone_ClipType:
255 SkDEBUGFAIL("Uninitialized Clip.");
256 break;
257 }
258 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000259
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000260 void setPath(const SkPath& path) {
261 fClipType = kPath_ClipType;
262 fPath = path;
263 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000264
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000265 void setRect(const SkRect& rect) {
266 fClipType = kRect_ClipType;
267 fRect = rect;
268 fPath.reset();
269 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000270
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000271 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000272
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000273 void getBounds(SkRect* bounds) const {
274 switch (fClipType) {
275 case kPath_ClipType:
276 *bounds = fPath.getBounds();
277 break;
278 case kRect_ClipType:
279 *bounds = fRect;
280 break;
281 case kNone_ClipType:
282 SkDEBUGFAIL("Uninitialized Clip.");
283 break;
284 }
285 }
286
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000287 private:
288 ClipType fClipType;
289 SkPath fPath;
290 SkRect fRect;
291 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000292
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000293 SkTLList<Clip> fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000294 SkBitmap fBmp;
295
296 typedef GM INHERITED;
297};
298
299DEF_GM( return SkNEW(ConvexPolyClip); )
300
301}