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" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 20 | #include "SkPaint.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 21 | #include "SkPicture.h" |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 22 | #include "SkPictureAnalyzer.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" |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 25 | #include "SkPixelSerializer.h" |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 26 | #include "SkMiniRecorder.h" |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 27 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 28 | #include "SkRandom.h" |
tomhudson | 158fcaa | 2014-11-19 10:41:14 -0800 | [diff] [blame] | 29 | #include "SkRecord.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 30 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 31 | #include "SkStream.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 32 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 33 | |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 34 | #include "Test.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 35 | |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 36 | #include "SkLumaColorFilter.h" |
| 37 | #include "SkColorFilterImageFilter.h" |
| 38 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 39 | 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] | 40 | bm->allocN32Pixels(w, h); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 41 | bm->eraseColor(color); |
| 42 | if (immutable) { |
| 43 | bm->setImmutable(); |
| 44 | } |
| 45 | } |
| 46 | |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 47 | // For a while willPlayBackBitmaps() ignored SkImages and just looked for SkBitmaps. |
| 48 | static void test_images_are_found_by_willPlayBackBitmaps(skiatest::Reporter* reporter) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 49 | // We just need _some_ SkImage |
| 50 | const SkPMColor pixel = 0; |
| 51 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 52 | sk_sp<SkImage> image(SkImage::MakeRasterCopy(SkPixmap(info, &pixel, sizeof(pixel)))); |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 53 | |
| 54 | SkPictureRecorder recorder; |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 55 | recorder.beginRecording(100,100)->drawImage(image, 0,0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 56 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 57 | |
| 58 | REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps()); |
| 59 | } |
| 60 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 61 | /* Hit a few SkPicture::Analysis cases not handled elsewhere. */ |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 62 | static void test_analysis(skiatest::Reporter* reporter) { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 63 | SkPictureRecorder recorder; |
| 64 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 65 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 66 | { |
| 67 | canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ()); |
| 68 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 69 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps()); |
| 71 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 72 | canvas = recorder.beginRecording(100, 100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 73 | { |
| 74 | SkPaint paint; |
| 75 | // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader |
| 76 | // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here. |
| 77 | SkBitmap bitmap; |
| 78 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2)); |
| 79 | bitmap.eraseColor(SK_ColorBLUE); |
| 80 | *(bitmap.getAddr32(0, 0)) = SK_ColorGREEN; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 81 | paint.setShader(SkShader::MakeBitmapShader(bitmap, SkShader::kClamp_TileMode, |
| 82 | SkShader::kClamp_TileMode)); |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, paint.getShader()->isAImage()); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 84 | |
| 85 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 86 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, recorder.finishRecordingAsPicture()->willPlayBackBitmaps()); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 91 | #ifdef SK_DEBUG |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 92 | // Ensure that deleting an empty SkPicture does not assert. Asserts only fire |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 93 | // in debug mode, so only run in debug mode. |
| 94 | static void test_deleting_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 95 | SkPictureRecorder recorder; |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 96 | // Creates an SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 97 | recorder.beginRecording(0, 0); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 98 | // Turns that into an SkPicture |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 99 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 100 | // Ceates a new SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 101 | recorder.beginRecording(0, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 105 | static void test_serializing_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 106 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 107 | recorder.beginRecording(0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 108 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 109 | SkDynamicMemoryWStream stream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 110 | picture->serialize(&stream); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 111 | } |
| 112 | #endif |
| 113 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 114 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 115 | SkPaint paint; |
| 116 | SkRect rect = SkRect::MakeWH(50, 50); |
| 117 | |
| 118 | SkScalar unit = rand.nextUScalar1(); |
| 119 | if (unit <= 0.3) { |
| 120 | // SkDebugf("save\n"); |
| 121 | canvas->save(); |
| 122 | } else if (unit <= 0.6) { |
| 123 | // SkDebugf("restore\n"); |
| 124 | canvas->restore(); |
| 125 | } else if (unit <= 0.9) { |
| 126 | // SkDebugf("clip\n"); |
| 127 | canvas->clipRect(rect); |
| 128 | } else { |
| 129 | // SkDebugf("draw\n"); |
| 130 | canvas->drawPaint(paint); |
| 131 | } |
| 132 | } |
| 133 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 134 | #if SK_SUPPORT_GPU |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 135 | |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 136 | static SkPath make_convex_path() { |
| 137 | SkPath path; |
| 138 | path.lineTo(100, 0); |
| 139 | path.lineTo(50, 100); |
| 140 | path.close(); |
| 141 | |
| 142 | return path; |
| 143 | } |
| 144 | |
| 145 | static SkPath make_concave_path() { |
| 146 | SkPath path; |
| 147 | path.lineTo(50, 50); |
| 148 | path.lineTo(100, 0); |
| 149 | path.lineTo(50, 100); |
| 150 | path.close(); |
| 151 | |
| 152 | return path; |
| 153 | } |
| 154 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 155 | static void test_gpu_veto(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 156 | SkPictureRecorder recorder; |
| 157 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 158 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 159 | { |
| 160 | SkPath path; |
| 161 | path.moveTo(0, 0); |
| 162 | path.lineTo(50, 50); |
| 163 | |
| 164 | SkScalar intervals[] = { 1.0f, 1.0f }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 165 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0)); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 166 | |
| 167 | SkPaint paint; |
| 168 | paint.setStyle(SkPaint::kStroke_Style); |
| 169 | paint.setPathEffect(dash); |
| 170 | |
robertphillips | 98b0315 | 2015-01-26 11:29:36 -0800 | [diff] [blame] | 171 | for (int i = 0; i < 50; ++i) { |
| 172 | canvas->drawPath(path, paint); |
| 173 | } |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 174 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 175 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 176 | // path effects currently render an SkPicture undesireable for GPU rendering |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 177 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | const char *reason = nullptr; |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 179 | REPORTER_ASSERT(reporter, |
| 180 | !SkPictureGpuAnalyzer(picture).suitableForGpuRasterization(&reason)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 181 | REPORTER_ASSERT(reporter, reason); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 182 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 183 | canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 184 | { |
| 185 | SkPath path; |
| 186 | |
| 187 | path.moveTo(0, 0); |
| 188 | path.lineTo(0, 50); |
| 189 | path.lineTo(25, 25); |
| 190 | path.lineTo(50, 50); |
| 191 | path.lineTo(50, 0); |
| 192 | path.close(); |
| 193 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 194 | |
| 195 | SkPaint paint; |
| 196 | paint.setAntiAlias(true); |
| 197 | for (int i = 0; i < 50; ++i) { |
| 198 | canvas->drawPath(path, paint); |
| 199 | } |
| 200 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 201 | picture = recorder.finishRecordingAsPicture(); |
jvanverth | d86b07a | 2014-11-04 08:50:15 -0800 | [diff] [blame] | 202 | // A lot of small AA concave paths should be fine for GPU rendering |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 203 | REPORTER_ASSERT(reporter, SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
jvanverth | d86b07a | 2014-11-04 08:50:15 -0800 | [diff] [blame] | 204 | |
| 205 | canvas = recorder.beginRecording(100, 100); |
| 206 | { |
| 207 | SkPath path; |
| 208 | |
| 209 | path.moveTo(0, 0); |
| 210 | path.lineTo(0, 100); |
| 211 | path.lineTo(50, 50); |
| 212 | path.lineTo(100, 100); |
| 213 | path.lineTo(100, 0); |
| 214 | path.close(); |
| 215 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 216 | |
| 217 | SkPaint paint; |
| 218 | paint.setAntiAlias(true); |
| 219 | for (int i = 0; i < 50; ++i) { |
| 220 | canvas->drawPath(path, paint); |
| 221 | } |
| 222 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 223 | picture = recorder.finishRecordingAsPicture(); |
jvanverth | d86b07a | 2014-11-04 08:50:15 -0800 | [diff] [blame] | 224 | // A lot of large AA concave paths currently render an SkPicture undesireable for GPU rendering |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 225 | REPORTER_ASSERT(reporter, !SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 226 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 227 | canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 228 | { |
| 229 | SkPath path; |
| 230 | |
| 231 | path.moveTo(0, 0); |
| 232 | path.lineTo(0, 50); |
| 233 | path.lineTo(25, 25); |
| 234 | path.lineTo(50, 50); |
| 235 | path.lineTo(50, 0); |
| 236 | path.close(); |
| 237 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 238 | |
| 239 | SkPaint paint; |
| 240 | paint.setAntiAlias(true); |
| 241 | paint.setStyle(SkPaint::kStroke_Style); |
| 242 | paint.setStrokeWidth(0); |
| 243 | for (int i = 0; i < 50; ++i) { |
| 244 | canvas->drawPath(path, paint); |
| 245 | } |
| 246 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 247 | picture = recorder.finishRecordingAsPicture(); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 248 | // hairline stroked AA concave paths are fine for GPU rendering |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 249 | REPORTER_ASSERT(reporter, SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 250 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 251 | canvas = recorder.beginRecording(100, 100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 252 | { |
| 253 | SkPaint paint; |
| 254 | SkScalar intervals [] = { 10, 20 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 255 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 25)); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 256 | |
| 257 | SkPoint points [2] = { { 0, 0 }, { 100, 0 } }; |
robertphillips | 98b0315 | 2015-01-26 11:29:36 -0800 | [diff] [blame] | 258 | |
| 259 | for (int i = 0; i < 50; ++i) { |
| 260 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint); |
| 261 | } |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 262 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 263 | picture = recorder.finishRecordingAsPicture(); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 264 | // fast-path dashed effects are fine for GPU rendering ... |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 265 | REPORTER_ASSERT(reporter, SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 266 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 267 | canvas = recorder.beginRecording(100, 100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 268 | { |
| 269 | SkPaint paint; |
| 270 | SkScalar intervals [] = { 10, 20 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 271 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 25)); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 272 | |
robertphillips | 98b0315 | 2015-01-26 11:29:36 -0800 | [diff] [blame] | 273 | for (int i = 0; i < 50; ++i) { |
| 274 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 275 | } |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 276 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 277 | picture = recorder.finishRecordingAsPicture(); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 278 | // ... but only when applied to drawPoint() calls |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, !SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 280 | |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 281 | canvas = recorder.beginRecording(100, 100); |
| 282 | { |
| 283 | const SkPath convexClip = make_convex_path(); |
| 284 | const SkPath concaveClip = make_concave_path(); |
| 285 | |
| 286 | for (int i = 0; i < 50; ++i) { |
| 287 | canvas->clipPath(convexClip); |
| 288 | canvas->clipPath(concaveClip); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 289 | canvas->clipPath(convexClip, kIntersect_SkClipOp, true); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 290 | canvas->drawRect(SkRect::MakeWH(100, 100), SkPaint()); |
| 291 | } |
| 292 | } |
| 293 | picture = recorder.finishRecordingAsPicture(); |
| 294 | // Convex clips and non-AA concave clips are fine on the GPU. |
| 295 | REPORTER_ASSERT(reporter, SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
| 296 | |
| 297 | canvas = recorder.beginRecording(100, 100); |
| 298 | { |
| 299 | const SkPath concaveClip = make_concave_path(); |
| 300 | for (int i = 0; i < 50; ++i) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 301 | canvas->clipPath(concaveClip, kIntersect_SkClipOp, true); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 302 | canvas->drawRect(SkRect::MakeWH(100, 100), SkPaint()); |
| 303 | } |
| 304 | } |
| 305 | picture = recorder.finishRecordingAsPicture(); |
| 306 | // ... but AA concave clips are not. |
| 307 | REPORTER_ASSERT(reporter, !SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
| 308 | |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 309 | // Nest the previous picture inside a new one. |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 310 | canvas = recorder.beginRecording(100, 100); |
| 311 | { |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 312 | canvas->drawPicture(picture); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 313 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 314 | picture = recorder.finishRecordingAsPicture(); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 315 | REPORTER_ASSERT(reporter, !SkPictureGpuAnalyzer(picture).suitableForGpuRasterization()); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 316 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 317 | |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 318 | #endif // SK_SUPPORT_GPU |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 319 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 320 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 321 | canvas->restoreToCount(1); |
| 322 | canvas->save(); |
| 323 | canvas->save(); |
| 324 | canvas->save(); |
| 325 | } |
| 326 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 327 | /** |
| 328 | * A canvas that records the number of saves, saveLayers and restores. |
| 329 | */ |
| 330 | class SaveCountingCanvas : public SkCanvas { |
| 331 | public: |
| 332 | SaveCountingCanvas(int width, int height) |
| 333 | : INHERITED(width, height) |
| 334 | , fSaveCount(0) |
| 335 | , fSaveLayerCount(0) |
| 336 | , fRestoreCount(0){ |
| 337 | } |
| 338 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 339 | SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 340 | ++fSaveLayerCount; |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 341 | return this->INHERITED::getSaveLayerStrategy(rec); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 342 | } |
| 343 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 344 | void willSave() override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 345 | ++fSaveCount; |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 346 | this->INHERITED::willSave(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 347 | } |
| 348 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 349 | void willRestore() override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 350 | ++fRestoreCount; |
| 351 | this->INHERITED::willRestore(); |
| 352 | } |
| 353 | |
| 354 | unsigned int getSaveCount() const { return fSaveCount; } |
| 355 | unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
| 356 | unsigned int getRestoreCount() const { return fRestoreCount; } |
| 357 | |
| 358 | private: |
| 359 | unsigned int fSaveCount; |
| 360 | unsigned int fSaveLayerCount; |
| 361 | unsigned int fRestoreCount; |
| 362 | |
| 363 | typedef SkCanvas INHERITED; |
| 364 | }; |
| 365 | |
skia.committer@gmail.com | 8e7d37d | 2014-05-28 03:06:06 +0000 | [diff] [blame] | 366 | void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 367 | unsigned int numSaves, unsigned int numSaveLayers, |
| 368 | unsigned int numRestores) { |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 369 | SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 370 | SkScalarCeilToInt(picture->cullRect().height())); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 371 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 372 | picture->playback(&canvas); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 373 | |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 374 | // Optimizations may have removed these, |
| 375 | // so expect to have seen no more than num{Saves,SaveLayers,Restores}. |
| 376 | REPORTER_ASSERT(reporter, numSaves >= canvas.getSaveCount()); |
| 377 | REPORTER_ASSERT(reporter, numSaveLayers >= canvas.getSaveLayerCount()); |
| 378 | REPORTER_ASSERT(reporter, numRestores >= canvas.getRestoreCount()); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // This class exists so SkPicture can friend it and give it access to |
| 382 | // the 'partialReplay' method. |
| 383 | class SkPictureRecorderReplayTester { |
| 384 | public: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 385 | static sk_sp<SkPicture> Copy(SkPictureRecorder* recorder) { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 386 | SkPictureRecorder recorder2; |
| 387 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 388 | SkCanvas* canvas = recorder2.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 389 | |
| 390 | recorder->partialReplay(canvas); |
| 391 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 392 | return recorder2.finishRecordingAsPicture(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 393 | } |
| 394 | }; |
| 395 | |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 396 | static void create_imbalance(SkCanvas* canvas) { |
| 397 | SkRect clipRect = SkRect::MakeWH(2, 2); |
| 398 | SkRect drawRect = SkRect::MakeWH(10, 10); |
| 399 | canvas->save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 400 | canvas->clipRect(clipRect, kReplace_SkClipOp); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 401 | canvas->translate(1.0f, 1.0f); |
| 402 | SkPaint p; |
| 403 | p.setColor(SK_ColorGREEN); |
| 404 | canvas->drawRect(drawRect, p); |
| 405 | // no restore |
| 406 | } |
| 407 | |
| 408 | // This tests that replaying a potentially unbalanced picture into a canvas |
| 409 | // doesn't affect the canvas' save count or matrix/clip state. |
| 410 | static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { |
| 411 | SkBitmap bm; |
| 412 | bm.allocN32Pixels(4, 3); |
| 413 | SkCanvas canvas(bm); |
| 414 | |
| 415 | int beforeSaveCount = canvas.getSaveCount(); |
| 416 | |
| 417 | SkMatrix beforeMatrix = canvas.getTotalMatrix(); |
| 418 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 419 | SkRect beforeClip = canvas.getLocalClipBounds(); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 420 | |
| 421 | canvas.drawPicture(picture); |
| 422 | |
| 423 | REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); |
| 424 | REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); |
| 425 | |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 426 | SkRect afterClip = canvas.getLocalClipBounds(); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 427 | |
| 428 | REPORTER_ASSERT(reporter, afterClip == beforeClip); |
| 429 | } |
| 430 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 431 | // Test out SkPictureRecorder::partialReplay |
| 432 | DEF_TEST(PictureRecorder_replay, reporter) { |
| 433 | // check save/saveLayer state |
| 434 | { |
| 435 | SkPictureRecorder recorder; |
| 436 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 437 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 438 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 439 | canvas->saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 440 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 441 | sk_sp<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 442 | |
| 443 | // The extra save and restore comes from the Copy process. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 444 | check_save_state(reporter, copy.get(), 2, 1, 3); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 445 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 446 | canvas->saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 447 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 448 | sk_sp<SkPicture> final(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 449 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 450 | check_save_state(reporter, final.get(), 1, 2, 3); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 451 | |
| 452 | // The copy shouldn't pick up any operations added after it was made |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 453 | check_save_state(reporter, copy.get(), 2, 1, 3); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // (partially) check leakage of draw ops |
| 457 | { |
| 458 | SkPictureRecorder recorder; |
| 459 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 460 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 461 | |
| 462 | SkRect r = SkRect::MakeWH(5, 5); |
| 463 | SkPaint p; |
| 464 | |
| 465 | canvas->drawRect(r, p); |
| 466 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 467 | sk_sp<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 468 | |
| 469 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 470 | |
| 471 | SkBitmap bm; |
| 472 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 473 | |
| 474 | r.offset(5.0f, 5.0f); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 475 | canvas->drawBitmapRect(bm, r, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 476 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 477 | sk_sp<SkPicture> final(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 478 | REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); |
| 479 | |
| 480 | REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); |
| 481 | |
| 482 | // The snapshot shouldn't pick up any operations added after it was made |
| 483 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 484 | } |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 485 | |
| 486 | // Recreate the Android partialReplay test case |
| 487 | { |
| 488 | SkPictureRecorder recorder; |
| 489 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 490 | SkCanvas* canvas = recorder.beginRecording(4, 3, nullptr, 0); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 491 | create_imbalance(canvas); |
| 492 | |
| 493 | int expectedSaveCount = canvas->getSaveCount(); |
| 494 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 495 | sk_sp<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 496 | check_balance(reporter, copy.get()); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 497 | |
| 498 | REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); |
| 499 | |
| 500 | // End the recording of source to test the picture finalization |
| 501 | // process isn't complicated by the partialReplay step |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 502 | sk_sp<SkPicture> final(recorder.finishRecordingAsPicture()); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 503 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 504 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 505 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 506 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 507 | SkCanvas testCanvas(100, 100); |
| 508 | set_canvas_to_save_count_4(&testCanvas); |
| 509 | |
| 510 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 511 | |
| 512 | SkPaint paint; |
| 513 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 514 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 515 | SkPictureRecorder recorder; |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 516 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 517 | { |
| 518 | // Create picture with 2 unbalanced saves |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 519 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 520 | canvas->save(); |
| 521 | canvas->translate(10, 10); |
| 522 | canvas->drawRect(rect, paint); |
| 523 | canvas->save(); |
| 524 | canvas->translate(10, 10); |
| 525 | canvas->drawRect(rect, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 526 | sk_sp<SkPicture> extraSavePicture(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 527 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 528 | testCanvas.drawPicture(extraSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 529 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 530 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 531 | |
| 532 | set_canvas_to_save_count_4(&testCanvas); |
| 533 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 534 | { |
| 535 | // Create picture with 2 unbalanced restores |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 536 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 537 | canvas->save(); |
| 538 | canvas->translate(10, 10); |
| 539 | canvas->drawRect(rect, paint); |
| 540 | canvas->save(); |
| 541 | canvas->translate(10, 10); |
| 542 | canvas->drawRect(rect, paint); |
| 543 | canvas->restore(); |
| 544 | canvas->restore(); |
| 545 | canvas->restore(); |
| 546 | canvas->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 547 | sk_sp<SkPicture> extraRestorePicture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 548 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 549 | testCanvas.drawPicture(extraRestorePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 550 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 551 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 552 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 553 | set_canvas_to_save_count_4(&testCanvas); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 554 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 555 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 556 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 557 | canvas->translate(10, 10); |
| 558 | canvas->drawRect(rect, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 559 | sk_sp<SkPicture> noSavePicture(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 560 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 561 | testCanvas.drawPicture(noSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 562 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 563 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 564 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 565 | } |
| 566 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 567 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 568 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 569 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 570 | SkPictureRecorder recorder; |
| 571 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 572 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 573 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 574 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 575 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 576 | |
| 577 | for (int i = 0; i < 1000; ++i) { |
| 578 | rand_op(canvas, rand); |
| 579 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 580 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 581 | |
| 582 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 586 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 587 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 588 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 589 | for (int i = 0; i < 100; ++i) { |
| 590 | canvas->save(); |
| 591 | } |
| 592 | while (canvas->getSaveCount() > 1) { |
| 593 | canvas->clipRect(rect); |
| 594 | canvas->restore(); |
| 595 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 596 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 600 | #ifndef SK_DEBUG |
| 601 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 602 | // should never do this. |
| 603 | static void test_bad_bitmap() { |
| 604 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 605 | // fail. |
| 606 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 607 | bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 608 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 609 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 610 | recordingCanvas->drawBitmap(bm, 0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 611 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 612 | |
| 613 | SkCanvas canvas; |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 614 | canvas.drawPicture(picture); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 615 | } |
| 616 | #endif |
| 617 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 618 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 619 | // Test for crbug.com/229011 |
| 620 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 621 | SkIntToScalar(2), SkIntToScalar(2)); |
| 622 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 623 | SkIntToScalar(1), SkIntToScalar(1)); |
| 624 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 625 | SkIntToScalar(1), SkIntToScalar(1)); |
| 626 | |
| 627 | SkPath invPath; |
| 628 | invPath.addOval(rect1); |
| 629 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 630 | SkPath path; |
| 631 | path.addOval(rect2); |
| 632 | SkPath path2; |
| 633 | path2.addOval(rect3); |
| 634 | SkIRect clipBounds; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 635 | SkPictureRecorder recorder; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 636 | |
| 637 | // Testing conservative-raster-clip that is enabled by PictureRecord |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 638 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 639 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 640 | canvas->clipPath(invPath); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 641 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 642 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 643 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 644 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 645 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 646 | } |
| 647 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 648 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 649 | canvas->clipPath(path); |
| 650 | canvas->clipPath(invPath); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 651 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 652 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 653 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 654 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 655 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 656 | } |
| 657 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 658 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 659 | canvas->clipPath(path); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 660 | canvas->clipPath(invPath, kUnion_SkClipOp); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 661 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 662 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 663 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 664 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 665 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 666 | } |
| 667 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 668 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 669 | canvas->clipPath(path, kDifference_SkClipOp); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 670 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 671 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 672 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 673 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 674 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 675 | } |
| 676 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 677 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 678 | canvas->clipPath(path, kReverseDifference_SkClipOp); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 679 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 680 | // True clip is actually empty in this case, but the best |
| 681 | // determination we can make using only bounds as input is that the |
| 682 | // clip is included in the bounds of 'path'. |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 683 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 684 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 685 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 686 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 687 | } |
| 688 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 689 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 690 | canvas->clipPath(path, kIntersect_SkClipOp); |
| 691 | canvas->clipPath(path2, kXOR_SkClipOp); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 692 | clipBounds = canvas->getDeviceClipBounds(); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 693 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 694 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 695 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 696 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 697 | } |
| 698 | } |
| 699 | |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 700 | static void test_cull_rect_reset(skiatest::Reporter* reporter) { |
| 701 | SkPictureRecorder recorder; |
| 702 | SkRect bounds = SkRect::MakeWH(10, 10); |
| 703 | SkRTreeFactory factory; |
| 704 | SkCanvas* canvas = recorder.beginRecording(bounds, &factory); |
| 705 | bounds = SkRect::MakeWH(100, 100); |
| 706 | SkPaint paint; |
| 707 | canvas->drawRect(bounds, paint); |
| 708 | canvas->drawRect(bounds, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 709 | sk_sp<SkPicture> p(recorder.finishRecordingAsPictureWithCull(bounds)); |
mtklein | eedc334 | 2015-07-08 08:26:39 -0700 | [diff] [blame] | 710 | const SkBigPicture* picture = p->asSkBigPicture(); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 711 | REPORTER_ASSERT(reporter, picture); |
| 712 | |
| 713 | SkRect finalCullRect = picture->cullRect(); |
| 714 | REPORTER_ASSERT(reporter, 0 == finalCullRect.fLeft); |
| 715 | REPORTER_ASSERT(reporter, 0 == finalCullRect.fTop); |
| 716 | REPORTER_ASSERT(reporter, 100 == finalCullRect.fBottom); |
| 717 | REPORTER_ASSERT(reporter, 100 == finalCullRect.fRight); |
| 718 | |
| 719 | const SkBBoxHierarchy* pictureBBH = picture->bbh(); |
| 720 | SkRect bbhCullRect = pictureBBH->getRootBound(); |
| 721 | REPORTER_ASSERT(reporter, 0 == bbhCullRect.fLeft); |
| 722 | REPORTER_ASSERT(reporter, 0 == bbhCullRect.fTop); |
| 723 | REPORTER_ASSERT(reporter, 100 == bbhCullRect.fBottom); |
| 724 | REPORTER_ASSERT(reporter, 100 == bbhCullRect.fRight); |
| 725 | } |
| 726 | |
| 727 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 728 | /** |
| 729 | * A canvas that records the number of clip commands. |
| 730 | */ |
| 731 | class ClipCountingCanvas : public SkCanvas { |
| 732 | public: |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 733 | ClipCountingCanvas(int width, int height) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 734 | : INHERITED(width, height) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 735 | , fClipCount(0){ |
| 736 | } |
| 737 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 738 | void onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 739 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 740 | this->INHERITED::onClipRect(r, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 743 | void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle)override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 744 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 745 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 748 | void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 749 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 750 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 751 | } |
| 752 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 753 | void onClipRegion(const SkRegion& deviceRgn, SkClipOp op) override { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 754 | fClipCount += 1; |
| 755 | this->INHERITED::onClipRegion(deviceRgn, op); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | unsigned getClipCount() const { return fClipCount; } |
| 759 | |
| 760 | private: |
| 761 | unsigned fClipCount; |
| 762 | |
| 763 | typedef SkCanvas INHERITED; |
| 764 | }; |
| 765 | |
| 766 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 767 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 768 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 769 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 770 | canvas->clipRect(SkRect::MakeEmpty(), kReplace_SkClipOp); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 771 | // The following expanding clip should not be skipped. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 772 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), kUnion_SkClipOp); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 773 | // Draw something so the optimizer doesn't just fold the world. |
| 774 | SkPaint p; |
| 775 | p.setColor(SK_ColorBLUE); |
| 776 | canvas->drawPaint(p); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 777 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 778 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 779 | ClipCountingCanvas testCanvas(10, 10); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 780 | picture->playback(&testCanvas); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 781 | |
| 782 | // Both clips should be present on playback. |
| 783 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 784 | } |
| 785 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 786 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 787 | SkBitmap bm; |
| 788 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 789 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 790 | SkPictureRecorder recorder; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 791 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 792 | recorder.beginRecording(10, 10); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 793 | sk_sp<SkPicture> childPlain(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 794 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 795 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 796 | recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 797 | sk_sp<SkPicture> childWithBitmap(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 798 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 799 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 800 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 801 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 802 | canvas->drawPicture(childPlain); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 803 | sk_sp<SkPicture> parentPP(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 804 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 805 | } |
| 806 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 807 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 808 | canvas->drawPicture(childWithBitmap); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 809 | sk_sp<SkPicture> parentPWB(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 810 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 811 | } |
| 812 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 813 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 814 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 815 | canvas->drawPicture(childPlain); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 816 | sk_sp<SkPicture> parentWBP(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 817 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 818 | } |
| 819 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 820 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 821 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 822 | canvas->drawPicture(childWithBitmap); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 823 | sk_sp<SkPicture> parentWBWB(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 824 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 825 | } |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 826 | } |
| 827 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 828 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 829 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 830 | SkPictureRecorder recorder; |
| 831 | recorder.beginRecording(0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 832 | sk_sp<SkPicture> empty(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 833 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 834 | // Empty pictures should still have a valid ID |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 835 | REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 836 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 837 | SkCanvas* canvas = recorder.beginRecording(1, 1); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 838 | canvas->drawColor(SK_ColorWHITE); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 839 | sk_sp<SkPicture> hasData(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 840 | // picture should have a non-zero id after recording |
| 841 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 842 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 843 | // both pictures should have different ids |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 844 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 845 | } |
| 846 | |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 847 | static void test_typeface(skiatest::Reporter* reporter) { |
| 848 | SkPictureRecorder recorder; |
| 849 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 850 | SkPaint paint; |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 851 | paint.setTypeface(SkTypeface::MakeFromName("Arial", SkFontStyle::Italic())); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 852 | canvas->drawString("Q", 0, 10, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 853 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 854 | SkDynamicMemoryWStream stream; |
| 855 | picture->serialize(&stream); |
| 856 | } |
| 857 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 858 | DEF_TEST(Picture, reporter) { |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 859 | test_typeface(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 860 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 861 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 862 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 863 | #else |
| 864 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 865 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 866 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 867 | test_peephole(); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 868 | #if SK_SUPPORT_GPU |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 869 | test_gpu_veto(reporter); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 870 | #endif |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 871 | test_images_are_found_by_willPlayBackBitmaps(reporter); |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 872 | test_analysis(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 873 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 874 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 875 | test_hierarchical(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 876 | test_gen_id(reporter); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 877 | test_cull_rect_reset(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 878 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 879 | |
| 880 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 881 | const SkPaint paint; |
| 882 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 883 | const SkIRect irect = { 2, 2, 3, 3 }; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 884 | int divs[] = { 2, 3 }; |
| 885 | SkCanvas::Lattice lattice; |
| 886 | lattice.fXCount = lattice.fYCount = 2; |
| 887 | lattice.fXDivs = lattice.fYDivs = divs; |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 888 | |
| 889 | // Don't care what these record, as long as they're legal. |
| 890 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 891 | canvas->drawBitmapRect(bitmap, rect, rect, &paint, SkCanvas::kStrict_SrcRectConstraint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 892 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 893 | canvas->drawBitmap(bitmap, 1, 1); // drawSprite |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 894 | canvas->drawBitmapLattice(bitmap, lattice, rect, &paint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 898 | SkBitmap empty; |
| 899 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 900 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 901 | draw_bitmaps(empty, canvas); |
| 902 | } |
| 903 | |
| 904 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 905 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 906 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 907 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 911 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 912 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 913 | SkCanvas canvas(dst); |
| 914 | |
| 915 | test_draw_bitmaps(&canvas); |
| 916 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 917 | |
| 918 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 919 | // This test is from crbug.com/344987. |
| 920 | // The commands are: |
| 921 | // saveLayer with paint that modifies alpha |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 922 | // drawBitmapRect |
| 923 | // drawBitmapRect |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 924 | // restore |
| 925 | // The bug was that this structure was modified so that: |
| 926 | // - The saveLayer and restore were eliminated |
| 927 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 928 | |
| 929 | // This test draws blue and red squares inside a 50% transparent |
| 930 | // layer. Both colours should show up muted. |
| 931 | // When the bug is present, the red square (the second bitmap) |
| 932 | // shows upwith full opacity. |
| 933 | |
| 934 | SkBitmap blueBM; |
| 935 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 936 | SkBitmap redBM; |
| 937 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 938 | SkPaint semiTransparent; |
| 939 | semiTransparent.setAlpha(0x80); |
| 940 | |
| 941 | SkPictureRecorder recorder; |
| 942 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 943 | canvas->drawColor(0); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 944 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 945 | canvas->saveLayer(nullptr, &semiTransparent); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 946 | canvas->drawBitmap(blueBM, 25, 25); |
| 947 | canvas->drawBitmap(redBM, 50, 50); |
| 948 | canvas->restore(); |
| 949 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 950 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 951 | |
| 952 | // Now replay the picture back on another canvas |
| 953 | // and check a couple of its pixels. |
| 954 | SkBitmap replayBM; |
| 955 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 956 | SkCanvas replayCanvas(replayBM); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 957 | picture->playback(&replayCanvas); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 958 | replayCanvas.flush(); |
| 959 | |
| 960 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 961 | // intead of a dark red. |
| 962 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 963 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 964 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 965 | |
| 966 | struct CountingBBH : public SkBBoxHierarchy { |
| 967 | mutable int searchCalls; |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 968 | SkRect rootBound; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 969 | |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 970 | CountingBBH(const SkRect& bound) : searchCalls(0), rootBound(bound) {} |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 971 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 972 | void search(const SkRect& query, SkTDArray<int>* results) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 973 | this->searchCalls++; |
| 974 | } |
| 975 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 976 | void insert(const SkRect[], int) override {} |
| 977 | virtual size_t bytesUsed() const override { return 0; } |
| 978 | SkRect getRootBound() const override { return rootBound; } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 982 | public: |
| 983 | explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 984 | SkBBoxHierarchy* operator()(const SkRect&) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 985 | return SkRef(fBBH); |
| 986 | } |
| 987 | private: |
| 988 | SkBBoxHierarchy* fBBH; |
| 989 | }; |
| 990 | |
| 991 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 992 | DEF_TEST(Picture_SkipBBH, r) { |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 993 | SkRect bound = SkRect::MakeWH(320, 240); |
| 994 | CountingBBH bbh(bound); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 995 | SpoonFedBBHFactory factory(&bbh); |
| 996 | |
| 997 | SkPictureRecorder recorder; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 998 | SkCanvas* c = recorder.beginRecording(bound, &factory); |
| 999 | // Record a few ops so we don't hit a small- or empty- picture optimization. |
| 1000 | c->drawRect(bound, SkPaint()); |
| 1001 | c->drawRect(bound, SkPaint()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1002 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1003 | |
| 1004 | SkCanvas big(640, 480), small(300, 200); |
| 1005 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1006 | picture->playback(&big); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1007 | REPORTER_ASSERT(r, bbh.searchCalls == 0); |
| 1008 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1009 | picture->playback(&small); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1010 | REPORTER_ASSERT(r, bbh.searchCalls == 1); |
| 1011 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1012 | |
| 1013 | DEF_TEST(Picture_BitmapLeak, r) { |
| 1014 | SkBitmap mut, immut; |
| 1015 | mut.allocN32Pixels(300, 200); |
| 1016 | immut.allocN32Pixels(300, 200); |
| 1017 | immut.setImmutable(); |
| 1018 | SkASSERT(!mut.isImmutable()); |
| 1019 | SkASSERT(immut.isImmutable()); |
| 1020 | |
| 1021 | // No one can hold a ref on our pixels yet. |
| 1022 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1023 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1024 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1025 | sk_sp<SkPicture> pic; |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 1026 | { |
| 1027 | // we want the recorder to go out of scope before our subsequent checks, so we |
| 1028 | // place it inside local braces. |
| 1029 | SkPictureRecorder rec; |
| 1030 | SkCanvas* canvas = rec.beginRecording(1920, 1200); |
| 1031 | canvas->drawBitmap(mut, 0, 0); |
| 1032 | canvas->drawBitmap(immut, 800, 600); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1033 | pic = rec.finishRecordingAsPicture(); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 1034 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1035 | |
| 1036 | // The picture shares the immutable pixels but copies the mutable ones. |
| 1037 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1038 | REPORTER_ASSERT(r, !immut.pixelRef()->unique()); |
| 1039 | |
| 1040 | // When the picture goes away, it's just our bitmaps holding the refs. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1041 | pic = nullptr; |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1042 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1043 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1044 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 1045 | |
| 1046 | // getRecordingCanvas() should return a SkCanvas when recording, null when not recording. |
| 1047 | DEF_TEST(Picture_getRecordingCanvas, r) { |
| 1048 | SkPictureRecorder rec; |
| 1049 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 1050 | for (int i = 0; i < 3; i++) { |
| 1051 | rec.beginRecording(100, 100); |
| 1052 | REPORTER_ASSERT(r, rec.getRecordingCanvas()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1053 | rec.finishRecordingAsPicture(); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 1054 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 1055 | } |
| 1056 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1057 | |
| 1058 | DEF_TEST(MiniRecorderLeftHanging, r) { |
| 1059 | // Any shader or other ref-counted effect will do just fine here. |
| 1060 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 1061 | paint.setShader(SkShader::MakeColorShader(SK_ColorRED)); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1062 | |
| 1063 | SkMiniRecorder rec; |
| 1064 | REPORTER_ASSERT(r, rec.drawRect(SkRect::MakeWH(20,30), paint)); |
| 1065 | // Don't call rec.detachPicture(). Test succeeds by not asserting or leaking the shader. |
| 1066 | } |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1067 | |
| 1068 | DEF_TEST(Picture_preserveCullRect, r) { |
| 1069 | SkPictureRecorder recorder; |
| 1070 | |
| 1071 | SkCanvas* c = recorder.beginRecording(SkRect::MakeLTRB(1, 2, 3, 4)); |
| 1072 | c->clear(SK_ColorCYAN); |
| 1073 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1074 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1075 | SkDynamicMemoryWStream wstream; |
| 1076 | picture->serialize(&wstream); |
| 1077 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1078 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 1079 | sk_sp<SkPicture> deserializedPicture(SkPicture::MakeFromStream(rstream.get())); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1080 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1081 | REPORTER_ASSERT(r, deserializedPicture != nullptr); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1082 | REPORTER_ASSERT(r, deserializedPicture->cullRect().left() == 1); |
| 1083 | REPORTER_ASSERT(r, deserializedPicture->cullRect().top() == 2); |
| 1084 | REPORTER_ASSERT(r, deserializedPicture->cullRect().right() == 3); |
| 1085 | REPORTER_ASSERT(r, deserializedPicture->cullRect().bottom() == 4); |
| 1086 | } |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1087 | |
| 1088 | #if SK_SUPPORT_GPU |
| 1089 | |
| 1090 | DEF_TEST(PictureGpuAnalyzer, r) { |
| 1091 | SkPictureRecorder recorder; |
| 1092 | |
| 1093 | { |
| 1094 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 1095 | SkPaint paint; |
| 1096 | SkScalar intervals [] = { 10, 20 }; |
| 1097 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 25)); |
| 1098 | |
| 1099 | for (int i = 0; i < 50; ++i) { |
| 1100 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 1101 | } |
| 1102 | } |
| 1103 | sk_sp<SkPicture> vetoPicture(recorder.finishRecordingAsPicture()); |
| 1104 | |
| 1105 | SkPictureGpuAnalyzer analyzer; |
| 1106 | REPORTER_ASSERT(r, analyzer.suitableForGpuRasterization()); |
| 1107 | |
fmalita | 019db3f | 2016-05-31 06:32:57 -0700 | [diff] [blame] | 1108 | analyzer.analyzePicture(vetoPicture.get()); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1109 | REPORTER_ASSERT(r, !analyzer.suitableForGpuRasterization()); |
| 1110 | |
| 1111 | analyzer.reset(); |
| 1112 | REPORTER_ASSERT(r, analyzer.suitableForGpuRasterization()); |
| 1113 | |
| 1114 | recorder.beginRecording(10, 10)->drawPicture(vetoPicture); |
| 1115 | sk_sp<SkPicture> nestedVetoPicture(recorder.finishRecordingAsPicture()); |
| 1116 | |
fmalita | 019db3f | 2016-05-31 06:32:57 -0700 | [diff] [blame] | 1117 | analyzer.analyzePicture(nestedVetoPicture.get()); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1118 | REPORTER_ASSERT(r, !analyzer.suitableForGpuRasterization()); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 1119 | |
| 1120 | analyzer.reset(); |
| 1121 | |
| 1122 | const SkPath convexClip = make_convex_path(); |
| 1123 | const SkPath concaveClip = make_concave_path(); |
| 1124 | for (int i = 0; i < 50; ++i) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1125 | analyzer.analyzeClipPath(convexClip, kIntersect_SkClipOp, false); |
| 1126 | analyzer.analyzeClipPath(convexClip, kIntersect_SkClipOp, true); |
| 1127 | analyzer.analyzeClipPath(concaveClip, kIntersect_SkClipOp, false); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 1128 | } |
| 1129 | REPORTER_ASSERT(r, analyzer.suitableForGpuRasterization()); |
| 1130 | |
| 1131 | for (int i = 0; i < 50; ++i) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1132 | analyzer.analyzeClipPath(concaveClip, kIntersect_SkClipOp, true); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 1133 | } |
| 1134 | REPORTER_ASSERT(r, !analyzer.suitableForGpuRasterization()); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | #endif // SK_SUPPORT_GPU |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 1138 | |
| 1139 | // If we record bounded ops into a picture with a big cull and calculate the |
| 1140 | // bounds of those ops, we should trim down the picture cull to the ops' bounds. |
| 1141 | // If we're not using an SkBBH, we shouldn't change it. |
| 1142 | DEF_TEST(Picture_UpdatedCull_1, r) { |
| 1143 | // Testing 1 draw exercises SkMiniPicture. |
| 1144 | SkRTreeFactory factory; |
| 1145 | SkPictureRecorder recorder; |
| 1146 | |
| 1147 | auto canvas = recorder.beginRecording(SkRect::MakeLargest(), &factory); |
| 1148 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1149 | auto pic = recorder.finishRecordingAsPicture(); |
| 1150 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,20)); |
| 1151 | |
| 1152 | canvas = recorder.beginRecording(SkRect::MakeLargest()); |
| 1153 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1154 | pic = recorder.finishRecordingAsPicture(); |
| 1155 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeLargest()); |
| 1156 | } |
| 1157 | DEF_TEST(Picture_UpdatedCull_2, r) { |
| 1158 | // Testing >1 draw exercises SkBigPicture. |
| 1159 | SkRTreeFactory factory; |
| 1160 | SkPictureRecorder recorder; |
| 1161 | |
| 1162 | auto canvas = recorder.beginRecording(SkRect::MakeLargest(), &factory); |
| 1163 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1164 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 1165 | auto pic = recorder.finishRecordingAsPicture(); |
| 1166 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,40)); |
| 1167 | |
| 1168 | canvas = recorder.beginRecording(SkRect::MakeLargest()); |
| 1169 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1170 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 1171 | pic = recorder.finishRecordingAsPicture(); |
| 1172 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeLargest()); |
| 1173 | } |
Mike Klein | 7cc49d6 | 2017-08-14 10:39:28 -0400 | [diff] [blame] | 1174 | |
| 1175 | DEF_TEST(Picture_RecordsFlush, r) { |
| 1176 | SkPictureRecorder recorder; |
| 1177 | |
| 1178 | auto canvas = recorder.beginRecording(SkRect::MakeWH(100,100)); |
| 1179 | for (int i = 0; i < 10; i++) { |
| 1180 | canvas->clear(0); |
| 1181 | for (int j = 0; j < 10; j++) { |
| 1182 | canvas->drawRect(SkRect::MakeXYWH(i*10,j*10,10,10), SkPaint()); |
| 1183 | } |
| 1184 | canvas->flush(); |
| 1185 | } |
| 1186 | |
| 1187 | // Did we record the flushes? |
| 1188 | auto pic = recorder.finishRecordingAsPicture(); |
| 1189 | REPORTER_ASSERT(r, pic->approximateOpCount() == 120); // 10 clears, 100 draws, 10 flushes |
| 1190 | |
| 1191 | // Do we serialize and deserialize flushes? |
| 1192 | auto skp = pic->serialize(); |
| 1193 | auto back = SkPicture::MakeFromData(skp->data(), skp->size()); |
| 1194 | REPORTER_ASSERT(r, back->approximateOpCount() == pic->approximateOpCount()); |
| 1195 | } |