blob: e475c74e06847c262009913129413780b7232ca2 [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*/
Hal Canary607c44f2019-01-23 10:40:02 -0500127DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000128 // 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);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000133 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000134 draw_rect_tests(canvas);
135
136 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000137 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000138 draw_rect_tests(canvas);
139
140 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000141 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000142 draw_rect_tests(canvas);
143
144 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000145 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000146 draw_rect_tests(canvas);
Hal Canary607c44f2019-01-23 10:40:02 -0500147}
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000148
149/////////////////////////////////////////////////////////////////////////
150
151#ifdef SK_BUILD_FOR_MAC
152
Mike Reed5df49342016-11-12 08:06:55 -0600153static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000154 const SkImageInfo& info = bm.info();
155 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600156 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
157 (SkPMColor*)bm.getPixels(),
158 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000159 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600160 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000161 }
162}
163
164#include "SkCGUtils.h"
165static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
166 SkBitmap bm;
167 bm.allocPixels(info);
168
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000169 if (info.isOpaque()) {
170 bm.eraseColor(SK_ColorGREEN);
171 } else {
172 bm.eraseColor(0);
173 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000174
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000175 SkPaint paint;
176 paint.setAntiAlias(true);
177 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600178 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000179 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000180
halcanary96fcdcc2015-08-27 07:41:13 -0700181 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000182
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000183 SkBitmap bm2;
184 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000185 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500186 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
187
188 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000189}
190
Hal Canary607c44f2019-01-23 10:40:02 -0500191DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000192 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 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000206
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000207 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 }
Hal Canary607c44f2019-01-23 10:40:02 -0500212}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000213
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000214#endif
reed8f76cb92015-04-22 17:38:23 -0700215
216///////////////////////////////////////////////////////////////////////////////////////////////////
217
halcanary6950de62015-11-07 05:29:00 -0800218// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700219class ClipCubicGM : public skiagm::GM {
220 const SkScalar W = 100;
221 const SkScalar H = 240;
222
223 SkPath fVPath, fHPath;
224public:
225 ClipCubicGM() {
226 fVPath.moveTo(W, 0);
227 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700228
reed8f76cb92015-04-22 17:38:23 -0700229 SkMatrix pivot;
230 pivot.setRotate(90, W/2, H/2);
231 fVPath.transform(pivot, &fHPath);
232 }
233
234protected:
235 SkString onShortName() override {
236 return SkString("clipcubic");
237 }
halcanary9d524f22016-03-29 09:03:52 -0700238
reed8f76cb92015-04-22 17:38:23 -0700239 SkISize onISize() override {
240 return SkISize::Make(400, 410);
241 }
242
243 void doDraw(SkCanvas* canvas, const SkPath& path) {
244 SkPaint paint;
245 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700246
Mike Kleind46dce32018-08-16 10:17:03 -0400247 paint.setColor(0xFFCCCCCC);
reed8f76cb92015-04-22 17:38:23 -0700248 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700249
reed8f76cb92015-04-22 17:38:23 -0700250 paint.setColor(SK_ColorRED);
251 paint.setStyle(SkPaint::kStroke_Style);
252 canvas->drawPath(path, paint);
253 }
254
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;
caryclark12596012015-07-29 05:27:47 -0700260 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700261
262 canvas->drawRect(r, paint);
263 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700264
reed8f76cb92015-04-22 17:38:23 -0700265 canvas->translate(dx, dy);
266
267 canvas->drawRect(r, paint);
268 canvas->clipRect(r);
269 this->doDraw(canvas, path);
270 }
271
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 }
halcanary9d524f22016-03-29 09:03:52 -0700278
reed8f76cb92015-04-22 17:38:23 -0700279private:
280 typedef skiagm::GM INHERITED;
281};
halcanary385fe4d2015-08-26 13:07:48 -0700282DEF_GM(return new ClipCubicGM;)