3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
12 #include "SkGradientShader.h"
15 static SkBitmap make_bmp(int w, int h) {
17 bmp.allocN32Pixels(w, h, true);
20 SkScalar wScalar = SkIntToScalar(w);
21 SkScalar hScalar = SkIntToScalar(h);
23 SkPoint pt = { wScalar / 2, hScalar / 2 };
25 SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
27 SkColor colors[] = { SK_ColorDKGRAY, 0xFF222255,
28 0xFF331133, 0xFF884422,
29 0xFF000022, SK_ColorWHITE,
41 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
42 SkMatrix mat = SkMatrix::I();
43 for (int i = 0; i < 4; ++i) {
44 paint.setShader(SkGradientShader::CreateRadial(
47 SK_ARRAY_COUNT(colors),
48 SkShader::kRepeat_TileMode,
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);
56 paint.setAntiAlias(true);
57 sk_tool_utils::set_portable_typeface(&paint);
58 paint.setTextSize(wScalar / 2.2f);
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);
73 * This GM tests convex polygon clips.
75 class ConvexPolyClip : public GM {
78 this->setBGColor(0xFFFFFFFF);
82 SkString onShortName() override {
83 return SkString("convex_poly_clip");
86 SkISize onISize() override {
87 // When benchmarking the saveLayer set of draws is skipped.
89 if (kBench_Mode != this->getMode()) {
92 return SkISize::Make(w, 540);
95 void onOnceBeforeDraw() override {
98 tri.lineTo(100.f, 20.f);
99 tri.lineTo(15.f, 100.f);
101 fClips.addToTail()->setPath(tri);
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;
109 point.fY = SkScalarSinCos(angle, &point.fX);
110 point.scale(kRadius);
111 point = center + point;
113 hexagon.moveTo(point);
115 hexagon.lineTo(point);
118 fClips.addToTail()->setPath(hexagon);
121 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
122 hexagon.transform(scaleM);
123 fClips.addToTail()->setPath(hexagon);
125 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
128 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
129 rotRect.addRect(rect);
131 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
132 rotRect.transform(rotM);
133 fClips.addToTail()->setPath(rotRect);
135 fBmp = make_bmp(100, 100);
138 void onDraw(SkCanvas* canvas) override {
140 static const SkScalar kMargin = 10.f;
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);
149 static const char kTxt[] = "Clip Me!";
151 txtPaint.setTextSize(23.f);
152 txtPaint.setAntiAlias(true);
153 sk_tool_utils::set_portable_typeface(&txtPaint);
154 txtPaint.setColor(SK_ColorDKGRAY);
155 SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1);
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);
163 const Clip* clip = iter.get();
165 for (int aa = 0; aa < 2; ++aa) {
168 clip->getBounds(&bounds);
171 canvas->saveLayer(&bounds, NULL);
175 canvas->translate(x, y);
176 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
177 canvas->drawBitmap(fBmp, 0, 0);
179 x += fBmp.width() + kMargin;
181 for (int aa = 0; aa < 2; ++aa) {
183 SkPaint clipOutlinePaint;
184 clipOutlinePaint.setAntiAlias(true);
185 clipOutlinePaint.setColor(0x50505050);
186 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
187 clipOutlinePaint.setStrokeWidth(0);
191 clip->getBounds(&bounds);
194 canvas->saveLayer(&bounds, NULL);
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(),
208 x += textW + 2 * kMargin;
210 y += fBmp.height() + kMargin;
213 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
217 bool runAsBench() const override { return true; }
228 Clip () : fClipType(kNone_ClipType) {}
230 void setOnCanvas(SkCanvas* canvas, SkRegion::Op op, bool aa) const {
233 canvas->clipPath(fPath, op, aa);
236 canvas->clipRect(fRect, op, aa);
239 SkDEBUGFAIL("Uninitialized Clip.");
244 void asClosedPath(SkPath* path) const {
252 path->addRect(fRect);
255 SkDEBUGFAIL("Uninitialized Clip.");
260 void setPath(const SkPath& path) {
261 fClipType = kPath_ClipType;
265 void setRect(const SkRect& rect) {
266 fClipType = kRect_ClipType;
271 ClipType getType() const { return fClipType; }
273 void getBounds(SkRect* bounds) const {
276 *bounds = fPath.getBounds();
282 SkDEBUGFAIL("Uninitialized Clip.");
293 SkTLList<Clip> fClips;
296 typedef GM INHERITED;
299 DEF_GM( return SkNEW(ConvexPolyClip); )