blob: e8255502a12c0b5de702c721cc41136942a6dda0 [file] [log] [blame]
robertphillips@google.com41570852013-06-03 17:13:25 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
robertphillips@google.com41570852013-06-03 17:13:25 +000010#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkCanvas.h"
robertphillips@google.com41570852013-06-03 17:13:25 +000012#include "SkColorFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000013#include "SkLayerDrawLooper.h"
robertphillips@google.com41570852013-06-03 17:13:25 +000014
15// This GM tests 3 different ways of drawing four shadows around a square:
16// just using 4 blurred rects
17// using 4 1-level draw loopers
18// using 1 4-level draw looper
19// They all produce exactly the same pixels
20class MegaLooperGM : public skiagm::GM {
21public:
22 // The types define "<# of loopers> x <# of stages per looper>"
23 enum Type {
24 k0x0_Type, // draw without loopers at all
25 k4x1_Type, // a looper for each shadow
26 k1x4_Type, // all four shadows in one looper
27 };
28
29 MegaLooperGM(Type type) : fType(type) {}
30
31protected:
32 virtual SkString onShortName() {
33 switch (fType) {
34 case k0x0_Type:
35 return SkString("megalooper_0x0");
36 break;
37 case k4x1_Type:
38 return SkString("megalooper_4x1");
39 break;
40 case k1x4_Type: // fall through
41 default:
42 return SkString("megalooper_1x4");
43 break;
44 }
45 }
46
47 virtual SkISize onISize() {
48 return SkISize::Make(kWidth, kHeight);
49 }
50
51 virtual void onDraw(SkCanvas* canvas) {
52 for (int y = 100; y < kHeight; y += 200) {
53 for (int x = 100; x < kWidth; x += 200) {
54 switch (fType) {
55 case k0x0_Type:
56 draw0x0(canvas, SkIntToScalar(x), SkIntToScalar(y));
57 break;
58 case k4x1_Type:
59 draw4x1(canvas, SkIntToScalar(x), SkIntToScalar(y));
60 break;
61 case k1x4_Type: // fall through
62 default:
63 draw1x4(canvas, SkIntToScalar(x), SkIntToScalar(y));
64 break;
65 }
66 }
67 }
68 }
69
70private:
71 static const int kWidth = 800;
72 static const int kHeight = 800;
73 static const int kHalfOuterClipSize = 100;
74 static const int kHalfSquareSize = 50;
75 static const int kOffsetToOutsideClip = kHalfSquareSize + kHalfOuterClipSize + 1;
76
77 static const SkPoint gBlurOffsets[4];
78 static const SkColor gColors[4];
79 Type fType;
80
81 // Just draw a blurred rect at each of the four corners of a square (centered at x,y).
82 // Use two clips to define a rectori where we want pixels to appear.
83 void draw0x0(SkCanvas* canvas, SkScalar x, SkScalar y) {
84 SkRect innerClip = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
85 innerClip.offset(x, y);
86
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +000087 SkRect outerClip = {
88 -kHalfOuterClipSize-kHalfSquareSize, -kHalfOuterClipSize-kHalfSquareSize,
robertphillips@google.com41570852013-06-03 17:13:25 +000089 kHalfOuterClipSize+kHalfSquareSize, kHalfOuterClipSize+kHalfSquareSize
90 };
91 outerClip.offset(x, y);
92
93 canvas->save();
94 canvas->clipRect(outerClip, SkRegion::kIntersect_Op);
95 canvas->clipRect(innerClip, SkRegion::kDifference_Op);
96
97 SkPaint paint;
98 paint.setAntiAlias(true);
99 paint.setMaskFilter(createBlur())->unref();
100
101 for (int i = 0; i < 4; ++i) {
102 paint.setColor(gColors[i]);
103
104 SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
105 rect.offset(gBlurOffsets[i]);
106 rect.offset(x, y);
107 canvas->drawRect(rect, paint);
108 }
109
110 canvas->restore();
111 }
112
113 SkMaskFilter* createBlur() {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000114 static const SkScalar kBlurSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(25));
robertphillips@google.com41570852013-06-03 17:13:25 +0000115
robertphillips@google.comb7061172013-09-06 14:16:12 +0000116 return SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
117 kBlurSigma,
robertphillips@google.com41570852013-06-03 17:13:25 +0000118 SkBlurMaskFilter::kHighQuality_BlurFlag);
119 }
120
121 // This draws 4 blurred shadows around a single square (centered at x, y).
122 // Each blur is offset +/- half the square's side in x & y from the original
123 // (so each blurred rect is centered at one of the corners of the original).
124 // For each blur a large outer clip is centered around the blurred rect
125 // while a difference clip stays at the location of the original rect.
126 // Each blurred rect is drawn with a draw looper where the original (non-
127 // blurred rect) is offset to reside outside of the large outer clip (so
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000128 // it never appears) but the offset in the draw looper is used to translate
robertphillips@google.com41570852013-06-03 17:13:25 +0000129 // the blurred version back into the clip.
130 void draw4x1(SkCanvas* canvas, SkScalar x, SkScalar y) {
131
132 for (int i = 0; i < 4; ++i) {
133 SkPaint loopPaint;
134
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000135 loopPaint.setLooper(create1Looper(-kOffsetToOutsideClip, 0, gColors[i]))->unref();
robertphillips@google.com41570852013-06-03 17:13:25 +0000136 loopPaint.setAntiAlias(true);
137
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000138 SkRect outerClip = {
139 -kHalfOuterClipSize, -kHalfOuterClipSize,
140 kHalfOuterClipSize, kHalfOuterClipSize
robertphillips@google.com41570852013-06-03 17:13:25 +0000141 };
142 outerClip.offset(x, y);
143 // center it on the blurred rect
144 outerClip.offset(gBlurOffsets[i]);
145
146 SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
147 rect.offset(x, y);
148
149 canvas->save();
150 canvas->clipRect(outerClip, SkRegion::kIntersect_Op);
151 canvas->clipRect(rect, SkRegion::kDifference_Op);
152
153 // move the rect to where we want the blur to appear
154 rect.offset(gBlurOffsets[i]);
155 // then move it outside the clip (the blur stage of the draw
156 // looper will undo this translation)
157 rect.offset(SkIntToScalar(kOffsetToOutsideClip), 0);
158
159 canvas->drawRect(rect, loopPaint);
160 canvas->restore();
161 }
162 }
163
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000164 // Create a 1-tier drawlooper
robertphillips@google.com41570852013-06-03 17:13:25 +0000165 SkLayerDrawLooper* create1Looper(SkScalar xOff, SkScalar yOff, SkColor color) {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000166 SkLayerDrawLooper::Builder looperBuilder;
robertphillips@google.com41570852013-06-03 17:13:25 +0000167 SkLayerDrawLooper::LayerInfo info;
168
robertphillips@google.com41570852013-06-03 17:13:25 +0000169 info.fPaintBits = SkLayerDrawLooper::kColorFilter_Bit |
170 SkLayerDrawLooper::kMaskFilter_Bit;
171 info.fColorMode = SkXfermode::kSrc_Mode;
172 info.fOffset.set(xOff, yOff);
173 info.fPostTranslate = false;
174
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000175 SkPaint* paint = looperBuilder.addLayer(info);
robertphillips@google.com41570852013-06-03 17:13:25 +0000176
177 paint->setMaskFilter(this->createBlur())->unref();
178
179 SkColorFilter* cf = SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcIn_Mode);
180 paint->setColorFilter(cf)->unref();
181
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000182 return looperBuilder.detachLooper();
robertphillips@google.com41570852013-06-03 17:13:25 +0000183 }
184
185 void draw1x4(SkCanvas* canvas, SkScalar x, SkScalar y) {
186 SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize };
187 rect.offset(x, y);
188
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000189 SkRect outerClip = {
190 -kHalfOuterClipSize-kHalfSquareSize, -kHalfOuterClipSize-kHalfSquareSize,
robertphillips@google.com41570852013-06-03 17:13:25 +0000191 kHalfOuterClipSize+kHalfSquareSize, kHalfOuterClipSize+kHalfSquareSize
192 };
193 outerClip.offset(x, y);
194
195 SkPaint paint;
196 paint.setAntiAlias(true);
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000197 paint.setLooper(create4Looper(-kOffsetToOutsideClip-kHalfSquareSize, 0))->unref();
robertphillips@google.com41570852013-06-03 17:13:25 +0000198
199 canvas->save();
200 canvas->clipRect(outerClip, SkRegion::kIntersect_Op);
201 canvas->clipRect(rect, SkRegion::kDifference_Op);
202
203 rect.offset(SkIntToScalar(kOffsetToOutsideClip+kHalfSquareSize), 0);
204 canvas->drawRect(rect, paint);
205 canvas->restore();
206 }
207
208 // Create a 4-tier draw looper
209 SkLayerDrawLooper* create4Looper(SkScalar xOff, SkScalar yOff) {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000210 SkLayerDrawLooper::Builder looperBuilder;
robertphillips@google.com41570852013-06-03 17:13:25 +0000211 SkLayerDrawLooper::LayerInfo info;
212
robertphillips@google.com41570852013-06-03 17:13:25 +0000213 info.fPaintBits = SkLayerDrawLooper::kColorFilter_Bit |
214 SkLayerDrawLooper::kMaskFilter_Bit;
215 info.fColorMode = SkXfermode::kSrc_Mode;
216 info.fPostTranslate = false;
217
218 SkPaint* paint;
219
220 for (int i = 3; i >= 0; --i) {
221 info.fOffset.set(xOff+gBlurOffsets[i].fX, yOff+gBlurOffsets[i].fY);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000222 paint = looperBuilder.addLayer(info);
robertphillips@google.com41570852013-06-03 17:13:25 +0000223
224 paint->setMaskFilter(this->createBlur())->unref();
225
226 SkColorFilter* cf = SkColorFilter::CreateModeFilter(gColors[i], SkXfermode::kSrcIn_Mode);
227 paint->setColorFilter(cf)->unref();
228 }
229
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000230 return looperBuilder.detachLooper();
robertphillips@google.com41570852013-06-03 17:13:25 +0000231 }
232
233 typedef GM INHERITED;
234};
235
236const SkPoint MegaLooperGM::gBlurOffsets[4] = {
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000237 { kHalfSquareSize, kHalfSquareSize },
238 { -kHalfSquareSize, kHalfSquareSize },
239 { kHalfSquareSize, -kHalfSquareSize },
robertphillips@google.com41570852013-06-03 17:13:25 +0000240 { -kHalfSquareSize, -kHalfSquareSize }
241};
242
243const SkColor MegaLooperGM::gColors[4] = {
244 SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLUE, SK_ColorRED
245};
246
247DEF_GM( return new MegaLooperGM(MegaLooperGM::k0x0_Type); )
248DEF_GM( return new MegaLooperGM(MegaLooperGM::k4x1_Type); )
249DEF_GM( return new MegaLooperGM(MegaLooperGM::k1x4_Type); )