blob: 4c228dc3d9d9aa3f3417d779fddb1dd50f93312f [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00002 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkClipOp.h"
12#include "include/core/SkColor.h"
Ben Wagnercbf6d302019-05-06 15:13:30 -040013#include "include/core/SkDocument.h"
14#include "include/core/SkFlattenable.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImageFilter.h"
16#include "include/core/SkImageInfo.h"
17#include "include/core/SkMatrix.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPath.h"
20#include "include/core/SkPictureRecorder.h"
21#include "include/core/SkPixmap.h"
22#include "include/core/SkPoint.h"
23#include "include/core/SkRect.h"
24#include "include/core/SkRefCnt.h"
25#include "include/core/SkRegion.h"
26#include "include/core/SkScalar.h"
27#include "include/core/SkShader.h"
28#include "include/core/SkSize.h"
29#include "include/core/SkStream.h"
30#include "include/core/SkString.h"
31#include "include/core/SkSurface.h"
32#include "include/core/SkTypes.h"
33#include "include/core/SkVertices.h"
34#include "include/docs/SkPDFDocument.h"
Michael Ludwig55edb502019-08-05 10:41:10 -040035#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "include/private/SkMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "include/private/SkTemplates.h"
38#include "include/utils/SkNWayCanvas.h"
39#include "include/utils/SkPaintFilterCanvas.h"
40#include "src/core/SkClipOpPriv.h"
Michael Ludwig8ee6cf32019-08-02 09:57:04 -040041#include "src/core/SkImageFilter_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/core/SkSpecialImage.h"
43#include "src/utils/SkCanvasStack.h"
44#include "tests/Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000045
Ben Wagnerb607a8f2018-03-12 13:46:21 -040046#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "include/core/SkColorSpace.h"
48#include "include/private/SkColorData.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040049#endif
50
51#include <memory>
52#include <utility>
53
Ben Wagnerb607a8f2018-03-12 13:46:21 -040054class SkReadBuffer;
Ben Wagnerb607a8f2018-03-12 13:46:21 -040055
Mike Reed918e1442017-01-23 11:39:45 -050056DEF_TEST(canvas_clipbounds, reporter) {
57 SkCanvas canvas(10, 10);
Mike Reed6f4a9b22017-01-24 09:13:40 -050058 SkIRect irect, irect2;
59 SkRect rect, rect2;
Mike Reed918e1442017-01-23 11:39:45 -050060
61 irect = canvas.getDeviceClipBounds();
62 REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10));
Mike Reed6f4a9b22017-01-24 09:13:40 -050063 REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2));
64 REPORTER_ASSERT(reporter, irect == irect2);
65
Mike Reed918e1442017-01-23 11:39:45 -050066 // local bounds are always too big today -- can we trim them?
67 rect = canvas.getLocalClipBounds();
68 REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10)));
Mike Reed6f4a9b22017-01-24 09:13:40 -050069 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2));
70 REPORTER_ASSERT(reporter, rect == rect2);
Mike Reed918e1442017-01-23 11:39:45 -050071
72 canvas.clipRect(SkRect::MakeEmpty());
73
74 irect = canvas.getDeviceClipBounds();
75 REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty());
Mike Reed6f4a9b22017-01-24 09:13:40 -050076 REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2));
77 REPORTER_ASSERT(reporter, irect == irect2);
78
Mike Reed918e1442017-01-23 11:39:45 -050079 rect = canvas.getLocalClipBounds();
80 REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty());
Mike Reed6f4a9b22017-01-24 09:13:40 -050081 REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2));
82 REPORTER_ASSERT(reporter, rect == rect2);
Mike Reed566e53c2017-03-10 10:49:45 -050083
84 // Test for wacky sizes that we (historically) have guarded against
85 {
86 SkCanvas c(-10, -20);
87 REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty());
Mike Reeddc9f0db2017-03-10 13:54:16 -050088
89 SkPictureRecorder().beginRecording({ 5, 5, 4, 4 });
Mike Reed566e53c2017-03-10 10:49:45 -050090 }
Mike Reed918e1442017-01-23 11:39:45 -050091}
92
Robert Phillips09dfc472017-09-13 15:25:47 -040093// Will call proc with multiple styles of canvas (recording, raster, pdf)
Mike Reed02be3c12017-03-23 12:34:15 -040094template <typename F> static void multi_canvas_driver(int w, int h, F proc) {
95 proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h)));
96
97 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -040098 if (auto doc = SkPDF::MakeDocument(&stream)) {
Hal Canary4125b612018-03-20 14:17:00 -040099 proc(doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)));
100 }
Mike Reed02be3c12017-03-23 12:34:15 -0400101
102 proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas());
103}
104
Mike Reed2a65cc02017-03-22 10:01:53 -0400105const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 };
106
107static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) {
108 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR);
109
110 const SkIRect restrictionR = { 2, 2, 8, 8 };
111 canvas->androidFramework_setDeviceClipRestriction(restrictionR);
112 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR);
113
114 const SkIRect clipR = { 4, 4, 6, 6 };
115 canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect);
116 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR);
117
Mike Reed14113bc2017-05-10 14:13:20 -0400118#ifdef SK_SUPPORT_DEPRECATED_CLIPOPS
Mike Reed2a65cc02017-03-22 10:01:53 -0400119 // now test that expanding clipops can't exceed the restriction
120 const SkClipOp expanders[] = {
121 SkClipOp::kUnion_deprecated,
122 SkClipOp::kXOR_deprecated,
123 SkClipOp::kReverseDifference_deprecated,
124 SkClipOp::kReplace_deprecated,
125 };
126
127 const SkRect expandR = { 0, 0, 5, 9 };
128 SkASSERT(!SkRect::Make(restrictionR).contains(expandR));
129
130 for (SkClipOp op : expanders) {
131 canvas->save();
132 canvas->clipRect(expandR, op);
133 REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds()));
134 canvas->restore();
135 }
Mike Reed14113bc2017-05-10 14:13:20 -0400136#endif
Mike Reed2a65cc02017-03-22 10:01:53 -0400137}
138
139/**
140 * Clip restriction logic exists in the canvas itself, and in various kinds of devices.
141 *
142 * This test explicitly tries to exercise that variety:
143 * - picture : empty device but exercises canvas itself
144 * - pdf : uses SkClipStack in its device (as does SVG and GPU)
145 * - raster : uses SkRasterClip in its device
146 */
147DEF_TEST(canvas_clip_restriction, reporter) {
Mike Reed02be3c12017-03-23 12:34:15 -0400148 multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(),
149 [reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); });
150}
Mike Reed2a65cc02017-03-22 10:01:53 -0400151
Mike Reed02be3c12017-03-23 12:34:15 -0400152DEF_TEST(canvas_empty_clip, reporter) {
153 multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) {
154 canvas->save();
155 canvas->clipRect({0, 0, 20, 40 });
156 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
157 canvas->clipRect({30, 0, 50, 40 });
158 REPORTER_ASSERT(reporter, canvas->isClipEmpty());
159 });
Mike Reed2a65cc02017-03-22 10:01:53 -0400160}
161
Ben Wagnercbf6d302019-05-06 15:13:30 -0400162DEF_TEST(CanvasNewRasterTest, reporter) {
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000163 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800164 const size_t minRowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400165 const size_t size = info.computeByteSize(minRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800166 SkAutoTMalloc<SkPMColor> storage(size);
167 SkPMColor* baseAddr = storage.get();
reed3054be12014-12-10 07:24:28 -0800168 sk_bzero(baseAddr, size);
169
Mike Reed5df49342016-11-12 08:06:55 -0600170 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000171 REPORTER_ASSERT(reporter, canvas);
172
reed6ceeebd2016-03-09 14:26:26 -0800173 SkPixmap pmap;
174 const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000175 REPORTER_ASSERT(reporter, addr);
reed6ceeebd2016-03-09 14:26:26 -0800176 REPORTER_ASSERT(reporter, info == pmap.info());
177 REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000178 for (int y = 0; y < info.height(); ++y) {
179 for (int x = 0; x < info.width(); ++x) {
180 REPORTER_ASSERT(reporter, 0 == addr[x]);
181 }
reed6ceeebd2016-03-09 14:26:26 -0800182 addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000183 }
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000184
Leon Scroggins III3eedc972020-01-22 10:44:00 -0500185 // unaligned rowBytes
186 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr,
187 minRowBytes + 1));
188
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000189 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700190 info = info.makeWH(-1, info.height());
Mike Reed5df49342016-11-12 08:06:55 -0600191 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000192
193 // too big
reede5ea5002014-09-03 11:54:58 -0700194 info = info.makeWH(1 << 30, 1 << 30);
Mike Reed5df49342016-11-12 08:06:55 -0600195 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000196
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000197 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700198 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
Mike Reed5df49342016-11-12 08:06:55 -0600199 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000200
Brian Osman431ac562018-10-10 13:20:38 -0400201 // We should not succeed with a zero-sized valid info
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000202 info = SkImageInfo::MakeN32Premul(0, 0);
Mike Reed5df49342016-11-12 08:06:55 -0600203 canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
Brian Osman431ac562018-10-10 13:20:38 -0400204 REPORTER_ASSERT(reporter, nullptr == canvas);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000205}
206
Hal Canary5f2807b2019-05-06 16:51:06 -0400207static SkPath make_path_from_rect(SkRect r) {
208 SkPath path;
209 path.addRect(r);
210 return path;
211}
reed@google.com37f3ae02011-11-28 16:06:04 +0000212
Hal Canary5f2807b2019-05-06 16:51:06 -0400213static SkRegion make_region_from_irect(SkIRect r) {
214 SkRegion region;
215 region.setRect(r);
216 return region;
217}
Ben Wagnercbf6d302019-05-06 15:13:30 -0400218
Hal Canary5f2807b2019-05-06 16:51:06 -0400219static SkBitmap make_n32_bitmap(int w, int h, SkColor c = SK_ColorWHITE) {
220 SkBitmap bm;
221 bm.allocN32Pixels(w, h);
222 bm.eraseColor(c);
223 return bm;
224}
Ben Wagnercbf6d302019-05-06 15:13:30 -0400225
Hal Canary5f2807b2019-05-06 16:51:06 -0400226// Constants used by test steps
227static constexpr SkRect kRect = {0, 0, 2, 1};
228static constexpr SkColor kColor = 0x01020304;
229static constexpr int kWidth = 2;
230static constexpr int kHeight = 2;
Ben Wagnercbf6d302019-05-06 15:13:30 -0400231
Hal Canary5f2807b2019-05-06 16:51:06 -0400232using CanvasTest = void (*)(SkCanvas*, skiatest::Reporter*);
Ben Wagnercbf6d302019-05-06 15:13:30 -0400233
Hal Canary5f2807b2019-05-06 16:51:06 -0400234static CanvasTest kCanvasTests[] = {
235 [](SkCanvas* c, skiatest::Reporter* r) {
236 c->translate(SkIntToScalar(1), SkIntToScalar(2));
237 },
238 [](SkCanvas* c, skiatest::Reporter* r) {
239 c->scale(SkIntToScalar(1), SkIntToScalar(2));
240 },
241 [](SkCanvas* c, skiatest::Reporter* r) {
242 c->rotate(SkIntToScalar(1));
243 },
244 [](SkCanvas* c, skiatest::Reporter* r) {
245 c->skew(SkIntToScalar(1), SkIntToScalar(2));
246 },
247 [](SkCanvas* c, skiatest::Reporter* r) {
Mike Reed1f607332020-05-21 12:11:27 -0400248 c->concat(SkMatrix::Scale(2, 3));
Hal Canary5f2807b2019-05-06 16:51:06 -0400249 },
250 [](SkCanvas* c, skiatest::Reporter* r) {
Mike Reed1f607332020-05-21 12:11:27 -0400251 c->setMatrix(SkMatrix::Scale(2, 3));
Hal Canary5f2807b2019-05-06 16:51:06 -0400252 },
253 [](SkCanvas* c, skiatest::Reporter* r) {
254 c->clipRect(kRect);
255 },
256 [](SkCanvas* c, skiatest::Reporter* r) {
257 c->clipPath(make_path_from_rect(SkRect{0, 0, 2, 1}));
258 },
259 [](SkCanvas* c, skiatest::Reporter* r) {
260 c->clipRegion(make_region_from_irect(SkIRect{0, 0, 2, 1}), kReplace_SkClipOp);
261 },
262 [](SkCanvas* c, skiatest::Reporter* r) {
263 c->clear(kColor);
264 },
265 [](SkCanvas* c, skiatest::Reporter* r) {
266 int saveCount = c->getSaveCount();
267 c->save();
268 c->translate(SkIntToScalar(1), SkIntToScalar(2));
269 c->clipRegion(make_region_from_irect(SkIRect{0, 0, 2, 1}));
270 c->restore();
271 REPORTER_ASSERT(r, c->getSaveCount() == saveCount);
272 REPORTER_ASSERT(r, c->getTotalMatrix().isIdentity());
273 //REPORTER_ASSERT(reporter, c->getTotalClip() != kTestRegion);
274 },
275 [](SkCanvas* c, skiatest::Reporter* r) {
276 int saveCount = c->getSaveCount();
277 c->saveLayer(nullptr, nullptr);
278 c->restore();
279 REPORTER_ASSERT(r, c->getSaveCount() == saveCount);
280 },
281 [](SkCanvas* c, skiatest::Reporter* r) {
282 int saveCount = c->getSaveCount();
283 c->saveLayer(&kRect, nullptr);
284 c->restore();
285 REPORTER_ASSERT(r, c->getSaveCount() == saveCount);
286 },
287 [](SkCanvas* c, skiatest::Reporter* r) {
288 int saveCount = c->getSaveCount();
289 SkPaint p;
290 c->saveLayer(nullptr, &p);
291 c->restore();
292 REPORTER_ASSERT(r, c->getSaveCount() == saveCount);
293 },
294 [](SkCanvas* c, skiatest::Reporter* r) {
295 // This test exercises a functionality in SkPicture that leads to the
296 // recording of restore offset placeholders. This test will trigger an
297 // assertion at playback time if the placeholders are not properly
298 // filled when the recording ends.
299 c->clipRect(kRect);
300 c->clipRegion(make_region_from_irect(SkIRect{0, 0, 2, 1}));
301 },
302 [](SkCanvas* c, skiatest::Reporter* r) {
303 // exercise fix for http://code.google.com/p/skia/issues/detail?id=560
304 // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
305 SkPaint paint;
306 paint.setStrokeWidth(SkIntToScalar(1));
307 paint.setStyle(SkPaint::kStroke_Style);
308 SkPath path;
309 path.moveTo(SkPoint{ 0, 0 });
310 path.lineTo(SkPoint{ 0, SK_ScalarNearlyZero });
311 path.lineTo(SkPoint{ SkIntToScalar(1), 0 });
312 path.lineTo(SkPoint{ SkIntToScalar(1), SK_ScalarNearlyZero/2 });
313 // test nearly zero length path
314 c->drawPath(path, paint);
315 },
316 [](SkCanvas* c, skiatest::Reporter* r) {
317 SkPictureRecorder recorder;
318 SkCanvas* testCanvas = recorder.beginRecording(
319 SkIntToScalar(kWidth), SkIntToScalar(kHeight), nullptr, 0);
320 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
321 testCanvas->clipRect(kRect);
322 testCanvas->drawRect(kRect, SkPaint());
323 c->drawPicture(recorder.finishRecordingAsPicture());
324 },
325 [](SkCanvas* c, skiatest::Reporter* r) {
326 int baseSaveCount = c->getSaveCount();
327 int n = c->save();
328 REPORTER_ASSERT(r, baseSaveCount == n);
329 REPORTER_ASSERT(r, baseSaveCount + 1 == c->getSaveCount());
330 c->save();
331 c->save();
332 REPORTER_ASSERT(r, baseSaveCount + 3 == c->getSaveCount());
333 c->restoreToCount(baseSaveCount + 1);
334 REPORTER_ASSERT(r, baseSaveCount + 1 == c->getSaveCount());
Ben Wagnercbf6d302019-05-06 15:13:30 -0400335
Hal Canary5f2807b2019-05-06 16:51:06 -0400336 // should this pin to 1, or be a no-op, or crash?
337 c->restoreToCount(0);
338 REPORTER_ASSERT(r, 1 == c->getSaveCount());
339 },
340 [](SkCanvas* c, skiatest::Reporter* r) {
341 // This test step challenges the TestDeferredCanvasStateConsistency
342 // test cases because the opaque paint can trigger an optimization
343 // that discards previously recorded commands. The challenge is to maintain
344 // correct clip and matrix stack state.
345 c->resetMatrix();
346 c->rotate(SkIntToScalar(30));
347 c->save();
348 c->translate(SkIntToScalar(2), SkIntToScalar(1));
349 c->save();
350 c->scale(SkIntToScalar(3), SkIntToScalar(3));
351 SkPaint paint;
352 paint.setColor(0xFFFFFFFF);
353 c->drawPaint(paint);
354 c->restore();
355 c->restore();
356 },
357 [](SkCanvas* c, skiatest::Reporter* r) {
358 // This test step challenges the TestDeferredCanvasStateConsistency
359 // test case because the canvas flush on a deferred canvas will
360 // reset the recording session. The challenge is to maintain correct
361 // clip and matrix stack state on the playback canvas.
362 c->resetMatrix();
363 c->rotate(SkIntToScalar(30));
364 c->save();
365 c->translate(SkIntToScalar(2), SkIntToScalar(1));
366 c->save();
367 c->scale(SkIntToScalar(3), SkIntToScalar(3));
368 c->drawRect(kRect, SkPaint());
369 c->flush();
370 c->restore();
371 c->restore();
372 },
373 [](SkCanvas* c, skiatest::Reporter* r) {
374 SkPoint pts[4];
375 pts[0].set(0, 0);
376 pts[1].set(SkIntToScalar(kWidth), 0);
377 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
378 pts[3].set(0, SkIntToScalar(kHeight));
379 SkPaint paint;
380 SkBitmap bitmap(make_n32_bitmap(kWidth, kHeight, 0x05060708));
381 paint.setShader(bitmap.makeShader());
382 c->drawVertices(
383 SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts, nullptr),
384 SkBlendMode::kModulate, paint);
385 }
386};
Ben Wagnercbf6d302019-05-06 15:13:30 -0400387
Hal Canary5f2807b2019-05-06 16:51:06 -0400388DEF_TEST(Canvas_bitmap, reporter) {
389 for (const CanvasTest& test : kCanvasTests) {
390 SkBitmap referenceStore = make_n32_bitmap(kWidth, kHeight);
Ben Wagnercbf6d302019-05-06 15:13:30 -0400391 SkCanvas referenceCanvas(referenceStore);
Hal Canary5f2807b2019-05-06 16:51:06 -0400392 test(&referenceCanvas, reporter);
393 }
394}
Ben Wagnercbf6d302019-05-06 15:13:30 -0400395
Hal Canary5f2807b2019-05-06 16:51:06 -0400396DEF_TEST(Canvas_pdf, reporter) {
397 for (const CanvasTest& test : kCanvasTests) {
398 SkNullWStream outStream;
399 if (auto doc = SkPDF::MakeDocument(&outStream)) {
400 SkCanvas* canvas = doc->beginPage(SkIntToScalar(kWidth),
401 SkIntToScalar(kHeight));
402 REPORTER_ASSERT(reporter, canvas);
403 test(canvas, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000404 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000405 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000406}
reedf0090cb2014-11-26 08:55:51 -0800407
408DEF_TEST(Canvas_SaveState, reporter) {
409 SkCanvas canvas(10, 10);
410 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
411
412 int n = canvas.save();
413 REPORTER_ASSERT(reporter, 1 == n);
414 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
415
halcanary96fcdcc2015-08-27 07:41:13 -0700416 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800417 REPORTER_ASSERT(reporter, 2 == n);
418 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
halcanary9d524f22016-03-29 09:03:52 -0700419
reedf0090cb2014-11-26 08:55:51 -0800420 canvas.restore();
421 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
422 canvas.restore();
423 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
424}
reedc1b11f12015-03-13 08:48:26 -0700425
426DEF_TEST(Canvas_ClipEmptyPath, reporter) {
427 SkCanvas canvas(10, 10);
428 canvas.save();
429 SkPath path;
430 canvas.clipPath(path);
431 canvas.restore();
432 canvas.save();
433 path.moveTo(5, 5);
434 canvas.clipPath(path);
435 canvas.restore();
436 canvas.save();
437 path.moveTo(7, 7);
438 canvas.clipPath(path); // should not assert here
439 canvas.restore();
440}
fmalitaf433bb22015-08-17 08:05:13 -0700441
442namespace {
443
444class MockFilterCanvas : public SkPaintFilterCanvas {
445public:
446 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
447
448protected:
Ben Wagnerf55fa0d2018-08-27 18:11:57 -0400449 bool onFilter(SkPaint&) const override { return true; }
fmalitaf433bb22015-08-17 08:05:13 -0700450
451private:
452 typedef SkPaintFilterCanvas INHERITED;
453};
454
455} // anonymous namespace
456
457// SkPaintFilterCanvas should inherit the initial target canvas state.
458DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
459 SkCanvas canvas(100, 100);
460 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
461 canvas.scale(0.5f, 0.75f);
462
fmalitaf433bb22015-08-17 08:05:13 -0700463 MockFilterCanvas filterCanvas(&canvas);
464 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500465 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds());
fmalitaf433bb22015-08-17 08:05:13 -0700466
467 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
468 filterCanvas.scale(0.75f, 0.5f);
469 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500470 REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds()));
fmalitaf433bb22015-08-17 08:05:13 -0700471}
reedbabc3de2016-07-08 08:43:27 -0700472
473///////////////////////////////////////////////////////////////////////////////////////////////////
474
Ben Wagnercbf6d302019-05-06 15:13:30 -0400475namespace {
476
Mike Reed584ca892016-11-15 11:52:55 -0500477// Subclass that takes a bool*, which it updates in its construct (true) and destructor (false)
478// to allow the caller to know how long the object is alive.
479class LifeLineCanvas : public SkCanvas {
480 bool* fLifeLine;
481public:
482 LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) {
483 *fLifeLine = true;
484 }
485 ~LifeLineCanvas() {
486 *fLifeLine = false;
487 }
488};
489
Ben Wagnercbf6d302019-05-06 15:13:30 -0400490}
491
Mike Reed584ca892016-11-15 11:52:55 -0500492// Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases
493DEF_TEST(NWayCanvas, r) {
494 const int w = 10;
495 const int h = 10;
496 bool life[2];
497 {
498 LifeLineCanvas c0(w, h, &life[0]);
499 REPORTER_ASSERT(r, life[0]);
500 }
501 REPORTER_ASSERT(r, !life[0]);
502
503
504 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
505 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
506 REPORTER_ASSERT(r, life[0]);
507 REPORTER_ASSERT(r, life[1]);
508
509 {
510 SkNWayCanvas nway(w, h);
511 nway.addCanvas(c0.get());
512 nway.addCanvas(c1.get());
513 REPORTER_ASSERT(r, life[0]);
514 REPORTER_ASSERT(r, life[1]);
515 }
516 // Now assert that the death of the nway has NOT also killed the sub-canvases
517 REPORTER_ASSERT(r, life[0]);
518 REPORTER_ASSERT(r, life[1]);
519}
520
521// Check that CanvasStack DOES manage the lifetime of its sub-canvases
522DEF_TEST(CanvasStack, r) {
523 const int w = 10;
524 const int h = 10;
525 bool life[2];
526 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
527 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
528 REPORTER_ASSERT(r, life[0]);
529 REPORTER_ASSERT(r, life[1]);
530
531 {
532 SkCanvasStack stack(w, h);
533 stack.pushCanvas(std::move(c0), {0,0});
534 stack.pushCanvas(std::move(c1), {0,0});
535 REPORTER_ASSERT(r, life[0]);
536 REPORTER_ASSERT(r, life[1]);
537 }
538 // Now assert that the death of the canvasstack has also killed the sub-canvases
539 REPORTER_ASSERT(r, !life[0]);
540 REPORTER_ASSERT(r, !life[1]);
541}
Mike Reedcd667b62017-03-02 15:21:11 -0500542
Mike Reed3b4c22d2017-03-02 20:07:46 -0500543static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) {
Mike Reedcd667b62017-03-02 15:21:11 -0500544 REPORTER_ASSERT(r, !canvas->isClipEmpty());
545 REPORTER_ASSERT(r, canvas->isClipRect());
546
547 canvas->save();
548 canvas->clipRect({0, 0, 0, 0});
549 REPORTER_ASSERT(r, canvas->isClipEmpty());
550 REPORTER_ASSERT(r, !canvas->isClipRect());
551 canvas->restore();
552
553 canvas->save();
554 canvas->clipRect({2, 2, 6, 6});
555 REPORTER_ASSERT(r, !canvas->isClipEmpty());
556 REPORTER_ASSERT(r, canvas->isClipRect());
557 canvas->restore();
558
559 canvas->save();
560 canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip
561 REPORTER_ASSERT(r, !canvas->isClipEmpty());
562 REPORTER_ASSERT(r, !canvas->isClipRect());
563 canvas->restore();
564
565 REPORTER_ASSERT(r, !canvas->isClipEmpty());
566 REPORTER_ASSERT(r, canvas->isClipRect());
567}
Mike Reed3b4c22d2017-03-02 20:07:46 -0500568
569DEF_TEST(CanvasClipType, r) {
570 // test rasterclip backend
571 test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r);
572
573 // test clipstack backend
574 SkDynamicMemoryWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -0400575 if (auto doc = SkPDF::MakeDocument(&stream)) {
Hal Canary4125b612018-03-20 14:17:00 -0400576 test_cliptype(doc->beginPage(100, 100), r);
577 }
Mike Reed3b4c22d2017-03-02 20:07:46 -0500578}
Matt Sarett31f99ce2017-04-11 08:46:01 -0400579
580#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
581DEF_TEST(Canvas_LegacyColorBehavior, r) {
Brian Osman82ebe042019-01-04 17:03:00 -0500582 sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
583 SkNamedGamut::kAdobeRGB);
Matt Sarett31f99ce2017-04-11 08:46:01 -0400584
585 // Make a Adobe RGB bitmap.
586 SkBitmap bitmap;
587 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs));
588 bitmap.eraseColor(0xFF000000);
589
590 // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas.
591 SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy);
592 REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace());
593 SkPaint p;
594 p.setColor(SK_ColorRED);
595 canvas.drawIRect(SkIRect::MakeWH(1, 1), p);
596 REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0)));
597}
598#endif
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800599
600namespace {
601
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400602class ZeroBoundsImageFilter : public SkImageFilter_Base {
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800603public:
604 static sk_sp<SkImageFilter> Make() { return sk_sp<SkImageFilter>(new ZeroBoundsImageFilter); }
605
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800606protected:
Michael Ludwige30a4852019-08-14 14:35:42 -0400607 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint*) const override {
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800608 return nullptr;
609 }
Robert Phillips12078432018-05-17 11:17:39 -0400610 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&,
611 MapDirection, const SkIRect* inputRect) const override {
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800612 return SkIRect::MakeEmpty();
613 }
614
615private:
Mike Klein4fee3232018-10-18 17:27:16 -0400616 SK_FLATTENABLE_HOOKS(ZeroBoundsImageFilter)
617
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800618 ZeroBoundsImageFilter() : INHERITED(nullptr, 0, nullptr) {}
619
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400620 typedef SkImageFilter_Base INHERITED;
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800621};
622
623sk_sp<SkFlattenable> ZeroBoundsImageFilter::CreateProc(SkReadBuffer& buffer) {
624 SkDEBUGFAIL("Should never get here");
625 return nullptr;
626}
627
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800628} // anonymous namespace
629
630DEF_TEST(Canvas_SaveLayerWithNullBoundsAndZeroBoundsImageFilter, r) {
631 SkCanvas canvas(10, 10);
632 SkPaint p;
633 p.setImageFilter(ZeroBoundsImageFilter::Make());
634 // This should not fail any assert.
635 canvas.saveLayer(nullptr, &p);
636 REPORTER_ASSERT(r, canvas.getDeviceClipBounds().isEmpty());
637 canvas.restore();
638}
Mike Reed490aa592018-04-13 15:34:16 -0400639
Mike Reed490aa592018-04-13 15:34:16 -0400640// Test that we don't crash/assert when building a canvas with degenerate coordintes
641// (esp. big ones, that might invoke tiling).
642DEF_TEST(Canvas_degenerate_dimension, reporter) {
643 // Need a paint that will sneak us past the quickReject in SkCanvas, so we can test the
644 // raster code further downstream.
645 SkPaint paint;
Michael Ludwig55edb502019-08-05 10:41:10 -0400646 paint.setImageFilter(SkImageFilters::Paint(SkPaint(), nullptr));
Mike Reed490aa592018-04-13 15:34:16 -0400647 REPORTER_ASSERT(reporter, !paint.canComputeFastBounds());
648
649 const int big = 100 * 1024; // big enough to definitely trigger tiling
650 const SkISize sizes[] {SkISize{0, big}, {big, 0}, {0, 0}};
651 for (SkISize size : sizes) {
652 SkBitmap bm;
653 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
654 SkCanvas canvas(bm);
655 canvas.drawRect({0, 0, 100, 90*1024}, paint);
656 }
657}
658
Robert Phillips3d0e8502018-04-20 10:27:27 -0400659DEF_TEST(Canvas_ClippedOutImageFilter, reporter) {
660 SkCanvas canvas(100, 100);
661
662 SkPaint p;
663 p.setColor(SK_ColorGREEN);
Michael Ludwig55edb502019-08-05 10:41:10 -0400664 p.setImageFilter(SkImageFilters::Blur(3.0f, 3.0f, nullptr, nullptr));
Robert Phillips3d0e8502018-04-20 10:27:27 -0400665
666 SkRect blurredRect = SkRect::MakeXYWH(60, 10, 30, 30);
667
668 SkMatrix invM;
669 invM.setRotate(-45);
670 invM.mapRect(&blurredRect);
671
672 const SkRect clipRect = SkRect::MakeXYWH(0, 50, 50, 50);
673
674 canvas.clipRect(clipRect);
675
676 canvas.rotate(45);
677 const SkMatrix preCTM = canvas.getTotalMatrix();
678 canvas.drawRect(blurredRect, p);
679 const SkMatrix postCTM = canvas.getTotalMatrix();
680 REPORTER_ASSERT(reporter, preCTM == postCTM);
681}
682
Mike Reed7fe6ee32020-04-09 12:35:09 -0400683DEF_TEST(canvas_markctm, reporter) {
684 SkCanvas canvas(10, 10);
685
686 SkM44 m;
Brian Osman548de742020-04-24 12:02:25 -0400687 const char* id_a = "a";
688 const char* id_b = "b";
Mike Reed7fe6ee32020-04-09 12:35:09 -0400689
690 REPORTER_ASSERT(reporter, !canvas.findMarkedCTM(id_a, nullptr));
691 REPORTER_ASSERT(reporter, !canvas.findMarkedCTM(id_b, nullptr));
692
693 // remember the starting state
694 SkM44 b = canvas.getLocalToDevice();
695 canvas.markCTM(id_b);
696
697 // test add
698 canvas.concat(SkM44::Scale(2, 4, 6));
699 SkM44 a = canvas.getLocalToDevice();
700 canvas.markCTM(id_a);
701 REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a);
702
703 // test replace
704 canvas.translate(1, 2);
705 SkM44 a1 = canvas.getLocalToDevice();
706 SkASSERT(a != a1);
707 canvas.markCTM(id_a);
708 REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a1);
709
710 // test nested
711 canvas.save();
712 // no change
713 REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_b, &m) && m == b);
714 REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a1);
715 canvas.translate(2, 3);
716 SkM44 a2 = canvas.getLocalToDevice();
717 SkASSERT(a2 != a1);
718 canvas.markCTM(id_a);
719 // found the new one
720 REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a2);
721 canvas.restore();
722 // found the previous one
723 REPORTER_ASSERT(reporter, canvas.findMarkedCTM(id_a, &m) && m == a1);
724}