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