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