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, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 21 | * skiatest::Reporter* reporter, |
| 22 | * CanvasTestStep* testStep) |
| 23 | * { |
| 24 | * canvas->someCanvasAPImethod(); |
| 25 | * (...) |
| 26 | * REPORTER_ASSERT_MESSAGE(reporter, (...), \ |
| 27 | * testStep->assertMessage()); |
| 28 | * } |
| 29 | * |
| 30 | * The definition of the test step function should be followed by an |
| 31 | * invocation of the TEST_STEP macro, which generates a class and |
| 32 | * instance for the test step: |
| 33 | * |
| 34 | * TEST_STEP(MyTestStep, MyTestStepFunction) |
| 35 | * |
| 36 | * There are also short hand macros for defining simple test steps |
| 37 | * in a single line of code. A simple test step is a one that is made |
| 38 | * of a single canvas API call. |
| 39 | * |
| 40 | * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod()); |
| 41 | * |
| 42 | * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that |
| 43 | * works the same way as SIMPLE_TEST_STEP, and additionally verifies |
| 44 | * that the invoked method returns a non-zero value. |
| 45 | */ |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 46 | #include "SkBitmap.h" |
| 47 | #include "SkCanvas.h" |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 48 | #include "SkClipStack.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 49 | #include "SkDevice.h" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 50 | #include "SkDocument.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 51 | #include "SkMatrix.h" |
| 52 | #include "SkNWayCanvas.h" |
| 53 | #include "SkPaint.h" |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 54 | #include "SkPaintFilterCanvas.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 55 | #include "SkPath.h" |
| 56 | #include "SkPicture.h" |
| 57 | #include "SkPictureRecord.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 58 | #include "SkPictureRecorder.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 59 | #include "SkRect.h" |
| 60 | #include "SkRegion.h" |
| 61 | #include "SkShader.h" |
| 62 | #include "SkStream.h" |
reed@google.com | 28183b4 | 2014-02-04 15:34:10 +0000 | [diff] [blame] | 63 | #include "SkSurface.h" |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 64 | #include "SkTDArray.h" |
| 65 | #include "Test.h" |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 66 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 67 | static const int kWidth = 2, kHeight = 2; |
| 68 | |
| 69 | static void createBitmap(SkBitmap* bm, SkColor color) { |
| 70 | bm->allocN32Pixels(kWidth, kHeight); |
| 71 | bm->eraseColor(color); |
| 72 | } |
| 73 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | // Constants used by test steps |
| 76 | const SkPoint kTestPoints[] = { |
| 77 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 78 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 79 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 80 | }; |
| 81 | const SkPoint kTestPoints2[] = { |
| 82 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 83 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 84 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 85 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 86 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 87 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 88 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 89 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 90 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 91 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 92 | { SkIntToScalar(10), SkIntToScalar(1) } |
| 93 | }; |
| 94 | |
| 95 | struct TestData { |
| 96 | public: |
| 97 | TestData() |
| 98 | : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 99 | SkIntToScalar(2), SkIntToScalar(1))) |
| 100 | , fMatrix(TestMatrix()) |
| 101 | , fPath(TestPath()) |
| 102 | , fNearlyZeroLengthPath(TestNearlyZeroLengthPath()) |
| 103 | , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1)) |
| 104 | , fRegion(TestRegion()) |
| 105 | , fColor(0x01020304) |
| 106 | , fPoints(kTestPoints) |
| 107 | , fPointCount(3) |
| 108 | , fWidth(2) |
| 109 | , fHeight(2) |
| 110 | , fText("Hello World") |
| 111 | , fPoints2(kTestPoints2) |
| 112 | , fBitmap(TestBitmap()) |
| 113 | { } |
| 114 | |
| 115 | SkRect fRect; |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 116 | SkMatrix fMatrix; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 117 | SkPath fPath; |
| 118 | SkPath fNearlyZeroLengthPath; |
| 119 | SkIRect fIRect; |
| 120 | SkRegion fRegion; |
| 121 | SkColor fColor; |
| 122 | SkPaint fPaint; |
| 123 | const SkPoint* fPoints; |
| 124 | size_t fPointCount; |
| 125 | int fWidth; |
| 126 | int fHeight; |
| 127 | SkString fText; |
| 128 | const SkPoint* fPoints2; |
| 129 | SkBitmap fBitmap; |
| 130 | |
| 131 | private: |
| 132 | static SkMatrix TestMatrix() { |
| 133 | SkMatrix matrix; |
| 134 | matrix.reset(); |
| 135 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 136 | |
| 137 | return matrix; |
| 138 | } |
| 139 | static SkPath TestPath() { |
| 140 | SkPath path; |
| 141 | path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 142 | SkIntToScalar(2), SkIntToScalar(1))); |
| 143 | return path; |
| 144 | } |
| 145 | static SkPath TestNearlyZeroLengthPath() { |
| 146 | SkPath path; |
| 147 | SkPoint pt1 = { 0, 0 }; |
| 148 | SkPoint pt2 = { 0, SK_ScalarNearlyZero }; |
| 149 | SkPoint pt3 = { SkIntToScalar(1), 0 }; |
| 150 | SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 }; |
| 151 | path.moveTo(pt1); |
| 152 | path.lineTo(pt2); |
| 153 | path.lineTo(pt3); |
| 154 | path.lineTo(pt4); |
| 155 | return path; |
| 156 | } |
| 157 | static SkRegion TestRegion() { |
| 158 | SkRegion region; |
| 159 | SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 160 | region.setRect(rect); |
| 161 | return region; |
| 162 | } |
| 163 | static SkBitmap TestBitmap() { |
| 164 | SkBitmap bitmap; |
| 165 | createBitmap(&bitmap, 0x05060708); |
| 166 | return bitmap; |
| 167 | } |
| 168 | }; |
| 169 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 170 | static bool equal_clips(const SkCanvas& a, const SkCanvas& b) { |
| 171 | if (a.isClipEmpty()) { |
| 172 | return b.isClipEmpty(); |
| 173 | } |
| 174 | if (!a.isClipRect()) { |
| 175 | // this is liberally true, since we don't expose a way to know this exactly (for non-rects) |
| 176 | return !b.isClipRect(); |
| 177 | } |
| 178 | SkIRect ar, br; |
| 179 | a.getClipDeviceBounds(&ar); |
| 180 | b.getClipDeviceBounds(&br); |
| 181 | return ar == br; |
| 182 | } |
| 183 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 184 | class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor { |
| 185 | public: |
| 186 | Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {} |
| 187 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 188 | void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override { |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 189 | fTarget->clipRect(r, op, aa); |
| 190 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 191 | void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 192 | fTarget->clipRRect(r, op, aa); |
| 193 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 194 | void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override { |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 195 | fTarget->clipPath(p, op, aa); |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | SkCanvas* fTarget; |
| 200 | }; |
| 201 | |
| 202 | static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) { |
| 203 | SkISize size = canvas->getDeviceSize(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 204 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 205 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 206 | bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height())); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 207 | SkCanvas c(bm); |
| 208 | |
| 209 | Canvas2CanvasClipVisitor visitor(&c); |
| 210 | canvas->replayClips(&visitor); |
| 211 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 212 | REPORTER_ASSERT(reporter, equal_clips(c, *canvas)); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 213 | } |
| 214 | |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 215 | static void test_clipstack(skiatest::Reporter* reporter) { |
| 216 | // The clipstack is refcounted, and needs to be able to out-live the canvas if a client has |
| 217 | // ref'd it. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | const SkClipStack* cs = nullptr; |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 219 | { |
| 220 | SkCanvas canvas(10, 10); |
| 221 | cs = SkRef(canvas.getClipStack()); |
| 222 | } |
| 223 | REPORTER_ASSERT(reporter, cs->unique()); |
| 224 | cs->unref(); |
| 225 | } |
| 226 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 227 | // Format strings that describe the test context. The %s token is where |
| 228 | // the name of the test step is inserted. The context is required for |
| 229 | // disambiguating the error in the case of failures that are reported in |
| 230 | // functions that are called multiple times in different contexts (test |
| 231 | // cases and test steps). |
| 232 | static const char* const kDefaultAssertMessageFormat = "%s"; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 233 | static const char* const kCanvasDrawAssertMessageFormat = |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 234 | "Drawing test step %s with SkCanvas"; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 235 | static const char* const kNWayDrawAssertMessageFormat = |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 236 | "Drawing test step %s with SkNWayCanvas"; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 237 | static const char* const kNWayStateAssertMessageFormat = |
| 238 | "test step %s, SkNWayCanvas state consistency"; |
| 239 | static const char* const kNWayIndirect1StateAssertMessageFormat = |
| 240 | "test step %s, SkNWayCanvas indirect canvas 1 state consistency"; |
| 241 | static const char* const kNWayIndirect2StateAssertMessageFormat = |
| 242 | "test step %s, SkNWayCanvas indirect canvas 2 state consistency"; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 243 | static const char* const kPdfAssertMessageFormat = |
| 244 | "PDF sanity check failed %s"; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 245 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 246 | class CanvasTestStep; |
| 247 | static SkTDArray<CanvasTestStep*>& testStepArray() { |
| 248 | static SkTDArray<CanvasTestStep*> theTests; |
| 249 | return theTests; |
| 250 | } |
| 251 | |
| 252 | class CanvasTestStep { |
| 253 | public: |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 254 | CanvasTestStep(bool fEnablePdfTesting = true) { |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 255 | *testStepArray().append() = this; |
| 256 | fAssertMessageFormat = kDefaultAssertMessageFormat; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 257 | this->fEnablePdfTesting = fEnablePdfTesting; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 258 | } |
djsollen@google.com | e63793a | 2012-03-21 15:39:03 +0000 | [diff] [blame] | 259 | virtual ~CanvasTestStep() { } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 260 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 261 | virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 262 | virtual const char* name() const = 0; |
| 263 | |
| 264 | const char* assertMessage() { |
| 265 | fAssertMessage.printf(fAssertMessageFormat, name()); |
| 266 | return fAssertMessage.c_str(); |
| 267 | } |
| 268 | |
| 269 | void setAssertMessageFormat(const char* format) { |
| 270 | fAssertMessageFormat = format; |
| 271 | } |
| 272 | |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 273 | bool enablePdfTesting() { return fEnablePdfTesting; } |
| 274 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 275 | private: |
| 276 | SkString fAssertMessage; |
| 277 | const char* fAssertMessageFormat; |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 278 | bool fEnablePdfTesting; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | /////////////////////////////////////////////////////////////////////////////// |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 282 | // Macros for defining test steps |
| 283 | |
| 284 | #define TEST_STEP(NAME, FUNCTION) \ |
| 285 | class NAME##_TestStep : public CanvasTestStep{ \ |
| 286 | public: \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 287 | virtual void draw(SkCanvas* canvas, const TestData& d, \ |
| 288 | skiatest::Reporter* reporter) { \ |
| 289 | FUNCTION (canvas, d, reporter, this); \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 290 | } \ |
| 291 | virtual const char* name() const {return #NAME ;} \ |
| 292 | }; \ |
| 293 | static NAME##_TestStep NAME##_TestStepInstance; |
| 294 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 295 | #define TEST_STEP_NO_PDF(NAME, FUNCTION) \ |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 296 | class NAME##_TestStep : public CanvasTestStep{ \ |
| 297 | public: \ |
| 298 | NAME##_TestStep() : CanvasTestStep(false) {} \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 299 | virtual void draw(SkCanvas* canvas, const TestData& d, \ |
| 300 | skiatest::Reporter* reporter) { \ |
| 301 | FUNCTION (canvas, d, reporter, this); \ |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 302 | } \ |
| 303 | virtual const char* name() const {return #NAME ;} \ |
| 304 | }; \ |
| 305 | static NAME##_TestStep NAME##_TestStepInstance; |
| 306 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 307 | #define SIMPLE_TEST_STEP(NAME, CALL) \ |
| 308 | static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \ |
| 309 | skiatest::Reporter*, CanvasTestStep*) { \ |
| 310 | canvas-> CALL ; \ |
| 311 | } \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 312 | TEST_STEP(NAME, NAME##TestStep ) |
| 313 | |
| 314 | #define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 315 | static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \ |
| 316 | skiatest::Reporter*, CanvasTestStep* testStep) { \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 317 | REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \ |
| 318 | testStep->assertMessage()); \ |
| 319 | } \ |
| 320 | TEST_STEP(NAME, NAME##TestStep ) |
| 321 | |
| 322 | |
| 323 | /////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 324 | // 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] | 325 | // the state of the canvas. |
| 326 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 327 | SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2))); |
| 328 | SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2))); |
| 329 | SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1))); |
| 330 | SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2))); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 331 | SIMPLE_TEST_STEP(Concat, concat(d.fMatrix)); |
| 332 | SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix)); |
| 333 | SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect)); |
| 334 | SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath)); |
| 335 | SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op)); |
| 336 | SIMPLE_TEST_STEP(Clear, clear(d.fColor)); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 337 | |
| 338 | /////////////////////////////////////////////////////////////////////////////// |
| 339 | // Complex test steps |
| 340 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 341 | static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d, |
| 342 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 343 | int saveCount = canvas->getSaveCount(); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 344 | canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 345 | canvas->translate(SkIntToScalar(1), SkIntToScalar(2)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 346 | canvas->clipRegion(d.fRegion); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 347 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 348 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 349 | testStep->assertMessage()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 350 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(), |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 351 | testStep->assertMessage()); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 352 | // REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 353 | } |
| 354 | TEST_STEP(SaveMatrixClip, SaveMatrixClipStep); |
| 355 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 356 | static void SaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 357 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 358 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 359 | canvas->saveLayer(nullptr, nullptr); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 360 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 361 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 362 | testStep->assertMessage()); |
| 363 | } |
| 364 | TEST_STEP(SaveLayer, SaveLayerStep); |
| 365 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 366 | static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 367 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 368 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 369 | canvas->saveLayer(&d.fRect, nullptr); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 370 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 371 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 372 | testStep->assertMessage()); |
| 373 | } |
| 374 | TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep); |
| 375 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 376 | static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d, |
| 377 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 378 | int saveCount = canvas->getSaveCount(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 379 | canvas->saveLayer(nullptr, &d.fPaint); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 380 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 381 | REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount, |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 382 | testStep->assertMessage()); |
| 383 | } |
| 384 | TEST_STEP(PaintSaveLayer, PaintSaveLayerStep); |
| 385 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 386 | static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d, |
| 387 | skiatest::Reporter*, CanvasTestStep*) { |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 388 | // This test exercises a functionality in SkPicture that leads to the |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 389 | // recording of restore offset placeholders. This test will trigger an |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 390 | // assertion at playback time if the placeholders are not properly |
| 391 | // filled when the recording ends. |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 392 | canvas->clipRect(d.fRect); |
| 393 | canvas->clipRegion(d.fRegion); |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 394 | } |
| 395 | TEST_STEP(TwoClipOps, TwoClipOpsStep); |
| 396 | |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 397 | // exercise fix for http://code.google.com/p/skia/issues/detail?id=560 |
| 398 | // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero') |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 399 | static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d, |
| 400 | skiatest::Reporter*, CanvasTestStep*) { |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 401 | SkPaint paint; |
| 402 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 403 | paint.setStyle(SkPaint::kStroke_Style); |
| 404 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 405 | canvas->drawPath(d.fNearlyZeroLengthPath, paint); |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 406 | } |
| 407 | TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep); |
| 408 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 409 | static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d, |
| 410 | skiatest::Reporter*, CanvasTestStep*) { |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 411 | SkPoint pts[4]; |
| 412 | pts[0].set(0, 0); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 413 | pts[1].set(SkIntToScalar(d.fWidth), 0); |
| 414 | pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight)); |
| 415 | pts[3].set(0, SkIntToScalar(d.fHeight)); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 416 | SkPaint paint; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 417 | SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 418 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 419 | paint.setShader(shader)->unref(); |
| 420 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 421 | nullptr, nullptr, nullptr, 0, paint); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 422 | } |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 423 | // NYI: issue 240. |
| 424 | TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 425 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 426 | static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d, |
| 427 | skiatest::Reporter*, CanvasTestStep*) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 428 | SkPictureRecorder recorder; |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 429 | SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 430 | nullptr, 0); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 431 | testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 432 | testCanvas->clipRect(d.fRect); |
| 433 | testCanvas->drawRect(d.fRect, d.fPaint); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 434 | SkAutoTUnref<SkPicture> testPicture(recorder.endRecording()); |
| 435 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 436 | canvas->drawPicture(testPicture); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 437 | } |
| 438 | TEST_STEP(DrawPicture, DrawPictureTestStep); |
| 439 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 440 | static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d, |
| 441 | skiatest::Reporter* reporter, CanvasTestStep* testStep) { |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 442 | int baseSaveCount = canvas->getSaveCount(); |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 443 | int n = canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 444 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage()); |
| 445 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 446 | testStep->assertMessage()); |
| 447 | canvas->save(); |
| 448 | canvas->save(); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 449 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 450 | testStep->assertMessage()); |
junov@chromium.org | 4e6dfa5 | 2012-07-16 14:04:59 +0000 | [diff] [blame] | 451 | canvas->restoreToCount(baseSaveCount + 1); |
| 452 | REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 453 | testStep->assertMessage()); |
| 454 | |
| 455 | // should this pin to 1, or be a no-op, or crash? |
| 456 | canvas->restoreToCount(0); |
| 457 | REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(), |
| 458 | testStep->assertMessage()); |
| 459 | } |
| 460 | TEST_STEP(SaveRestore, SaveRestoreTestStep); |
| 461 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 462 | static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d, |
| 463 | skiatest::Reporter*, CanvasTestStep*) { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 464 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 465 | // test cases because the opaque paint can trigger an optimization |
| 466 | // that discards previously recorded commands. The challenge is to maintain |
| 467 | // correct clip and matrix stack state. |
| 468 | canvas->resetMatrix(); |
| 469 | canvas->rotate(SkIntToScalar(30)); |
| 470 | canvas->save(); |
| 471 | canvas->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 472 | canvas->save(); |
| 473 | canvas->scale(SkIntToScalar(3), SkIntToScalar(3)); |
| 474 | SkPaint paint; |
| 475 | paint.setColor(0xFFFFFFFF); |
| 476 | canvas->drawPaint(paint); |
| 477 | canvas->restore(); |
| 478 | canvas->restore(); |
| 479 | } |
| 480 | TEST_STEP(NestedSaveRestoreWithSolidPaint, \ |
| 481 | NestedSaveRestoreWithSolidPaintTestStep); |
| 482 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 483 | static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d, |
| 484 | skiatest::Reporter*, CanvasTestStep*) { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 485 | // This test step challenges the TestDeferredCanvasStateConsistency |
| 486 | // test case because the canvas flush on a deferred canvas will |
| 487 | // reset the recording session. The challenge is to maintain correct |
| 488 | // clip and matrix stack state on the playback canvas. |
| 489 | canvas->resetMatrix(); |
| 490 | canvas->rotate(SkIntToScalar(30)); |
| 491 | canvas->save(); |
| 492 | canvas->translate(SkIntToScalar(2), SkIntToScalar(1)); |
| 493 | canvas->save(); |
| 494 | canvas->scale(SkIntToScalar(3), SkIntToScalar(3)); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 495 | canvas->drawRect(d.fRect,d.fPaint); |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 496 | canvas->flush(); |
| 497 | canvas->restore(); |
| 498 | canvas->restore(); |
| 499 | } |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 500 | TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 501 | |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 502 | static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d, |
| 503 | const SkCanvas* canvas1, const SkCanvas* canvas2, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 504 | CanvasTestStep* testStep) { |
| 505 | REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() == |
| 506 | canvas2->getDeviceSize(), testStep->assertMessage()); |
| 507 | REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() == |
| 508 | canvas2->getSaveCount(), testStep->assertMessage()); |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 509 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 510 | SkRect bounds1, bounds2; |
| 511 | REPORTER_ASSERT_MESSAGE(reporter, |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 512 | canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2), |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 513 | testStep->assertMessage()); |
| 514 | REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2, |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 515 | testStep->assertMessage()); |
| 516 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 517 | REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() == |
| 518 | canvas2->getDrawFilter(), testStep->assertMessage()); |
| 519 | SkIRect deviceBounds1, deviceBounds2; |
| 520 | REPORTER_ASSERT_MESSAGE(reporter, |
| 521 | canvas1->getClipDeviceBounds(&deviceBounds1) == |
| 522 | canvas2->getClipDeviceBounds(&deviceBounds2), |
| 523 | testStep->assertMessage()); |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 524 | REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 525 | REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() == |
| 526 | canvas2->getTotalMatrix(), testStep->assertMessage()); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 527 | REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 528 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 529 | SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false); |
| 530 | SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false); |
| 531 | while (!layerIter1.done() && !layerIter2.done()) { |
| 532 | REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() == |
| 533 | layerIter2.matrix(), testStep->assertMessage()); |
| 534 | REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() == |
| 535 | layerIter2.clip(), testStep->assertMessage()); |
| 536 | REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() == |
| 537 | layerIter2.paint(), testStep->assertMessage()); |
| 538 | REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() == |
| 539 | layerIter2.x(), testStep->assertMessage()); |
| 540 | REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() == |
| 541 | layerIter2.y(), testStep->assertMessage()); |
| 542 | layerIter1.next(); |
| 543 | layerIter2.next(); |
| 544 | } |
| 545 | REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(), |
| 546 | testStep->assertMessage()); |
| 547 | REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(), |
| 548 | testStep->assertMessage()); |
piotaixr | 76993ed | 2014-10-27 15:31:34 -0700 | [diff] [blame] | 549 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 550 | } |
| 551 | |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 552 | static void TestPdfDevice(skiatest::Reporter* reporter, |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 553 | const TestData& d, |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 554 | CanvasTestStep* testStep) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 555 | SkDynamicMemoryWStream outStream; |
| 556 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream)); |
halcanary | 8ee06f2 | 2015-08-11 10:30:12 -0700 | [diff] [blame] | 557 | #if SK_SUPPORT_PDF |
| 558 | REPORTER_ASSERT(reporter, doc); |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 559 | #else |
| 560 | REPORTER_ASSERT(reporter, !doc); |
| 561 | #endif // SK_SUPPORT_PDF |
| 562 | if (!doc) { |
| 563 | return; |
| 564 | } |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 565 | SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth), |
| 566 | SkIntToScalar(d.fHeight)); |
| 567 | REPORTER_ASSERT(reporter, canvas); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 568 | testStep->setAssertMessageFormat(kPdfAssertMessageFormat); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 569 | testStep->draw(canvas, d, reporter); |
| 570 | |
| 571 | REPORTER_ASSERT(reporter, doc->close()); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 572 | } |
| 573 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 574 | // unused |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 575 | static void TestNWayCanvasStateConsistency( |
| 576 | skiatest::Reporter* reporter, |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 577 | const TestData& d, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 578 | CanvasTestStep* testStep, |
| 579 | const SkCanvas& referenceCanvas) { |
| 580 | |
| 581 | SkBitmap indirectStore1; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 582 | createBitmap(&indirectStore1, 0xFFFFFFFF); |
reed | 2a8ca93 | 2014-06-26 22:12:09 -0700 | [diff] [blame] | 583 | SkCanvas indirectCanvas1(indirectStore1); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 584 | |
| 585 | SkBitmap indirectStore2; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 586 | createBitmap(&indirectStore2, 0xFFFFFFFF); |
reed | 2a8ca93 | 2014-06-26 22:12:09 -0700 | [diff] [blame] | 587 | SkCanvas indirectCanvas2(indirectStore2); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 588 | |
djsollen@google.com | f0a062b | 2012-05-01 16:50:25 +0000 | [diff] [blame] | 589 | SkISize canvasSize = referenceCanvas.getDeviceSize(); |
| 590 | SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height()); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 591 | nWayCanvas.addCanvas(&indirectCanvas1); |
| 592 | nWayCanvas.addCanvas(&indirectCanvas2); |
| 593 | |
| 594 | testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 595 | testStep->draw(&nWayCanvas, d, reporter); |
scroggo | 648238c | 2015-01-29 11:58:51 -0800 | [diff] [blame] | 596 | // Verify that the SkNWayCanvas reports consitent state |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 597 | testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 598 | AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 599 | // Verify that the indirect canvases report consitent state |
| 600 | testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 601 | AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 602 | testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 603 | AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
| 607 | * This sub-test verifies that the test step passes when executed |
| 608 | * with SkCanvas and with classes derrived from SkCanvas. It also verifies |
| 609 | * that the all canvas derivatives report the same state as an SkCanvas |
| 610 | * after having executed the test step. |
| 611 | */ |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 612 | static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d, |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 613 | CanvasTestStep* testStep) { |
| 614 | SkBitmap referenceStore; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 615 | createBitmap(&referenceStore, 0xFFFFFFFF); |
reed | 2a8ca93 | 2014-06-26 22:12:09 -0700 | [diff] [blame] | 616 | SkCanvas referenceCanvas(referenceStore); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 617 | testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat); |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 618 | testStep->draw(&referenceCanvas, d, reporter); |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 619 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 620 | // The following test code is disabled because SkNWayCanvas does not |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 621 | // report correct clipping and device bounds information |
| 622 | // Issue: http://code.google.com/p/skia/issues/detail?id=501 |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 623 | |
| 624 | if (false) { // avoid bit rot, suppress warning |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 625 | TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 626 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 627 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 628 | if (false) { // avoid bit rot, suppress warning |
| 629 | test_clipVisitor(reporter, &referenceCanvas); |
| 630 | } |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 631 | test_clipstack(reporter); |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 632 | } |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 633 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 634 | static void test_newraster(skiatest::Reporter* reporter) { |
| 635 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 636 | const size_t minRowBytes = info.minRowBytes(); |
| 637 | const size_t size = info.getSafeSize(minRowBytes); |
| 638 | SkAutoMalloc storage(size); |
| 639 | SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get()); |
| 640 | sk_bzero(baseAddr, size); |
| 641 | |
| 642 | SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 643 | REPORTER_ASSERT(reporter, canvas); |
| 644 | |
| 645 | SkImageInfo info2; |
| 646 | size_t rowBytes; |
| 647 | const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes); |
| 648 | REPORTER_ASSERT(reporter, addr); |
| 649 | REPORTER_ASSERT(reporter, info == info2); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 650 | REPORTER_ASSERT(reporter, minRowBytes == rowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 651 | for (int y = 0; y < info.height(); ++y) { |
| 652 | for (int x = 0; x < info.width(); ++x) { |
| 653 | REPORTER_ASSERT(reporter, 0 == addr[x]); |
| 654 | } |
| 655 | addr = (const SkPMColor*)((const char*)addr + rowBytes); |
| 656 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 657 | delete canvas; |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 658 | |
| 659 | // now try a deliberately bad info |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 660 | info = info.makeWH(-1, info.height()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 661 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 662 | |
| 663 | // too big |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 664 | info = info.makeWH(1 << 30, 1 << 30); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 665 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes)); |
skia.committer@gmail.com | 0e53075 | 2014-02-28 03:02:05 +0000 | [diff] [blame] | 666 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 667 | // not a valid pixel type |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 668 | info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 669 | REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes)); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 670 | |
| 671 | // We should succeed with a zero-sized valid info |
| 672 | info = SkImageInfo::MakeN32Premul(0, 0); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 673 | canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes); |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 674 | REPORTER_ASSERT(reporter, canvas); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 675 | delete canvas; |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 676 | } |
| 677 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 678 | DEF_TEST(Canvas, reporter) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 679 | TestData d; |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 680 | |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 681 | for (int testStep = 0; testStep < testStepArray().count(); testStep++) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 682 | TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 683 | if (testStepArray()[testStep]->enablePdfTesting()) { |
piotaixr | f05f5a7 | 2014-10-03 13:26:55 -0700 | [diff] [blame] | 684 | TestPdfDevice(reporter, d, testStepArray()[testStep]); |
edisonn@google.com | 7790912 | 2012-10-18 15:58:23 +0000 | [diff] [blame] | 685 | } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 686 | } |
junov@chromium.org | cd62ecf | 2012-08-02 17:43:25 +0000 | [diff] [blame] | 687 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 688 | test_newraster(reporter); |
reed@google.com | 37f3ae0 | 2011-11-28 16:06:04 +0000 | [diff] [blame] | 689 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 690 | |
| 691 | DEF_TEST(Canvas_SaveState, reporter) { |
| 692 | SkCanvas canvas(10, 10); |
| 693 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 694 | |
| 695 | int n = canvas.save(); |
| 696 | REPORTER_ASSERT(reporter, 1 == n); |
| 697 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 698 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 699 | n = canvas.saveLayer(nullptr, nullptr); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 700 | REPORTER_ASSERT(reporter, 2 == n); |
| 701 | REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount()); |
| 702 | |
| 703 | canvas.restore(); |
| 704 | REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount()); |
| 705 | canvas.restore(); |
| 706 | REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount()); |
| 707 | } |
reed | c1b11f1 | 2015-03-13 08:48:26 -0700 | [diff] [blame] | 708 | |
| 709 | DEF_TEST(Canvas_ClipEmptyPath, reporter) { |
| 710 | SkCanvas canvas(10, 10); |
| 711 | canvas.save(); |
| 712 | SkPath path; |
| 713 | canvas.clipPath(path); |
| 714 | canvas.restore(); |
| 715 | canvas.save(); |
| 716 | path.moveTo(5, 5); |
| 717 | canvas.clipPath(path); |
| 718 | canvas.restore(); |
| 719 | canvas.save(); |
| 720 | path.moveTo(7, 7); |
| 721 | canvas.clipPath(path); // should not assert here |
| 722 | canvas.restore(); |
| 723 | } |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 724 | |
| 725 | namespace { |
| 726 | |
| 727 | class MockFilterCanvas : public SkPaintFilterCanvas { |
| 728 | public: |
| 729 | MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { } |
| 730 | |
| 731 | protected: |
| 732 | void onFilterPaint(SkPaint *paint, Type type) const override { } |
| 733 | |
| 734 | private: |
| 735 | typedef SkPaintFilterCanvas INHERITED; |
| 736 | }; |
| 737 | |
| 738 | } // anonymous namespace |
| 739 | |
| 740 | // SkPaintFilterCanvas should inherit the initial target canvas state. |
| 741 | DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) { |
| 742 | SkCanvas canvas(100, 100); |
| 743 | canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75)); |
| 744 | canvas.scale(0.5f, 0.75f); |
| 745 | |
| 746 | SkRect clip1, clip2; |
| 747 | |
| 748 | MockFilterCanvas filterCanvas(&canvas); |
| 749 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
| 750 | REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2)); |
| 751 | REPORTER_ASSERT(reporter, clip1 == clip2); |
| 752 | |
| 753 | filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100)); |
| 754 | filterCanvas.scale(0.75f, 0.5f); |
| 755 | REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix()); |
| 756 | REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2)); |
| 757 | REPORTER_ASSERT(reporter, clip1 == clip2); |
| 758 | } |