blob: 1431eb6a7cf3ee01f208a8812d163903c15c829f [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"
tomhudson@google.comef279d32011-12-21 14:27:14 +000010#include "SkCanvas.h"
11#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/**
22 * Exercise kDontClipToLayer_Legacy_SaveLayerFlag flag, which does not limit the clip to the
23 * 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;
62 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
63 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();
reed28d1ce52016-09-06 14:57:00 -070068
69 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -070070}
71
tomhudson@google.comef279d32011-12-21 14:27:14 +000072/** Draw a 2px border around the target, then red behind the target;
73 set the clip to match the target, then draw >> the target in blue.
74*/
75
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000076static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000077 SkPaint borderPaint;
78 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
79 borderPaint.setAntiAlias(true);
80 SkPaint backgroundPaint;
81 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
82 backgroundPaint.setAntiAlias(true);
83 SkPaint foregroundPaint;
84 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
85 foregroundPaint.setAntiAlias(true);
86
87 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000088 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
89 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000090 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000091 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000092 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -070093 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000094 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000095 canvas->drawRect(target, foregroundPaint);
96 canvas->restore();
97}
98
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000099static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000100 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
101 draw(canvas, target, x, y);
102}
103
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000104static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000105 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
106 draw(canvas, target, x, y);
107}
108
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000109static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000110 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
111 draw(canvas, target, x, y);
112}
113
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000114static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000115 draw_square(canvas, 10, 10);
116 draw_column(canvas, 30, 10);
117 draw_bar(canvas, 10, 30);
118}
119
120/**
121 Test a set of clipping problems discovered while writing blitAntiRect,
122 and test all the code paths through the clipping blitters.
123 Each region should show as a blue center surrounded by a 2px green
124 border, with no red.
125*/
126
reed@google.comff26b7e2013-05-23 17:04:19 +0000127class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000128public:
129 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000130
tomhudson@google.comef279d32011-12-21 14:27:14 +0000131 }
132
133protected:
mtklein36352bf2015-03-25 18:17:31 -0700134 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000135 return SkString("aaclip");
136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000139 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000143 // Initial pixel-boundary-aligned draw
144 draw_rect_tests(canvas);
145
146 // Repeat 4x with .2, .4, .6, .8 px offsets
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 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000160 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000161 draw_rect_tests(canvas);
162 }
163
tomhudson@google.comef279d32011-12-21 14:27:14 +0000164private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000165 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000166};
167
halcanary385fe4d2015-08-26 13:07:48 -0700168DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000169
170/////////////////////////////////////////////////////////////////////////
171
172#ifdef SK_BUILD_FOR_MAC
173
Mike Reed5df49342016-11-12 08:06:55 -0600174static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000175 const SkImageInfo& info = bm.info();
176 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600177 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
178 (SkPMColor*)bm.getPixels(),
179 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000180 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600181 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000182 }
183}
184
185#include "SkCGUtils.h"
186static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
187 SkBitmap bm;
188 bm.allocPixels(info);
189
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000190 if (info.isOpaque()) {
191 bm.eraseColor(SK_ColorGREEN);
192 } else {
193 bm.eraseColor(0);
194 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000195
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000196 SkPaint paint;
197 paint.setAntiAlias(true);
198 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600199 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000200 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000201
halcanary96fcdcc2015-08-27 07:41:13 -0700202 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000203
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000204 SkBitmap bm2;
205 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000206 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500207 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
208
209 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000210}
211
212class CGImageGM : public skiagm::GM {
213public:
214 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000215
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000216protected:
mtklein36352bf2015-03-25 18:17:31 -0700217 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000218 return SkString("cgimage");
219 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000220
mtklein36352bf2015-03-25 18:17:31 -0700221 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000222 return SkISize::Make(800, 250);
223 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000224
mtklein36352bf2015-03-25 18:17:31 -0700225 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000226 const struct {
227 SkColorType fCT;
228 SkAlphaType fAT;
229 } rec[] = {
230 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
231
232 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
233 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
234 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
235
236 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
237 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
238 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
239 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000240
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000241 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
242 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
243 test_image(canvas, info);
244 canvas->translate(info.width() + 10, 0);
245 }
246 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000247
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000248private:
249 typedef skiagm::GM INHERITED;
250};
Mike Reed463c8482016-12-21 12:01:12 -0500251//DEF_GM( return new CGImageGM; )
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000252#endif
reed8f76cb92015-04-22 17:38:23 -0700253
254///////////////////////////////////////////////////////////////////////////////////////////////////
255
halcanary6950de62015-11-07 05:29:00 -0800256// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700257class ClipCubicGM : public skiagm::GM {
258 const SkScalar W = 100;
259 const SkScalar H = 240;
260
261 SkPath fVPath, fHPath;
262public:
263 ClipCubicGM() {
264 fVPath.moveTo(W, 0);
265 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700266
reed8f76cb92015-04-22 17:38:23 -0700267 SkMatrix pivot;
268 pivot.setRotate(90, W/2, H/2);
269 fVPath.transform(pivot, &fHPath);
270 }
271
272protected:
273 SkString onShortName() override {
274 return SkString("clipcubic");
275 }
halcanary9d524f22016-03-29 09:03:52 -0700276
reed8f76cb92015-04-22 17:38:23 -0700277 SkISize onISize() override {
278 return SkISize::Make(400, 410);
279 }
280
281 void doDraw(SkCanvas* canvas, const SkPath& path) {
282 SkPaint paint;
283 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700284
caryclark12596012015-07-29 05:27:47 -0700285 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed8f76cb92015-04-22 17:38:23 -0700286 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700287
reed8f76cb92015-04-22 17:38:23 -0700288 paint.setColor(SK_ColorRED);
289 paint.setStyle(SkPaint::kStroke_Style);
290 canvas->drawPath(path, paint);
291 }
292
293 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
294 SkAutoCanvasRestore acr(canvas, true);
295
296 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
297 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700298 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700299
300 canvas->drawRect(r, paint);
301 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700302
reed8f76cb92015-04-22 17:38:23 -0700303 canvas->translate(dx, dy);
304
305 canvas->drawRect(r, paint);
306 canvas->clipRect(r);
307 this->doDraw(canvas, path);
308 }
309
310 void onDraw(SkCanvas* canvas) override {
311 canvas->translate(80, 10);
312 this->drawAndClip(canvas, fVPath, 200, 0);
313 canvas->translate(0, 200);
314 this->drawAndClip(canvas, fHPath, 200, 0);
315 }
halcanary9d524f22016-03-29 09:03:52 -0700316
reed8f76cb92015-04-22 17:38:23 -0700317private:
318 typedef skiagm::GM INHERITED;
319};
halcanary385fe4d2015-08-26 13:07:48 -0700320DEF_GM(return new ClipCubicGM;)