scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBBHFactory.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkClipOp.h" |
| 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkData.h" |
| 14 | #include "include/core/SkFontStyle.h" |
| 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkMatrix.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkPath.h" |
| 19 | #include "include/core/SkPictureRecorder.h" |
| 20 | #include "include/core/SkPixelRef.h" |
| 21 | #include "include/core/SkRect.h" |
| 22 | #include "include/core/SkRefCnt.h" |
| 23 | #include "include/core/SkScalar.h" |
| 24 | #include "include/core/SkShader.h" |
| 25 | #include "include/core/SkStream.h" |
| 26 | #include "include/core/SkTypeface.h" |
| 27 | #include "include/core/SkTypes.h" |
| 28 | #include "include/utils/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/core/SkBigPicture.h" |
Mike Klein | a8ceb77 | 2019-05-20 12:45:50 +0000 | [diff] [blame] | 30 | #include "src/core/SkMiniRecorder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/core/SkPicturePriv.h" |
| 32 | #include "src/core/SkRectPriv.h" |
| 33 | #include "tests/Test.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 34 | |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 35 | #include <memory> |
| 36 | |
| 37 | class SkRRect; |
| 38 | class SkRegion; |
| 39 | template <typename T> class SkTDArray; |
| 40 | |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 41 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 42 | static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 43 | bm->allocN32Pixels(w, h); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 44 | bm->eraseColor(color); |
| 45 | if (immutable) { |
| 46 | bm->setImmutable(); |
| 47 | } |
| 48 | } |
| 49 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 50 | #ifdef SK_DEBUG |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 51 | // Ensure that deleting an empty SkPicture does not assert. Asserts only fire |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 52 | // in debug mode, so only run in debug mode. |
| 53 | static void test_deleting_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 54 | SkPictureRecorder recorder; |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 55 | // Creates an SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 56 | recorder.beginRecording(0, 0); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 57 | // Turns that into an SkPicture |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 58 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 59 | // Ceates a new SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 60 | recorder.beginRecording(0, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 64 | static void test_serializing_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 65 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 66 | recorder.beginRecording(0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 67 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 68 | SkDynamicMemoryWStream stream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 69 | picture->serialize(&stream); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 70 | } |
| 71 | #endif |
| 72 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 73 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 74 | SkPaint paint; |
| 75 | SkRect rect = SkRect::MakeWH(50, 50); |
| 76 | |
| 77 | SkScalar unit = rand.nextUScalar1(); |
| 78 | if (unit <= 0.3) { |
| 79 | // SkDebugf("save\n"); |
| 80 | canvas->save(); |
| 81 | } else if (unit <= 0.6) { |
| 82 | // SkDebugf("restore\n"); |
| 83 | canvas->restore(); |
| 84 | } else if (unit <= 0.9) { |
| 85 | // SkDebugf("clip\n"); |
| 86 | canvas->clipRect(rect); |
| 87 | } else { |
| 88 | // SkDebugf("draw\n"); |
| 89 | canvas->drawPaint(paint); |
| 90 | } |
| 91 | } |
| 92 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 93 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 94 | canvas->restoreToCount(1); |
| 95 | canvas->save(); |
| 96 | canvas->save(); |
| 97 | canvas->save(); |
| 98 | } |
| 99 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 100 | /** |
| 101 | * A canvas that records the number of saves, saveLayers and restores. |
| 102 | */ |
| 103 | class SaveCountingCanvas : public SkCanvas { |
| 104 | public: |
| 105 | SaveCountingCanvas(int width, int height) |
| 106 | : INHERITED(width, height) |
| 107 | , fSaveCount(0) |
| 108 | , fSaveLayerCount(0) |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 109 | , fSaveBehindCount(0) |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 110 | , fRestoreCount(0){ |
| 111 | } |
| 112 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 113 | SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 114 | ++fSaveLayerCount; |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 115 | return this->INHERITED::getSaveLayerStrategy(rec); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 118 | bool onDoSaveBehind(const SkRect* subset) override { |
| 119 | ++fSaveBehindCount; |
| 120 | return this->INHERITED::onDoSaveBehind(subset); |
| 121 | } |
| 122 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 123 | void willSave() override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 124 | ++fSaveCount; |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 125 | this->INHERITED::willSave(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 126 | } |
| 127 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 128 | void willRestore() override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 129 | ++fRestoreCount; |
| 130 | this->INHERITED::willRestore(); |
| 131 | } |
| 132 | |
| 133 | unsigned int getSaveCount() const { return fSaveCount; } |
| 134 | unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 135 | unsigned int getSaveBehindCount() const { return fSaveBehindCount; } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 136 | unsigned int getRestoreCount() const { return fRestoreCount; } |
| 137 | |
| 138 | private: |
| 139 | unsigned int fSaveCount; |
| 140 | unsigned int fSaveLayerCount; |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 141 | unsigned int fSaveBehindCount; |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 142 | unsigned int fRestoreCount; |
| 143 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 144 | using INHERITED = SkCanvas; |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
skia.committer@gmail.com | 8e7d37d | 2014-05-28 03:06:06 +0000 | [diff] [blame] | 147 | void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 148 | unsigned int numSaves, unsigned int numSaveLayers, |
| 149 | unsigned int numRestores) { |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 150 | SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 151 | SkScalarCeilToInt(picture->cullRect().height())); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 152 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 153 | picture->playback(&canvas); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 154 | |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 155 | // Optimizations may have removed these, |
| 156 | // so expect to have seen no more than num{Saves,SaveLayers,Restores}. |
| 157 | REPORTER_ASSERT(reporter, numSaves >= canvas.getSaveCount()); |
| 158 | REPORTER_ASSERT(reporter, numSaveLayers >= canvas.getSaveLayerCount()); |
| 159 | REPORTER_ASSERT(reporter, numRestores >= canvas.getRestoreCount()); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // This class exists so SkPicture can friend it and give it access to |
| 163 | // the 'partialReplay' method. |
| 164 | class SkPictureRecorderReplayTester { |
| 165 | public: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 166 | static sk_sp<SkPicture> Copy(SkPictureRecorder* recorder) { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 167 | SkPictureRecorder recorder2; |
| 168 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 169 | SkCanvas* canvas = recorder2.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 170 | |
| 171 | recorder->partialReplay(canvas); |
| 172 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 173 | return recorder2.finishRecordingAsPicture(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 174 | } |
| 175 | }; |
| 176 | |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 177 | static void create_imbalance(SkCanvas* canvas) { |
| 178 | SkRect clipRect = SkRect::MakeWH(2, 2); |
| 179 | SkRect drawRect = SkRect::MakeWH(10, 10); |
| 180 | canvas->save(); |
Michael Ludwig | 2f6e2f8 | 2021-08-03 13:08:50 -0400 | [diff] [blame] | 181 | canvas->clipRect(clipRect, SkClipOp::kIntersect); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 182 | canvas->translate(1.0f, 1.0f); |
| 183 | SkPaint p; |
| 184 | p.setColor(SK_ColorGREEN); |
| 185 | canvas->drawRect(drawRect, p); |
| 186 | // no restore |
| 187 | } |
| 188 | |
| 189 | // This tests that replaying a potentially unbalanced picture into a canvas |
| 190 | // doesn't affect the canvas' save count or matrix/clip state. |
| 191 | static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { |
| 192 | SkBitmap bm; |
| 193 | bm.allocN32Pixels(4, 3); |
| 194 | SkCanvas canvas(bm); |
| 195 | |
| 196 | int beforeSaveCount = canvas.getSaveCount(); |
| 197 | |
| 198 | SkMatrix beforeMatrix = canvas.getTotalMatrix(); |
| 199 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 200 | SkRect beforeClip = canvas.getLocalClipBounds(); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 201 | |
| 202 | canvas.drawPicture(picture); |
| 203 | |
| 204 | REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); |
| 205 | REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); |
| 206 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 207 | SkRect afterClip = canvas.getLocalClipBounds(); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 208 | |
| 209 | REPORTER_ASSERT(reporter, afterClip == beforeClip); |
| 210 | } |
| 211 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 212 | // Test out SkPictureRecorder::partialReplay |
| 213 | DEF_TEST(PictureRecorder_replay, reporter) { |
| 214 | // check save/saveLayer state |
| 215 | { |
| 216 | SkPictureRecorder recorder; |
| 217 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 218 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 219 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 220 | canvas->saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 221 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 222 | sk_sp<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 223 | |
| 224 | // The extra save and restore comes from the Copy process. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 225 | check_save_state(reporter, copy.get(), 2, 1, 3); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 226 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | canvas->saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 228 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 229 | sk_sp<SkPicture> final(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 230 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 231 | check_save_state(reporter, final.get(), 1, 2, 3); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 232 | |
| 233 | // The copy shouldn't pick up any operations added after it was made |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 234 | check_save_state(reporter, copy.get(), 2, 1, 3); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 235 | } |
| 236 | |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 237 | // Recreate the Android partialReplay test case |
| 238 | { |
| 239 | SkPictureRecorder recorder; |
| 240 | |
Mike Reed | 457c6dd | 2020-08-21 13:42:01 -0400 | [diff] [blame] | 241 | SkCanvas* canvas = recorder.beginRecording(4, 3); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 242 | create_imbalance(canvas); |
| 243 | |
| 244 | int expectedSaveCount = canvas->getSaveCount(); |
| 245 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 246 | sk_sp<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 247 | check_balance(reporter, copy.get()); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 248 | |
| 249 | REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); |
| 250 | |
| 251 | // End the recording of source to test the picture finalization |
| 252 | // process isn't complicated by the partialReplay step |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 253 | sk_sp<SkPicture> final(recorder.finishRecordingAsPicture()); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 254 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 255 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 256 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 257 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 258 | SkCanvas testCanvas(100, 100); |
| 259 | set_canvas_to_save_count_4(&testCanvas); |
| 260 | |
| 261 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 262 | |
| 263 | SkPaint paint; |
| 264 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 265 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 266 | SkPictureRecorder recorder; |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 267 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 268 | { |
| 269 | // Create picture with 2 unbalanced saves |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 270 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 271 | canvas->save(); |
| 272 | canvas->translate(10, 10); |
| 273 | canvas->drawRect(rect, paint); |
| 274 | canvas->save(); |
| 275 | canvas->translate(10, 10); |
| 276 | canvas->drawRect(rect, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 277 | sk_sp<SkPicture> extraSavePicture(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 278 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 279 | testCanvas.drawPicture(extraSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 280 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 281 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 282 | |
| 283 | set_canvas_to_save_count_4(&testCanvas); |
| 284 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 285 | { |
| 286 | // Create picture with 2 unbalanced restores |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 287 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 288 | canvas->save(); |
| 289 | canvas->translate(10, 10); |
| 290 | canvas->drawRect(rect, paint); |
| 291 | canvas->save(); |
| 292 | canvas->translate(10, 10); |
| 293 | canvas->drawRect(rect, paint); |
| 294 | canvas->restore(); |
| 295 | canvas->restore(); |
| 296 | canvas->restore(); |
| 297 | canvas->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 298 | sk_sp<SkPicture> extraRestorePicture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 299 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 300 | testCanvas.drawPicture(extraRestorePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 301 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 302 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 303 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 304 | set_canvas_to_save_count_4(&testCanvas); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 305 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 306 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 307 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 308 | canvas->translate(10, 10); |
| 309 | canvas->drawRect(rect, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 310 | sk_sp<SkPicture> noSavePicture(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 311 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 312 | testCanvas.drawPicture(noSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 314 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 315 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 316 | } |
| 317 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 318 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 319 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 320 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 321 | SkPictureRecorder recorder; |
| 322 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 323 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 324 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 325 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 326 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 327 | |
| 328 | for (int i = 0; i < 1000; ++i) { |
| 329 | rand_op(canvas, rand); |
| 330 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 331 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 332 | |
| 333 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 337 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 338 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 339 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 340 | for (int i = 0; i < 100; ++i) { |
| 341 | canvas->save(); |
| 342 | } |
| 343 | while (canvas->getSaveCount() > 1) { |
| 344 | canvas->clipRect(rect); |
| 345 | canvas->restore(); |
| 346 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 347 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Mike Reed | 993e92d | 2021-01-28 10:44:58 -0500 | [diff] [blame] | 351 | static void test_bad_bitmap(skiatest::Reporter* reporter) { |
| 352 | // missing pixels should return null for image |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 353 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 354 | bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); |
Mike Reed | 993e92d | 2021-01-28 10:44:58 -0500 | [diff] [blame] | 355 | auto img = bm.asImage(); |
| 356 | REPORTER_ASSERT(reporter, !img); |
| 357 | |
| 358 | // make sure we don't crash on a null image |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 359 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 360 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); |
Mike Reed | 993e92d | 2021-01-28 10:44:58 -0500 | [diff] [blame] | 361 | recordingCanvas->drawImage(nullptr, 0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 362 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 363 | |
| 364 | SkCanvas canvas; |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 365 | canvas.drawPicture(picture); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 366 | } |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 367 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 368 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 369 | // Test for crbug.com/229011 |
| 370 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 371 | SkIntToScalar(2), SkIntToScalar(2)); |
| 372 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 373 | SkIntToScalar(1), SkIntToScalar(1)); |
| 374 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 375 | SkIntToScalar(1), SkIntToScalar(1)); |
| 376 | |
| 377 | SkPath invPath; |
| 378 | invPath.addOval(rect1); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 379 | invPath.setFillType(SkPathFillType::kInverseEvenOdd); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 380 | SkPath path; |
| 381 | path.addOval(rect2); |
| 382 | SkPath path2; |
| 383 | path2.addOval(rect3); |
| 384 | SkIRect clipBounds; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 385 | SkPictureRecorder recorder; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 386 | |
| 387 | // Testing conservative-raster-clip that is enabled by PictureRecord |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 388 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 389 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 390 | canvas->clipPath(invPath); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 391 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 392 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 393 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 394 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 395 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 396 | } |
| 397 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 398 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 399 | canvas->clipPath(path); |
| 400 | canvas->clipPath(invPath); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 401 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 402 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 403 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 404 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 405 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 406 | } |
| 407 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 408 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
Michael Ludwig | 2f6e2f8 | 2021-08-03 13:08:50 -0400 | [diff] [blame] | 409 | canvas->clipPath(path, SkClipOp::kDifference); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 410 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 411 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 412 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 413 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 414 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 415 | } |
| 416 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 417 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
Michael Ludwig | 2f6e2f8 | 2021-08-03 13:08:50 -0400 | [diff] [blame] | 418 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 419 | canvas->clipPath(path2, SkClipOp::kDifference); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 420 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 421 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 422 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 423 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 424 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 425 | } |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 426 | } |
| 427 | |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 428 | static void test_cull_rect_reset(skiatest::Reporter* reporter) { |
| 429 | SkPictureRecorder recorder; |
| 430 | SkRect bounds = SkRect::MakeWH(10, 10); |
| 431 | SkRTreeFactory factory; |
| 432 | SkCanvas* canvas = recorder.beginRecording(bounds, &factory); |
| 433 | bounds = SkRect::MakeWH(100, 100); |
| 434 | SkPaint paint; |
| 435 | canvas->drawRect(bounds, paint); |
| 436 | canvas->drawRect(bounds, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 437 | sk_sp<SkPicture> p(recorder.finishRecordingAsPictureWithCull(bounds)); |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 438 | const SkBigPicture* picture = SkPicturePriv::AsSkBigPicture(p); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 439 | REPORTER_ASSERT(reporter, picture); |
| 440 | |
| 441 | SkRect finalCullRect = picture->cullRect(); |
| 442 | REPORTER_ASSERT(reporter, 0 == finalCullRect.fLeft); |
| 443 | REPORTER_ASSERT(reporter, 0 == finalCullRect.fTop); |
| 444 | REPORTER_ASSERT(reporter, 100 == finalCullRect.fBottom); |
| 445 | REPORTER_ASSERT(reporter, 100 == finalCullRect.fRight); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 449 | /** |
| 450 | * A canvas that records the number of clip commands. |
| 451 | */ |
| 452 | class ClipCountingCanvas : public SkCanvas { |
| 453 | public: |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 454 | ClipCountingCanvas(int width, int height) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 455 | : INHERITED(width, height) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 456 | , fClipCount(0){ |
| 457 | } |
| 458 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 459 | void onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 460 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 461 | this->INHERITED::onClipRect(r, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 464 | void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle)override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 465 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 466 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 469 | void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 470 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 471 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 472 | } |
| 473 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 474 | void onClipRegion(const SkRegion& deviceRgn, SkClipOp op) override { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 475 | fClipCount += 1; |
| 476 | this->INHERITED::onClipRegion(deviceRgn, op); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | unsigned getClipCount() const { return fClipCount; } |
| 480 | |
| 481 | private: |
| 482 | unsigned fClipCount; |
| 483 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 484 | using INHERITED = SkCanvas; |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 487 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 488 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 489 | SkPictureRecorder recorder; |
| 490 | recorder.beginRecording(0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 491 | sk_sp<SkPicture> empty(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 492 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 493 | // Empty pictures should still have a valid ID |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 494 | REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 495 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 496 | SkCanvas* canvas = recorder.beginRecording(1, 1); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 497 | canvas->drawColor(SK_ColorWHITE); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 498 | sk_sp<SkPicture> hasData(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 499 | // picture should have a non-zero id after recording |
| 500 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 501 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 502 | // both pictures should have different ids |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 503 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 504 | } |
| 505 | |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 506 | static void test_typeface(skiatest::Reporter* reporter) { |
| 507 | SkPictureRecorder recorder; |
| 508 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 509 | SkFont font(SkTypeface::MakeFromName("Arial", SkFontStyle::Italic())); |
| 510 | canvas->drawString("Q", 0, 10, font, SkPaint()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 511 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 512 | SkDynamicMemoryWStream stream; |
| 513 | picture->serialize(&stream); |
| 514 | } |
| 515 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 516 | DEF_TEST(Picture, reporter) { |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 517 | test_typeface(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 518 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 519 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 520 | test_serializing_empty_picture(); |
| 521 | #endif |
Mike Reed | 993e92d | 2021-01-28 10:44:58 -0500 | [diff] [blame] | 522 | test_bad_bitmap(reporter); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 523 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 524 | test_peephole(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 525 | test_clip_bound_opt(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 526 | test_gen_id(reporter); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 527 | test_cull_rect_reset(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 528 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 529 | |
| 530 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 531 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame] | 532 | auto img = bitmap.asImage(); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 533 | |
| 534 | // Don't care what these record, as long as they're legal. |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame] | 535 | canvas->drawImage(img, 0.0f, 0.0f); |
| 536 | canvas->drawImageRect(img, rect, rect, SkSamplingOptions(), nullptr, |
| 537 | SkCanvas::kStrict_SrcRectConstraint); |
| 538 | canvas->drawImage(img, 1, 1); // drawSprite |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 542 | SkBitmap empty; |
| 543 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 544 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 545 | draw_bitmaps(empty, canvas); |
| 546 | } |
| 547 | |
| 548 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 549 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 550 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 551 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 555 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 556 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 557 | SkCanvas canvas(dst); |
| 558 | |
| 559 | test_draw_bitmaps(&canvas); |
| 560 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 561 | |
| 562 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 563 | // This test is from crbug.com/344987. |
| 564 | // The commands are: |
| 565 | // saveLayer with paint that modifies alpha |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 566 | // drawBitmapRect |
| 567 | // drawBitmapRect |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 568 | // restore |
| 569 | // The bug was that this structure was modified so that: |
| 570 | // - The saveLayer and restore were eliminated |
| 571 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 572 | |
| 573 | // This test draws blue and red squares inside a 50% transparent |
| 574 | // layer. Both colours should show up muted. |
| 575 | // When the bug is present, the red square (the second bitmap) |
| 576 | // shows upwith full opacity. |
| 577 | |
| 578 | SkBitmap blueBM; |
| 579 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 580 | SkBitmap redBM; |
| 581 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 582 | SkPaint semiTransparent; |
| 583 | semiTransparent.setAlpha(0x80); |
| 584 | |
| 585 | SkPictureRecorder recorder; |
| 586 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 587 | canvas->drawColor(0); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 588 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 589 | canvas->saveLayer(nullptr, &semiTransparent); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 590 | canvas->drawImage(blueBM.asImage(), 25, 25); |
| 591 | canvas->drawImage(redBM.asImage(), 50, 50); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 592 | canvas->restore(); |
| 593 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 594 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 595 | |
| 596 | // Now replay the picture back on another canvas |
| 597 | // and check a couple of its pixels. |
| 598 | SkBitmap replayBM; |
| 599 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 600 | SkCanvas replayCanvas(replayBM); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 601 | picture->playback(&replayCanvas); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 602 | |
| 603 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 604 | // intead of a dark red. |
| 605 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 606 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 607 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 608 | |
Mike Klein | 508fd32 | 2020-02-12 09:45:41 -0600 | [diff] [blame] | 609 | struct CountingBBH : public SkBBoxHierarchy { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 610 | mutable int searchCalls; |
| 611 | |
Mike Klein | e1bad06 | 2020-02-11 16:16:12 -0600 | [diff] [blame] | 612 | CountingBBH() : searchCalls(0) {} |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 613 | |
Mike Klein | 508fd32 | 2020-02-12 09:45:41 -0600 | [diff] [blame] | 614 | void search(const SkRect& query, std::vector<int>* results) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 615 | this->searchCalls++; |
| 616 | } |
| 617 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 618 | void insert(const SkRect[], int) override {} |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 619 | size_t bytesUsed() const override { return 0; } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 620 | }; |
| 621 | |
| 622 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 623 | public: |
Mike Klein | 7a7ee94 | 2020-01-21 10:34:55 -0600 | [diff] [blame] | 624 | explicit SpoonFedBBHFactory(sk_sp<SkBBoxHierarchy> bbh) : fBBH(bbh) {} |
| 625 | sk_sp<SkBBoxHierarchy> operator()() const override { |
| 626 | return fBBH; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 627 | } |
| 628 | private: |
Mike Klein | 7a7ee94 | 2020-01-21 10:34:55 -0600 | [diff] [blame] | 629 | sk_sp<SkBBoxHierarchy> fBBH; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 630 | }; |
| 631 | |
| 632 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 633 | DEF_TEST(Picture_SkipBBH, r) { |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 634 | SkRect bound = SkRect::MakeWH(320, 240); |
Mike Klein | 7a7ee94 | 2020-01-21 10:34:55 -0600 | [diff] [blame] | 635 | |
Mike Klein | e1bad06 | 2020-02-11 16:16:12 -0600 | [diff] [blame] | 636 | auto bbh = sk_make_sp<CountingBBH>(); |
Mike Klein | 7a7ee94 | 2020-01-21 10:34:55 -0600 | [diff] [blame] | 637 | SpoonFedBBHFactory factory(bbh); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 638 | |
| 639 | SkPictureRecorder recorder; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 640 | SkCanvas* c = recorder.beginRecording(bound, &factory); |
| 641 | // Record a few ops so we don't hit a small- or empty- picture optimization. |
| 642 | c->drawRect(bound, SkPaint()); |
| 643 | c->drawRect(bound, SkPaint()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 644 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 645 | |
| 646 | SkCanvas big(640, 480), small(300, 200); |
| 647 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 648 | picture->playback(&big); |
Mike Klein | 7a7ee94 | 2020-01-21 10:34:55 -0600 | [diff] [blame] | 649 | REPORTER_ASSERT(r, bbh->searchCalls == 0); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 650 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 651 | picture->playback(&small); |
Mike Klein | 7a7ee94 | 2020-01-21 10:34:55 -0600 | [diff] [blame] | 652 | REPORTER_ASSERT(r, bbh->searchCalls == 1); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 653 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 654 | |
| 655 | DEF_TEST(Picture_BitmapLeak, r) { |
| 656 | SkBitmap mut, immut; |
| 657 | mut.allocN32Pixels(300, 200); |
| 658 | immut.allocN32Pixels(300, 200); |
| 659 | immut.setImmutable(); |
| 660 | SkASSERT(!mut.isImmutable()); |
| 661 | SkASSERT(immut.isImmutable()); |
| 662 | |
| 663 | // No one can hold a ref on our pixels yet. |
| 664 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 665 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 666 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 667 | sk_sp<SkPicture> pic; |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 668 | { |
| 669 | // we want the recorder to go out of scope before our subsequent checks, so we |
| 670 | // place it inside local braces. |
| 671 | SkPictureRecorder rec; |
| 672 | SkCanvas* canvas = rec.beginRecording(1920, 1200); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 673 | canvas->drawImage(mut.asImage(), 0, 0); |
| 674 | canvas->drawImage(immut.asImage(), 800, 600); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 675 | pic = rec.finishRecordingAsPicture(); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 676 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 677 | |
| 678 | // The picture shares the immutable pixels but copies the mutable ones. |
| 679 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 680 | REPORTER_ASSERT(r, !immut.pixelRef()->unique()); |
| 681 | |
| 682 | // When the picture goes away, it's just our bitmaps holding the refs. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 683 | pic = nullptr; |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 684 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 685 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 686 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 687 | |
| 688 | // getRecordingCanvas() should return a SkCanvas when recording, null when not recording. |
| 689 | DEF_TEST(Picture_getRecordingCanvas, r) { |
| 690 | SkPictureRecorder rec; |
| 691 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 692 | for (int i = 0; i < 3; i++) { |
| 693 | rec.beginRecording(100, 100); |
| 694 | REPORTER_ASSERT(r, rec.getRecordingCanvas()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 695 | rec.finishRecordingAsPicture(); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 696 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 697 | } |
| 698 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 699 | |
Mike Klein | a8ceb77 | 2019-05-20 12:45:50 +0000 | [diff] [blame] | 700 | DEF_TEST(MiniRecorderLeftHanging, r) { |
| 701 | // Any shader or other ref-counted effect will do just fine here. |
| 702 | SkPaint paint; |
| 703 | paint.setShader(SkShaders::Color(SK_ColorRED)); |
| 704 | |
| 705 | SkMiniRecorder rec; |
| 706 | REPORTER_ASSERT(r, rec.drawRect(SkRect::MakeWH(20,30), paint)); |
| 707 | // Don't call rec.detachPicture(). Test succeeds by not asserting or leaking the shader. |
| 708 | } |
| 709 | |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 710 | DEF_TEST(Picture_preserveCullRect, r) { |
| 711 | SkPictureRecorder recorder; |
| 712 | |
| 713 | SkCanvas* c = recorder.beginRecording(SkRect::MakeLTRB(1, 2, 3, 4)); |
| 714 | c->clear(SK_ColorCYAN); |
| 715 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 716 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 717 | SkDynamicMemoryWStream wstream; |
| 718 | picture->serialize(&wstream); |
| 719 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 720 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 721 | sk_sp<SkPicture> deserializedPicture(SkPicture::MakeFromStream(rstream.get())); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 722 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 723 | REPORTER_ASSERT(r, deserializedPicture != nullptr); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 724 | REPORTER_ASSERT(r, deserializedPicture->cullRect().left() == 1); |
| 725 | REPORTER_ASSERT(r, deserializedPicture->cullRect().top() == 2); |
| 726 | REPORTER_ASSERT(r, deserializedPicture->cullRect().right() == 3); |
| 727 | REPORTER_ASSERT(r, deserializedPicture->cullRect().bottom() == 4); |
| 728 | } |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 729 | |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 730 | |
| 731 | // If we record bounded ops into a picture with a big cull and calculate the |
| 732 | // bounds of those ops, we should trim down the picture cull to the ops' bounds. |
| 733 | // If we're not using an SkBBH, we shouldn't change it. |
| 734 | DEF_TEST(Picture_UpdatedCull_1, r) { |
Mike Klein | a8ceb77 | 2019-05-20 12:45:50 +0000 | [diff] [blame] | 735 | // Testing 1 draw exercises SkMiniPicture. |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 736 | SkRTreeFactory factory; |
| 737 | SkPictureRecorder recorder; |
| 738 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 739 | auto canvas = recorder.beginRecording(SkRectPriv::MakeLargest(), &factory); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 740 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 741 | auto pic = recorder.finishRecordingAsPicture(); |
| 742 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,20)); |
| 743 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 744 | canvas = recorder.beginRecording(SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 745 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 746 | pic = recorder.finishRecordingAsPicture(); |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 747 | REPORTER_ASSERT(r, pic->cullRect() == SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 748 | } |
| 749 | DEF_TEST(Picture_UpdatedCull_2, r) { |
| 750 | // Testing >1 draw exercises SkBigPicture. |
| 751 | SkRTreeFactory factory; |
| 752 | SkPictureRecorder recorder; |
| 753 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 754 | auto canvas = recorder.beginRecording(SkRectPriv::MakeLargest(), &factory); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 755 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 756 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 757 | auto pic = recorder.finishRecordingAsPicture(); |
| 758 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,40)); |
| 759 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 760 | canvas = recorder.beginRecording(SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 761 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 762 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 763 | pic = recorder.finishRecordingAsPicture(); |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 764 | REPORTER_ASSERT(r, pic->cullRect() == SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 765 | } |
Mike Klein | 7cc49d6 | 2017-08-14 10:39:28 -0400 | [diff] [blame] | 766 | |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 767 | DEF_TEST(Picture_RecordsFlush, r) { |
| 768 | SkPictureRecorder recorder; |
| 769 | |
| 770 | auto canvas = recorder.beginRecording(SkRect::MakeWH(100,100)); |
| 771 | for (int i = 0; i < 10; i++) { |
| 772 | canvas->clear(0); |
| 773 | for (int j = 0; j < 10; j++) { |
| 774 | canvas->drawRect(SkRect::MakeXYWH(i*10,j*10,10,10), SkPaint()); |
| 775 | } |
| 776 | canvas->flush(); |
| 777 | } |
| 778 | |
| 779 | // Did we record the flushes? |
| 780 | auto pic = recorder.finishRecordingAsPicture(); |
| 781 | REPORTER_ASSERT(r, pic->approximateOpCount() == 120); // 10 clears, 100 draws, 10 flushes |
| 782 | |
| 783 | // Do we serialize and deserialize flushes? |
| 784 | auto skp = pic->serialize(); |
| 785 | auto back = SkPicture::MakeFromData(skp->data(), skp->size()); |
| 786 | REPORTER_ASSERT(r, back->approximateOpCount() == pic->approximateOpCount()); |
| 787 | } |
Mike Klein | fbe6620 | 2018-01-26 09:49:48 -0500 | [diff] [blame] | 788 | |
| 789 | DEF_TEST(Placeholder, r) { |
| 790 | SkRect cull = { 0,0, 10,20 }; |
| 791 | |
| 792 | // Each placeholder is unique. |
| 793 | sk_sp<SkPicture> p1 = SkPicture::MakePlaceholder(cull), |
| 794 | p2 = SkPicture::MakePlaceholder(cull); |
| 795 | REPORTER_ASSERT(r, p1->cullRect() == p2->cullRect()); |
| 796 | REPORTER_ASSERT(r, p1->cullRect() == cull); |
| 797 | REPORTER_ASSERT(r, p1->uniqueID() != p2->uniqueID()); |
| 798 | |
| 799 | // Placeholders are never unrolled by SkCanvas (while other small pictures may be). |
| 800 | SkPictureRecorder recorder; |
| 801 | SkCanvas* canvas = recorder.beginRecording(cull); |
| 802 | canvas->drawPicture(p1); |
| 803 | canvas->drawPicture(p2); |
| 804 | sk_sp<SkPicture> pic = recorder.finishRecordingAsPicture(); |
| 805 | REPORTER_ASSERT(r, pic->approximateOpCount() == 2); |
Mike Klein | f647dc7 | 2020-08-19 13:55:05 -0500 | [diff] [blame] | 806 | |
| 807 | // Any upper limit when recursing into nested placeholders is fine as long |
| 808 | // as it doesn't overflow an int. |
| 809 | REPORTER_ASSERT(r, pic->approximateOpCount(/*nested?*/true) >= 2); |
| 810 | REPORTER_ASSERT(r, pic->approximateOpCount(/*nested?*/true) <= 10); |
Mike Klein | fbe6620 | 2018-01-26 09:49:48 -0500 | [diff] [blame] | 811 | } |
Mike Reed | 40d8297 | 2018-02-01 14:45:50 -0500 | [diff] [blame] | 812 | |
| 813 | DEF_TEST(Picture_empty_serial, reporter) { |
| 814 | SkPictureRecorder rec; |
| 815 | (void)rec.beginRecording(10, 10); |
| 816 | auto pic = rec.finishRecordingAsPicture(); |
| 817 | REPORTER_ASSERT(reporter, pic); |
| 818 | |
| 819 | auto data = pic->serialize(); |
| 820 | REPORTER_ASSERT(reporter, data); |
| 821 | |
| 822 | auto pic2 = SkPicture::MakeFromData(data->data(), data->size()); |
| 823 | REPORTER_ASSERT(reporter, pic2); |
| 824 | } |
| 825 | |
Mike Klein | 6d20d99 | 2019-09-12 12:51:27 -0500 | [diff] [blame] | 826 | |
| 827 | DEF_TEST(Picture_drawsNothing, r) { |
| 828 | // Tests that pic->cullRect().isEmpty() is a good way to test a picture |
| 829 | // recorded with an R-tree draws nothing. |
| 830 | struct { |
| 831 | bool draws_nothing; |
| 832 | void (*fn)(SkCanvas*); |
| 833 | } cases[] = { |
| 834 | { true, [](SkCanvas* c) { } }, |
| 835 | { true, [](SkCanvas* c) { c->save(); c->restore(); } }, |
| 836 | { true, [](SkCanvas* c) { c->save(); c->clipRect({0,0,5,5}); c->restore(); } }, |
| 837 | { true, [](SkCanvas* c) { c->clipRect({0,0,5,5}); } }, |
| 838 | |
| 839 | { false, [](SkCanvas* c) { c->drawRect({0,0,5,5}, SkPaint{}); } }, |
| 840 | { false, [](SkCanvas* c) { c->save(); c->drawRect({0,0,5,5}, SkPaint{}); c->restore(); } }, |
| 841 | { false, [](SkCanvas* c) { |
| 842 | c->drawRect({0,0, 5, 5}, SkPaint{}); |
| 843 | c->drawRect({5,5,10,10}, SkPaint{}); |
| 844 | }}, |
| 845 | }; |
| 846 | |
| 847 | for (const auto& c : cases) { |
| 848 | SkPictureRecorder rec; |
| 849 | SkRTreeFactory factory; |
| 850 | c.fn(rec.beginRecording(10,10, &factory)); |
| 851 | sk_sp<SkPicture> pic = rec.finishRecordingAsPicture(); |
| 852 | |
| 853 | REPORTER_ASSERT(r, pic->cullRect().isEmpty() == c.draws_nothing); |
| 854 | } |
| 855 | } |
Mike Klein | edf2d72 | 2019-10-02 10:53:39 -0500 | [diff] [blame] | 856 | |
| 857 | DEF_TEST(Picture_emptyNestedPictureBug, r) { |
| 858 | const SkRect bounds = {-5000, -5000, 5000, 5000}; |
| 859 | |
| 860 | SkPictureRecorder recorder; |
| 861 | SkRTreeFactory factory; |
| 862 | |
| 863 | // These three pictures should all draw the same but due to bugs they don't: |
| 864 | // |
| 865 | // 1) inner has enough content that it is recoreded as an SkBigPicture, |
| 866 | // and all its content falls outside the positive/positive quadrant, |
| 867 | // and it is recorded with an R-tree so we contract the cullRect to those bounds; |
| 868 | // |
| 869 | // 2) middle wraps inner, |
| 870 | // and it its recorded with an R-tree so we update middle's cullRect to inner's; |
| 871 | // |
| 872 | // 3) outer wraps inner, |
| 873 | // and notices that middle contains only one op, drawPicture(inner), |
| 874 | // so it plays middle back during recording rather than ref'ing middle, |
| 875 | // querying middle's R-tree with its SkCanvas' bounds* {0,0, 5000,5000}, |
| 876 | // finding nothing to draw. |
| 877 | // |
| 878 | // * The bug was that these bounds were not tracked as {-5000,-5000, 5000,5000}. |
| 879 | { |
| 880 | SkCanvas* canvas = recorder.beginRecording(bounds, &factory); |
| 881 | canvas->translate(-100,-100); |
| 882 | canvas->drawRect({0,0,50,50}, SkPaint{}); |
| 883 | } |
| 884 | sk_sp<SkPicture> inner = recorder.finishRecordingAsPicture(); |
| 885 | |
| 886 | recorder.beginRecording(bounds, &factory)->drawPicture(inner); |
| 887 | sk_sp<SkPicture> middle = recorder.finishRecordingAsPicture(); |
| 888 | |
| 889 | // This doesn't need &factory to reproduce the bug, |
| 890 | // but it's nice to see we come up with the same {-100,-100, -50,-50} bounds. |
| 891 | recorder.beginRecording(bounds, &factory)->drawPicture(middle); |
| 892 | sk_sp<SkPicture> outer = recorder.finishRecordingAsPicture(); |
| 893 | |
| 894 | REPORTER_ASSERT(r, (inner ->cullRect() == SkRect{-100,-100, -50,-50})); |
| 895 | REPORTER_ASSERT(r, (middle->cullRect() == SkRect{-100,-100, -50,-50})); |
| 896 | REPORTER_ASSERT(r, (outer ->cullRect() == SkRect{-100,-100, -50,-50})); // Used to fail. |
| 897 | } |
Mike Klein | 196e9fb | 2020-01-21 10:48:46 -0600 | [diff] [blame] | 898 | |
| 899 | DEF_TEST(Picture_fillsBBH, r) { |
| 900 | // Test empty (0 draws), mini (1 draw), and big (2+) pictures, making sure they fill the BBH. |
| 901 | const SkRect rects[] = { |
| 902 | { 0, 0, 20,20}, |
| 903 | {20,20, 40,40}, |
| 904 | }; |
| 905 | |
| 906 | for (int n = 0; n <= 2; n++) { |
| 907 | SkRTreeFactory factory; |
| 908 | SkPictureRecorder rec; |
| 909 | |
| 910 | sk_sp<SkBBoxHierarchy> bbh = factory(); |
| 911 | |
| 912 | SkCanvas* c = rec.beginRecording({0,0, 100,100}, bbh); |
| 913 | for (int i = 0; i < n; i++) { |
| 914 | c->drawRect(rects[i], SkPaint{}); |
| 915 | } |
| 916 | sk_sp<SkPicture> pic = rec.finishRecordingAsPicture(); |
| 917 | |
Mike Klein | 508fd32 | 2020-02-12 09:45:41 -0600 | [diff] [blame] | 918 | std::vector<int> results; |
| 919 | bbh->search({0,0, 100,100}, &results); |
| 920 | REPORTER_ASSERT(r, (int)results.size() == n, |
| 921 | "results.size() == %d, want %d\n", (int)results.size(), n); |
Mike Klein | 196e9fb | 2020-01-21 10:48:46 -0600 | [diff] [blame] | 922 | } |
| 923 | } |
Mike Reed | 18305c3 | 2020-08-17 17:12:13 -0400 | [diff] [blame] | 924 | |
| 925 | DEF_TEST(Picture_nested_op_count, r) { |
| 926 | auto make_pic = [](int n, sk_sp<SkPicture> pic) { |
| 927 | SkPictureRecorder rec; |
| 928 | SkCanvas* c = rec.beginRecording({0,0, 100,100}); |
| 929 | for (int i = 0; i < n; i++) { |
| 930 | if (pic) { |
| 931 | c->drawPicture(pic); |
| 932 | } else { |
| 933 | c->drawRect({0,0, 100,100}, SkPaint{}); |
| 934 | } |
| 935 | } |
| 936 | return rec.finishRecordingAsPicture(); |
| 937 | }; |
| 938 | |
| 939 | auto check = [r](sk_sp<SkPicture> pic, int shallow, int nested) { |
| 940 | int s = pic->approximateOpCount(false); |
| 941 | int n = pic->approximateOpCount(true); |
| 942 | REPORTER_ASSERT(r, s == shallow); |
| 943 | REPORTER_ASSERT(r, n == nested); |
| 944 | }; |
| 945 | |
| 946 | sk_sp<SkPicture> leaf1 = make_pic(1, nullptr); |
| 947 | check(leaf1, 1, 1); |
| 948 | |
| 949 | sk_sp<SkPicture> leaf10 = make_pic(10, nullptr); |
| 950 | check(leaf10, 10, 10); |
| 951 | |
| 952 | check(make_pic( 1, leaf1), 1, 1); |
| 953 | check(make_pic( 1, leaf10), 1, 10); |
| 954 | check(make_pic(10, leaf1), 10, 10); |
| 955 | check(make_pic(10, leaf10), 10, 100); |
| 956 | } |