blob: db357bbfd7e6ab607a692af05c9f76fd12bad547 [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 {
86 return make_isize(435, 440);
87 }
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
95 fPaths.addToTail(tri);
96
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 }
112 fPaths.addToTail(hexagon);
113
114 SkMatrix scaleM;
115 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
116 hexagon.transform(scaleM);
117 fPaths.addToTail(hexagon);
118
119 SkPath rotRect;
120 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
121 rotRect.addRect(rect);
122 SkMatrix rotM;
123 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
124 rotRect.transform(rotM);
125 fPaths.addToTail(rotRect);
126
127 fBmp = make_bmp(100, 100);
128 }
129
130 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
131 const SkPath* path;
132 SkScalar y = 0;
133 static const SkScalar kMargin = 10.f;
134
135 SkPaint bgPaint;
136 bgPaint.setAlpha(0x15);
137 SkISize size = canvas->getDeviceSize();
138 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(size.fWidth),
139 SkIntToScalar(size.fHeight));
140 canvas->drawBitmapRectToRect(fBmp, NULL, dstRect, &bgPaint);
141
142 for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_IterStart);
143 NULL != (path = iter.get());
144 iter.next()) {
145 SkScalar x = 0;
146 for (int aa = 0; aa < 2; ++aa) {
147 canvas->save();
148 canvas->translate(x, y);
149 canvas->clipPath(*path, SkRegion::kIntersect_Op, SkToBool(aa));
150 canvas->drawBitmap(fBmp, 0, 0);
151 canvas->restore();
152 x += fBmp.width() + kMargin;
153 }
154 for (int aa = 0; aa < 2; ++aa) {
155 static const char kTxt[] = "Clip Me!";
156 SkPaint txtPaint;
157 txtPaint.setTextSize(23.f);
158 txtPaint.setAntiAlias(true);
159 txtPaint.setColor(SK_ColorDKGRAY);
160
161 SkPaint clipOutlinePaint;
162 clipOutlinePaint.setAntiAlias(true);
163 clipOutlinePaint.setColor(0x50505050);
164 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
165 clipOutlinePaint.setStrokeWidth(0);
166
167 canvas->save();
168 canvas->translate(x, y);
169 SkPath closedClipPath = *path;
170 closedClipPath.close();
171 canvas->drawPath(closedClipPath, clipOutlinePaint);
172 canvas->clipPath(*path, SkRegion::kIntersect_Op, SkToBool(aa));
173 canvas->scale(1.f, 1.8f);
174 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
175 0, 1.5f * txtPaint.getTextSize(),
176 txtPaint);
177 canvas->restore();
178 x += fBmp.width() + kMargin;
179 }
180
181 y += fBmp.height() + kMargin;
182 }
183 }
184
185private:
186 SkTLList<SkPath> fPaths;
187 SkBitmap fBmp;
188
189 typedef GM INHERITED;
190};
191
192DEF_GM( return SkNEW(ConvexPolyClip); )
193
194}