blob: 0822f4c21970362781b223d5cdae6e359b82e818 [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;
41 paint.setShader(SkGradientShader::CreateRadial(
42 pt, radius,
43 colors, pos,
44 SK_ARRAY_COUNT(colors),
45 SkShader::kRepeat_TileMode))->unref();
46 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
47 SkMatrix mat = SkMatrix::I();
48 for (int i = 0; i < 4; ++i) {
49 paint.getShader()->setLocalMatrix(mat);
50 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);
57 paint.setTextSize(wScalar / 2.2f);
58 paint.setShader(0);
59 paint.setColor(SK_ColorLTGRAY);
60 static const char kTxt[] = "Skia";
61 SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
62 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
63 paint.setColor(SK_ColorBLACK);
64 paint.setStyle(SkPaint::kStroke_Style);
65 paint.setStrokeWidth(SK_Scalar1);
66 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
67 return bmp;
68}
69
70namespace skiagm {
71/**
72 * This GM tests convex polygon clips.
73 */
74class ConvexPolyClip : public GM {
75public:
76 ConvexPolyClip() {
77 this->setBGColor(0xFFFFFFFF);
78 }
79
80protected:
81 virtual SkString onShortName() SK_OVERRIDE {
82 return SkString("convex_poly_clip");
83 }
84
85 virtual SkISize onISize() SK_OVERRIDE {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +000086 return make_isize(435, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000087 }
88
89 virtual void onOnceBeforeDraw() SK_OVERRIDE {
90 SkPath tri;
91 tri.moveTo(5.f, 5.f);
92 tri.lineTo(100.f, 20.f);
93 tri.lineTo(15.f, 100.f);
94
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +000095 fClips.addToTail()->setPath(tri);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000096
97 SkPath hexagon;
98 static const SkScalar kRadius = 45.f;
99 const SkPoint center = { kRadius, kRadius };
100 for (int i = 0; i < 6; ++i) {
101 SkScalar angle = 2 * SK_ScalarPI * i / 6;
102 SkPoint point;
103 point.fY = SkScalarSinCos(angle, &point.fX);
104 point.scale(kRadius);
105 point = center + point;
106 if (0 == i) {
107 hexagon.moveTo(point);
108 } else {
109 hexagon.lineTo(point);
110 }
111 }
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000112 fClips.addToTail()->setPath(hexagon);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000113
114 SkMatrix scaleM;
115 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
116 hexagon.transform(scaleM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000117 fClips.addToTail()->setPath(hexagon);
118
119 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000120
121 SkPath rotRect;
122 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
123 rotRect.addRect(rect);
124 SkMatrix rotM;
125 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
126 rotRect.transform(rotM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000127 fClips.addToTail()->setPath(rotRect);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000128
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000129 fBmp = make_bmp(100, 100);
130 }
131
132 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000133 SkScalar y = 0;
134 static const SkScalar kMargin = 10.f;
135
136 SkPaint bgPaint;
137 bgPaint.setAlpha(0x15);
138 SkISize size = canvas->getDeviceSize();
139 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(size.fWidth),
140 SkIntToScalar(size.fHeight));
141 canvas->drawBitmapRectToRect(fBmp, NULL, dstRect, &bgPaint);
142
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000143 for (SkTLList<Clip>::Iter iter(fClips, SkTLList<Clip>::Iter::kHead_IterStart);
144 NULL != iter.get();
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000145 iter.next()) {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000146 const Clip* clip = iter.get();
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000147 SkScalar x = 0;
148 for (int aa = 0; aa < 2; ++aa) {
149 canvas->save();
150 canvas->translate(x, y);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000151 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000152 canvas->drawBitmap(fBmp, 0, 0);
153 canvas->restore();
154 x += fBmp.width() + kMargin;
155 }
156 for (int aa = 0; aa < 2; ++aa) {
157 static const char kTxt[] = "Clip Me!";
158 SkPaint txtPaint;
159 txtPaint.setTextSize(23.f);
160 txtPaint.setAntiAlias(true);
161 txtPaint.setColor(SK_ColorDKGRAY);
162
163 SkPaint clipOutlinePaint;
164 clipOutlinePaint.setAntiAlias(true);
165 clipOutlinePaint.setColor(0x50505050);
166 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
167 clipOutlinePaint.setStrokeWidth(0);
168
169 canvas->save();
170 canvas->translate(x, y);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000171 SkPath closedClipPath;
172 clip->asClosedPath(&closedClipPath);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000173 canvas->drawPath(closedClipPath, clipOutlinePaint);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000174 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000175 canvas->scale(1.f, 1.8f);
176 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
177 0, 1.5f * txtPaint.getTextSize(),
178 txtPaint);
179 canvas->restore();
180 x += fBmp.width() + kMargin;
181 }
182
183 y += fBmp.height() + kMargin;
184 }
185 }
186
commit-bot@chromium.org6adce672014-02-03 14:48:17 +0000187 virtual uint32_t onGetFlags() const {
188 return kAsBench_Flag;
189 }
190
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000191private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000192 class Clip {
193 public:
194 enum ClipType {
195 kNone_ClipType,
196 kPath_ClipType,
197 kRect_ClipType
198 };
199
200 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000201
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000202 void setOnCanvas(SkCanvas* canvas, SkRegion::Op op, bool aa) const {
203 switch (fClipType) {
204 case kPath_ClipType:
205 canvas->clipPath(fPath, op, aa);
206 break;
207 case kRect_ClipType:
208 canvas->clipRect(fRect, op, aa);
209 break;
210 case kNone_ClipType:
211 SkDEBUGFAIL("Uninitialized Clip.");
212 break;
213 }
214 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000215
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000216 void asClosedPath(SkPath* path) const {
217 switch (fClipType) {
218 case kPath_ClipType:
219 *path = fPath;
220 path->close();
221 break;
222 case kRect_ClipType:
223 path->reset();
224 path->addRect(fRect);
225 break;
226 case kNone_ClipType:
227 SkDEBUGFAIL("Uninitialized Clip.");
228 break;
229 }
230 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000231
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000232 void setPath(const SkPath& path) {
233 fClipType = kPath_ClipType;
234 fPath = path;
235 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000236
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000237 void setRect(const SkRect& rect) {
238 fClipType = kRect_ClipType;
239 fRect = rect;
240 fPath.reset();
241 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000242
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000243 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000244
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000245 private:
246 ClipType fClipType;
247 SkPath fPath;
248 SkRect fRect;
249 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000250
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000251 SkTLList<Clip> fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000252 SkBitmap fBmp;
253
254 typedef GM INHERITED;
255};
256
257DEF_GM( return SkNEW(ConvexPolyClip); )
258
259}