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