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