blob: 9b341d375be6fb655b73ceb13005051a9e599b12 [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 *
31 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
32 * it will wrote 0x80 to the canvas' alpha, making it appear darker when shown in the window.
33 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas, so
34 * they will appear lighter (since the canvas was erased to white initially).
35 *
36 * Thus the expected result is the upper half to be light-blue w/ 0xFF for its alpha, and
37 * the lower half to be darker blue with 0x80 for its alpha.
38 */
39DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
40 SkRect r { 10, 10, 110, 110 };
41 SkRect r0 = SkRect::MakeXYWH(r.left(), r.top(), r.width(), r.height()/2);
42
43 SkCanvas::SaveLayerRec rec;
44 rec.fPaint = nullptr;
45 rec.fBounds = &r0;
46 rec.fBackdrop = nullptr;
47 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
48 canvas->saveLayer(rec);
49 do_draw(canvas, r);
50 canvas->restore();
51}
52
tomhudson@google.comef279d32011-12-21 14:27:14 +000053/** Draw a 2px border around the target, then red behind the target;
54 set the clip to match the target, then draw >> the target in blue.
55*/
56
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000057static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000058 SkPaint borderPaint;
59 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
60 borderPaint.setAntiAlias(true);
61 SkPaint backgroundPaint;
62 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
63 backgroundPaint.setAntiAlias(true);
64 SkPaint foregroundPaint;
65 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
66 foregroundPaint.setAntiAlias(true);
67
68 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000069 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
70 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000071 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000072 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000073 canvas->drawRect(target, backgroundPaint);
74 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000075 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000076 canvas->drawRect(target, foregroundPaint);
77 canvas->restore();
78}
79
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000080static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000081 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
82 draw(canvas, target, x, y);
83}
84
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000085static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000086 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
87 draw(canvas, target, x, y);
88}
89
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000090static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000091 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
92 draw(canvas, target, x, y);
93}
94
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000095static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000096 draw_square(canvas, 10, 10);
97 draw_column(canvas, 30, 10);
98 draw_bar(canvas, 10, 30);
99}
100
101/**
102 Test a set of clipping problems discovered while writing blitAntiRect,
103 and test all the code paths through the clipping blitters.
104 Each region should show as a blue center surrounded by a 2px green
105 border, with no red.
106*/
107
reed@google.comff26b7e2013-05-23 17:04:19 +0000108class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000109public:
110 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000111
tomhudson@google.comef279d32011-12-21 14:27:14 +0000112 }
113
114protected:
mtklein36352bf2015-03-25 18:17:31 -0700115 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000116 return SkString("aaclip");
117 }
118
mtklein36352bf2015-03-25 18:17:31 -0700119 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000120 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000121 }
122
mtklein36352bf2015-03-25 18:17:31 -0700123 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000124 // Initial pixel-boundary-aligned draw
125 draw_rect_tests(canvas);
126
127 // Repeat 4x with .2, .4, .6, .8 px offsets
128 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000129 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000130 draw_rect_tests(canvas);
131
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
tomhudson@google.comef279d32011-12-21 14:27:14 +0000145private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000146 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000147};
148
halcanary385fe4d2015-08-26 13:07:48 -0700149DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000150
151/////////////////////////////////////////////////////////////////////////
152
153#ifdef SK_BUILD_FOR_MAC
154
155static SkCanvas* make_canvas(const SkBitmap& bm) {
156 const SkImageInfo& info = bm.info();
157 if (info.bytesPerPixel() == 4) {
158 return SkCanvas::NewRasterDirectN32(info.width(), info.height(),
159 (SkPMColor*)bm.getPixels(),
160 bm.rowBytes());
161 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700162 return new SkCanvas(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000163 }
164}
165
166#include "SkCGUtils.h"
167static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
168 SkBitmap bm;
169 bm.allocPixels(info);
170
171 SkAutoTUnref<SkCanvas> newc(make_canvas(bm));
172 if (info.isOpaque()) {
173 bm.eraseColor(SK_ColorGREEN);
174 } else {
175 bm.eraseColor(0);
176 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000177
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000178 SkPaint paint;
179 paint.setAntiAlias(true);
180 paint.setColor(SK_ColorBLUE);
181 newc->drawCircle(50, 50, 49, paint);
182 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000183
halcanary96fcdcc2015-08-27 07:41:13 -0700184 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000185
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000186 SkBitmap bm2;
187 SkCreateBitmapFromCGImage(&bm2, image);
188 CGImageRelease(image);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000189
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000190 canvas->drawBitmap(bm2, 10, 120);
191}
192
193class CGImageGM : public skiagm::GM {
194public:
195 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000196
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000197protected:
mtklein36352bf2015-03-25 18:17:31 -0700198 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000199 return SkString("cgimage");
200 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000201
mtklein36352bf2015-03-25 18:17:31 -0700202 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000203 return SkISize::Make(800, 250);
204 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000205
mtklein36352bf2015-03-25 18:17:31 -0700206 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000207 const struct {
208 SkColorType fCT;
209 SkAlphaType fAT;
210 } rec[] = {
211 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
212
213 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
214 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
215 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
216
217 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
218 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
219 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
220 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000221
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000222 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
223 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
224 test_image(canvas, info);
225 canvas->translate(info.width() + 10, 0);
226 }
227 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000228
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000229private:
230 typedef skiagm::GM INHERITED;
231};
232
bsalomon@google.com2a9e3ad2014-04-23 18:05:36 +0000233#if 0 // Disabled pending fix from reed@
halcanary385fe4d2015-08-26 13:07:48 -0700234DEF_GM( return new CGImageGM; )
bsalomon@google.com2a9e3ad2014-04-23 18:05:36 +0000235#endif
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000236#endif
reed8f76cb92015-04-22 17:38:23 -0700237
238///////////////////////////////////////////////////////////////////////////////////////////////////
239
halcanary6950de62015-11-07 05:29:00 -0800240// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700241class ClipCubicGM : public skiagm::GM {
242 const SkScalar W = 100;
243 const SkScalar H = 240;
244
245 SkPath fVPath, fHPath;
246public:
247 ClipCubicGM() {
248 fVPath.moveTo(W, 0);
249 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700250
reed8f76cb92015-04-22 17:38:23 -0700251 SkMatrix pivot;
252 pivot.setRotate(90, W/2, H/2);
253 fVPath.transform(pivot, &fHPath);
254 }
255
256protected:
257 SkString onShortName() override {
258 return SkString("clipcubic");
259 }
halcanary9d524f22016-03-29 09:03:52 -0700260
reed8f76cb92015-04-22 17:38:23 -0700261 SkISize onISize() override {
262 return SkISize::Make(400, 410);
263 }
264
265 void doDraw(SkCanvas* canvas, const SkPath& path) {
266 SkPaint paint;
267 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700268
caryclark12596012015-07-29 05:27:47 -0700269 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed8f76cb92015-04-22 17:38:23 -0700270 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700271
reed8f76cb92015-04-22 17:38:23 -0700272 paint.setColor(SK_ColorRED);
273 paint.setStyle(SkPaint::kStroke_Style);
274 canvas->drawPath(path, paint);
275 }
276
277 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
278 SkAutoCanvasRestore acr(canvas, true);
279
280 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
281 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700282 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700283
284 canvas->drawRect(r, paint);
285 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700286
reed8f76cb92015-04-22 17:38:23 -0700287 canvas->translate(dx, dy);
288
289 canvas->drawRect(r, paint);
290 canvas->clipRect(r);
291 this->doDraw(canvas, path);
292 }
293
294 void onDraw(SkCanvas* canvas) override {
295 canvas->translate(80, 10);
296 this->drawAndClip(canvas, fVPath, 200, 0);
297 canvas->translate(0, 200);
298 this->drawAndClip(canvas, fHPath, 200, 0);
299 }
halcanary9d524f22016-03-29 09:03:52 -0700300
reed8f76cb92015-04-22 17:38:23 -0700301private:
302 typedef skiagm::GM INHERITED;
303};
halcanary385fe4d2015-08-26 13:07:48 -0700304DEF_GM(return new ClipCubicGM;)