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