blob: 129a95213aa18d35286a51a5ba646aff8183db72 [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
Mike Reed4c79ecf2018-01-04 17:05:11 -050014
15#include "SkCubicMap.h"
16
17static void test_cubic(SkCanvas* canvas) {
18 const SkPoint pts[] = {
19 { 0.333333f, 0.333333f }, { 0.666666f, 0.666666f },
20 { 1, 0 }, { 0, 1 },
21 { 0, 1 }, { 1, 0 },
22 { 0, 0 }, { 1, 1 },
23 { 1, 1 }, { 0, 0 },
24 { 0, 1 }, { 0, 1 },
25 { 1, 0 }, { 1, 0 },
26 };
27
28 SkPaint paint0, paint1;
29 paint0.setAntiAlias(true); paint0.setStrokeWidth(3/256.0f); paint0.setColor(SK_ColorRED);
30 paint1.setAntiAlias(true);
31
32 SkCubicMap cmap;
33
34 canvas->translate(10, 266);
35 canvas->scale(256, -256);
36 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); i += 2) {
37 cmap.setPts(pts[i], pts[i+1]);
38
39 const int N = 128;
40 SkPoint tmp0[N+1], tmp1[N+1], tmp2[N+1];
41 for (int j = 0; j <= N; ++j) {
42 float p = j * 1.0f / N;
43 tmp0[j] = cmap.computeFromT(p);
44 tmp1[j].set(p, cmap.computeYFromX(p));
45 tmp2[j].set(p, cmap.hackYFromX(p));
46 }
47
48 canvas->save();
49 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp0, paint0);
50 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp1, paint1);
51 canvas->translate(0, -1.2f);
52 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp0, paint0);
53 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N+1, tmp2, paint1);
54 canvas->restore();
55
56 canvas->translate(1.1f, 0);
57 }
58}
59
reed02f9ed72016-09-06 09:06:18 -070060static void do_draw(SkCanvas* canvas, const SkRect& r) {
61 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070062 paint.setBlendMode(SkBlendMode::kSrc);
reed02f9ed72016-09-06 09:06:18 -070063 paint.setColor(0x800000FF);
64 canvas->drawRect(r, paint);
65}
66
67/**
68 * Exercise kDontClipToLayer_Legacy_SaveLayerFlag flag, which does not limit the clip to the
69 * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
70 * and/or draw onto the surrounding portions of the canvas, or both.
71 *
72 * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
73 * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
74 *
75 * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
76 * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
77 * has no paint, so it gets the default xfermode during restore).
78 *
reed28d1ce52016-09-06 14:57:00 -070079 * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
80 * "outer" layer we created (filled with red). This is a testing detail, so that our final
81 * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
reed02f9ed72016-09-06 09:06:18 -070082 *
reed28d1ce52016-09-06 14:57:00 -070083 * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
84 * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
85 * the GM's white background.
86 *
87 * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
88 * pixels, making magenta.
89 *
90 * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
91 * light blue 0xFF7F7FFF.
reed02f9ed72016-09-06 09:06:18 -070092 */
93DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
reed28d1ce52016-09-06 14:57:00 -070094 const SkRect r { 10, 10, 110, 110 };
95
96 // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
97 // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
98 canvas->saveLayer(&r, nullptr);
99 canvas->drawColor(SK_ColorRED);
100
Mike Reed8a8937c2017-02-14 10:12:34 -0500101 SkRect r0 = { 20, 20, 100, 55 };
102 SkRect r1 = { 20, 65, 100, 100 };
reed02f9ed72016-09-06 09:06:18 -0700103
104 SkCanvas::SaveLayerRec rec;
105 rec.fPaint = nullptr;
106 rec.fBounds = &r0;
107 rec.fBackdrop = nullptr;
108 rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
109 canvas->saveLayer(rec);
Mike Reed8a8937c2017-02-14 10:12:34 -0500110 rec.fBounds = &r1;
111 canvas->saveLayer(rec);
reed02f9ed72016-09-06 09:06:18 -0700112 do_draw(canvas, r);
113 canvas->restore();
Matt Sarett25d82962017-04-24 13:59:54 -0400114 canvas->restore();
reed28d1ce52016-09-06 14:57:00 -0700115
116 canvas->restore(); // red-layer
reed02f9ed72016-09-06 09:06:18 -0700117}
118
tomhudson@google.comef279d32011-12-21 14:27:14 +0000119/** Draw a 2px border around the target, then red behind the target;
120 set the clip to match the target, then draw >> the target in blue.
121*/
122
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000123static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000124 SkPaint borderPaint;
125 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
126 borderPaint.setAntiAlias(true);
127 SkPaint backgroundPaint;
128 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
129 backgroundPaint.setAntiAlias(true);
130 SkPaint foregroundPaint;
131 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
132 foregroundPaint.setAntiAlias(true);
133
134 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000135 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
136 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000137 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000138 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000139 canvas->drawRect(target, backgroundPaint);
reed66998382016-09-21 11:15:07 -0700140 canvas->clipRect(target, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000141 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000142 canvas->drawRect(target, foregroundPaint);
143 canvas->restore();
144}
145
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000146static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000147 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
148 draw(canvas, target, x, y);
149}
150
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000151static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000152 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
153 draw(canvas, target, x, y);
154}
155
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000156static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000157 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
158 draw(canvas, target, x, y);
159}
160
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000161static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000162 draw_square(canvas, 10, 10);
163 draw_column(canvas, 30, 10);
164 draw_bar(canvas, 10, 30);
165}
166
167/**
168 Test a set of clipping problems discovered while writing blitAntiRect,
169 and test all the code paths through the clipping blitters.
170 Each region should show as a blue center surrounded by a 2px green
171 border, with no red.
172*/
173
reed@google.comff26b7e2013-05-23 17:04:19 +0000174class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000175public:
176 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000177
tomhudson@google.comef279d32011-12-21 14:27:14 +0000178 }
179
180protected:
mtklein36352bf2015-03-25 18:17:31 -0700181 SkString onShortName() override {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000182 return SkString("aaclip");
183 }
184
mtklein36352bf2015-03-25 18:17:31 -0700185 SkISize onISize() override {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +0000186 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000187 }
188
mtklein36352bf2015-03-25 18:17:31 -0700189 void onDraw(SkCanvas* canvas) override {
Mike Reed4c79ecf2018-01-04 17:05:11 -0500190 if (0) { test_cubic(canvas); return; }
191
tomhudson@google.comef279d32011-12-21 14:27:14 +0000192 // Initial pixel-boundary-aligned draw
193 draw_rect_tests(canvas);
194
195 // Repeat 4x with .2, .4, .6, .8 px offsets
196 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000197 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000198 draw_rect_tests(canvas);
199
200 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000201 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000202 draw_rect_tests(canvas);
203
204 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000205 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000206 draw_rect_tests(canvas);
207
208 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000209 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000210 draw_rect_tests(canvas);
211 }
212
tomhudson@google.comef279d32011-12-21 14:27:14 +0000213private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000214 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000215};
216
halcanary385fe4d2015-08-26 13:07:48 -0700217DEF_GM(return new AAClipGM;)
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000218
219/////////////////////////////////////////////////////////////////////////
220
221#ifdef SK_BUILD_FOR_MAC
222
Mike Reed5df49342016-11-12 08:06:55 -0600223static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000224 const SkImageInfo& info = bm.info();
225 if (info.bytesPerPixel() == 4) {
Mike Reed5df49342016-11-12 08:06:55 -0600226 return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
227 (SkPMColor*)bm.getPixels(),
228 bm.rowBytes());
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000229 } else {
Mike Reed5df49342016-11-12 08:06:55 -0600230 return skstd::make_unique<SkCanvas>(bm);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000231 }
232}
233
234#include "SkCGUtils.h"
235static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
236 SkBitmap bm;
237 bm.allocPixels(info);
238
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000239 if (info.isOpaque()) {
240 bm.eraseColor(SK_ColorGREEN);
241 } else {
242 bm.eraseColor(0);
243 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000244
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000245 SkPaint paint;
246 paint.setAntiAlias(true);
247 paint.setColor(SK_ColorBLUE);
Mike Reed5df49342016-11-12 08:06:55 -0600248 make_canvas(bm)->drawCircle(50, 50, 49, paint);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000249 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000250
halcanary96fcdcc2015-08-27 07:41:13 -0700251 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000252
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000253 SkBitmap bm2;
254 SkCreateBitmapFromCGImage(&bm2, image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000255 canvas->drawBitmap(bm2, 10, 120);
Mike Reed463c8482016-12-21 12:01:12 -0500256 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
257
258 CGImageRelease(image);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000259}
260
261class CGImageGM : public skiagm::GM {
262public:
263 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000264
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000265protected:
mtklein36352bf2015-03-25 18:17:31 -0700266 SkString onShortName() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000267 return SkString("cgimage");
268 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000269
mtklein36352bf2015-03-25 18:17:31 -0700270 SkISize onISize() override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000271 return SkISize::Make(800, 250);
272 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000273
mtklein36352bf2015-03-25 18:17:31 -0700274 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000275 const struct {
276 SkColorType fCT;
277 SkAlphaType fAT;
278 } rec[] = {
279 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
280
281 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
282 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
283 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
284
285 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
286 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
287 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
288 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000289
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000290 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
291 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
292 test_image(canvas, info);
293 canvas->translate(info.width() + 10, 0);
294 }
295 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000296
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000297private:
298 typedef skiagm::GM INHERITED;
299};
Mike Reed463c8482016-12-21 12:01:12 -0500300//DEF_GM( return new CGImageGM; )
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000301#endif
reed8f76cb92015-04-22 17:38:23 -0700302
303///////////////////////////////////////////////////////////////////////////////////////////////////
304
halcanary6950de62015-11-07 05:29:00 -0800305// https://bug.skia.org/3716
reed8f76cb92015-04-22 17:38:23 -0700306class ClipCubicGM : public skiagm::GM {
307 const SkScalar W = 100;
308 const SkScalar H = 240;
309
310 SkPath fVPath, fHPath;
311public:
312 ClipCubicGM() {
313 fVPath.moveTo(W, 0);
314 fVPath.cubicTo(W, H-10, 0, 10, 0, H);
halcanary9d524f22016-03-29 09:03:52 -0700315
reed8f76cb92015-04-22 17:38:23 -0700316 SkMatrix pivot;
317 pivot.setRotate(90, W/2, H/2);
318 fVPath.transform(pivot, &fHPath);
319 }
320
321protected:
322 SkString onShortName() override {
323 return SkString("clipcubic");
324 }
halcanary9d524f22016-03-29 09:03:52 -0700325
reed8f76cb92015-04-22 17:38:23 -0700326 SkISize onISize() override {
327 return SkISize::Make(400, 410);
328 }
329
330 void doDraw(SkCanvas* canvas, const SkPath& path) {
331 SkPaint paint;
332 paint.setAntiAlias(true);
halcanary9d524f22016-03-29 09:03:52 -0700333
caryclark12596012015-07-29 05:27:47 -0700334 paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed8f76cb92015-04-22 17:38:23 -0700335 canvas->drawPath(path, paint);
halcanary9d524f22016-03-29 09:03:52 -0700336
reed8f76cb92015-04-22 17:38:23 -0700337 paint.setColor(SK_ColorRED);
338 paint.setStyle(SkPaint::kStroke_Style);
339 canvas->drawPath(path, paint);
340 }
341
342 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
343 SkAutoCanvasRestore acr(canvas, true);
344
345 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
346 SkPaint paint;
caryclark12596012015-07-29 05:27:47 -0700347 paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
reed8f76cb92015-04-22 17:38:23 -0700348
349 canvas->drawRect(r, paint);
350 this->doDraw(canvas, path);
halcanary9d524f22016-03-29 09:03:52 -0700351
reed8f76cb92015-04-22 17:38:23 -0700352 canvas->translate(dx, dy);
353
354 canvas->drawRect(r, paint);
355 canvas->clipRect(r);
356 this->doDraw(canvas, path);
357 }
358
359 void onDraw(SkCanvas* canvas) override {
360 canvas->translate(80, 10);
361 this->drawAndClip(canvas, fVPath, 200, 0);
362 canvas->translate(0, 200);
363 this->drawAndClip(canvas, fHPath, 200, 0);
364 }
halcanary9d524f22016-03-29 09:03:52 -0700365
reed8f76cb92015-04-22 17:38:23 -0700366private:
367 typedef skiagm::GM INHERITED;
368};
halcanary385fe4d2015-08-26 13:07:48 -0700369DEF_GM(return new ClipCubicGM;)