1 /*
2 * Copyright 2011 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"
9 #include "sk_tool_utils.h"
10 #include "SkCanvasPriv.h"
11 #include "SkPath.h"
12 #include "SkMakeUnique.h"
13
do_draw(SkCanvas * canvas,const SkRect & r)14 static void do_draw(SkCanvas* canvas, const SkRect& r) {
15 SkPaint paint;
16 paint.setBlendMode(SkBlendMode::kSrc);
17 paint.setColor(0x800000FF);
18 canvas->drawRect(r, paint);
19 }
20
21 /**
22 * Exercise SkCanvasPriv::kDontClipToLayer_SaveLayerFlag flag, which does not limit the clip to the
23 * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
24 * and/or draw onto the surrounding portions of the canvas, or both.
25 *
26 * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
27 * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
28 *
29 * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
30 * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
31 * has no paint, so it gets the default xfermode during restore).
32 *
33 * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
34 * "outer" layer we created (filled with red). This is a testing detail, so that our final
35 * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
36 *
37 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
38 * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
39 * the GM's white background.
40 *
41 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
42 * pixels, making magenta.
43 *
44 * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
45 * light blue 0xFF7F7FFF.
46 */
47 DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
48 const SkRect r { 10, 10, 110, 110 };
49
50 // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
51 // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
52 canvas->saveLayer(&r, nullptr);
53 canvas->drawColor(SK_ColorRED);
54
55 SkRect r0 = { 20, 20, 100, 55 };
56 SkRect r1 = { 20, 65, 100, 100 };
57
58 SkCanvas::SaveLayerRec rec;
59 rec.fPaint = nullptr;
60 rec.fBounds = &r0;
61 rec.fBackdrop = nullptr;
62 rec.fSaveLayerFlags = SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
63 canvas->saveLayer(rec);
64 rec.fBounds = &r1;
65 canvas->saveLayer(rec);
66 do_draw(canvas, r);
67 canvas->restore();
68 canvas->restore();
69
70 canvas->restore(); // red-layer
71 }
72
73 /** Draw a 2px border around the target, then red behind the target;
74 set the clip to match the target, then draw >> the target in blue.
75 */
76
draw(SkCanvas * canvas,SkRect & target,int x,int y)77 static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
78 SkPaint borderPaint;
79 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
80 borderPaint.setAntiAlias(true);
81 SkPaint backgroundPaint;
82 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
83 backgroundPaint.setAntiAlias(true);
84 SkPaint foregroundPaint;
85 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
86 foregroundPaint.setAntiAlias(true);
87
88 canvas->save();
89 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
90 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
91 canvas->drawRect(target, borderPaint);
92 target.inset(SkIntToScalar(2), SkIntToScalar(2));
93 canvas->drawRect(target, backgroundPaint);
94 canvas->clipRect(target, true);
95 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
96 canvas->drawRect(target, foregroundPaint);
97 canvas->restore();
98 }
99
draw_square(SkCanvas * canvas,int x,int y)100 static void draw_square(SkCanvas* canvas, int x, int y) {
101 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
102 draw(canvas, target, x, y);
103 }
104
draw_column(SkCanvas * canvas,int x,int y)105 static void draw_column(SkCanvas* canvas, int x, int y) {
106 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
107 draw(canvas, target, x, y);
108 }
109
draw_bar(SkCanvas * canvas,int x,int y)110 static void draw_bar(SkCanvas* canvas, int x, int y) {
111 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
112 draw(canvas, target, x, y);
113 }
114
draw_rect_tests(SkCanvas * canvas)115 static void draw_rect_tests(SkCanvas* canvas) {
116 draw_square(canvas, 10, 10);
117 draw_column(canvas, 30, 10);
118 draw_bar(canvas, 10, 30);
119 }
120
121 /**
122 Test a set of clipping problems discovered while writing blitAntiRect,
123 and test all the code paths through the clipping blitters.
124 Each region should show as a blue center surrounded by a 2px green
125 border, with no red.
126 */
127 DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
128 // Initial pixel-boundary-aligned draw
129 draw_rect_tests(canvas);
130
131 // Repeat 4x with .2, .4, .6, .8 px offsets
132 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
133 canvas->translate(SkIntToScalar(50), 0);
134 draw_rect_tests(canvas);
135
136 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
137 canvas->translate(SkIntToScalar(50), 0);
138 draw_rect_tests(canvas);
139
140 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
141 canvas->translate(SkIntToScalar(50), 0);
142 draw_rect_tests(canvas);
143
144 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
145 canvas->translate(SkIntToScalar(50), 0);
146 draw_rect_tests(canvas);
147 }
148
149 /////////////////////////////////////////////////////////////////////////
150
151 #ifdef SK_BUILD_FOR_MAC
152
make_canvas(const SkBitmap & bm)153 static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
154 const SkImageInfo& info = bm.info();
155 if (info.bytesPerPixel() == 4) {
156 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
157 (SkPMColor*)bm.getPixels(),
158 bm.rowBytes());
159 } else {
160 return skstd::make_unique<SkCanvas>(bm);
161 }
162 }
163
164 #include "SkCGUtils.h"
test_image(SkCanvas * canvas,const SkImageInfo & info)165 static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
166 SkBitmap bm;
167 bm.allocPixels(info);
168
169 if (info.isOpaque()) {
170 bm.eraseColor(SK_ColorGREEN);
171 } else {
172 bm.eraseColor(0);
173 }
174
175 SkPaint paint;
176 paint.setAntiAlias(true);
177 paint.setColor(SK_ColorBLUE);
178 make_canvas(bm)->drawCircle(50, 50, 49, paint);
179 canvas->drawBitmap(bm, 10, 10);
180
181 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
182
183 SkBitmap bm2;
184 SkCreateBitmapFromCGImage(&bm2, image);
185 canvas->drawBitmap(bm2, 10, 120);
186 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
187
188 CGImageRelease(image);
189 }
190
191 DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
192 const struct {
193 SkColorType fCT;
194 SkAlphaType fAT;
195 } rec[] = {
196 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
197
198 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
199 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
200 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
201
202 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
203 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
204 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
205 };
206
207 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
208 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
209 test_image(canvas, info);
210 canvas->translate(info.width() + 10, 0);
211 }
212 }
213
214 #endif
215
216 ///////////////////////////////////////////////////////////////////////////////////////////////////
217
218 // https://bug.skia.org/3716
219 class ClipCubicGM : public skiagm::GM {
220 const SkScalar W = 100;
221 const SkScalar H = 240;
222
223 SkPath fVPath, fHPath;
224 public:
ClipCubicGM()225 ClipCubicGM() {
226 fVPath.moveTo(W, 0);
227 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
228
229 SkMatrix pivot;
230 pivot.setRotate(90, W/2, H/2);
231 fVPath.transform(pivot, &fHPath);
232 }
233
234 protected:
onShortName()235 SkString onShortName() override {
236 return SkString("clipcubic");
237 }
238
onISize()239 SkISize onISize() override {
240 return SkISize::Make(400, 410);
241 }
242
doDraw(SkCanvas * canvas,const SkPath & path)243 void doDraw(SkCanvas* canvas, const SkPath& path) {
244 SkPaint paint;
245 paint.setAntiAlias(true);
246
247 paint.setColor(0xFFCCCCCC);
248 canvas->drawPath(path, paint);
249
250 paint.setColor(SK_ColorRED);
251 paint.setStyle(SkPaint::kStroke_Style);
252 canvas->drawPath(path, paint);
253 }
254
drawAndClip(SkCanvas * canvas,const SkPath & path,SkScalar dx,SkScalar dy)255 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
256 SkAutoCanvasRestore acr(canvas, true);
257
258 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
259 SkPaint paint;
260 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
261
262 canvas->drawRect(r, paint);
263 this->doDraw(canvas, path);
264
265 canvas->translate(dx, dy);
266
267 canvas->drawRect(r, paint);
268 canvas->clipRect(r);
269 this->doDraw(canvas, path);
270 }
271
onDraw(SkCanvas * canvas)272 void onDraw(SkCanvas* canvas) override {
273 canvas->translate(80, 10);
274 this->drawAndClip(canvas, fVPath, 200, 0);
275 canvas->translate(0, 200);
276 this->drawAndClip(canvas, fHPath, 200, 0);
277 }
278
279 private:
280 typedef skiagm::GM INHERITED;
281 };
282 DEF_GM(return new ClipCubicGM;)
283