1
2 /*
3 * Copyright 2013 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 #include "gm.h"
9 #include "SkBitmap.h"
10 #include "SkRandom.h"
11 #include "SkShader.h"
12 #include "SkXfermode.h"
13
14 namespace skiagm {
15
16 /**
17 * Renders overlapping shapes with colorburn against a checkerboard.
18 */
19 class DstReadShuffle : public GM {
20 public:
DstReadShuffle()21 DstReadShuffle() {
22 this->setBGColor(SkColorSetARGB(0xff, 0xff, 0, 0xff));
23 }
24
25 protected:
26 enum ShapeType {
27 kCircle_ShapeType,
28 kRoundRect_ShapeType,
29 kRect_ShapeType,
30 kConvexPath_ShapeType,
31 kConcavePath_ShapeType,
32 kText_ShapeType,
33 kNumShapeTypes
34 };
35
onShortName()36 SkString onShortName() override {
37 return SkString("dstreadshuffle");
38 }
39
onISize()40 SkISize onISize() override {
41 return SkISize::Make(kWidth, kHeight);
42 }
43
drawShape(SkCanvas * canvas,SkPaint * paint,ShapeType type)44 void drawShape(SkCanvas* canvas,
45 SkPaint* paint,
46 ShapeType type) {
47 static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
48 SkIntToScalar(75), SkIntToScalar(105));
49 switch (type) {
50 case kCircle_ShapeType:
51 canvas->drawCircle(0, 0, 50, *paint);
52 break;
53 case kRoundRect_ShapeType:
54 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), *paint);
55 break;
56 case kRect_ShapeType:
57 canvas->drawRect(kRect, *paint);
58 break;
59 case kConvexPath_ShapeType:
60 if (fConvexPath.isEmpty()) {
61 SkPoint points[4];
62 kRect.toQuad(points);
63 fConvexPath.moveTo(points[0]);
64 fConvexPath.quadTo(points[1], points[2]);
65 fConvexPath.quadTo(points[3], points[0]);
66 SkASSERT(fConvexPath.isConvex());
67 }
68 canvas->drawPath(fConvexPath, *paint);
69 break;
70 case kConcavePath_ShapeType:
71 if (fConcavePath.isEmpty()) {
72 SkPoint points[5] = {{0, SkIntToScalar(-50)} };
73 SkMatrix rot;
74 rot.setRotate(SkIntToScalar(360) / 5);
75 for (int i = 1; i < 5; ++i) {
76 rot.mapPoints(points + i, points + i - 1, 1);
77 }
78 fConcavePath.moveTo(points[0]);
79 for (int i = 0; i < 5; ++i) {
80 fConcavePath.lineTo(points[(2 * i) % 5]);
81 }
82 fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
83 SkASSERT(!fConcavePath.isConvex());
84 }
85 canvas->drawPath(fConcavePath, *paint);
86 break;
87 case kText_ShapeType: {
88 const char* text = "Hello!";
89 paint->setTextSize(30);
90 canvas->drawText(text, strlen(text), 0, 0, *paint);
91 }
92 default:
93 break;
94 }
95 }
96
GetColor(SkRandom * random,int i,int nextColor)97 static SkColor GetColor(SkRandom* random, int i, int nextColor) {
98 static SkColor colors[] = { SK_ColorRED,
99 0xFFFF7F00, // Orange
100 SK_ColorYELLOW,
101 SK_ColorGREEN,
102 SK_ColorBLUE,
103 0xFF4B0082, // indigo
104 0xFF7F00FF }; // violet
105 SkColor color;
106 int index = nextColor % SK_ARRAY_COUNT(colors);
107 switch (i) {
108 case 0:
109 color = SK_ColorTRANSPARENT;
110 break;
111 case 1:
112 color = SkColorSetARGB(0xff,
113 SkColorGetR(colors[index]),
114 SkColorGetG(colors[index]),
115 SkColorGetB(colors[index]));
116 break;
117 default:
118 uint8_t alpha = 0x80;
119 color = SkColorSetARGB(alpha,
120 SkColorGetR(colors[index]),
121 SkColorGetG(colors[index]),
122 SkColorGetB(colors[index]));
123 break;
124 }
125 return color;
126 }
127
SetStyle(SkPaint * p,int style,int width)128 static void SetStyle(SkPaint* p, int style, int width) {
129 switch (style) {
130 case 0:
131 p->setStyle(SkPaint::kStroke_Style);
132 p->setStrokeWidth((SkScalar)width);
133 break;
134 case 1:
135 p->setStyle(SkPaint::kStrokeAndFill_Style);
136 p->setStrokeWidth((SkScalar)width);
137 break;
138 default:
139 p->setStyle(SkPaint::kFill_Style);
140 break;
141 }
142 }
143
onDraw(SkCanvas * canvas)144 void onDraw(SkCanvas* canvas) override {
145 SkRandom random;
146 SkScalar y = 100;
147 for (int i = 0; i < kNumShapeTypes; i++) {
148 ShapeType shapeType = static_cast<ShapeType>(i);
149 SkScalar x = 25;
150 for (int style = 0; style < 3; style++) {
151 for (int width = 0; width <= 1; width++) {
152 for (int alpha = 0; alpha <= 2; alpha++) {
153 for (int r = 0; r <= 5; r++) {
154 SkColor color = GetColor(&random, alpha, style + width + alpha + r);
155
156 SkPaint p;
157 p.setAntiAlias(true);
158 p.setColor(color);
159 // In order to get some batching on the GPU backend we do 2 src over for
160 // each xfer mode which requires a dst read
161 p.setXfermodeMode(r % 3 == 0 ? SkXfermode::kLighten_Mode :
162 SkXfermode::kSrcOver_Mode);
163 SetStyle(&p, style, width);
164 canvas->save();
165 canvas->translate(x, y);
166 canvas->rotate((SkScalar)(r < 3 ? 10 : 0));
167 this->drawShape(canvas, &p, shapeType);
168 canvas->restore();
169 x += 8;
170 }
171 }
172 }
173 }
174 y += 50;
175 }
176 }
177
178 private:
179 enum {
180 kNumShapes = 100,
181 };
182 SkAutoTUnref<SkShader> fBG;
183 SkPath fConcavePath;
184 SkPath fConvexPath;
185 static const int kWidth = 900;
186 static const int kHeight = 400;
187 typedef GM INHERITED;
188 };
189
190 //////////////////////////////////////////////////////////////////////////////
191
MyFactory(void *)192 static GM* MyFactory(void*) { return new DstReadShuffle; }
193 static GMRegistry reg(MyFactory);
194
195 }
196