blob: 1f7886c0cb5dedafc0ba64147837ae802546caed [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Cary Clark7eddfb82018-03-13 14:41:10 -040010#include "SkCanvasPriv.h"
tomhudson@google.comef279d32011-12-21 14:27:14 +000011#include "SkPath.h"
Mike Reed5df49342016-11-12 08:06:55 -060012#include "SkMakeUnique.h"
tomhudson@google.comef279d32011-12-21 14:27:14 +000013
reed02f9ed72016-09-06 09:06:18 -070014static void do_draw(SkCanvas* canvas, const SkRect& r) {
15 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070016 paint.setBlendMode(SkBlendMode::kSrc);
reed02f9ed72016-09-06 09:06:18 -070017 paint.setColor(0x800000FF);
18 canvas->drawRect(r, paint);
19}
20
21/**
Cary Clark7eddfb82018-03-13 14:41:10 -040022 * Exercise SkCanvasPriv::kDontClipToLayer_SaveLayerFlag flag, which does not limit the clip to the
reed02f9ed72016-09-06 09:06:18 -070023 * 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 *
reed28d1ce52016-09-06 14:57:00 -070033 * 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.
reed02f9ed72016-09-06 09:06:18 -070036 *
reed28d1ce52016-09-06 14:57:00 -070037 * 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.
reed02f9ed72016-09-06 09:06:18 -070046 */
47DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
reed28d1ce52016-09-06 14:57:00 -070048 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
Mike Reed8a8937c2017-02-14 10:12:34 -050055 SkRect r0 = { 20, 20, 100, 55 };
56 SkRect r1 = { 20, 65, 100, 100 };
reed02f9ed72016-09-06 09:06:18 -070057
58 SkCanvas::SaveLayerRec rec;
59 rec.fPaint = nullptr;
60 rec.fBounds = &r0;
61 rec.fBackdrop = nullptr;
Cary Clark7eddfb82018-03-13 14:41:10 -040062 rec.fSaveLayerFlags = SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
reed02f9ed72016-09-06 09:06:18 -070063 canvas->saveLayer(rec);
Mike Reed8a8937c2017-02-14 10:12:34 -050064 rec.fBounds = &r1;
65 canvas->saveLayer(rec);
reed02f9ed72016-09-06 09:06:18 -070066 do_draw(canvas, r);
67 canvas->restore();
Matt Sarett25d82962017-04-24 13:59:54 -040068 canvas->restore();
reed28d1ce52016-09-06 14:57:00 -070069
70 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -070071}
72
tomhudson@google.comef279d32011-12-21 14:27:14 +000073/** 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
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000077static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000078 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();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000089 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
90 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000091 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000092 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000093 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -070094 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000095 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000096 canvas->drawRect(target, foregroundPaint);
97 canvas->restore();
98}
99
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000100static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000101 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
102 draw(canvas, target, x, y);
103}
104
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000105static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000106 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
107 draw(canvas, target, x, y);
108}
109
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000110static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000111 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
112 draw(canvas, target, x, y);
113}
114
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000115static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000116 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
reed@google.comff26b7e2013-05-23 17:04:19 +0000128class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000129public:
130 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000131
tomhudson@google.comef279d32011-12-21 14:27:14 +0000132 }
133
134protected:
mtklein36352bf2015-03-25 18:17:31 -0700135 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000136 return SkString("aaclip");
137 }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000140 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000141 }
142
mtklein36352bf2015-03-25 18:17:31 -0700143 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000144 // Initial pixel-boundary-aligned draw
145 draw_rect_tests(canvas);
146
147 // Repeat 4x with .2, .4, .6, .8 px offsets
148 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000149 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000150 draw_rect_tests(canvas);
151
152 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000153 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000154 draw_rect_tests(canvas);
155
156 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000157 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000158 draw_rect_tests(canvas);
159
160 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000161 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000162 draw_rect_tests(canvas);
163 }
164
tomhudson@google.comef279d32011-12-21 14:27:14 +0000165private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000166 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000167};
168
halcanary385fe4d2015-08-26 13:07:48 -0700169DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000170
171/////////////////////////////////////////////////////////////////////////
172
173#ifdef SK_BUILD_FOR_MAC
174
Mike Reed5df49342016-11-12 08:06:55 -0600175static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000176 const SkImageInfo& info = bm.info();
177 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600178 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
179 (SkPMColor*)bm.getPixels(),
180 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000181 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600182 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000183 }
184}
185
186#include "SkCGUtils.h"
187static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
188 SkBitmap bm;
189 bm.allocPixels(info);
190
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000191 if (info.isOpaque()) {
192 bm.eraseColor(SK_ColorGREEN);
193 } else {
194 bm.eraseColor(0);
195 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000196
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000197 SkPaint paint;
198 paint.setAntiAlias(true);
199 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600200 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000201 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000202
halcanary96fcdcc2015-08-27 07:41:13 -0700203 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000204
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000205 SkBitmap bm2;
206 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000207 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500208 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
209
210 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000211}
212
213class CGImageGM : public skiagm::GM {
214public:
215 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000216
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000217protected:
mtklein36352bf2015-03-25 18:17:31 -0700218 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000219 return SkString("cgimage");
220 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000221
mtklein36352bf2015-03-25 18:17:31 -0700222 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000223 return SkISize::Make(800, 250);
224 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000225
mtklein36352bf2015-03-25 18:17:31 -0700226 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000227 const struct {
228 SkColorType fCT;
229 SkAlphaType fAT;
230 } rec[] = {
231 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
232
233 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
234 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
235 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
236
237 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
238 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
239 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
240 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000241
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000242 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
243 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
244 test_image(canvas, info);
245 canvas->translate(info.width() + 10, 0);
246 }
247 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000248
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000249private:
250 typedef skiagm::GM INHERITED;
251};
Mike Reed463c8482016-12-21 12:01:12 -0500252//DEF_GM( return new CGImageGM; )
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000253#endif
reed8f76cb92015-04-22 17:38:23 -0700254
255///////////////////////////////////////////////////////////////////////////////////////////////////
256
halcanary6950de62015-11-07 05:29:00 -0800257// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700258class ClipCubicGM : public skiagm::GM {
259 const SkScalar W = 100;
260 const SkScalar H = 240;
261
262 SkPath fVPath, fHPath;
263public:
264 ClipCubicGM() {
265 fVPath.moveTo(W, 0);
266 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700267
reed8f76cb92015-04-22 17:38:23 -0700268 SkMatrix pivot;
269 pivot.setRotate(90, W/2, H/2);
270 fVPath.transform(pivot, &fHPath);
271 }
272
273protected:
274 SkString onShortName() override {
275 return SkString("clipcubic");
276 }
halcanary9d524f22016-03-29 09:03:52 -0700277
reed8f76cb92015-04-22 17:38:23 -0700278 SkISize onISize() override {
279 return SkISize::Make(400, 410);
280 }
281
282 void doDraw(SkCanvas* canvas, const SkPath& path) {
283 SkPaint paint;
284 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700285
Mike Kleind46dce32018-08-16 10:17:03 -0400286 paint.setColor(0xFFCCCCCC);
reed8f76cb92015-04-22 17:38:23 -0700287 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700288
reed8f76cb92015-04-22 17:38:23 -0700289 paint.setColor(SK_ColorRED);
290 paint.setStyle(SkPaint::kStroke_Style);
291 canvas->drawPath(path, paint);
292 }
293
294 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
295 SkAutoCanvasRestore acr(canvas, true);
296
297 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
298 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700299 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700300
301 canvas->drawRect(r, paint);
302 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700303
reed8f76cb92015-04-22 17:38:23 -0700304 canvas->translate(dx, dy);
305
306 canvas->drawRect(r, paint);
307 canvas->clipRect(r);
308 this->doDraw(canvas, path);
309 }
310
311 void onDraw(SkCanvas* canvas) override {
312 canvas->translate(80, 10);
313 this->drawAndClip(canvas, fVPath, 200, 0);
314 canvas->translate(0, 200);
315 this->drawAndClip(canvas, fHPath, 200, 0);
316 }
halcanary9d524f22016-03-29 09:03:52 -0700317
reed8f76cb92015-04-22 17:38:23 -0700318private:
319 typedef skiagm::GM INHERITED;
320};
halcanary385fe4d2015-08-26 13:07:48 -0700321DEF_GM(return new ClipCubicGM;)