blob: d2c5ff54c66fa72faf5976a3b5b5ea79218272c9 [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"
Mike Reed5df49342016-11-12 08:06:55 -060011#include "SkMakeUnique.h"
tomhudson@google.comef279d32011-12-21 14:27:14 +000012
reed02f9ed72016-09-06 09:06:18 -070013static void do_draw(SkCanvas* canvas, const SkRect& r) {
14 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070015 paint.setBlendMode(SkBlendMode::kSrc);
reed02f9ed72016-09-06 09:06:18 -070016 paint.setColor(0x800000FF);
17 canvas->drawRect(r, paint);
18}
19
20/**
21 * Exercise kDontClipToLayer_Legacy_SaveLayerFlag flag, which does not limit the clip to the
22 * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
23 * and/or draw onto the surrounding portions of the canvas, or both.
24 *
25 * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
26 * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
27 *
28 * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
29 * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
30 * has no paint, so it gets the default xfermode during restore).
31 *
reed28d1ce52016-09-06 14:57:00 -070032 * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
33 * "outer" layer we created (filled with red). This is a testing detail, so that our final
34 * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
reed02f9ed72016-09-06 09:06:18 -070035 *
reed28d1ce52016-09-06 14:57:00 -070036 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
37 * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
38 * the GM's white background.
39 *
40 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
41 * pixels, making magenta.
42 *
43 * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
44 * light blue 0xFF7F7FFF.
reed02f9ed72016-09-06 09:06:18 -070045 */
46DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
reed28d1ce52016-09-06 14:57:00 -070047 const SkRect r { 10, 10, 110, 110 };
48
49 // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
50 // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
51 canvas->saveLayer(&r, nullptr);
52 canvas->drawColor(SK_ColorRED);
53
Mike Reed8a8937c2017-02-14 10:12:34 -050054 SkRect r0 = { 20, 20, 100, 55 };
55 SkRect r1 = { 20, 65, 100, 100 };
reed02f9ed72016-09-06 09:06:18 -070056
57 SkCanvas::SaveLayerRec rec;
58 rec.fPaint = nullptr;
59 rec.fBounds = &r0;
60 rec.fBackdrop = nullptr;
61 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
62 canvas->saveLayer(rec);
Mike Reed8a8937c2017-02-14 10:12:34 -050063 rec.fBounds = &r1;
64 canvas->saveLayer(rec);
reed02f9ed72016-09-06 09:06:18 -070065 do_draw(canvas, r);
66 canvas->restore();
reed28d1ce52016-09-06 14:57:00 -070067
68 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -070069}
70
tomhudson@google.comef279d32011-12-21 14:27:14 +000071/** Draw a 2px border around the target, then red behind the target;
72 set the clip to match the target, then draw >> the target in blue.
73*/
74
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000075static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000076 SkPaint borderPaint;
77 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
78 borderPaint.setAntiAlias(true);
79 SkPaint backgroundPaint;
80 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
81 backgroundPaint.setAntiAlias(true);
82 SkPaint foregroundPaint;
83 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
84 foregroundPaint.setAntiAlias(true);
85
86 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000087 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
88 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000089 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000090 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000091 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -070092 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000093 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000094 canvas->drawRect(target, foregroundPaint);
95 canvas->restore();
96}
97
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000098static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000099 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
100 draw(canvas, target, x, y);
101}
102
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000103static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000104 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
105 draw(canvas, target, x, y);
106}
107
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000108static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000109 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
110 draw(canvas, target, x, y);
111}
112
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000113static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000114 draw_square(canvas, 10, 10);
115 draw_column(canvas, 30, 10);
116 draw_bar(canvas, 10, 30);
117}
118
119/**
120 Test a set of clipping problems discovered while writing blitAntiRect,
121 and test all the code paths through the clipping blitters.
122 Each region should show as a blue center surrounded by a 2px green
123 border, with no red.
124*/
125
reed@google.comff26b7e2013-05-23 17:04:19 +0000126class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000127public:
128 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000129
tomhudson@google.comef279d32011-12-21 14:27:14 +0000130 }
131
132protected:
mtklein36352bf2015-03-25 18:17:31 -0700133 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000134 return SkString("aaclip");
135 }
136
mtklein36352bf2015-03-25 18:17:31 -0700137 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000138 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000139 }
140
mtklein36352bf2015-03-25 18:17:31 -0700141 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000142 // Initial pixel-boundary-aligned draw
143 draw_rect_tests(canvas);
144
145 // Repeat 4x with .2, .4, .6, .8 px offsets
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 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000159 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000160 draw_rect_tests(canvas);
161 }
162
tomhudson@google.comef279d32011-12-21 14:27:14 +0000163private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000164 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000165};
166
halcanary385fe4d2015-08-26 13:07:48 -0700167DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000168
169/////////////////////////////////////////////////////////////////////////
170
171#ifdef SK_BUILD_FOR_MAC
172
Mike Reed5df49342016-11-12 08:06:55 -0600173static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000174 const SkImageInfo& info = bm.info();
175 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600176 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
177 (SkPMColor*)bm.getPixels(),
178 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000179 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600180 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000181 }
182}
183
184#include "SkCGUtils.h"
185static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
186 SkBitmap bm;
187 bm.allocPixels(info);
188
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000189 if (info.isOpaque()) {
190 bm.eraseColor(SK_ColorGREEN);
191 } else {
192 bm.eraseColor(0);
193 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000194
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000195 SkPaint paint;
196 paint.setAntiAlias(true);
197 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600198 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000199 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000200
halcanary96fcdcc2015-08-27 07:41:13 -0700201 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000202
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000203 SkBitmap bm2;
204 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000205 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500206 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
207
208 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000209}
210
211class CGImageGM : public skiagm::GM {
212public:
213 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000214
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000215protected:
mtklein36352bf2015-03-25 18:17:31 -0700216 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000217 return SkString("cgimage");
218 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000219
mtklein36352bf2015-03-25 18:17:31 -0700220 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000221 return SkISize::Make(800, 250);
222 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000223
mtklein36352bf2015-03-25 18:17:31 -0700224 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000225 const struct {
226 SkColorType fCT;
227 SkAlphaType fAT;
228 } rec[] = {
229 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
230
231 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
232 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
233 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
234
235 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
236 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
237 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
238 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000239
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000240 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
241 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
242 test_image(canvas, info);
243 canvas->translate(info.width() + 10, 0);
244 }
245 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000246
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000247private:
248 typedef skiagm::GM INHERITED;
249};
Mike Reed463c8482016-12-21 12:01:12 -0500250//DEF_GM( return new CGImageGM; )
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000251#endif
reed8f76cb92015-04-22 17:38:23 -0700252
253///////////////////////////////////////////////////////////////////////////////////////////////////
254
halcanary6950de62015-11-07 05:29:00 -0800255// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700256class ClipCubicGM : public skiagm::GM {
257 const SkScalar W = 100;
258 const SkScalar H = 240;
259
260 SkPath fVPath, fHPath;
261public:
262 ClipCubicGM() {
263 fVPath.moveTo(W, 0);
264 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700265
reed8f76cb92015-04-22 17:38:23 -0700266 SkMatrix pivot;
267 pivot.setRotate(90, W/2, H/2);
268 fVPath.transform(pivot, &fHPath);
269 }
270
271protected:
272 SkString onShortName() override {
273 return SkString("clipcubic");
274 }
halcanary9d524f22016-03-29 09:03:52 -0700275
reed8f76cb92015-04-22 17:38:23 -0700276 SkISize onISize() override {
277 return SkISize::Make(400, 410);
278 }
279
280 void doDraw(SkCanvas* canvas, const SkPath& path) {
281 SkPaint paint;
282 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700283
caryclark12596012015-07-29 05:27:47 -0700284 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed8f76cb92015-04-22 17:38:23 -0700285 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700286
reed8f76cb92015-04-22 17:38:23 -0700287 paint.setColor(SK_ColorRED);
288 paint.setStyle(SkPaint::kStroke_Style);
289 canvas->drawPath(path, paint);
290 }
291
292 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
293 SkAutoCanvasRestore acr(canvas, true);
294
295 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
296 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700297 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700298
299 canvas->drawRect(r, paint);
300 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700301
reed8f76cb92015-04-22 17:38:23 -0700302 canvas->translate(dx, dy);
303
304 canvas->drawRect(r, paint);
305 canvas->clipRect(r);
306 this->doDraw(canvas, path);
307 }
308
309 void onDraw(SkCanvas* canvas) override {
310 canvas->translate(80, 10);
311 this->drawAndClip(canvas, fVPath, 200, 0);
312 canvas->translate(0, 200);
313 this->drawAndClip(canvas, fHPath, 200, 0);
314 }
halcanary9d524f22016-03-29 09:03:52 -0700315
reed8f76cb92015-04-22 17:38:23 -0700316private:
317 typedef skiagm::GM INHERITED;
318};
halcanary385fe4d2015-08-26 13:07:48 -0700319DEF_GM(return new ClipCubicGM;)