blob: e9d4476762b9c94c193f6d172a5a614d1847b4b7 [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
reed02f9ed72016-09-06 09:06:18 -070054 SkRect r0 = SkRect::MakeXYWH(r.left(), r.top(), r.width(), r.height()/2);
55
56 SkCanvas::SaveLayerRec rec;
57 rec.fPaint = nullptr;
58 rec.fBounds = &r0;
59 rec.fBackdrop = nullptr;
60 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
61 canvas->saveLayer(rec);
62 do_draw(canvas, r);
63 canvas->restore();
reed28d1ce52016-09-06 14:57:00 -070064
65 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -070066}
67
tomhudson@google.comef279d32011-12-21 14:27:14 +000068/** Draw a 2px border around the target, then red behind the target;
69 set the clip to match the target, then draw >> the target in blue.
70*/
71
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000072static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000073 SkPaint borderPaint;
74 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
75 borderPaint.setAntiAlias(true);
76 SkPaint backgroundPaint;
77 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
78 backgroundPaint.setAntiAlias(true);
79 SkPaint foregroundPaint;
80 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
81 foregroundPaint.setAntiAlias(true);
82
83 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000084 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
85 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000086 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000087 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000088 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -070089 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000090 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000091 canvas->drawRect(target, foregroundPaint);
92 canvas->restore();
93}
94
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000095static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000096 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
97 draw(canvas, target, x, y);
98}
99
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000100static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000101 SkRect target (SkRect::MakeWH(1 * 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_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000106 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
107 draw(canvas, target, x, y);
108}
109
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000110static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000111 draw_square(canvas, 10, 10);
112 draw_column(canvas, 30, 10);
113 draw_bar(canvas, 10, 30);
114}
115
116/**
117 Test a set of clipping problems discovered while writing blitAntiRect,
118 and test all the code paths through the clipping blitters.
119 Each region should show as a blue center surrounded by a 2px green
120 border, with no red.
121*/
122
reed@google.comff26b7e2013-05-23 17:04:19 +0000123class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000124public:
125 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000126
tomhudson@google.comef279d32011-12-21 14:27:14 +0000127 }
128
129protected:
mtklein36352bf2015-03-25 18:17:31 -0700130 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000131 return SkString("aaclip");
132 }
133
mtklein36352bf2015-03-25 18:17:31 -0700134 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000135 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000139 // Initial pixel-boundary-aligned draw
140 draw_rect_tests(canvas);
141
142 // Repeat 4x with .2, .4, .6, .8 px offsets
143 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000144 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000145 draw_rect_tests(canvas);
146
147 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000148 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000149 draw_rect_tests(canvas);
150
151 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000152 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000153 draw_rect_tests(canvas);
154
155 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000156 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000157 draw_rect_tests(canvas);
158 }
159
tomhudson@google.comef279d32011-12-21 14:27:14 +0000160private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000161 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000162};
163
halcanary385fe4d2015-08-26 13:07:48 -0700164DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000165
166/////////////////////////////////////////////////////////////////////////
167
168#ifdef SK_BUILD_FOR_MAC
169
Mike Reed5df49342016-11-12 08:06:55 -0600170static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000171 const SkImageInfo& info = bm.info();
172 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600173 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
174 (SkPMColor*)bm.getPixels(),
175 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000176 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600177 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000178 }
179}
180
181#include "SkCGUtils.h"
182static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
183 SkBitmap bm;
184 bm.allocPixels(info);
185
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000186 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);
Mike Reed5df49342016-11-12 08:06:55 -0600195 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000196 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);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000202 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500203 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
204
205 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000206}
207
208class CGImageGM : public skiagm::GM {
209public:
210 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000211
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000212protected:
mtklein36352bf2015-03-25 18:17:31 -0700213 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000214 return SkString("cgimage");
215 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000216
mtklein36352bf2015-03-25 18:17:31 -0700217 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000218 return SkISize::Make(800, 250);
219 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000220
mtklein36352bf2015-03-25 18:17:31 -0700221 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000222 const struct {
223 SkColorType fCT;
224 SkAlphaType fAT;
225 } rec[] = {
226 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
227
228 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
229 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
230 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
231
232 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
233 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
234 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
235 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000236
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000237 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
238 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
239 test_image(canvas, info);
240 canvas->translate(info.width() + 10, 0);
241 }
242 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000243
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000244private:
245 typedef skiagm::GM INHERITED;
246};
Mike Reed463c8482016-12-21 12:01:12 -0500247//DEF_GM( return new CGImageGM; )
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000248#endif
reed8f76cb92015-04-22 17:38:23 -0700249
250///////////////////////////////////////////////////////////////////////////////////////////////////
251
halcanary6950de62015-11-07 05:29:00 -0800252// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700253class ClipCubicGM : public skiagm::GM {
254 const SkScalar W = 100;
255 const SkScalar H = 240;
256
257 SkPath fVPath, fHPath;
258public:
259 ClipCubicGM() {
260 fVPath.moveTo(W, 0);
261 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700262
reed8f76cb92015-04-22 17:38:23 -0700263 SkMatrix pivot;
264 pivot.setRotate(90, W/2, H/2);
265 fVPath.transform(pivot, &fHPath);
266 }
267
268protected:
269 SkString onShortName() override {
270 return SkString("clipcubic");
271 }
halcanary9d524f22016-03-29 09:03:52 -0700272
reed8f76cb92015-04-22 17:38:23 -0700273 SkISize onISize() override {
274 return SkISize::Make(400, 410);
275 }
276
277 void doDraw(SkCanvas* canvas, const SkPath& path) {
278 SkPaint paint;
279 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700280
caryclark12596012015-07-29 05:27:47 -0700281 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed8f76cb92015-04-22 17:38:23 -0700282 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700283
reed8f76cb92015-04-22 17:38:23 -0700284 paint.setColor(SK_ColorRED);
285 paint.setStyle(SkPaint::kStroke_Style);
286 canvas->drawPath(path, paint);
287 }
288
289 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
290 SkAutoCanvasRestore acr(canvas, true);
291
292 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
293 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700294 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700295
296 canvas->drawRect(r, paint);
297 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700298
reed8f76cb92015-04-22 17:38:23 -0700299 canvas->translate(dx, dy);
300
301 canvas->drawRect(r, paint);
302 canvas->clipRect(r);
303 this->doDraw(canvas, path);
304 }
305
306 void onDraw(SkCanvas* canvas) override {
307 canvas->translate(80, 10);
308 this->drawAndClip(canvas, fVPath, 200, 0);
309 canvas->translate(0, 200);
310 this->drawAndClip(canvas, fHPath, 200, 0);
311 }
halcanary9d524f22016-03-29 09:03:52 -0700312
reed8f76cb92015-04-22 17:38:23 -0700313private:
314 typedef skiagm::GM INHERITED;
315};
halcanary385fe4d2015-08-26 13:07:48 -0700316DEF_GM(return new ClipCubicGM;)