reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 1 | /* |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 2 | * Copyright 2012 Google Inc. |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 3 | * |
| 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.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 7 | |
| 8 | /* Description: |
| 9 | * This test defines a series of elementatry test steps that perform |
| 10 | * a single or a small group of canvas API calls. Each test step is |
| 11 | * used in several test cases that verify that different types of SkCanvas |
| 12 | * flavors and derivatives pass it and yield consistent behavior. The |
| 13 | * test cases analyse results that are queryable through the API. They do |
| 14 | * not look at rendering results. |
| 15 | * |
| 16 | * Adding test stepss: |
| 17 | * The general pattern for creating a new test step is to write a test |
| 18 | * function of the form: |
| 19 | * |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | * static void MyTestStepFunction(SkCanvas* canvas, |
tomhudson | cb3bd18 | 2016-05-18 07:24:16 -0700 | [diff] [blame] | 21 | * const TestData& d, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 22 | * skiatest::Reporter* reporter, |
| 23 | * CanvasTestStep* testStep) |
| 24 | * { |
| 25 | * canvas->someCanvasAPImethod(); |
| 26 | * (...) |
| 27 | * REPORTER_ASSERT_MESSAGE(reporter, (...), \ |
| 28 | * testStep->assertMessage()); |
| 29 | * } |
| 30 | * |
| 31 | * The definition of the test step function should be followed by an |
| 32 | * invocation of the TEST_STEP macro, which generates a class and |
| 33 | * instance for the test step: |
| 34 | * |
| 35 | * TEST_STEP(MyTestStep, MyTestStepFunction) |
| 36 | * |
| 37 | * There are also short hand macros for defining simple test steps |
| 38 | * in a single line of code. A simple test step is a one that is made |
| 39 | * of a single canvas API call. |
| 40 | * |
| 41 | * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod()); |
| 42 | * |
| 43 | * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that |
| 44 | * works the same way as SIMPLE_TEST_STEP, and additionally verifies |
| 45 | * that the invoked method returns a non-zero value. |
| 46 | */ |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 47 | |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 48 | #include "SkBitmap.h" |
| 49 | #include "SkCanvas.h" |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 50 | #include "SkClipStack.h" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 51 | #include "SkDocument.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 52 | #include "SkMatrix.h" |
| 53 | #include "SkNWayCanvas.h" |
| 54 | #include "SkPaint.h" |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 55 | #include "SkPaintFilterCanvas.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 56 | #include "SkPath.h" |
| 57 | #include "SkPicture.h" |
| 58 | #include "SkPictureRecord.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 59 | #include "SkPictureRecorder.h" |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 60 | #include "SkRasterClip.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 61 | #include "SkRect.h" |
| 62 | #include "SkRegion.h" |
| 63 | #include "SkShader.h" |
| 64 | #include "SkStream.h" |
reed@google.com | 28183b4 | 2014-02-04 15:34:10 +0000 | [diff] [blame] | 65 | #include "SkSurface.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 66 | #include "SkTemplates.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 67 | #include "SkTDArray.h" |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 68 | #include "SkVertices.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 69 | #include "Test.h" |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 70 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 71 | DEF_TEST(canvas_clipbounds, reporter) { |
| 72 | SkCanvas canvas(10, 10); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 73 | SkIRect irect, irect2; |
| 74 | SkRect rect, rect2; |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 75 | |
| 76 | irect = canvas.getDeviceClipBounds(); |
| 77 | REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10)); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2)); |
| 79 | REPORTER_ASSERT(reporter, irect == irect2); |
| 80 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 81 | // local bounds are always too big today -- can we trim them? |
| 82 | rect = canvas.getLocalClipBounds(); |
| 83 | REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10))); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2)); |
| 85 | REPORTER_ASSERT(reporter, rect == rect2); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 86 | |
| 87 | canvas.clipRect(SkRect::MakeEmpty()); |
| 88 | |
| 89 | irect = canvas.getDeviceClipBounds(); |
| 90 | REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty()); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2)); |
| 92 | REPORTER_ASSERT(reporter, irect == irect2); |
| 93 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 94 | rect = canvas.getLocalClipBounds(); |
| 95 | REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty()); |
Mike Reed | 6f4a9b2 | 2017-01-24 09:13:40 -0500 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2)); |
| 97 | REPORTER_ASSERT(reporter, rect == rect2); |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 98 | |
| 99 | // Test for wacky sizes that we (historically) have guarded against |
| 100 | { |
| 101 | SkCanvas c(-10, -20); |
| 102 | REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty()); |
Mike Reed | dc9f0db | 2017-03-10 13:54:16 -0500 | [diff] [blame] | 103 | |
| 104 | SkPictureRecorder().beginRecording({ 5, 5, 4, 4 }); |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 105 | } |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 108 | // Will call proc with multiple styles of canse (recording, raster, pdf) |
| 109 | // |
| 110 | template <typename F> static void multi_canvas_driver(int w, int h, F proc) { |
| 111 | proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h))); |
| 112 | |
| 113 | SkNullWStream stream; |
| 114 | proc(SkDocument::MakePDF(&stream)->beginPage(SkIntToScalar(w), SkIntToScalar(h))); |
| 115 | |
| 116 | proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas()); |
| 117 | } |
| 118 | |
| 119 | |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 120 | const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 }; |
| 121 | |
| 122 | static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) { |
| 123 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR); |
| 124 | |
| 125 | const SkIRect restrictionR = { 2, 2, 8, 8 }; |
| 126 | canvas->androidFramework_setDeviceClipRestriction(restrictionR); |
| 127 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR); |
| 128 | |
| 129 | const SkIRect clipR = { 4, 4, 6, 6 }; |
| 130 | canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect); |
| 131 | REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR); |
| 132 | |
| 133 | // now test that expanding clipops can't exceed the restriction |
| 134 | const SkClipOp expanders[] = { |
| 135 | SkClipOp::kUnion_deprecated, |
| 136 | SkClipOp::kXOR_deprecated, |
| 137 | SkClipOp::kReverseDifference_deprecated, |
| 138 | SkClipOp::kReplace_deprecated, |
| 139 | }; |
| 140 | |
| 141 | const SkRect expandR = { 0, 0, 5, 9 }; |
| 142 | SkASSERT(!SkRect::Make(restrictionR).contains(expandR)); |
| 143 | |
| 144 | for (SkClipOp op : expanders) { |
| 145 | canvas->save(); |
| 146 | canvas->clipRect(expandR, op); |
| 147 | REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds())); |
| 148 | canvas->restore(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Clip restriction logic exists in the canvas itself, and in various kinds of devices. |
| 154 | * |
| 155 | * This test explicitly tries to exercise that variety: |
| 156 | * - picture : empty device but exercises canvas itself |
| 157 | * - pdf : uses SkClipStack in its device (as does SVG and GPU) |
| 158 | * - raster : uses SkRasterClip in its device |
| 159 | */ |
| 160 | DEF_TEST(canvas_clip_restriction, reporter) { |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 161 | multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(), |
| 162 | [reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); }); |
| 163 | } |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 164 | |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 165 | DEF_TEST(canvas_empty_clip, reporter) { |
| 166 | multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) { |
| 167 | canvas->save(); |
| 168 | canvas->clipRect({0, 0, 20, 40 }); |
| 169 | REPORTER_ASSERT(reporter, !canvas->isClipEmpty()); |
| 170 | canvas->clipRect({30, 0, 50, 40 }); |
| 171 | REPORTER_ASSERT(reporter, canvas->isClipEmpty()); |
| 172 | }); |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 173 | } |
| 174 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 175 | static const int kWidth = 2, kHeight = 2; |
| 176 | |
| 177 | static void createBitmap(SkBitmap* bm, SkColor color) { |
| 178 | bm->allocN32Pixels(kWidth, kHeight); |
| 179 | bm->eraseColor(color); |
| 180 | } |
| 181 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 182 | /////////////////////////////////////////////////////////////////////////////// |
| 183 | // Constants used by test steps |
| 184 | const SkPoint kTestPoints[] = { |
| 185 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 186 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 187 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 188 | }; |
| 189 | const SkPoint kTestPoints2[] = { |
| 190 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 191 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 192 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 193 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 194 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 195 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 196 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 197 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 198 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 199 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 200 | { SkIntToScalar(10), SkIntToScalar(1) } |
| 201 | }; |
| 202 | |
| 203 | struct TestData { |
| 204 | public: |
| 205 | TestData() |
| 206 | : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 207 | SkIntToScalar(2), SkIntToScalar(1))) |
| 208 | , fMatrix(TestMatrix()) |
| 209 | , fPath(TestPath()) |
| 210 | , fNearlyZeroLengthPath(TestNearlyZeroLengthPath()) |
| 211 | , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1)) |
| 212 | , fRegion(TestRegion()) |
| 213 | , fColor(0x01020304) |
| 214 | , fPoints(kTestPoints) |
| 215 | , fPointCount(3) |
| 216 | , fWidth(2) |
| 217 | , fHeight(2) |
| 218 | , fText("Hello World") |
| 219 | , fPoints2(kTestPoints2) |
| 220 | , fBitmap(TestBitmap()) |
| 221 | { } |
| 222 | |
| 223 | SkRect fRect; |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 224 | SkMatrix fMatrix; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 225 | SkPath fPath; |
| 226 | SkPath fNearlyZeroLengthPath; |
| 227 | SkIRect fIRect; |
| 228 | SkRegion fRegion; |
| 229 | SkColor fColor; |
| 230 | SkPaint fPaint; |
| 231 | const SkPoint* fPoints; |
| 232 | size_t fPointCount; |
| 233 | int fWidth; |
| 234 | int fHeight; |
| 235 | SkString fText; |
| 236 | const SkPoint* fPoints2; |
| 237 | SkBitmap fBitmap; |
| 238 | |
| 239 | private: |
| 240 | static SkMatrix TestMatrix() { |
| 241 | SkMatrix matrix; |
| 242 | matrix.reset(); |
| 243 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 244 | |
| 245 | return matrix; |
| 246 | } |
| 247 | static SkPath TestPath() { |
| 248 | SkPath path; |
| 249 | path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 250 | SkIntToScalar(2), SkIntToScalar(1))); |
| 251 | return path; |
| 252 | } |
| 253 | static SkPath TestNearlyZeroLengthPath() { |
| 254 | SkPath path; |
| 255 | SkPoint pt1 = { 0, 0 }; |
| 256 | SkPoint pt2 = { 0, SK_ScalarNearlyZero }; |
| 257 | SkPoint pt3 = { SkIntToScalar(1), 0 }; |
| 258 | SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 }; |
| 259 | path.moveTo(pt1); |
| 260 | path.lineTo(pt2); |
| 261 | path.lineTo(pt3); |
| 262 | path.lineTo(pt4); |
| 263 | return path; |
| 264 | } |
| 265 | static SkRegion TestRegion() { |
| 266 | SkRegion region; |
| 267 | SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 268 | region.setRect(rect); |
| 269 | return region; |
| 270 | } |
| 271 | static SkBitmap TestBitmap() { |
| 272 | SkBitmap bitmap; |
| 273 | createBitmap(&bitmap, 0x05060708); |
| 274 | return bitmap; |
| 275 | } |
| 276 | }; |
| 277 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 278 | // Format strings that describe the test context. The %s token is where |
| 279 | // the name of the test step is inserted. The context is required for |
| 280 | // disambiguating the error in the case of failures that are reported in |
| 281 | // functions that are called multiple times in different contexts (test |
| 282 | // cases and test steps). |
| 283 | static const char* const kDefaultAssertMessageFormat = "%s"; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 284 | static const char* const kCanvasDrawAssertMessageFormat = |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 285 | "Drawing test step %s with SkCanvas"; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 286 | static const char* const kPdfAssertMessageFormat = |
| 287 | "PDF sanity check failed %s"; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 288 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 289 | class CanvasTestStep; |
| 290 | static SkTDArray<CanvasTestStep*>& testStepArray() { |
| 291 | static SkTDArray<CanvasTestStep*> theTests; |
| 292 | return theTests; |
| 293 | } |
| 294 | |
| 295 | class CanvasTestStep { |
| 296 | public: |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 297 | CanvasTestStep(bool fEnablePdfTesting = true) { |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 298 | *testStepArray().append() = this; |
| 299 | fAssertMessageFormat = kDefaultAssertMessageFormat; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 300 | this->fEnablePdfTesting = fEnablePdfTesting; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 301 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 302 | virtual ~CanvasTestStep() { } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 303 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 304 | virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 305 | virtual const char* name() const = 0; |
| 306 | |
| 307 | const char* assertMessage() { |
| 308 | fAssertMessage.printf(fAssertMessageFormat, name()); |
| 309 | return fAssertMessage.c_str(); |
| 310 | } |
| 311 | |
| 312 | void setAssertMessageFormat(const char* format) { |
| 313 | fAssertMessageFormat = format; |
| 314 | } |
| 315 | |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 316 | bool enablePdfTesting() { return fEnablePdfTesting; } |
| 317 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 318 | private: |
| 319 | SkString fAssertMessage; |
| 320 | const char* fAssertMessageFormat; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 321 | bool fEnablePdfTesting; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | /////////////////////////////////////////////////////////////////////////////// |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 325 | // Macros for defining test steps |
| 326 | |
| 327 | #define TEST_STEP(NAME, FUNCTION) \ |
| 328 | class NAME##_TestStep : public CanvasTestStep{ \ |
| 329 | public: \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 330 | virtual void draw(SkCanvas* canvas, const TestData& d, \ |
| 331 | skiatest::Reporter* reporter) { \ |
| 332 | FUNCTION (canvas, d, reporter, this); \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 333 | } \ |
| 334 | virtual const char* name() const {return #NAME ;} \ |
| 335 | }; \ |
| 336 | static NAME##_TestStep NAME##_TestStepInstance; |
| 337 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 338 | #define TEST_STEP_NO_PDF(NAME, FUNCTION) \ |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 339 | class NAME##_TestStep : public CanvasTestStep{ \ |
| 340 | public: \ |
| 341 | NAME##_TestStep() : CanvasTestStep(false) {} \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 342 | virtual void draw(SkCanvas* canvas, const TestData& d, \ |
| 343 | skiatest::Reporter* reporter) { \ |
| 344 | FUNCTION (canvas, d, reporter, this); \ |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 345 | } \ |
| 346 | virtual const char* name() const {return #NAME ;} \ |
| 347 | }; \ |
| 348 | static NAME##_TestStep NAME##_TestStepInstance; |
| 349 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 350 | #define SIMPLE_TEST_STEP(NAME, CALL) \ |
| 351 | static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \ |
| 352 | skiatest::Reporter*, CanvasTestStep*) { \ |
| 353 | canvas-> CALL ; \ |
| 354 | } \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 355 | TEST_STEP(NAME, NAME##TestStep ) |
| 356 | |
| 357 | #define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 358 | static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \ |
| 359 | skiatest::Reporter*, CanvasTestStep* testStep) { \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 360 | REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \ |
| 361 | testStep->assertMessage()); \ |
| 362 | } \ |
| 363 | TEST_STEP(NAME, NAME##TestStep ) |
| 364 | |
| 365 | |
| 366 | /////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 367 | // Basic test steps for most virtual methods in SkCanvas that draw or affect |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 368 | // the state of the canvas. |
| 369 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 370 | SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2))); |
| 371 | SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2))); |
| 372 | SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1))); |
| 373 | SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2))); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 374 | SIMPLE_TEST_STEP(Concat, concat(d.fMatrix)); |
| 375 | SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix)); |
| 376 | SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect)); |
| 377 | SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 378 | SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, kReplace_SkClipOp)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 379 | SIMPLE_TEST_STEP(Clear, clear(d.fColor)); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 380 | |
| 381 | /////////////////////////////////////////////////////////////////////////////// |
| 382 | // Complex test steps |
| 383 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 384 | static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d, |
| 385 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 386 | int saveCount = canvas->getSaveCount(); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 387 | canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 388 | canvas->translate(SkIntToScalar(1), SkIntToScalar(2)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 389 | canvas->clipRegion(d.fRegion); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 390 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 391 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 392 | testStep->assertMessage()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 393 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(), |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 394 | testStep->assertMessage()); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 395 | // REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 396 | } |
| 397 | TEST_STEP(SaveMatrixClip, SaveMatrixClipStep); |
| 398 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 399 | static void SaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 400 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 401 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 402 | canvas->saveLayer(nullptr, nullptr); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 403 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 404 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 405 | testStep->assertMessage()); |
| 406 | } |
| 407 | TEST_STEP(SaveLayer, SaveLayerStep); |
| 408 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 409 | static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 410 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 411 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 412 | canvas->saveLayer(&d.fRect, nullptr); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 413 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 414 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 415 | testStep->assertMessage()); |
| 416 | } |
| 417 | TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep); |
| 418 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 419 | static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 420 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 421 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 422 | canvas->saveLayer(nullptr, &d.fPaint); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 423 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 424 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 425 | testStep->assertMessage()); |
| 426 | } |
| 427 | TEST_STEP(PaintSaveLayer, PaintSaveLayerStep); |
| 428 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 429 | static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d, |
| 430 | skiatest::Reporter*, CanvasTestStep*) { |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 431 | // This test exercises a functionality in SkPicture that leads to the |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 432 | // recording of restore offset placeholders. This test will trigger an |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 433 | // assertion at playback time if the placeholders are not properly |
| 434 | // filled when the recording ends. |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 435 | canvas->clipRect(d.fRect); |
| 436 | canvas->clipRegion(d.fRegion); |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 437 | } |
| 438 | TEST_STEP(TwoClipOps, TwoClipOpsStep); |
| 439 | |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 440 | // exercise fix for http://code.google.com/p/skia/issues/detail?id=560 |
| 441 | // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero') |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 442 | static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d, |
| 443 | skiatest::Reporter*, CanvasTestStep*) { |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 444 | SkPaint paint; |
| 445 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 446 | paint.setStyle(SkPaint::kStroke_Style); |
| 447 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 448 | canvas->drawPath(d.fNearlyZeroLengthPath, paint); |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 449 | } |
| 450 | TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep); |
| 451 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 452 | static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d, |
| 453 | skiatest::Reporter*, CanvasTestStep*) { |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 454 | SkPoint pts[4]; |
| 455 | pts[0].set(0, 0); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 456 | pts[1].set(SkIntToScalar(d.fWidth), 0); |
| 457 | pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight)); |
| 458 | pts[3].set(0, SkIntToScalar(d.fHeight)); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 459 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 460 | paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode, |
| 461 | SkShader::kClamp_TileMode)); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 462 | canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts, |
| 463 | nullptr), |
| 464 | SkBlendMode::kModulate, paint); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 465 | } |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 466 | // NYI: issue 240. |
| 467 | TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 468 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 469 | static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d, |
| 470 | skiatest::Reporter*, CanvasTestStep*) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 471 | SkPictureRecorder recorder; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 472 | SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 473 | nullptr, 0); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 474 | testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 475 | testCanvas->clipRect(d.fRect); |
| 476 | testCanvas->drawRect(d.fRect, d.fPaint); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 477 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 478 | canvas->drawPicture(recorder.finishRecordingAsPicture()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 479 | } |
| 480 | TEST_STEP(DrawPicture, DrawPictureTestStep); |
| 481 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 482 | static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d, |
| 483 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 484 | int baseSaveCount = canvas->getSaveCount(); |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 485 | int n = canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 486 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage()); |
| 487 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 488 | testStep->assertMessage()); |
| 489 | canvas->save(); |
| 490 | canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 491 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 492 | testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 493 | canvas->restoreToCount(baseSaveCount + 1); |
| 494 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 495 | testStep->assertMessage()); |
| 496 | |
| 497 | // should this pin to 1, or be a no-op, or crash? |
| 498 | canvas->restoreToCount(0); |
| 499 | REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(), |
| 500 | testStep->assertMessage()); |
| 501 | } |
| 502 | TEST_STEP(SaveRestore, SaveRestoreTestStep); |
| 503 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 504 | static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d, |
| 505 | skiatest::Reporter*, CanvasTestStep*) { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 506 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 507 | // test cases because the opaque paint can trigger an optimization |
| 508 | // that discards previously recorded commands. The challenge is to maintain |
| 509 | // correct clip and matrix stack state. |
| 510 | canvas->resetMatrix(); |
| 511 | canvas->rotate(SkIntToScalar(30)); |
| 512 | canvas->save(); |
| 513 | canvas->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 514 | canvas->save(); |
| 515 | canvas->scale(SkIntToScalar(3), SkIntToScalar(3)); |
| 516 | SkPaint paint; |
| 517 | paint.setColor(0xFFFFFFFF); |
| 518 | canvas->drawPaint(paint); |
| 519 | canvas->restore(); |
| 520 | canvas->restore(); |
| 521 | } |
| 522 | TEST_STEP(NestedSaveRestoreWithSolidPaint, \ |
| 523 | NestedSaveRestoreWithSolidPaintTestStep); |
| 524 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 525 | static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d, |
| 526 | skiatest::Reporter*, CanvasTestStep*) { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 527 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 528 | // test case because the canvas flush on a deferred canvas will |
| 529 | // reset the recording session. The challenge is to maintain correct |
| 530 | // clip and matrix stack state on the playback canvas. |
| 531 | canvas->resetMatrix(); |
| 532 | canvas->rotate(SkIntToScalar(30)); |
| 533 | canvas->save(); |
| 534 | canvas->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 535 | canvas->save(); |
| 536 | canvas->scale(SkIntToScalar(3), SkIntToScalar(3)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 537 | canvas->drawRect(d.fRect,d.fPaint); |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 538 | canvas->flush(); |
| 539 | canvas->restore(); |
| 540 | canvas->restore(); |
| 541 | } |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 542 | TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 543 | |
tomhudson | cb3bd18 | 2016-05-18 07:24:16 -0700 | [diff] [blame] | 544 | static void DescribeTopLayerTestStep(SkCanvas* canvas, |
| 545 | const TestData& d, |
| 546 | skiatest::Reporter* reporter, |
| 547 | CanvasTestStep* testStep) { |
| 548 | SkMatrix m; |
| 549 | SkIRect r; |
| 550 | // NOTE: adjustToTopLayer() does *not* reduce the clip size, even if the canvas |
| 551 | // is smaller than 10x10! |
| 552 | |
| 553 | canvas->temporary_internal_describeTopLayer(&m, &r); |
| 554 | REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage()); |
| 555 | REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2), |
| 556 | testStep->assertMessage()); |
| 557 | |
| 558 | // Putting a full-canvas layer on it should make no change to the results. |
| 559 | SkRect layerBounds = SkRect::MakeXYWH(0.f, 0.f, 10.f, 10.f); |
| 560 | canvas->saveLayer(layerBounds, nullptr); |
| 561 | canvas->temporary_internal_describeTopLayer(&m, &r); |
| 562 | REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage()); |
| 563 | REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2), |
| 564 | testStep->assertMessage()); |
| 565 | canvas->restore(); |
| 566 | |
| 567 | // Adding a translated layer translates the results. |
| 568 | // Default canvas is only 2x2, so can't offset our layer by very much at all; |
| 569 | // saveLayer() aborts if the bounds don't intersect. |
| 570 | layerBounds = SkRect::MakeXYWH(1.f, 1.f, 6.f, 6.f); |
| 571 | canvas->saveLayer(layerBounds, nullptr); |
| 572 | canvas->temporary_internal_describeTopLayer(&m, &r); |
| 573 | REPORTER_ASSERT_MESSAGE(reporter, m == SkMatrix::MakeTrans(-1.f, -1.f), |
| 574 | testStep->assertMessage()); |
| 575 | REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 1, 1), |
| 576 | testStep->assertMessage()); |
| 577 | canvas->restore(); |
| 578 | |
| 579 | } |
| 580 | TEST_STEP(DescribeTopLayer, DescribeTopLayerTestStep); |
| 581 | |
| 582 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 583 | static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, CanvasTestStep* step) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 584 | SkDynamicMemoryWStream outStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 585 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
halcanary | 8ee06f2 | 2015-08-11 10:30:12 -0700 | [diff] [blame] | 586 | REPORTER_ASSERT(reporter, doc); |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 587 | if (!doc) { |
| 588 | return; |
| 589 | } |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 590 | SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth), |
| 591 | SkIntToScalar(d.fHeight)); |
| 592 | REPORTER_ASSERT(reporter, canvas); |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 593 | step->setAssertMessageFormat(kPdfAssertMessageFormat); |
| 594 | step->draw(canvas, d, reporter); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 595 | } |
| 596 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 597 | /* |
| 598 | * This sub-test verifies that the test step passes when executed |
| 599 | * with SkCanvas and with classes derrived from SkCanvas. It also verifies |
| 600 | * that the all canvas derivatives report the same state as an SkCanvas |
| 601 | * after having executed the test step. |
| 602 | */ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 603 | static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 604 | CanvasTestStep* testStep) { |
| 605 | SkBitmap referenceStore; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 606 | createBitmap(&referenceStore, 0xFFFFFFFF); |
reed | 2a8ca93 | 2014-06-26 22:12:09 -0700 | [diff] [blame] | 607 | SkCanvas referenceCanvas(referenceStore); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 608 | testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 609 | testStep->draw(&referenceCanvas, d, reporter); |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 610 | } |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 611 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 612 | static void test_newraster(skiatest::Reporter* reporter) { |
| 613 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 614 | const size_t minRowBytes = info.minRowBytes(); |
| 615 | const size_t size = info.getSafeSize(minRowBytes); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 616 | SkAutoTMalloc<SkPMColor> storage(size); |
| 617 | SkPMColor* baseAddr = storage.get(); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 618 | sk_bzero(baseAddr, size); |
| 619 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 620 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 621 | REPORTER_ASSERT(reporter, canvas); |
| 622 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 623 | SkPixmap pmap; |
| 624 | const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr; |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 625 | REPORTER_ASSERT(reporter, addr); |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 626 | REPORTER_ASSERT(reporter, info == pmap.info()); |
| 627 | REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes()); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 628 | for (int y = 0; y < info.height(); ++y) { |
| 629 | for (int x = 0; x < info.width(); ++x) { |
| 630 | REPORTER_ASSERT(reporter, 0 == addr[x]); |
| 631 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 632 | addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes()); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 633 | } |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 634 | |
| 635 | // now try a deliberately bad info |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 636 | info = info.makeWH(-1, info.height()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 637 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 638 | |
| 639 | // too big |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 640 | info = info.makeWH(1 << 30, 1 << 30); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 641 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
skia.committer@gmail.com | 0e53075 | 2014-02-28 03:02:05 +0000 | [diff] [blame] | 642 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 643 | // not a valid pixel type |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 644 | info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 645 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 646 | |
| 647 | // We should succeed with a zero-sized valid info |
| 648 | info = SkImageInfo::MakeN32Premul(0, 0); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 649 | canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 650 | REPORTER_ASSERT(reporter, canvas); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 651 | } |
| 652 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 653 | DEF_TEST(Canvas, reporter) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 654 | TestData d; |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 655 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 656 | for (int testStep = 0; testStep < testStepArray().count(); testStep++) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 657 | TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 658 | if (testStepArray()[testStep]->enablePdfTesting()) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 659 | TestPdfDevice(reporter, d, testStepArray()[testStep]); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 660 | } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 661 | } |
junov@chromium.org | cd62ecf | 2012-08-02 17:43:25 +0000 | [diff] [blame] | 662 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 663 | test_newraster(reporter); |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 664 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 665 | |
| 666 | DEF_TEST(Canvas_SaveState, reporter) { |
| 667 | SkCanvas canvas(10, 10); |
| 668 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 669 | |
| 670 | int n = canvas.save(); |
| 671 | REPORTER_ASSERT(reporter, 1 == n); |
| 672 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 673 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 674 | n = canvas.saveLayer(nullptr, nullptr); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 675 | REPORTER_ASSERT(reporter, 2 == n); |
| 676 | REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 677 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 678 | canvas.restore(); |
| 679 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 680 | canvas.restore(); |
| 681 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 682 | } |
reed | c1b11f1 | 2015-03-13 08:48:26 -0700 | [diff] [blame] | 683 | |
| 684 | DEF_TEST(Canvas_ClipEmptyPath, reporter) { |
| 685 | SkCanvas canvas(10, 10); |
| 686 | canvas.save(); |
| 687 | SkPath path; |
| 688 | canvas.clipPath(path); |
| 689 | canvas.restore(); |
| 690 | canvas.save(); |
| 691 | path.moveTo(5, 5); |
| 692 | canvas.clipPath(path); |
| 693 | canvas.restore(); |
| 694 | canvas.save(); |
| 695 | path.moveTo(7, 7); |
| 696 | canvas.clipPath(path); // should not assert here |
| 697 | canvas.restore(); |
| 698 | } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 699 | |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 700 | #define SHADOW_TEST_CANVAS_CONST 10 |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 701 | #ifdef SK_EXPERIMENTAL_SHADOWING |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 702 | class SkShadowTestCanvas : public SkPaintFilterCanvas { |
| 703 | public: |
| 704 | |
| 705 | SkShadowTestCanvas(int x, int y, skiatest::Reporter* reporter) |
| 706 | : INHERITED(x,y) |
| 707 | , fReporter(reporter) {} |
| 708 | |
| 709 | bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const { |
| 710 | REPORTER_ASSERT(this->fReporter, this->getZ() == SHADOW_TEST_CANVAS_CONST); |
| 711 | |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | void testUpdateDepth(skiatest::Reporter *reporter) { |
| 716 | // set some depths (with picture enabled), then check them as they get set |
| 717 | |
| 718 | REPORTER_ASSERT(reporter, this->getZ() == 0); |
| 719 | this->translateZ(-10); |
| 720 | REPORTER_ASSERT(reporter, this->getZ() == -10); |
| 721 | |
| 722 | this->save(); |
| 723 | this->translateZ(20); |
| 724 | REPORTER_ASSERT(reporter, this->getZ() == 10); |
| 725 | |
| 726 | this->restore(); |
| 727 | REPORTER_ASSERT(reporter, this->getZ() == -10); |
| 728 | |
| 729 | this->translateZ(13.14f); |
| 730 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(this->getZ(), 3.14f)); |
| 731 | } |
| 732 | |
| 733 | private: |
| 734 | skiatest::Reporter* fReporter; |
| 735 | |
| 736 | typedef SkPaintFilterCanvas INHERITED; |
| 737 | }; |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 738 | #endif |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 739 | |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 740 | namespace { |
| 741 | |
| 742 | class MockFilterCanvas : public SkPaintFilterCanvas { |
| 743 | public: |
| 744 | MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { } |
| 745 | |
| 746 | protected: |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 747 | bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 748 | |
| 749 | private: |
| 750 | typedef SkPaintFilterCanvas INHERITED; |
| 751 | }; |
| 752 | |
| 753 | } // anonymous namespace |
| 754 | |
| 755 | // SkPaintFilterCanvas should inherit the initial target canvas state. |
| 756 | DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) { |
| 757 | SkCanvas canvas(100, 100); |
| 758 | canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75)); |
| 759 | canvas.scale(0.5f, 0.75f); |
| 760 | |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 761 | MockFilterCanvas filterCanvas(&canvas); |
| 762 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 763 | REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds()); |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 764 | |
| 765 | filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100)); |
| 766 | filterCanvas.scale(0.75f, 0.5f); |
| 767 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 768 | REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds())); |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 769 | |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 770 | #ifdef SK_EXPERIMENTAL_SHADOWING |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 771 | SkShadowTestCanvas* tCanvas = new SkShadowTestCanvas(100,100, reporter); |
| 772 | tCanvas->testUpdateDepth(reporter); |
| 773 | delete(tCanvas); |
| 774 | |
| 775 | SkPictureRecorder recorder; |
| 776 | SkShadowTestCanvas *tSCanvas = new SkShadowTestCanvas(100, 100, reporter); |
| 777 | SkCanvas *tPCanvas = recorder.beginRecording(SkRect::MakeIWH(100, 100)); |
| 778 | |
| 779 | tPCanvas->translateZ(SHADOW_TEST_CANVAS_CONST); |
| 780 | sk_sp<SkPicture> pic = recorder.finishRecordingAsPicture(); |
| 781 | tSCanvas->drawPicture(pic); |
| 782 | |
| 783 | delete(tSCanvas); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 784 | #endif |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 785 | } |
reed | babc3de | 2016-07-08 08:43:27 -0700 | [diff] [blame] | 786 | |
| 787 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 788 | |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 789 | #include "SkCanvasStack.h" |
| 790 | #include "SkNWayCanvas.h" |
| 791 | |
| 792 | // Subclass that takes a bool*, which it updates in its construct (true) and destructor (false) |
| 793 | // to allow the caller to know how long the object is alive. |
| 794 | class LifeLineCanvas : public SkCanvas { |
| 795 | bool* fLifeLine; |
| 796 | public: |
| 797 | LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) { |
| 798 | *fLifeLine = true; |
| 799 | } |
| 800 | ~LifeLineCanvas() { |
| 801 | *fLifeLine = false; |
| 802 | } |
| 803 | }; |
| 804 | |
| 805 | // Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases |
| 806 | DEF_TEST(NWayCanvas, r) { |
| 807 | const int w = 10; |
| 808 | const int h = 10; |
| 809 | bool life[2]; |
| 810 | { |
| 811 | LifeLineCanvas c0(w, h, &life[0]); |
| 812 | REPORTER_ASSERT(r, life[0]); |
| 813 | } |
| 814 | REPORTER_ASSERT(r, !life[0]); |
| 815 | |
| 816 | |
| 817 | std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); |
| 818 | std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); |
| 819 | REPORTER_ASSERT(r, life[0]); |
| 820 | REPORTER_ASSERT(r, life[1]); |
| 821 | |
| 822 | { |
| 823 | SkNWayCanvas nway(w, h); |
| 824 | nway.addCanvas(c0.get()); |
| 825 | nway.addCanvas(c1.get()); |
| 826 | REPORTER_ASSERT(r, life[0]); |
| 827 | REPORTER_ASSERT(r, life[1]); |
| 828 | } |
| 829 | // Now assert that the death of the nway has NOT also killed the sub-canvases |
| 830 | REPORTER_ASSERT(r, life[0]); |
| 831 | REPORTER_ASSERT(r, life[1]); |
| 832 | } |
| 833 | |
| 834 | // Check that CanvasStack DOES manage the lifetime of its sub-canvases |
| 835 | DEF_TEST(CanvasStack, r) { |
| 836 | const int w = 10; |
| 837 | const int h = 10; |
| 838 | bool life[2]; |
| 839 | std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0])); |
| 840 | std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1])); |
| 841 | REPORTER_ASSERT(r, life[0]); |
| 842 | REPORTER_ASSERT(r, life[1]); |
| 843 | |
| 844 | { |
| 845 | SkCanvasStack stack(w, h); |
| 846 | stack.pushCanvas(std::move(c0), {0,0}); |
| 847 | stack.pushCanvas(std::move(c1), {0,0}); |
| 848 | REPORTER_ASSERT(r, life[0]); |
| 849 | REPORTER_ASSERT(r, life[1]); |
| 850 | } |
| 851 | // Now assert that the death of the canvasstack has also killed the sub-canvases |
| 852 | REPORTER_ASSERT(r, !life[0]); |
| 853 | REPORTER_ASSERT(r, !life[1]); |
| 854 | } |
Mike Reed | cd667b6 | 2017-03-02 15:21:11 -0500 | [diff] [blame] | 855 | |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 856 | static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) { |
Mike Reed | cd667b6 | 2017-03-02 15:21:11 -0500 | [diff] [blame] | 857 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 858 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 859 | |
| 860 | canvas->save(); |
| 861 | canvas->clipRect({0, 0, 0, 0}); |
| 862 | REPORTER_ASSERT(r, canvas->isClipEmpty()); |
| 863 | REPORTER_ASSERT(r, !canvas->isClipRect()); |
| 864 | canvas->restore(); |
| 865 | |
| 866 | canvas->save(); |
| 867 | canvas->clipRect({2, 2, 6, 6}); |
| 868 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 869 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 870 | canvas->restore(); |
| 871 | |
| 872 | canvas->save(); |
| 873 | canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip |
| 874 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 875 | REPORTER_ASSERT(r, !canvas->isClipRect()); |
| 876 | canvas->restore(); |
| 877 | |
| 878 | REPORTER_ASSERT(r, !canvas->isClipEmpty()); |
| 879 | REPORTER_ASSERT(r, canvas->isClipRect()); |
| 880 | } |
Mike Reed | 3b4c22d | 2017-03-02 20:07:46 -0500 | [diff] [blame] | 881 | |
| 882 | DEF_TEST(CanvasClipType, r) { |
| 883 | // test rasterclip backend |
| 884 | test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r); |
| 885 | |
| 886 | // test clipstack backend |
| 887 | SkDynamicMemoryWStream stream; |
| 888 | test_cliptype(SkDocument::MakePDF(&stream)->beginPage(100, 100), r); |
| 889 | } |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame^] | 890 | |
| 891 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 892 | DEF_TEST(Canvas_LegacyColorBehavior, r) { |
| 893 | sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 894 | SkColorSpace::kAdobeRGB_Gamut); |
| 895 | |
| 896 | // Make a Adobe RGB bitmap. |
| 897 | SkBitmap bitmap; |
| 898 | bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs)); |
| 899 | bitmap.eraseColor(0xFF000000); |
| 900 | |
| 901 | // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas. |
| 902 | SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy); |
| 903 | REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace()); |
| 904 | SkPaint p; |
| 905 | p.setColor(SK_ColorRED); |
| 906 | canvas.drawIRect(SkIRect::MakeWH(1, 1), p); |
| 907 | REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0))); |
| 908 | } |
| 909 | #endif |