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