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; |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 851 | paint.setTypeface(SkTypeface::MakeFromName("Arial", |
| 852 | SkFontStyle::FromOldStyle(SkTypeface::kItalic))); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 853 | canvas->drawString("Q", 0, 10, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 854 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 855 | SkDynamicMemoryWStream stream; |
| 856 | picture->serialize(&stream); |
| 857 | } |
| 858 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 859 | DEF_TEST(Picture, reporter) { |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 860 | test_typeface(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 861 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 862 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 863 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 864 | #else |
| 865 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 866 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 867 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 868 | test_peephole(); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 869 | #if SK_SUPPORT_GPU |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 870 | test_gpu_veto(reporter); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 871 | #endif |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 872 | test_images_are_found_by_willPlayBackBitmaps(reporter); |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 873 | test_analysis(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 874 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 875 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 876 | test_hierarchical(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 877 | test_gen_id(reporter); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 878 | test_cull_rect_reset(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 879 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 880 | |
| 881 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 882 | const SkPaint paint; |
| 883 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 884 | const SkIRect irect = { 2, 2, 3, 3 }; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 885 | int divs[] = { 2, 3 }; |
| 886 | SkCanvas::Lattice lattice; |
| 887 | lattice.fXCount = lattice.fYCount = 2; |
| 888 | lattice.fXDivs = lattice.fYDivs = divs; |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 889 | |
| 890 | // Don't care what these record, as long as they're legal. |
| 891 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 892 | canvas->drawBitmapRect(bitmap, rect, rect, &paint, SkCanvas::kStrict_SrcRectConstraint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 893 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 894 | canvas->drawBitmap(bitmap, 1, 1); // drawSprite |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 895 | canvas->drawBitmapLattice(bitmap, lattice, rect, &paint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 899 | SkBitmap empty; |
| 900 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 901 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 902 | draw_bitmaps(empty, canvas); |
| 903 | } |
| 904 | |
| 905 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 906 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 907 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 908 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 912 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 913 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 914 | SkCanvas canvas(dst); |
| 915 | |
| 916 | test_draw_bitmaps(&canvas); |
| 917 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 918 | |
| 919 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 920 | // This test is from crbug.com/344987. |
| 921 | // The commands are: |
| 922 | // saveLayer with paint that modifies alpha |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 923 | // drawBitmapRect |
| 924 | // drawBitmapRect |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 925 | // restore |
| 926 | // The bug was that this structure was modified so that: |
| 927 | // - The saveLayer and restore were eliminated |
| 928 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 929 | |
| 930 | // This test draws blue and red squares inside a 50% transparent |
| 931 | // layer. Both colours should show up muted. |
| 932 | // When the bug is present, the red square (the second bitmap) |
| 933 | // shows upwith full opacity. |
| 934 | |
| 935 | SkBitmap blueBM; |
| 936 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 937 | SkBitmap redBM; |
| 938 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 939 | SkPaint semiTransparent; |
| 940 | semiTransparent.setAlpha(0x80); |
| 941 | |
| 942 | SkPictureRecorder recorder; |
| 943 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 944 | canvas->drawColor(0); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 945 | |
| 946 | canvas->saveLayer(0, &semiTransparent); |
| 947 | canvas->drawBitmap(blueBM, 25, 25); |
| 948 | canvas->drawBitmap(redBM, 50, 50); |
| 949 | canvas->restore(); |
| 950 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 951 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 952 | |
| 953 | // Now replay the picture back on another canvas |
| 954 | // and check a couple of its pixels. |
| 955 | SkBitmap replayBM; |
| 956 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 957 | SkCanvas replayCanvas(replayBM); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 958 | picture->playback(&replayCanvas); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 959 | replayCanvas.flush(); |
| 960 | |
| 961 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 962 | // intead of a dark red. |
| 963 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 964 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 965 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 966 | |
| 967 | struct CountingBBH : public SkBBoxHierarchy { |
| 968 | mutable int searchCalls; |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 969 | SkRect rootBound; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 970 | |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 971 | CountingBBH(const SkRect& bound) : searchCalls(0), rootBound(bound) {} |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 972 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 973 | void search(const SkRect& query, SkTDArray<int>* results) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 974 | this->searchCalls++; |
| 975 | } |
| 976 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 977 | void insert(const SkRect[], int) override {} |
| 978 | virtual size_t bytesUsed() const override { return 0; } |
| 979 | SkRect getRootBound() const override { return rootBound; } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 980 | }; |
| 981 | |
| 982 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 983 | public: |
| 984 | explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 985 | SkBBoxHierarchy* operator()(const SkRect&) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 986 | return SkRef(fBBH); |
| 987 | } |
| 988 | private: |
| 989 | SkBBoxHierarchy* fBBH; |
| 990 | }; |
| 991 | |
| 992 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 993 | DEF_TEST(Picture_SkipBBH, r) { |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 994 | SkRect bound = SkRect::MakeWH(320, 240); |
| 995 | CountingBBH bbh(bound); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 996 | SpoonFedBBHFactory factory(&bbh); |
| 997 | |
| 998 | SkPictureRecorder recorder; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 999 | SkCanvas* c = recorder.beginRecording(bound, &factory); |
| 1000 | // Record a few ops so we don't hit a small- or empty- picture optimization. |
| 1001 | c->drawRect(bound, SkPaint()); |
| 1002 | c->drawRect(bound, SkPaint()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1003 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1004 | |
| 1005 | SkCanvas big(640, 480), small(300, 200); |
| 1006 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1007 | picture->playback(&big); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1008 | REPORTER_ASSERT(r, bbh.searchCalls == 0); |
| 1009 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1010 | picture->playback(&small); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1011 | REPORTER_ASSERT(r, bbh.searchCalls == 1); |
| 1012 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1013 | |
| 1014 | DEF_TEST(Picture_BitmapLeak, r) { |
| 1015 | SkBitmap mut, immut; |
| 1016 | mut.allocN32Pixels(300, 200); |
| 1017 | immut.allocN32Pixels(300, 200); |
| 1018 | immut.setImmutable(); |
| 1019 | SkASSERT(!mut.isImmutable()); |
| 1020 | SkASSERT(immut.isImmutable()); |
| 1021 | |
| 1022 | // No one can hold a ref on our pixels yet. |
| 1023 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1024 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1025 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1026 | sk_sp<SkPicture> pic; |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 1027 | { |
| 1028 | // we want the recorder to go out of scope before our subsequent checks, so we |
| 1029 | // place it inside local braces. |
| 1030 | SkPictureRecorder rec; |
| 1031 | SkCanvas* canvas = rec.beginRecording(1920, 1200); |
| 1032 | canvas->drawBitmap(mut, 0, 0); |
| 1033 | canvas->drawBitmap(immut, 800, 600); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1034 | pic = rec.finishRecordingAsPicture(); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 1035 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1036 | |
| 1037 | // The picture shares the immutable pixels but copies the mutable ones. |
| 1038 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1039 | REPORTER_ASSERT(r, !immut.pixelRef()->unique()); |
| 1040 | |
| 1041 | // When the picture goes away, it's just our bitmaps holding the refs. |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1042 | pic = nullptr; |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1043 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1044 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1045 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 1046 | |
| 1047 | // getRecordingCanvas() should return a SkCanvas when recording, null when not recording. |
| 1048 | DEF_TEST(Picture_getRecordingCanvas, r) { |
| 1049 | SkPictureRecorder rec; |
| 1050 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 1051 | for (int i = 0; i < 3; i++) { |
| 1052 | rec.beginRecording(100, 100); |
| 1053 | REPORTER_ASSERT(r, rec.getRecordingCanvas()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1054 | rec.finishRecordingAsPicture(); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 1055 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 1056 | } |
| 1057 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1058 | |
| 1059 | DEF_TEST(MiniRecorderLeftHanging, r) { |
| 1060 | // Any shader or other ref-counted effect will do just fine here. |
| 1061 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 1062 | paint.setShader(SkShader::MakeColorShader(SK_ColorRED)); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1063 | |
| 1064 | SkMiniRecorder rec; |
| 1065 | REPORTER_ASSERT(r, rec.drawRect(SkRect::MakeWH(20,30), paint)); |
| 1066 | // Don't call rec.detachPicture(). Test succeeds by not asserting or leaking the shader. |
| 1067 | } |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1068 | |
| 1069 | DEF_TEST(Picture_preserveCullRect, r) { |
| 1070 | SkPictureRecorder recorder; |
| 1071 | |
| 1072 | SkCanvas* c = recorder.beginRecording(SkRect::MakeLTRB(1, 2, 3, 4)); |
| 1073 | c->clear(SK_ColorCYAN); |
| 1074 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1075 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1076 | SkDynamicMemoryWStream wstream; |
| 1077 | picture->serialize(&wstream); |
| 1078 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1079 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 1080 | sk_sp<SkPicture> deserializedPicture(SkPicture::MakeFromStream(rstream.get())); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1081 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1082 | REPORTER_ASSERT(r, deserializedPicture != nullptr); |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1083 | REPORTER_ASSERT(r, deserializedPicture->cullRect().left() == 1); |
| 1084 | REPORTER_ASSERT(r, deserializedPicture->cullRect().top() == 2); |
| 1085 | REPORTER_ASSERT(r, deserializedPicture->cullRect().right() == 3); |
| 1086 | REPORTER_ASSERT(r, deserializedPicture->cullRect().bottom() == 4); |
| 1087 | } |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1088 | |
| 1089 | #if SK_SUPPORT_GPU |
| 1090 | |
| 1091 | DEF_TEST(PictureGpuAnalyzer, r) { |
| 1092 | SkPictureRecorder recorder; |
| 1093 | |
| 1094 | { |
| 1095 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 1096 | SkPaint paint; |
| 1097 | SkScalar intervals [] = { 10, 20 }; |
| 1098 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 25)); |
| 1099 | |
| 1100 | for (int i = 0; i < 50; ++i) { |
| 1101 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 1102 | } |
| 1103 | } |
| 1104 | sk_sp<SkPicture> vetoPicture(recorder.finishRecordingAsPicture()); |
| 1105 | |
| 1106 | SkPictureGpuAnalyzer analyzer; |
| 1107 | REPORTER_ASSERT(r, analyzer.suitableForGpuRasterization()); |
| 1108 | |
fmalita | 019db3f | 2016-05-31 06:32:57 -0700 | [diff] [blame] | 1109 | analyzer.analyzePicture(vetoPicture.get()); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1110 | REPORTER_ASSERT(r, !analyzer.suitableForGpuRasterization()); |
| 1111 | |
| 1112 | analyzer.reset(); |
| 1113 | REPORTER_ASSERT(r, analyzer.suitableForGpuRasterization()); |
| 1114 | |
| 1115 | recorder.beginRecording(10, 10)->drawPicture(vetoPicture); |
| 1116 | sk_sp<SkPicture> nestedVetoPicture(recorder.finishRecordingAsPicture()); |
| 1117 | |
fmalita | 019db3f | 2016-05-31 06:32:57 -0700 | [diff] [blame] | 1118 | analyzer.analyzePicture(nestedVetoPicture.get()); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1119 | REPORTER_ASSERT(r, !analyzer.suitableForGpuRasterization()); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 1120 | |
| 1121 | analyzer.reset(); |
| 1122 | |
| 1123 | const SkPath convexClip = make_convex_path(); |
| 1124 | const SkPath concaveClip = make_concave_path(); |
| 1125 | for (int i = 0; i < 50; ++i) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1126 | analyzer.analyzeClipPath(convexClip, kIntersect_SkClipOp, false); |
| 1127 | analyzer.analyzeClipPath(convexClip, kIntersect_SkClipOp, true); |
| 1128 | analyzer.analyzeClipPath(concaveClip, kIntersect_SkClipOp, false); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 1129 | } |
| 1130 | REPORTER_ASSERT(r, analyzer.suitableForGpuRasterization()); |
| 1131 | |
| 1132 | for (int i = 0; i < 50; ++i) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1133 | analyzer.analyzeClipPath(concaveClip, kIntersect_SkClipOp, true); |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 1134 | } |
| 1135 | REPORTER_ASSERT(r, !analyzer.suitableForGpuRasterization()); |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | #endif // SK_SUPPORT_GPU |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 1139 | |
| 1140 | // If we record bounded ops into a picture with a big cull and calculate the |
| 1141 | // bounds of those ops, we should trim down the picture cull to the ops' bounds. |
| 1142 | // If we're not using an SkBBH, we shouldn't change it. |
| 1143 | DEF_TEST(Picture_UpdatedCull_1, r) { |
| 1144 | // Testing 1 draw exercises SkMiniPicture. |
| 1145 | SkRTreeFactory factory; |
| 1146 | SkPictureRecorder recorder; |
| 1147 | |
| 1148 | auto canvas = recorder.beginRecording(SkRect::MakeLargest(), &factory); |
| 1149 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1150 | auto pic = recorder.finishRecordingAsPicture(); |
| 1151 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,20)); |
| 1152 | |
| 1153 | canvas = recorder.beginRecording(SkRect::MakeLargest()); |
| 1154 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1155 | pic = recorder.finishRecordingAsPicture(); |
| 1156 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeLargest()); |
| 1157 | } |
| 1158 | DEF_TEST(Picture_UpdatedCull_2, r) { |
| 1159 | // Testing >1 draw exercises SkBigPicture. |
| 1160 | SkRTreeFactory factory; |
| 1161 | SkPictureRecorder recorder; |
| 1162 | |
| 1163 | auto canvas = recorder.beginRecording(SkRect::MakeLargest(), &factory); |
| 1164 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1165 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 1166 | auto pic = recorder.finishRecordingAsPicture(); |
| 1167 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeWH(20,40)); |
| 1168 | |
| 1169 | canvas = recorder.beginRecording(SkRect::MakeLargest()); |
| 1170 | canvas->drawRect(SkRect::MakeWH(20,20), SkPaint{}); |
| 1171 | canvas->drawRect(SkRect::MakeWH(10,40), SkPaint{}); |
| 1172 | pic = recorder.finishRecordingAsPicture(); |
| 1173 | REPORTER_ASSERT(r, pic->cullRect() == SkRect::MakeLargest()); |
| 1174 | } |