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 | |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 8 | #include "SkBBHFactory.h" |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 9 | #include "SkBBoxHierarchy.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 10 | #include "SkBigPicture.h" |
| 11 | #include "SkBitmap.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 13 | #include "SkClipOp.h" |
| 14 | #include "SkClipOpPriv.h" |
| 15 | #include "SkColor.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 16 | #include "SkData.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 17 | #include "SkFontStyle.h" |
| 18 | #include "SkImageInfo.h" |
| 19 | #include "SkMatrix.h" |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 20 | #include "SkMiniRecorder.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 21 | #include "SkPaint.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 22 | #include "SkPath.h" |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 23 | #include "SkPicturePriv.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 24 | #include "SkPictureRecorder.h" |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 25 | #include "SkPixelRef.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 26 | #include "SkRandom.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 27 | #include "SkRect.h" |
| 28 | #include "SkRectPriv.h" |
| 29 | #include "SkRefCnt.h" |
| 30 | #include "SkScalar.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 31 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 32 | #include "SkStream.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 33 | #include "SkTypeface.h" |
| 34 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 35 | #include "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); |
| 561 | SkPaint paint; |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 562 | paint.setTypeface(SkTypeface::MakeFromName("Arial", SkFontStyle::Italic())); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 563 | canvas->drawString("Q", 0, 10, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 564 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 565 | SkDynamicMemoryWStream stream; |
| 566 | picture->serialize(&stream); |
| 567 | } |
| 568 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 569 | DEF_TEST(Picture, reporter) { |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 570 | test_typeface(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 571 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 572 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 573 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 574 | #else |
| 575 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 576 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 577 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 578 | test_peephole(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 579 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 580 | test_clip_expansion(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 581 | test_gen_id(reporter); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 582 | test_cull_rect_reset(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 583 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 584 | |
| 585 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 586 | const SkPaint paint; |
| 587 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 588 | const SkIRect irect = { 2, 2, 3, 3 }; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 589 | int divs[] = { 2, 3 }; |
| 590 | SkCanvas::Lattice lattice; |
| 591 | lattice.fXCount = lattice.fYCount = 2; |
| 592 | lattice.fXDivs = lattice.fYDivs = divs; |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 593 | |
| 594 | // Don't care what these record, as long as they're legal. |
| 595 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 596 | canvas->drawBitmapRect(bitmap, rect, rect, &paint, SkCanvas::kStrict_SrcRectConstraint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 597 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 598 | canvas->drawBitmap(bitmap, 1, 1); // drawSprite |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 599 | canvas->drawBitmapLattice(bitmap, lattice, rect, &paint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 603 | SkBitmap empty; |
| 604 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 605 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 606 | draw_bitmaps(empty, canvas); |
| 607 | } |
| 608 | |
| 609 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 610 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 611 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 612 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 616 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 617 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 618 | SkCanvas canvas(dst); |
| 619 | |
| 620 | test_draw_bitmaps(&canvas); |
| 621 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 622 | |
| 623 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 624 | // This test is from crbug.com/344987. |
| 625 | // The commands are: |
| 626 | // saveLayer with paint that modifies alpha |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 627 | // drawBitmapRect |
| 628 | // drawBitmapRect |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 629 | // restore |
| 630 | // The bug was that this structure was modified so that: |
| 631 | // - The saveLayer and restore were eliminated |
| 632 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 633 | |
| 634 | // This test draws blue and red squares inside a 50% transparent |
| 635 | // layer. Both colours should show up muted. |
| 636 | // When the bug is present, the red square (the second bitmap) |
| 637 | // shows upwith full opacity. |
| 638 | |
| 639 | SkBitmap blueBM; |
| 640 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 641 | SkBitmap redBM; |
| 642 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 643 | SkPaint semiTransparent; |
| 644 | semiTransparent.setAlpha(0x80); |
| 645 | |
| 646 | SkPictureRecorder recorder; |
| 647 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 648 | canvas->drawColor(0); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 649 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 650 | canvas->saveLayer(nullptr, &semiTransparent); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 651 | canvas->drawBitmap(blueBM, 25, 25); |
| 652 | canvas->drawBitmap(redBM, 50, 50); |
| 653 | canvas->restore(); |
| 654 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 655 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 656 | |
| 657 | // Now replay the picture back on another canvas |
| 658 | // and check a couple of its pixels. |
| 659 | SkBitmap replayBM; |
| 660 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 661 | SkCanvas replayCanvas(replayBM); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 662 | picture->playback(&replayCanvas); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 663 | replayCanvas.flush(); |
| 664 | |
| 665 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 666 | // intead of a dark red. |
| 667 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 668 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 669 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 670 | |
| 671 | struct CountingBBH : public SkBBoxHierarchy { |
| 672 | mutable int searchCalls; |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 673 | SkRect rootBound; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 674 | |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 675 | CountingBBH(const SkRect& bound) : searchCalls(0), rootBound(bound) {} |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 676 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 677 | void search(const SkRect& query, SkTDArray<int>* results) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 678 | this->searchCalls++; |
| 679 | } |
| 680 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 681 | void insert(const SkRect[], int) override {} |
| 682 | virtual size_t bytesUsed() const override { return 0; } |
| 683 | SkRect getRootBound() const override { return rootBound; } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 687 | public: |
| 688 | explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 689 | SkBBoxHierarchy* operator()(const SkRect&) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 690 | return SkRef(fBBH); |
| 691 | } |
| 692 | private: |
| 693 | SkBBoxHierarchy* fBBH; |
| 694 | }; |
| 695 | |
| 696 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 697 | DEF_TEST(Picture_SkipBBH, r) { |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 698 | SkRect bound = SkRect::MakeWH(320, 240); |
| 699 | CountingBBH bbh(bound); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 700 | SpoonFedBBHFactory factory(&bbh); |
| 701 | |
| 702 | SkPictureRecorder recorder; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 703 | SkCanvas* c = recorder.beginRecording(bound, &factory); |
| 704 | // Record a few ops so we don't hit a small- or empty- picture optimization. |
| 705 | c->drawRect(bound, SkPaint()); |
| 706 | c->drawRect(bound, SkPaint()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 707 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 708 | |
| 709 | SkCanvas big(640, 480), small(300, 200); |
| 710 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 711 | picture->playback(&big); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 712 | REPORTER_ASSERT(r, bbh.searchCalls == 0); |
| 713 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 714 | picture->playback(&small); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 715 | REPORTER_ASSERT(r, bbh.searchCalls == 1); |
| 716 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 717 | |
| 718 | DEF_TEST(Picture_BitmapLeak, r) { |
| 719 | SkBitmap mut, immut; |
| 720 | mut.allocN32Pixels(300, 200); |
| 721 | immut.allocN32Pixels(300, 200); |
| 722 | immut.setImmutable(); |
| 723 | SkASSERT(!mut.isImmutable()); |
| 724 | SkASSERT(immut.isImmutable()); |
| 725 | |
| 726 | // No one can hold a ref on our pixels yet. |
| 727 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 728 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 729 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 730 | sk_sp<SkPicture> pic; |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 731 | { |
| 732 | // we want the recorder to go out of scope before our subsequent checks, so we |
| 733 | // place it inside local braces. |
| 734 | SkPictureRecorder rec; |
| 735 | SkCanvas* canvas = rec.beginRecording(1920, 1200); |
| 736 | canvas->drawBitmap(mut, 0, 0); |
| 737 | canvas->drawBitmap(immut, 800, 600); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 738 | pic = rec.finishRecordingAsPicture(); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 739 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 740 | |
| 741 | // The picture shares the immutable pixels but copies the mutable ones. |
| 742 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 743 | REPORTER_ASSERT(r, !immut.pixelRef()->unique()); |
| 744 | |
| 745 | // When the picture goes away, it's just our bitmaps holding the refs. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 746 | pic = nullptr; |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 747 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 748 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 749 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 750 | |
| 751 | // getRecordingCanvas() should return a SkCanvas when recording, null when not recording. |
| 752 | DEF_TEST(Picture_getRecordingCanvas, r) { |
| 753 | SkPictureRecorder rec; |
| 754 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 755 | for (int i = 0; i < 3; i++) { |
| 756 | rec.beginRecording(100, 100); |
| 757 | REPORTER_ASSERT(r, rec.getRecordingCanvas()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 758 | rec.finishRecordingAsPicture(); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 759 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 760 | } |
| 761 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 762 | |
| 763 | DEF_TEST(MiniRecorderLeftHanging, r) { |
| 764 | // Any shader or other ref-counted effect will do just fine here. |
| 765 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 766 | paint.setShader(SkShader::MakeColorShader(SK_ColorRED)); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 767 | |
| 768 | SkMiniRecorder rec; |
| 769 | REPORTER_ASSERT(r, rec.drawRect(SkRect::MakeWH(20,30), paint)); |
| 770 | // Don't call rec.detachPicture(). Test succeeds by not asserting or leaking the shader. |
| 771 | } |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 772 | |
| 773 | DEF_TEST(Picture_preserveCullRect, r) { |
| 774 | SkPictureRecorder recorder; |
| 775 | |
| 776 | SkCanvas* c = recorder.beginRecording(SkRect::MakeLTRB(1, 2, 3, 4)); |
| 777 | c->clear(SK_ColorCYAN); |
| 778 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 779 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 780 | SkDynamicMemoryWStream wstream; |
| 781 | picture->serialize(&wstream); |
| 782 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 783 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 784 | sk_sp<SkPicture> deserializedPicture(SkPicture::MakeFromStream(rstream.get())); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 785 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 786 | REPORTER_ASSERT(r, deserializedPicture != nullptr); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 787 | REPORTER_ASSERT(r, deserializedPicture->cullRect().left() == 1); |
| 788 | REPORTER_ASSERT(r, deserializedPicture->cullRect().top() == 2); |
| 789 | REPORTER_ASSERT(r, deserializedPicture->cullRect().right() == 3); |
| 790 | REPORTER_ASSERT(r, deserializedPicture->cullRect().bottom() == 4); |
| 791 | } |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 792 | |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 793 | |
| 794 | // If we record bounded ops into a picture with a big cull and calculate the |
| 795 | // bounds of those ops, we should trim down the picture cull to the ops' bounds. |
| 796 | // If we're not using an SkBBH, we shouldn't change it. |
| 797 | DEF_TEST(Picture_UpdatedCull_1, r) { |
| 798 | // Testing 1 draw exercises SkMiniPicture. |
| 799 | SkRTreeFactory factory; |
| 800 | SkPictureRecorder recorder; |
| 801 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 802 | auto canvas = recorder.beginRecording(SkRectPriv::MakeLargest(), &factory); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 803 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 804 | auto pic = recorder.finishRecordingAsPicture(); |
| 805 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,20)); |
| 806 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 807 | canvas = recorder.beginRecording(SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 808 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 809 | pic = recorder.finishRecordingAsPicture(); |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 810 | REPORTER_ASSERT(r, pic->cullRect() == SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 811 | } |
| 812 | DEF_TEST(Picture_UpdatedCull_2, r) { |
| 813 | // Testing >1 draw exercises SkBigPicture. |
| 814 | SkRTreeFactory factory; |
| 815 | SkPictureRecorder recorder; |
| 816 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 817 | auto canvas = recorder.beginRecording(SkRectPriv::MakeLargest(), &factory); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 818 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 819 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 820 | auto pic = recorder.finishRecordingAsPicture(); |
| 821 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,40)); |
| 822 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 823 | canvas = recorder.beginRecording(SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 824 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 825 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 826 | pic = recorder.finishRecordingAsPicture(); |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 827 | REPORTER_ASSERT(r, pic->cullRect() == SkRectPriv::MakeLargest()); |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 828 | } |
Mike Klein | 7cc49d6 | 2017-08-14 10:39:28 -0400 | [diff] [blame] | 829 | |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 830 | DEF_TEST(Picture_RecordsFlush, r) { |
| 831 | SkPictureRecorder recorder; |
| 832 | |
| 833 | auto canvas = recorder.beginRecording(SkRect::MakeWH(100,100)); |
| 834 | for (int i = 0; i < 10; i++) { |
| 835 | canvas->clear(0); |
| 836 | for (int j = 0; j < 10; j++) { |
| 837 | canvas->drawRect(SkRect::MakeXYWH(i*10,j*10,10,10), SkPaint()); |
| 838 | } |
| 839 | canvas->flush(); |
| 840 | } |
| 841 | |
| 842 | // Did we record the flushes? |
| 843 | auto pic = recorder.finishRecordingAsPicture(); |
| 844 | REPORTER_ASSERT(r, pic->approximateOpCount() == 120); // 10 clears, 100 draws, 10 flushes |
| 845 | |
| 846 | // Do we serialize and deserialize flushes? |
| 847 | auto skp = pic->serialize(); |
| 848 | auto back = SkPicture::MakeFromData(skp->data(), skp->size()); |
| 849 | REPORTER_ASSERT(r, back->approximateOpCount() == pic->approximateOpCount()); |
| 850 | } |
Mike Klein | fbe6620 | 2018-01-26 09:49:48 -0500 | [diff] [blame] | 851 | |
| 852 | DEF_TEST(Placeholder, r) { |
| 853 | SkRect cull = { 0,0, 10,20 }; |
| 854 | |
| 855 | // Each placeholder is unique. |
| 856 | sk_sp<SkPicture> p1 = SkPicture::MakePlaceholder(cull), |
| 857 | p2 = SkPicture::MakePlaceholder(cull); |
| 858 | REPORTER_ASSERT(r, p1->cullRect() == p2->cullRect()); |
| 859 | REPORTER_ASSERT(r, p1->cullRect() == cull); |
| 860 | REPORTER_ASSERT(r, p1->uniqueID() != p2->uniqueID()); |
| 861 | |
| 862 | // Placeholders are never unrolled by SkCanvas (while other small pictures may be). |
| 863 | SkPictureRecorder recorder; |
| 864 | SkCanvas* canvas = recorder.beginRecording(cull); |
| 865 | canvas->drawPicture(p1); |
| 866 | canvas->drawPicture(p2); |
| 867 | sk_sp<SkPicture> pic = recorder.finishRecordingAsPicture(); |
| 868 | REPORTER_ASSERT(r, pic->approximateOpCount() == 2); |
| 869 | } |
Mike Reed | 40d8297 | 2018-02-01 14:45:50 -0500 | [diff] [blame] | 870 | |
| 871 | DEF_TEST(Picture_empty_serial, reporter) { |
| 872 | SkPictureRecorder rec; |
| 873 | (void)rec.beginRecording(10, 10); |
| 874 | auto pic = rec.finishRecordingAsPicture(); |
| 875 | REPORTER_ASSERT(reporter, pic); |
| 876 | |
| 877 | auto data = pic->serialize(); |
| 878 | REPORTER_ASSERT(reporter, data); |
| 879 | |
| 880 | auto pic2 = SkPicture::MakeFromData(data->data(), data->size()); |
| 881 | REPORTER_ASSERT(reporter, pic2); |
| 882 | } |
| 883 | |