2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
12 #include "SkGradientShader.h"
16 static SkBitmap make_bmp(int w, int h) {
18 bmp.allocN32Pixels(w, h, true);
21 SkScalar wScalar = SkIntToScalar(w);
22 SkScalar hScalar = SkIntToScalar(h);
24 SkPoint pt = { wScalar / 2, hScalar / 2 };
26 SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
28 SkColor colors[] = { sk_tool_utils::color_to_565(SK_ColorDKGRAY),
29 sk_tool_utils::color_to_565(0xFF222255),
30 sk_tool_utils::color_to_565(0xFF331133),
31 sk_tool_utils::color_to_565(0xFF884422),
32 sk_tool_utils::color_to_565(0xFF000022), SK_ColorWHITE,
33 sk_tool_utils::color_to_565(0xFFAABBCC) };
44 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
45 SkMatrix mat = SkMatrix::I();
46 for (int i = 0; i < 4; ++i) {
47 paint.setShader(SkGradientShader::MakeRadial(
50 SK_ARRAY_COUNT(colors),
51 SkShader::kRepeat_TileMode,
53 canvas.drawRect(rect, paint);
54 rect.inset(wScalar / 8, hScalar / 8);
55 mat.preTranslate(6 * wScalar, 6 * hScalar);
56 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
59 paint.setAntiAlias(true);
60 sk_tool_utils::set_portable_typeface(&paint);
61 paint.setTextSize(wScalar / 2.2f);
63 paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
64 constexpr char kTxt[] = "Skia";
65 SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
66 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
67 paint.setColor(SK_ColorBLACK);
68 paint.setStyle(SkPaint::kStroke_Style);
69 paint.setStrokeWidth(SK_Scalar1);
70 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
76 * This GM tests convex polygon clips.
78 class ConvexPolyClip : public GM {
81 this->setBGColor(0xFFFFFFFF);
85 SkString onShortName() override {
86 return SkString("convex_poly_clip");
89 SkISize onISize() override {
90 // When benchmarking the saveLayer set of draws is skipped.
92 if (kBench_Mode != this->getMode()) {
95 return SkISize::Make(w, 540);
98 void onOnceBeforeDraw() override {
100 tri.moveTo(5.f, 5.f);
101 tri.lineTo(100.f, 20.f);
102 tri.lineTo(15.f, 100.f);
104 fClips.addToTail()->setPath(tri);
107 constexpr SkScalar kRadius = 45.f;
108 const SkPoint center = { kRadius, kRadius };
109 for (int i = 0; i < 6; ++i) {
110 SkScalar angle = 2 * SK_ScalarPI * i / 6;
112 point.fY = SkScalarSinCos(angle, &point.fX);
113 point.scale(kRadius);
114 point = center + point;
116 hexagon.moveTo(point);
118 hexagon.lineTo(point);
121 fClips.addToTail()->setPath(hexagon);
124 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
125 hexagon.transform(scaleM);
126 fClips.addToTail()->setPath(hexagon);
128 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
131 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
132 rotRect.addRect(rect);
134 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
135 rotRect.transform(rotM);
136 fClips.addToTail()->setPath(rotRect);
138 fBmp = make_bmp(100, 100);
141 void onDraw(SkCanvas* canvas) override {
143 constexpr SkScalar kMargin = 10.f;
146 bgPaint.setAlpha(0x15);
147 SkISize size = canvas->getBaseLayerSize();
148 canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &bgPaint);
150 constexpr char kTxt[] = "Clip Me!";
152 txtPaint.setTextSize(23.f);
153 txtPaint.setAntiAlias(true);
154 sk_tool_utils::set_portable_typeface(&txtPaint);
155 txtPaint.setColor(sk_tool_utils::color_to_565(SK_ColorDKGRAY));
156 SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1);
159 int testLayers = kBench_Mode != this->getMode();
160 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
161 for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart);
164 const Clip* clip = iter.get();
166 for (int aa = 0; aa < 2; ++aa) {
169 clip->getBounds(&bounds);
172 canvas->saveLayer(&bounds, nullptr);
176 canvas->translate(x, y);
177 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
178 canvas->drawBitmap(fBmp, 0, 0);
180 x += fBmp.width() + kMargin;
182 for (int aa = 0; aa < 2; ++aa) {
184 SkPaint clipOutlinePaint;
185 clipOutlinePaint.setAntiAlias(true);
186 clipOutlinePaint.setColor(0x50505050);
187 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
188 clipOutlinePaint.setStrokeWidth(0);
192 clip->getBounds(&bounds);
195 canvas->saveLayer(&bounds, nullptr);
199 canvas->translate(x, y);
200 SkPath closedClipPath;
201 clip->asClosedPath(&closedClipPath);
202 canvas->drawPath(closedClipPath, clipOutlinePaint);
203 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
204 canvas->scale(1.f, 1.8f);
205 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
206 0, 1.5f * txtPaint.getTextSize(),
209 x += textW + 2 * kMargin;
211 y += fBmp.height() + kMargin;
214 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
218 bool runAsBench() const override { return true; }
229 Clip () : fClipType(kNone_ClipType) {}
231 void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const {
234 canvas->clipPath(fPath, op, aa);
237 canvas->clipRect(fRect, op, aa);
240 SkDEBUGFAIL("Uninitialized Clip.");
245 void asClosedPath(SkPath* path) const {
253 path->addRect(fRect);
256 SkDEBUGFAIL("Uninitialized Clip.");
261 void setPath(const SkPath& path) {
262 fClipType = kPath_ClipType;
266 void setRect(const SkRect& rect) {
267 fClipType = kRect_ClipType;
272 ClipType getType() const { return fClipType; }
274 void getBounds(SkRect* bounds) const {
277 *bounds = fPath.getBounds();
283 SkDEBUGFAIL("Uninitialized Clip.");
294 typedef SkTLList<Clip, 1> ClipList;
298 typedef GM INHERITED;
301 DEF_GM(return new ConvexPolyClip;)