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