blob: cb8418b6d30842baecfd601aac1e43a35f561404 [file] [log] [blame]
tomhudson@google.comef279d32011-12-21 14:27:14 +00001/*
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 "SkCanvas.h"
10#include "SkPath.h"
11
reed02f9ed72016-09-06 09:06:18 -070012static void do_draw(SkCanvas* canvas, const SkRect& r) {
13 SkPaint paint;
14 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
15 paint.setColor(0x800000FF);
16 canvas->drawRect(r, paint);
17}
18
19/**
20 * Exercise kDontClipToLayer_Legacy_SaveLayerFlag flag, which does not limit the clip to the
21 * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
22 * and/or draw onto the surrounding portions of the canvas, or both.
23 *
24 * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
25 * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
26 *
27 * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
28 * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
29 * has no paint, so it gets the default xfermode during restore).
30 *
reed28d1ce52016-09-06 14:57:00 -070031 * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
32 * "outer" layer we created (filled with red). This is a testing detail, so that our final
33 * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
reed02f9ed72016-09-06 09:06:18 -070034 *
reed28d1ce52016-09-06 14:57:00 -070035 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
36 * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
37 * the GM's white background.
38 *
39 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
40 * pixels, making magenta.
41 *
42 * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
43 * light blue 0xFF7F7FFF.
reed02f9ed72016-09-06 09:06:18 -070044 */
45DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
reed28d1ce52016-09-06 14:57:00 -070046 const SkRect r { 10, 10, 110, 110 };
47
48 // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
49 // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
50 canvas->saveLayer(&r, nullptr);
51 canvas->drawColor(SK_ColorRED);
52
reed02f9ed72016-09-06 09:06:18 -070053 SkRect r0 = SkRect::MakeXYWH(r.left(), r.top(), r.width(), r.height()/2);
54
55 SkCanvas::SaveLayerRec rec;
56 rec.fPaint = nullptr;
57 rec.fBounds = &r0;
58 rec.fBackdrop = nullptr;
59 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
60 canvas->saveLayer(rec);
61 do_draw(canvas, r);
62 canvas->restore();
reed28d1ce52016-09-06 14:57:00 -070063
64 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -070065}
66
tomhudson@google.comef279d32011-12-21 14:27:14 +000067/** Draw a 2px border around the target, then red behind the target;
68 set the clip to match the target, then draw >> the target in blue.
69*/
70
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000071static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000072 SkPaint borderPaint;
73 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
74 borderPaint.setAntiAlias(true);
75 SkPaint backgroundPaint;
76 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
77 backgroundPaint.setAntiAlias(true);
78 SkPaint foregroundPaint;
79 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
80 foregroundPaint.setAntiAlias(true);
81
82 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000083 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
84 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000085 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000086 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000087 canvas->drawRect(target, backgroundPaint);
88 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000089 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000090 canvas->drawRect(target, foregroundPaint);
91 canvas->restore();
92}
93
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000094static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000095 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
96 draw(canvas, target, x, y);
97}
98
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000099static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000100 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
101 draw(canvas, target, x, y);
102}
103
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000104static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000105 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
106 draw(canvas, target, x, y);
107}
108
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000109static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000110 draw_square(canvas, 10, 10);
111 draw_column(canvas, 30, 10);
112 draw_bar(canvas, 10, 30);
113}
114
115/**
116 Test a set of clipping problems discovered while writing blitAntiRect,
117 and test all the code paths through the clipping blitters.
118 Each region should show as a blue center surrounded by a 2px green
119 border, with no red.
120*/
121
reed@google.comff26b7e2013-05-23 17:04:19 +0000122class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000123public:
124 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000125
tomhudson@google.comef279d32011-12-21 14:27:14 +0000126 }
127
128protected:
mtklein36352bf2015-03-25 18:17:31 -0700129 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000130 return SkString("aaclip");
131 }
132
mtklein36352bf2015-03-25 18:17:31 -0700133 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000134 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000135 }
136
mtklein36352bf2015-03-25 18:17:31 -0700137 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000138 // Initial pixel-boundary-aligned draw
139 draw_rect_tests(canvas);
140
141 // Repeat 4x with .2, .4, .6, .8 px offsets
142 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000143 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000144 draw_rect_tests(canvas);
145
146 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000147 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000148 draw_rect_tests(canvas);
149
150 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000151 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000152 draw_rect_tests(canvas);
153
154 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000155 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000156 draw_rect_tests(canvas);
157 }
158
tomhudson@google.comef279d32011-12-21 14:27:14 +0000159private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000160 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000161};
162
halcanary385fe4d2015-08-26 13:07:48 -0700163DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000164
165/////////////////////////////////////////////////////////////////////////
166
167#ifdef SK_BUILD_FOR_MAC
168
169static SkCanvas* make_canvas(const SkBitmap& bm) {
170 const SkImageInfo& info = bm.info();
171 if (info.bytesPerPixel() == 4) {
172 return SkCanvas::NewRasterDirectN32(info.width(), info.height(),
173 (SkPMColor*)bm.getPixels(),
174 bm.rowBytes());
175 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700176 return new SkCanvas(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000177 }
178}
179
180#include "SkCGUtils.h"
181static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
182 SkBitmap bm;
183 bm.allocPixels(info);
184
185 SkAutoTUnref<SkCanvas> newc(make_canvas(bm));
186 if (info.isOpaque()) {
187 bm.eraseColor(SK_ColorGREEN);
188 } else {
189 bm.eraseColor(0);
190 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000191
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000192 SkPaint paint;
193 paint.setAntiAlias(true);
194 paint.setColor(SK_ColorBLUE);
195 newc->drawCircle(50, 50, 49, paint);
196 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000197
halcanary96fcdcc2015-08-27 07:41:13 -0700198 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000199
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000200 SkBitmap bm2;
201 SkCreateBitmapFromCGImage(&bm2, image);
202 CGImageRelease(image);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000203
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000204 canvas->drawBitmap(bm2, 10, 120);
205}
206
207class CGImageGM : public skiagm::GM {
208public:
209 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000210
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000211protected:
mtklein36352bf2015-03-25 18:17:31 -0700212 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000213 return SkString("cgimage");
214 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000215
mtklein36352bf2015-03-25 18:17:31 -0700216 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000217 return SkISize::Make(800, 250);
218 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000219
mtklein36352bf2015-03-25 18:17:31 -0700220 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000221 const struct {
222 SkColorType fCT;
223 SkAlphaType fAT;
224 } rec[] = {
225 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
226
227 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
228 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
229 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
230
231 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
232 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
233 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
234 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000235
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000236 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
237 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
238 test_image(canvas, info);
239 canvas->translate(info.width() + 10, 0);
240 }
241 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000242
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000243private:
244 typedef skiagm::GM INHERITED;
245};
246
bsalomon@google.com2a9e3ad2014-04-23 18:05:36 +0000247#if 0 // Disabled pending fix from reed@
halcanary385fe4d2015-08-26 13:07:48 -0700248DEF_GM( return new CGImageGM; )
bsalomon@google.com2a9e3ad2014-04-23 18:05:36 +0000249#endif
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000250#endif
reed8f76cb92015-04-22 17:38:23 -0700251
252///////////////////////////////////////////////////////////////////////////////////////////////////
253
halcanary6950de62015-11-07 05:29:00 -0800254// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700255class ClipCubicGM : public skiagm::GM {
256 const SkScalar W = 100;
257 const SkScalar H = 240;
258
259 SkPath fVPath, fHPath;
260public:
261 ClipCubicGM() {
262 fVPath.moveTo(W, 0);
263 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700264
reed8f76cb92015-04-22 17:38:23 -0700265 SkMatrix pivot;
266 pivot.setRotate(90, W/2, H/2);
267 fVPath.transform(pivot, &fHPath);
268 }
269
270protected:
271 SkString onShortName() override {
272 return SkString("clipcubic");
273 }
halcanary9d524f22016-03-29 09:03:52 -0700274
reed8f76cb92015-04-22 17:38:23 -0700275 SkISize onISize() override {
276 return SkISize::Make(400, 410);
277 }
278
279 void doDraw(SkCanvas* canvas, const SkPath& path) {
280 SkPaint paint;
281 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700282
caryclark12596012015-07-29 05:27:47 -0700283 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed8f76cb92015-04-22 17:38:23 -0700284 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700285
reed8f76cb92015-04-22 17:38:23 -0700286 paint.setColor(SK_ColorRED);
287 paint.setStyle(SkPaint::kStroke_Style);
288 canvas->drawPath(path, paint);
289 }
290
291 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
292 SkAutoCanvasRestore acr(canvas, true);
293
294 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
295 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700296 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700297
298 canvas->drawRect(r, paint);
299 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700300
reed8f76cb92015-04-22 17:38:23 -0700301 canvas->translate(dx, dy);
302
303 canvas->drawRect(r, paint);
304 canvas->clipRect(r);
305 this->doDraw(canvas, path);
306 }
307
308 void onDraw(SkCanvas* canvas) override {
309 canvas->translate(80, 10);
310 this->drawAndClip(canvas, fVPath, 200, 0);
311 canvas->translate(0, 200);
312 this->drawAndClip(canvas, fHPath, 200, 0);
313 }
halcanary9d524f22016-03-29 09:03:52 -0700314
reed8f76cb92015-04-22 17:38:23 -0700315private:
316 typedef skiagm::GM INHERITED;
317};
halcanary385fe4d2015-08-26 13:07:48 -0700318DEF_GM(return new ClipCubicGM;)