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 | |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 8 | #include "SkBBoxHierarchy.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 9 | #include "SkBlurImageFilter.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
robertphillips | d8aa7b7 | 2014-10-30 16:45:02 -0700 | [diff] [blame] | 11 | #include "SkColorMatrixFilter.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 13 | #include "SkDashPathEffect.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 14 | #include "SkData.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 15 | #include "SkImageGenerator.h" |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 16 | #include "SkError.h" |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 17 | #include "SkImageEncoder.h" |
| 18 | #include "SkImageGenerator.h" |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 19 | #include "SkLayerInfo.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" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 22 | #include "SkPictureRecorder.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 23 | #include "SkPictureUtils.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); |
| 52 | SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(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); |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 56 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 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 | } |
| 69 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 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; |
| 81 | SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode, |
| 82 | SkShader::kClamp_TileMode); |
| 83 | paint.setShader(shader)->unref(); |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, shader->isABitmap()); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 85 | |
| 86 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 87 | } |
| 88 | picture.reset(recorder.endRecording()); |
| 89 | REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps()); |
| 90 | } |
| 91 | |
| 92 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 93 | #ifdef SK_DEBUG |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 94 | // Ensure that deleting an empty SkPicture does not assert. Asserts only fire |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 95 | // in debug mode, so only run in debug mode. |
| 96 | static void test_deleting_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 97 | SkPictureRecorder recorder; |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 98 | // Creates an SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 99 | recorder.beginRecording(0, 0); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 100 | // Turns that into an SkPicture |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 101 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 102 | // Ceates a new SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 103 | recorder.beginRecording(0, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 107 | static void test_serializing_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 108 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 109 | recorder.beginRecording(0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 110 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 111 | SkDynamicMemoryWStream stream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 112 | picture->serialize(&stream); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 113 | } |
| 114 | #endif |
| 115 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 116 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 117 | SkPaint paint; |
| 118 | SkRect rect = SkRect::MakeWH(50, 50); |
| 119 | |
| 120 | SkScalar unit = rand.nextUScalar1(); |
| 121 | if (unit <= 0.3) { |
| 122 | // SkDebugf("save\n"); |
| 123 | canvas->save(); |
| 124 | } else if (unit <= 0.6) { |
| 125 | // SkDebugf("restore\n"); |
| 126 | canvas->restore(); |
| 127 | } else if (unit <= 0.9) { |
| 128 | // SkDebugf("clip\n"); |
| 129 | canvas->clipRect(rect); |
| 130 | } else { |
| 131 | // SkDebugf("draw\n"); |
| 132 | canvas->drawPaint(paint); |
| 133 | } |
| 134 | } |
| 135 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 136 | #if SK_SUPPORT_GPU |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 137 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 138 | static void test_gpu_veto(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 139 | SkPictureRecorder recorder; |
| 140 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 141 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 142 | { |
| 143 | SkPath path; |
| 144 | path.moveTo(0, 0); |
| 145 | path.lineTo(50, 50); |
| 146 | |
| 147 | SkScalar intervals[] = { 1.0f, 1.0f }; |
reed | 5e1ddb1 | 2015-12-21 08:52:45 -0800 | [diff] [blame] | 148 | SkAutoTUnref<SkPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 149 | |
| 150 | SkPaint paint; |
| 151 | paint.setStyle(SkPaint::kStroke_Style); |
| 152 | paint.setPathEffect(dash); |
| 153 | |
robertphillips | 98b0315 | 2015-01-26 11:29:36 -0800 | [diff] [blame] | 154 | for (int i = 0; i < 50; ++i) { |
| 155 | canvas->drawPath(path, paint); |
| 156 | } |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 157 | } |
| 158 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 159 | // path effects currently render an SkPicture undesireable for GPU rendering |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 160 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | const char *reason = nullptr; |
| 162 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(nullptr, &reason)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 163 | REPORTER_ASSERT(reporter, reason); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 164 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 165 | canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 166 | { |
| 167 | SkPath path; |
| 168 | |
| 169 | path.moveTo(0, 0); |
| 170 | path.lineTo(0, 50); |
| 171 | path.lineTo(25, 25); |
| 172 | path.lineTo(50, 50); |
| 173 | path.lineTo(50, 0); |
| 174 | path.close(); |
| 175 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 176 | |
| 177 | SkPaint paint; |
| 178 | paint.setAntiAlias(true); |
| 179 | for (int i = 0; i < 50; ++i) { |
| 180 | canvas->drawPath(path, paint); |
| 181 | } |
| 182 | } |
| 183 | picture.reset(recorder.endRecording()); |
jvanverth | d86b07a | 2014-11-04 08:50:15 -0800 | [diff] [blame] | 184 | // A lot of small AA concave paths should be fine for GPU rendering |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(nullptr)); |
jvanverth | d86b07a | 2014-11-04 08:50:15 -0800 | [diff] [blame] | 186 | |
| 187 | canvas = recorder.beginRecording(100, 100); |
| 188 | { |
| 189 | SkPath path; |
| 190 | |
| 191 | path.moveTo(0, 0); |
| 192 | path.lineTo(0, 100); |
| 193 | path.lineTo(50, 50); |
| 194 | path.lineTo(100, 100); |
| 195 | path.lineTo(100, 0); |
| 196 | path.close(); |
| 197 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 198 | |
| 199 | SkPaint paint; |
| 200 | paint.setAntiAlias(true); |
| 201 | for (int i = 0; i < 50; ++i) { |
| 202 | canvas->drawPath(path, paint); |
| 203 | } |
| 204 | } |
| 205 | picture.reset(recorder.endRecording()); |
| 206 | // A lot of large AA concave paths currently render an SkPicture undesireable for GPU rendering |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(nullptr)); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 208 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 209 | canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 210 | { |
| 211 | SkPath path; |
| 212 | |
| 213 | path.moveTo(0, 0); |
| 214 | path.lineTo(0, 50); |
| 215 | path.lineTo(25, 25); |
| 216 | path.lineTo(50, 50); |
| 217 | path.lineTo(50, 0); |
| 218 | path.close(); |
| 219 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 220 | |
| 221 | SkPaint paint; |
| 222 | paint.setAntiAlias(true); |
| 223 | paint.setStyle(SkPaint::kStroke_Style); |
| 224 | paint.setStrokeWidth(0); |
| 225 | for (int i = 0; i < 50; ++i) { |
| 226 | canvas->drawPath(path, paint); |
| 227 | } |
| 228 | } |
| 229 | picture.reset(recorder.endRecording()); |
| 230 | // hairline stroked AA concave paths are fine for GPU rendering |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 231 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(nullptr)); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 232 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 233 | canvas = recorder.beginRecording(100, 100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 234 | { |
| 235 | SkPaint paint; |
| 236 | SkScalar intervals [] = { 10, 20 }; |
| 237 | SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25); |
| 238 | paint.setPathEffect(pe)->unref(); |
| 239 | |
| 240 | SkPoint points [2] = { { 0, 0 }, { 100, 0 } }; |
robertphillips | 98b0315 | 2015-01-26 11:29:36 -0800 | [diff] [blame] | 241 | |
| 242 | for (int i = 0; i < 50; ++i) { |
| 243 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint); |
| 244 | } |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 245 | } |
| 246 | picture.reset(recorder.endRecording()); |
| 247 | // fast-path dashed effects are fine for GPU rendering ... |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(nullptr)); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 249 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 250 | canvas = recorder.beginRecording(100, 100); |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 251 | { |
| 252 | SkPaint paint; |
| 253 | SkScalar intervals [] = { 10, 20 }; |
| 254 | SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25); |
| 255 | paint.setPathEffect(pe)->unref(); |
| 256 | |
robertphillips | 98b0315 | 2015-01-26 11:29:36 -0800 | [diff] [blame] | 257 | for (int i = 0; i < 50; ++i) { |
| 258 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 259 | } |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 260 | } |
| 261 | picture.reset(recorder.endRecording()); |
| 262 | // ... but only when applied to drawPoint() calls |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(nullptr)); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 264 | |
| 265 | // Nest the previous picture inside a new one. |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 266 | canvas = recorder.beginRecording(100, 100); |
| 267 | { |
| 268 | canvas->drawPicture(picture.get()); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 269 | } |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 270 | picture.reset(recorder.endRecording()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(nullptr)); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 272 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 273 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 274 | #endif |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 275 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 276 | static void test_savelayer_extraction(skiatest::Reporter* reporter) { |
| 277 | static const int kWidth = 100; |
| 278 | static const int kHeight = 100; |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 279 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 280 | // Create complex paint that the bounding box computation code can't |
| 281 | // optimize away |
| 282 | SkScalar blueToRedMatrix[20] = { 0 }; |
| 283 | blueToRedMatrix[2] = blueToRedMatrix[18] = SK_Scalar1; |
| 284 | SkAutoTUnref<SkColorFilter> blueToRed(SkColorMatrixFilter::Create(blueToRedMatrix)); |
| 285 | SkAutoTUnref<SkImageFilter> filter(SkColorFilterImageFilter::Create(blueToRed.get())); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 286 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 287 | SkPaint complexPaint; |
| 288 | complexPaint.setImageFilter(filter); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 289 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 290 | SkAutoTUnref<SkPicture> pict, child; |
| 291 | SkRTreeFactory bbhFactory; |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 292 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 293 | { |
| 294 | SkPictureRecorder recorder; |
robertphillips | d8aa7b7 | 2014-10-30 16:45:02 -0700 | [diff] [blame] | 295 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 296 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight), |
| 297 | &bbhFactory, |
| 298 | SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag); |
robertphillips | d8aa7b7 | 2014-10-30 16:45:02 -0700 | [diff] [blame] | 299 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 300 | c->saveLayer(nullptr, &complexPaint); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 301 | c->restore(); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 302 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 303 | child.reset(recorder.endRecording()); |
| 304 | } |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 305 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 306 | // create a picture with the structure: |
| 307 | // 1) |
| 308 | // SaveLayer |
| 309 | // Restore |
| 310 | // 2) |
| 311 | // SaveLayer |
| 312 | // Translate |
| 313 | // SaveLayer w/ bound |
| 314 | // Restore |
| 315 | // Restore |
| 316 | // 3) |
| 317 | // SaveLayer w/ copyable paint |
| 318 | // Restore |
| 319 | // 4) |
| 320 | // SaveLayer |
| 321 | // DrawPicture (which has a SaveLayer/Restore pair) |
| 322 | // Restore |
| 323 | // 5) |
| 324 | // SaveLayer |
| 325 | // DrawPicture with Matrix & Paint (with SaveLayer/Restore pair) |
| 326 | // Restore |
| 327 | { |
| 328 | SkPictureRecorder recorder; |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 329 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 330 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), |
| 331 | SkIntToScalar(kHeight), |
| 332 | &bbhFactory, |
| 333 | SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 334 | // 1) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 335 | c->saveLayer(nullptr, &complexPaint); // layer #0 |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 336 | c->restore(); |
| 337 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 338 | // 2) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 339 | c->saveLayer(nullptr, nullptr); // layer #1 |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 340 | c->translate(kWidth / 2.0f, kHeight / 2.0f); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 341 | SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); |
| 342 | c->saveLayer(&r, &complexPaint); // layer #2 |
| 343 | c->restore(); |
| 344 | c->restore(); |
| 345 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 346 | // 3) |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 347 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 348 | c->saveLayer(nullptr, &complexPaint); // layer #3 |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 349 | c->restore(); |
| 350 | } |
| 351 | |
| 352 | SkPaint layerPaint; |
| 353 | layerPaint.setColor(SK_ColorRED); // Non-alpha only to avoid SaveLayerDrawRestoreNooper |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 354 | // 4) |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 355 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 356 | c->saveLayer(nullptr, &layerPaint); // layer #4 |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 357 | c->drawPicture(child); // layer #5 inside picture |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 358 | c->restore(); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 359 | } |
| 360 | // 5 |
| 361 | { |
| 362 | SkPaint picturePaint; |
| 363 | SkMatrix trans; |
| 364 | trans.setTranslate(10, 10); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 365 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 366 | c->saveLayer(nullptr, &layerPaint); // layer #6 |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 367 | c->drawPicture(child, &trans, &picturePaint); // layer #7 inside picture |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 368 | c->restore(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 369 | } |
| 370 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 371 | pict.reset(recorder.endRecording()); |
| 372 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 373 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 374 | // Now test out the SaveLayer extraction |
reed | d990e2f | 2014-12-22 11:58:30 -0800 | [diff] [blame] | 375 | if (!SkCanvas::Internal_Private_GetIgnoreSaveLayerBounds()) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 376 | const SkBigPicture* bp = pict->asSkBigPicture(); |
| 377 | REPORTER_ASSERT(reporter, bp); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 378 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 379 | const SkBigPicture::AccelData* data = bp->accelData(); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 380 | REPORTER_ASSERT(reporter, data); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 381 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 382 | const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data); |
| 383 | REPORTER_ASSERT(reporter, 8 == gpuData->numBlocks()); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 384 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 385 | const SkLayerInfo::BlockInfo& info0 = gpuData->block(0); |
| 386 | // The parent/child layers appear in reverse order |
| 387 | const SkLayerInfo::BlockInfo& info1 = gpuData->block(2); |
| 388 | const SkLayerInfo::BlockInfo& info2 = gpuData->block(1); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 389 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 390 | const SkLayerInfo::BlockInfo& info3 = gpuData->block(3); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 391 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 392 | // The parent/child layers appear in reverse order |
| 393 | const SkLayerInfo::BlockInfo& info4 = gpuData->block(5); |
| 394 | const SkLayerInfo::BlockInfo& info5 = gpuData->block(4); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 395 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 396 | // The parent/child layers appear in reverse order |
| 397 | const SkLayerInfo::BlockInfo& info6 = gpuData->block(7); |
| 398 | const SkLayerInfo::BlockInfo& info7 = gpuData->block(6); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 399 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 400 | REPORTER_ASSERT(reporter, nullptr == info0.fPicture); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 401 | REPORTER_ASSERT(reporter, kWidth == info0.fBounds.width() && |
| 402 | kHeight == info0.fBounds.height()); |
| 403 | REPORTER_ASSERT(reporter, info0.fLocalMat.isIdentity()); |
| 404 | REPORTER_ASSERT(reporter, info0.fPreMat.isIdentity()); |
| 405 | REPORTER_ASSERT(reporter, 0 == info0.fBounds.fLeft && 0 == info0.fBounds.fTop); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 406 | REPORTER_ASSERT(reporter, nullptr != info0.fPaint); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 407 | REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 408 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 409 | REPORTER_ASSERT(reporter, nullptr == info1.fPicture); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 410 | REPORTER_ASSERT(reporter, kWidth/2.0 == info1.fBounds.width() && |
| 411 | kHeight/2.0 == info1.fBounds.height()); |
| 412 | REPORTER_ASSERT(reporter, info1.fLocalMat.isIdentity()); |
| 413 | REPORTER_ASSERT(reporter, info1.fPreMat.isIdentity()); |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 414 | REPORTER_ASSERT(reporter, kWidth/2.0 == info1.fBounds.fLeft && |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 415 | kHeight/2.0 == info1.fBounds.fTop); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 416 | REPORTER_ASSERT(reporter, nullptr == info1.fPaint); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, !info1.fIsNested && |
| 418 | info1.fHasNestedLayers); // has a nested SL |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 419 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 420 | REPORTER_ASSERT(reporter, nullptr == info2.fPicture); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 421 | REPORTER_ASSERT(reporter, kWidth / 2 == info2.fBounds.width() && |
| 422 | kHeight / 2 == info2.fBounds.height()); // bound reduces size |
| 423 | REPORTER_ASSERT(reporter, !info2.fLocalMat.isIdentity()); |
| 424 | REPORTER_ASSERT(reporter, info2.fPreMat.isIdentity()); |
| 425 | REPORTER_ASSERT(reporter, kWidth / 2 == info2.fBounds.fLeft && // translated |
| 426 | kHeight / 2 == info2.fBounds.fTop); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 427 | REPORTER_ASSERT(reporter, nullptr != info2.fPaint); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 428 | REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 429 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 430 | REPORTER_ASSERT(reporter, nullptr == info3.fPicture); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 431 | REPORTER_ASSERT(reporter, kWidth == info3.fBounds.width() && |
| 432 | kHeight == info3.fBounds.height()); |
| 433 | REPORTER_ASSERT(reporter, info3.fLocalMat.isIdentity()); |
| 434 | REPORTER_ASSERT(reporter, info3.fPreMat.isIdentity()); |
| 435 | REPORTER_ASSERT(reporter, 0 == info3.fBounds.fLeft && 0 == info3.fBounds.fTop); |
| 436 | REPORTER_ASSERT(reporter, info3.fPaint); |
| 437 | REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 438 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 439 | REPORTER_ASSERT(reporter, nullptr == info4.fPicture); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 440 | REPORTER_ASSERT(reporter, kWidth == info4.fBounds.width() && |
| 441 | kHeight == info4.fBounds.height()); |
| 442 | REPORTER_ASSERT(reporter, 0 == info4.fBounds.fLeft && 0 == info4.fBounds.fTop); |
| 443 | REPORTER_ASSERT(reporter, info4.fLocalMat.isIdentity()); |
| 444 | REPORTER_ASSERT(reporter, info4.fPreMat.isIdentity()); |
| 445 | REPORTER_ASSERT(reporter, info4.fPaint); |
| 446 | REPORTER_ASSERT(reporter, !info4.fIsNested && |
| 447 | info4.fHasNestedLayers); // has a nested SL |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 448 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 449 | REPORTER_ASSERT(reporter, child == info5.fPicture); // in a child picture |
| 450 | REPORTER_ASSERT(reporter, kWidth == info5.fBounds.width() && |
| 451 | kHeight == info5.fBounds.height()); |
| 452 | REPORTER_ASSERT(reporter, 0 == info5.fBounds.fLeft && 0 == info5.fBounds.fTop); |
| 453 | REPORTER_ASSERT(reporter, info5.fLocalMat.isIdentity()); |
| 454 | REPORTER_ASSERT(reporter, info5.fPreMat.isIdentity()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 455 | REPORTER_ASSERT(reporter, nullptr != info5.fPaint); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 456 | REPORTER_ASSERT(reporter, info5.fIsNested && !info5.fHasNestedLayers); // is nested |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 457 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 458 | REPORTER_ASSERT(reporter, nullptr == info6.fPicture); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 459 | REPORTER_ASSERT(reporter, kWidth-10 == info6.fBounds.width() && |
| 460 | kHeight-10 == info6.fBounds.height()); |
| 461 | REPORTER_ASSERT(reporter, 10 == info6.fBounds.fLeft && 10 == info6.fBounds.fTop); |
| 462 | REPORTER_ASSERT(reporter, info6.fLocalMat.isIdentity()); |
| 463 | REPORTER_ASSERT(reporter, info6.fPreMat.isIdentity()); |
| 464 | REPORTER_ASSERT(reporter, info6.fPaint); |
| 465 | REPORTER_ASSERT(reporter, !info6.fIsNested && |
| 466 | info6.fHasNestedLayers); // has a nested SL |
| 467 | |
| 468 | REPORTER_ASSERT(reporter, child == info7.fPicture); // in a child picture |
| 469 | REPORTER_ASSERT(reporter, kWidth == info7.fBounds.width() && |
| 470 | kHeight == info7.fBounds.height()); |
| 471 | REPORTER_ASSERT(reporter, 0 == info7.fBounds.fLeft && 0 == info7.fBounds.fTop); |
| 472 | REPORTER_ASSERT(reporter, info7.fLocalMat.isIdentity()); |
| 473 | REPORTER_ASSERT(reporter, info7.fPreMat.isIdentity()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 474 | REPORTER_ASSERT(reporter, nullptr != info7.fPaint); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 475 | REPORTER_ASSERT(reporter, info7.fIsNested && !info7.fHasNestedLayers); // is nested |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 479 | static void test_has_text(skiatest::Reporter* reporter) { |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 480 | SkPictureRecorder recorder; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 481 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 482 | SkCanvas* canvas = recorder.beginRecording(100,100); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 483 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 484 | canvas->drawRect(SkRect::MakeWH(20, 20), SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 485 | } |
| 486 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 487 | REPORTER_ASSERT(reporter, !picture->hasText()); |
| 488 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 489 | SkPoint point = SkPoint::Make(10, 10); |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 490 | canvas = recorder.beginRecording(100,100); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 491 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 492 | canvas->drawText("Q", 1, point.fX, point.fY, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 493 | } |
| 494 | picture.reset(recorder.endRecording()); |
| 495 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 496 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 497 | canvas = recorder.beginRecording(100,100); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 498 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 499 | canvas->drawPosText("Q", 1, &point, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 500 | } |
| 501 | picture.reset(recorder.endRecording()); |
| 502 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 503 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 504 | canvas = recorder.beginRecording(100,100); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 505 | { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 506 | canvas->drawPosTextH("Q", 1, &point.fX, point.fY, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 507 | } |
| 508 | picture.reset(recorder.endRecording()); |
| 509 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 510 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 511 | canvas = recorder.beginRecording(100,100); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 512 | { |
| 513 | SkPath path; |
| 514 | path.moveTo(0, 0); |
| 515 | path.lineTo(50, 50); |
| 516 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 517 | canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 518 | } |
| 519 | picture.reset(recorder.endRecording()); |
| 520 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 521 | |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 522 | canvas = recorder.beginRecording(100,100); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 523 | { |
| 524 | SkPath path; |
| 525 | path.moveTo(0, 0); |
| 526 | path.lineTo(50, 50); |
| 527 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 528 | canvas->drawTextOnPath("Q", 1, path, nullptr, SkPaint()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 529 | } |
| 530 | picture.reset(recorder.endRecording()); |
| 531 | REPORTER_ASSERT(reporter, picture->hasText()); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 532 | |
| 533 | // Nest the previous picture inside a new one. |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 534 | canvas = recorder.beginRecording(100,100); |
| 535 | { |
| 536 | canvas->drawPicture(picture.get()); |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 537 | } |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 538 | picture.reset(recorder.endRecording()); |
| 539 | REPORTER_ASSERT(reporter, picture->hasText()); |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 540 | } |
| 541 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 542 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 543 | canvas->restoreToCount(1); |
| 544 | canvas->save(); |
| 545 | canvas->save(); |
| 546 | canvas->save(); |
| 547 | } |
| 548 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 549 | /** |
| 550 | * A canvas that records the number of saves, saveLayers and restores. |
| 551 | */ |
| 552 | class SaveCountingCanvas : public SkCanvas { |
| 553 | public: |
| 554 | SaveCountingCanvas(int width, int height) |
| 555 | : INHERITED(width, height) |
| 556 | , fSaveCount(0) |
| 557 | , fSaveLayerCount(0) |
| 558 | , fRestoreCount(0){ |
| 559 | } |
| 560 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 561 | SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 562 | ++fSaveLayerCount; |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 563 | return this->INHERITED::getSaveLayerStrategy(rec); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 564 | } |
| 565 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 566 | void willSave() override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 567 | ++fSaveCount; |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 568 | this->INHERITED::willSave(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 569 | } |
| 570 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 571 | void willRestore() override { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 572 | ++fRestoreCount; |
| 573 | this->INHERITED::willRestore(); |
| 574 | } |
| 575 | |
| 576 | unsigned int getSaveCount() const { return fSaveCount; } |
| 577 | unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
| 578 | unsigned int getRestoreCount() const { return fRestoreCount; } |
| 579 | |
| 580 | private: |
| 581 | unsigned int fSaveCount; |
| 582 | unsigned int fSaveLayerCount; |
| 583 | unsigned int fRestoreCount; |
| 584 | |
| 585 | typedef SkCanvas INHERITED; |
| 586 | }; |
| 587 | |
skia.committer@gmail.com | 8e7d37d | 2014-05-28 03:06:06 +0000 | [diff] [blame] | 588 | void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 589 | unsigned int numSaves, unsigned int numSaveLayers, |
| 590 | unsigned int numRestores) { |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 591 | SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 592 | SkScalarCeilToInt(picture->cullRect().height())); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 593 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 594 | picture->playback(&canvas); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 595 | |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 596 | // Optimizations may have removed these, |
| 597 | // so expect to have seen no more than num{Saves,SaveLayers,Restores}. |
| 598 | REPORTER_ASSERT(reporter, numSaves >= canvas.getSaveCount()); |
| 599 | REPORTER_ASSERT(reporter, numSaveLayers >= canvas.getSaveLayerCount()); |
| 600 | REPORTER_ASSERT(reporter, numRestores >= canvas.getRestoreCount()); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // This class exists so SkPicture can friend it and give it access to |
| 604 | // the 'partialReplay' method. |
| 605 | class SkPictureRecorderReplayTester { |
| 606 | public: |
| 607 | static SkPicture* Copy(SkPictureRecorder* recorder) { |
| 608 | SkPictureRecorder recorder2; |
| 609 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 610 | SkCanvas* canvas = recorder2.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 611 | |
| 612 | recorder->partialReplay(canvas); |
| 613 | |
| 614 | return recorder2.endRecording(); |
| 615 | } |
| 616 | }; |
| 617 | |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 618 | static void create_imbalance(SkCanvas* canvas) { |
| 619 | SkRect clipRect = SkRect::MakeWH(2, 2); |
| 620 | SkRect drawRect = SkRect::MakeWH(10, 10); |
| 621 | canvas->save(); |
| 622 | canvas->clipRect(clipRect, SkRegion::kReplace_Op); |
| 623 | canvas->translate(1.0f, 1.0f); |
| 624 | SkPaint p; |
| 625 | p.setColor(SK_ColorGREEN); |
| 626 | canvas->drawRect(drawRect, p); |
| 627 | // no restore |
| 628 | } |
| 629 | |
| 630 | // This tests that replaying a potentially unbalanced picture into a canvas |
| 631 | // doesn't affect the canvas' save count or matrix/clip state. |
| 632 | static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { |
| 633 | SkBitmap bm; |
| 634 | bm.allocN32Pixels(4, 3); |
| 635 | SkCanvas canvas(bm); |
| 636 | |
| 637 | int beforeSaveCount = canvas.getSaveCount(); |
| 638 | |
| 639 | SkMatrix beforeMatrix = canvas.getTotalMatrix(); |
| 640 | |
| 641 | SkRect beforeClip; |
| 642 | |
| 643 | canvas.getClipBounds(&beforeClip); |
| 644 | |
| 645 | canvas.drawPicture(picture); |
| 646 | |
| 647 | REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); |
| 648 | REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); |
| 649 | |
| 650 | SkRect afterClip; |
| 651 | |
| 652 | canvas.getClipBounds(&afterClip); |
| 653 | |
| 654 | REPORTER_ASSERT(reporter, afterClip == beforeClip); |
| 655 | } |
| 656 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 657 | // Test out SkPictureRecorder::partialReplay |
| 658 | DEF_TEST(PictureRecorder_replay, reporter) { |
| 659 | // check save/saveLayer state |
| 660 | { |
| 661 | SkPictureRecorder recorder; |
| 662 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 663 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 664 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 665 | canvas->saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 666 | |
| 667 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 668 | |
| 669 | // The extra save and restore comes from the Copy process. |
| 670 | check_save_state(reporter, copy, 2, 1, 3); |
| 671 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 672 | canvas->saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 673 | |
| 674 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 675 | |
| 676 | check_save_state(reporter, final, 1, 2, 3); |
| 677 | |
| 678 | // The copy shouldn't pick up any operations added after it was made |
| 679 | check_save_state(reporter, copy, 2, 1, 3); |
| 680 | } |
| 681 | |
| 682 | // (partially) check leakage of draw ops |
| 683 | { |
| 684 | SkPictureRecorder recorder; |
| 685 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 686 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 687 | |
| 688 | SkRect r = SkRect::MakeWH(5, 5); |
| 689 | SkPaint p; |
| 690 | |
| 691 | canvas->drawRect(r, p); |
| 692 | |
| 693 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 694 | |
| 695 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 696 | |
| 697 | SkBitmap bm; |
| 698 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 699 | |
| 700 | r.offset(5.0f, 5.0f); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 701 | canvas->drawBitmapRect(bm, r, nullptr); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 702 | |
| 703 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 704 | REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); |
| 705 | |
| 706 | REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); |
| 707 | |
| 708 | // The snapshot shouldn't pick up any operations added after it was made |
| 709 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 710 | } |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 711 | |
| 712 | // Recreate the Android partialReplay test case |
| 713 | { |
| 714 | SkPictureRecorder recorder; |
| 715 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 716 | SkCanvas* canvas = recorder.beginRecording(4, 3, nullptr, 0); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 717 | create_imbalance(canvas); |
| 718 | |
| 719 | int expectedSaveCount = canvas->getSaveCount(); |
| 720 | |
| 721 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 722 | check_balance(reporter, copy); |
| 723 | |
| 724 | REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); |
| 725 | |
| 726 | // End the recording of source to test the picture finalization |
| 727 | // process isn't complicated by the partialReplay step |
| 728 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 729 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 730 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 731 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 732 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 733 | SkCanvas testCanvas(100, 100); |
| 734 | set_canvas_to_save_count_4(&testCanvas); |
| 735 | |
| 736 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 737 | |
| 738 | SkPaint paint; |
| 739 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 740 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 741 | SkPictureRecorder recorder; |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 742 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 743 | { |
| 744 | // Create picture with 2 unbalanced saves |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 745 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 746 | canvas->save(); |
| 747 | canvas->translate(10, 10); |
| 748 | canvas->drawRect(rect, paint); |
| 749 | canvas->save(); |
| 750 | canvas->translate(10, 10); |
| 751 | canvas->drawRect(rect, paint); |
| 752 | SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); |
| 753 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 754 | testCanvas.drawPicture(extraSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 755 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 756 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 757 | |
| 758 | set_canvas_to_save_count_4(&testCanvas); |
| 759 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 760 | { |
| 761 | // Create picture with 2 unbalanced restores |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 762 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 763 | canvas->save(); |
| 764 | canvas->translate(10, 10); |
| 765 | canvas->drawRect(rect, paint); |
| 766 | canvas->save(); |
| 767 | canvas->translate(10, 10); |
| 768 | canvas->drawRect(rect, paint); |
| 769 | canvas->restore(); |
| 770 | canvas->restore(); |
| 771 | canvas->restore(); |
| 772 | canvas->restore(); |
| 773 | SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 774 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 775 | testCanvas.drawPicture(extraRestorePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 776 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 777 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 778 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 779 | set_canvas_to_save_count_4(&testCanvas); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 780 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 781 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 782 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 783 | canvas->translate(10, 10); |
| 784 | canvas->drawRect(rect, paint); |
| 785 | SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); |
| 786 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 787 | testCanvas.drawPicture(noSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 788 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 789 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 790 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 791 | } |
| 792 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 793 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 794 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 795 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 796 | SkPictureRecorder recorder; |
| 797 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 798 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 799 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 800 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 801 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 802 | |
| 803 | for (int i = 0; i < 1000; ++i) { |
| 804 | rand_op(canvas, rand); |
| 805 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 806 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 807 | |
| 808 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 812 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 813 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 814 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 815 | for (int i = 0; i < 100; ++i) { |
| 816 | canvas->save(); |
| 817 | } |
| 818 | while (canvas->getSaveCount() > 1) { |
| 819 | canvas->clipRect(rect); |
| 820 | canvas->restore(); |
| 821 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 822 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 826 | #ifndef SK_DEBUG |
| 827 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 828 | // should never do this. |
| 829 | static void test_bad_bitmap() { |
| 830 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 831 | // fail. |
| 832 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 833 | bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 834 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 835 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 836 | recordingCanvas->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 837 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 838 | |
| 839 | SkCanvas canvas; |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 840 | canvas.drawPicture(picture); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 841 | } |
| 842 | #endif |
| 843 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 844 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 845 | SkPictureRecorder recorder; |
mtklein | 87c4138 | 2014-09-08 07:31:18 -0700 | [diff] [blame] | 846 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bitmap.width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 847 | SkIntToScalar(bitmap.height())); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 848 | canvas->drawBitmap(bitmap, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 849 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 850 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 851 | SkDynamicMemoryWStream wStream; |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 852 | SkAutoTUnref<SkPixelSerializer> serializer( |
| 853 | SkImageEncoder::CreatePixelSerializer()); |
| 854 | picture->serialize(&wStream, serializer); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 855 | return wStream.copyToData(); |
| 856 | } |
| 857 | |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 858 | struct ErrorContext { |
| 859 | int fErrors; |
| 860 | skiatest::Reporter* fReporter; |
| 861 | }; |
| 862 | |
| 863 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 864 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 865 | errorContext->fErrors++; |
| 866 | // This test only expects one error, and that is a kParseError. If there are others, |
| 867 | // there is some unknown problem. |
| 868 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 869 | "This threw more errors than expected."); |
| 870 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 871 | SkGetLastErrorString()); |
| 872 | } |
| 873 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 874 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 875 | // Create a bitmap that will be encoded. |
| 876 | SkBitmap original; |
| 877 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 878 | SkDynamicMemoryWStream wStream; |
| 879 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 880 | return; |
| 881 | } |
| 882 | SkAutoDataUnref data(wStream.copyToData()); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 883 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 884 | SkBitmap bm; |
reed | d114645 | 2015-09-25 06:56:57 -0700 | [diff] [blame] | 885 | bool installSuccess = SkDEPRECATED_InstallDiscardablePixelRef(data, &bm); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 886 | REPORTER_ASSERT(reporter, installSuccess); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 887 | |
| 888 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 889 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 890 | // will use the already encoded data. |
| 891 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 892 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 893 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 894 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 895 | // providing a function to decode the bitmap. |
| 896 | ErrorContext context; |
| 897 | context.fErrors = 0; |
| 898 | context.fReporter = reporter; |
| 899 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 900 | SkMemoryStream pictureStream(picture1); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 901 | SkClearLastError(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 902 | SkAutoTUnref<SkPicture> pictureFromStream(SkPicture::CreateFromStream(&pictureStream, nullptr)); |
| 903 | REPORTER_ASSERT(reporter, pictureFromStream.get() != nullptr); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 904 | SkClearLastError(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 905 | SkSetErrorCallback(nullptr, nullptr); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 906 | } |
| 907 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 908 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 909 | // Test for crbug.com/229011 |
| 910 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 911 | SkIntToScalar(2), SkIntToScalar(2)); |
| 912 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 913 | SkIntToScalar(1), SkIntToScalar(1)); |
| 914 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 915 | SkIntToScalar(1), SkIntToScalar(1)); |
| 916 | |
| 917 | SkPath invPath; |
| 918 | invPath.addOval(rect1); |
| 919 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 920 | SkPath path; |
| 921 | path.addOval(rect2); |
| 922 | SkPath path2; |
| 923 | path2.addOval(rect3); |
| 924 | SkIRect clipBounds; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 925 | SkPictureRecorder recorder; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 926 | |
| 927 | // Testing conservative-raster-clip that is enabled by PictureRecord |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 928 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 929 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 930 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 931 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 932 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 933 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 934 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 935 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 936 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 937 | } |
| 938 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 939 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 940 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 941 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 942 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 943 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 944 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 945 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 946 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 947 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 948 | } |
| 949 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 950 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 951 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 952 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 953 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 954 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 955 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 956 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 957 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 958 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 959 | } |
| 960 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 961 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 962 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 963 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 964 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 965 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 966 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 967 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 968 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 969 | } |
| 970 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 971 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 972 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 973 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 974 | // True clip is actually empty in this case, but the best |
| 975 | // determination we can make using only bounds as input is that the |
| 976 | // clip is included in the bounds of 'path'. |
| 977 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 978 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 979 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 980 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 981 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 982 | } |
| 983 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 984 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 985 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 986 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 987 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 988 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 989 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 990 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 991 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 992 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 993 | } |
| 994 | } |
| 995 | |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 996 | static void test_cull_rect_reset(skiatest::Reporter* reporter) { |
| 997 | SkPictureRecorder recorder; |
| 998 | SkRect bounds = SkRect::MakeWH(10, 10); |
| 999 | SkRTreeFactory factory; |
| 1000 | SkCanvas* canvas = recorder.beginRecording(bounds, &factory); |
| 1001 | bounds = SkRect::MakeWH(100, 100); |
| 1002 | SkPaint paint; |
| 1003 | canvas->drawRect(bounds, paint); |
| 1004 | canvas->drawRect(bounds, paint); |
mtklein | eedc334 | 2015-07-08 08:26:39 -0700 | [diff] [blame] | 1005 | SkAutoTUnref<const SkPicture> p(recorder.endRecordingAsPicture(bounds)); |
| 1006 | const SkBigPicture* picture = p->asSkBigPicture(); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 1007 | REPORTER_ASSERT(reporter, picture); |
| 1008 | |
| 1009 | SkRect finalCullRect = picture->cullRect(); |
| 1010 | REPORTER_ASSERT(reporter, 0 == finalCullRect.fLeft); |
| 1011 | REPORTER_ASSERT(reporter, 0 == finalCullRect.fTop); |
| 1012 | REPORTER_ASSERT(reporter, 100 == finalCullRect.fBottom); |
| 1013 | REPORTER_ASSERT(reporter, 100 == finalCullRect.fRight); |
| 1014 | |
| 1015 | const SkBBoxHierarchy* pictureBBH = picture->bbh(); |
| 1016 | SkRect bbhCullRect = pictureBBH->getRootBound(); |
| 1017 | REPORTER_ASSERT(reporter, 0 == bbhCullRect.fLeft); |
| 1018 | REPORTER_ASSERT(reporter, 0 == bbhCullRect.fTop); |
| 1019 | REPORTER_ASSERT(reporter, 100 == bbhCullRect.fBottom); |
| 1020 | REPORTER_ASSERT(reporter, 100 == bbhCullRect.fRight); |
| 1021 | } |
| 1022 | |
| 1023 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1024 | /** |
| 1025 | * A canvas that records the number of clip commands. |
| 1026 | */ |
| 1027 | class ClipCountingCanvas : public SkCanvas { |
| 1028 | public: |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1029 | ClipCountingCanvas(int width, int height) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1030 | : INHERITED(width, height) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1031 | , fClipCount(0){ |
| 1032 | } |
| 1033 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1034 | virtual void onClipRect(const SkRect& r, |
| 1035 | SkRegion::Op op, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1036 | ClipEdgeStyle edgeStyle) override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1037 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1038 | this->INHERITED::onClipRect(r, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1041 | virtual void onClipRRect(const SkRRect& rrect, |
| 1042 | SkRegion::Op op, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1043 | ClipEdgeStyle edgeStyle)override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1044 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1045 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1048 | virtual void onClipPath(const SkPath& path, |
| 1049 | SkRegion::Op op, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1050 | ClipEdgeStyle edgeStyle) override { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1051 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1052 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 1053 | } |
| 1054 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1055 | void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) override { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1056 | fClipCount += 1; |
| 1057 | this->INHERITED::onClipRegion(deviceRgn, op); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | unsigned getClipCount() const { return fClipCount; } |
| 1061 | |
| 1062 | private: |
| 1063 | unsigned fClipCount; |
| 1064 | |
| 1065 | typedef SkCanvas INHERITED; |
| 1066 | }; |
| 1067 | |
| 1068 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1069 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1070 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1071 | |
| 1072 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 1073 | // The following expanding clip should not be skipped. |
| 1074 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 1075 | // Draw something so the optimizer doesn't just fold the world. |
| 1076 | SkPaint p; |
| 1077 | p.setColor(SK_ColorBLUE); |
| 1078 | canvas->drawPaint(p); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1079 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1080 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1081 | ClipCountingCanvas testCanvas(10, 10); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1082 | picture->playback(&testCanvas); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Both clips should be present on playback. |
| 1085 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 1086 | } |
| 1087 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1088 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 1089 | SkBitmap bm; |
| 1090 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1091 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1092 | SkPictureRecorder recorder; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1093 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1094 | recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1095 | SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); |
| 1096 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1097 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1098 | recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1099 | SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); |
| 1100 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1101 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1102 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1103 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1104 | canvas->drawPicture(childPlain); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1105 | SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); |
| 1106 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 1107 | } |
| 1108 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1109 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1110 | canvas->drawPicture(childWithBitmap); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1111 | SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); |
| 1112 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 1113 | } |
| 1114 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1115 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1116 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1117 | canvas->drawPicture(childPlain); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1118 | SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); |
| 1119 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 1120 | } |
| 1121 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1122 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1123 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1124 | canvas->drawPicture(childWithBitmap); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1125 | SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); |
| 1126 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 1127 | } |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1130 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 1131 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1132 | SkPictureRecorder recorder; |
| 1133 | recorder.beginRecording(0, 0); |
| 1134 | SkAutoTUnref<SkPicture> empty(recorder.endRecording()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1135 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1136 | // Empty pictures should still have a valid ID |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1137 | REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1138 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1139 | SkCanvas* canvas = recorder.beginRecording(1, 1); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1140 | canvas->drawARGB(255, 255, 255, 255); |
| 1141 | SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); |
| 1142 | // picture should have a non-zero id after recording |
| 1143 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1144 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1145 | // both pictures should have different ids |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1146 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 1149 | static void test_typeface(skiatest::Reporter* reporter) { |
| 1150 | SkPictureRecorder recorder; |
| 1151 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 1152 | SkPaint paint; |
| 1153 | paint.setTypeface(SkTypeface::CreateFromName("Arial", SkTypeface::kItalic)); |
| 1154 | canvas->drawText("Q", 1, 0, 10, paint); |
| 1155 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1156 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1157 | SkDynamicMemoryWStream stream; |
| 1158 | picture->serialize(&stream); |
| 1159 | } |
| 1160 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1161 | DEF_TEST(Picture, reporter) { |
caryclark | 5ef194c | 2015-08-31 09:22:38 -0700 | [diff] [blame] | 1162 | test_typeface(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1163 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 1164 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1165 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1166 | #else |
| 1167 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1168 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1169 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1170 | test_peephole(); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1171 | #if SK_SUPPORT_GPU |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 1172 | test_gpu_veto(reporter); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1173 | #endif |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 1174 | test_has_text(reporter); |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 1175 | test_images_are_found_by_willPlayBackBitmaps(reporter); |
mtklein | 8e12656 | 2014-10-01 09:29:35 -0700 | [diff] [blame] | 1176 | test_analysis(reporter); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1177 | test_bitmap_with_encoded_data(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1178 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1179 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1180 | test_hierarchical(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1181 | test_gen_id(reporter); |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 1182 | test_savelayer_extraction(reporter); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 1183 | test_cull_rect_reset(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1184 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1185 | |
| 1186 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 1187 | const SkPaint paint; |
| 1188 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 1189 | const SkIRect irect = { 2, 2, 3, 3 }; |
| 1190 | |
| 1191 | // Don't care what these record, as long as they're legal. |
| 1192 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1193 | canvas->drawBitmapRect(bitmap, rect, rect, &paint, SkCanvas::kStrict_SrcRectConstraint); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1194 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1195 | canvas->drawBitmap(bitmap, 1, 1); // drawSprite |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 1199 | SkBitmap empty; |
| 1200 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1201 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1202 | draw_bitmaps(empty, canvas); |
| 1203 | } |
| 1204 | |
| 1205 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1206 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1207 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1208 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1212 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1213 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1214 | SkCanvas canvas(dst); |
| 1215 | |
| 1216 | test_draw_bitmaps(&canvas); |
| 1217 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1218 | |
| 1219 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 1220 | // This test is from crbug.com/344987. |
| 1221 | // The commands are: |
| 1222 | // saveLayer with paint that modifies alpha |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1223 | // drawBitmapRect |
| 1224 | // drawBitmapRect |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1225 | // restore |
| 1226 | // The bug was that this structure was modified so that: |
| 1227 | // - The saveLayer and restore were eliminated |
| 1228 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 1229 | |
| 1230 | // This test draws blue and red squares inside a 50% transparent |
| 1231 | // layer. Both colours should show up muted. |
| 1232 | // When the bug is present, the red square (the second bitmap) |
| 1233 | // shows upwith full opacity. |
| 1234 | |
| 1235 | SkBitmap blueBM; |
| 1236 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 1237 | SkBitmap redBM; |
| 1238 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 1239 | SkPaint semiTransparent; |
| 1240 | semiTransparent.setAlpha(0x80); |
| 1241 | |
| 1242 | SkPictureRecorder recorder; |
| 1243 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 1244 | canvas->drawARGB(0, 0, 0, 0); |
| 1245 | |
| 1246 | canvas->saveLayer(0, &semiTransparent); |
| 1247 | canvas->drawBitmap(blueBM, 25, 25); |
| 1248 | canvas->drawBitmap(redBM, 50, 50); |
| 1249 | canvas->restore(); |
| 1250 | |
| 1251 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1252 | |
| 1253 | // Now replay the picture back on another canvas |
| 1254 | // and check a couple of its pixels. |
| 1255 | SkBitmap replayBM; |
| 1256 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 1257 | SkCanvas replayCanvas(replayBM); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1258 | picture->playback(&replayCanvas); |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1259 | replayCanvas.flush(); |
| 1260 | |
| 1261 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 1262 | // intead of a dark red. |
| 1263 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 1264 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 1265 | } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1266 | |
| 1267 | struct CountingBBH : public SkBBoxHierarchy { |
| 1268 | mutable int searchCalls; |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 1269 | SkRect rootBound; |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1270 | |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 1271 | CountingBBH(const SkRect& bound) : searchCalls(0), rootBound(bound) {} |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1272 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 1273 | void search(const SkRect& query, SkTDArray<int>* results) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1274 | this->searchCalls++; |
| 1275 | } |
| 1276 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1277 | void insert(const SkRect[], int) override {} |
| 1278 | virtual size_t bytesUsed() const override { return 0; } |
| 1279 | SkRect getRootBound() const override { return rootBound; } |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1280 | }; |
| 1281 | |
| 1282 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 1283 | public: |
| 1284 | explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1285 | SkBBoxHierarchy* operator()(const SkRect&) const override { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1286 | return SkRef(fBBH); |
| 1287 | } |
| 1288 | private: |
| 1289 | SkBBoxHierarchy* fBBH; |
| 1290 | }; |
| 1291 | |
| 1292 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 1293 | DEF_TEST(Picture_SkipBBH, r) { |
schenney | 23d8593 | 2015-03-06 16:20:28 -0800 | [diff] [blame] | 1294 | SkRect bound = SkRect::MakeWH(320, 240); |
| 1295 | CountingBBH bbh(bound); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1296 | SpoonFedBBHFactory factory(&bbh); |
| 1297 | |
| 1298 | SkPictureRecorder recorder; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1299 | SkCanvas* c = recorder.beginRecording(bound, &factory); |
| 1300 | // Record a few ops so we don't hit a small- or empty- picture optimization. |
| 1301 | c->drawRect(bound, SkPaint()); |
| 1302 | c->drawRect(bound, SkPaint()); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1303 | SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); |
| 1304 | |
| 1305 | SkCanvas big(640, 480), small(300, 200); |
| 1306 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1307 | picture->playback(&big); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1308 | REPORTER_ASSERT(r, bbh.searchCalls == 0); |
| 1309 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 1310 | picture->playback(&small); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1311 | REPORTER_ASSERT(r, bbh.searchCalls == 1); |
| 1312 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1313 | |
| 1314 | DEF_TEST(Picture_BitmapLeak, r) { |
| 1315 | SkBitmap mut, immut; |
| 1316 | mut.allocN32Pixels(300, 200); |
| 1317 | immut.allocN32Pixels(300, 200); |
| 1318 | immut.setImmutable(); |
| 1319 | SkASSERT(!mut.isImmutable()); |
| 1320 | SkASSERT(immut.isImmutable()); |
| 1321 | |
| 1322 | // No one can hold a ref on our pixels yet. |
| 1323 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1324 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1325 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 1326 | SkAutoTUnref<const SkPicture> pic; |
| 1327 | { |
| 1328 | // we want the recorder to go out of scope before our subsequent checks, so we |
| 1329 | // place it inside local braces. |
| 1330 | SkPictureRecorder rec; |
| 1331 | SkCanvas* canvas = rec.beginRecording(1920, 1200); |
| 1332 | canvas->drawBitmap(mut, 0, 0); |
| 1333 | canvas->drawBitmap(immut, 800, 600); |
| 1334 | pic.reset(rec.endRecording()); |
| 1335 | } |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1336 | |
| 1337 | // The picture shares the immutable pixels but copies the mutable ones. |
| 1338 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1339 | REPORTER_ASSERT(r, !immut.pixelRef()->unique()); |
| 1340 | |
| 1341 | // When the picture goes away, it's just our bitmaps holding the refs. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1342 | pic.reset(nullptr); |
mtklein | d72094d | 2014-08-27 12:12:23 -0700 | [diff] [blame] | 1343 | REPORTER_ASSERT(r, mut.pixelRef()->unique()); |
| 1344 | REPORTER_ASSERT(r, immut.pixelRef()->unique()); |
| 1345 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 1346 | |
| 1347 | // getRecordingCanvas() should return a SkCanvas when recording, null when not recording. |
| 1348 | DEF_TEST(Picture_getRecordingCanvas, r) { |
| 1349 | SkPictureRecorder rec; |
| 1350 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 1351 | for (int i = 0; i < 3; i++) { |
| 1352 | rec.beginRecording(100, 100); |
| 1353 | REPORTER_ASSERT(r, rec.getRecordingCanvas()); |
| 1354 | rec.endRecording()->unref(); |
| 1355 | REPORTER_ASSERT(r, !rec.getRecordingCanvas()); |
| 1356 | } |
| 1357 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1358 | |
| 1359 | DEF_TEST(MiniRecorderLeftHanging, r) { |
| 1360 | // Any shader or other ref-counted effect will do just fine here. |
| 1361 | SkPaint paint; |
| 1362 | paint.setShader(SkShader::CreateColorShader(SK_ColorRED))->unref(); |
| 1363 | |
| 1364 | SkMiniRecorder rec; |
| 1365 | REPORTER_ASSERT(r, rec.drawRect(SkRect::MakeWH(20,30), paint)); |
| 1366 | // Don't call rec.detachPicture(). Test succeeds by not asserting or leaking the shader. |
| 1367 | } |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 1368 | |
| 1369 | DEF_TEST(Picture_preserveCullRect, r) { |
| 1370 | SkPictureRecorder recorder; |
| 1371 | |
| 1372 | SkCanvas* c = recorder.beginRecording(SkRect::MakeLTRB(1, 2, 3, 4)); |
| 1373 | c->clear(SK_ColorCYAN); |
| 1374 | |
| 1375 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1376 | SkDynamicMemoryWStream wstream; |
| 1377 | picture->serialize(&wstream); |
| 1378 | |
| 1379 | SkAutoTDelete<SkStream> rstream(wstream.detachAsStream()); |
| 1380 | SkAutoTUnref<SkPicture> deserializedPicture(SkPicture::CreateFromStream(rstream)); |
| 1381 | |
| 1382 | REPORTER_ASSERT(r, SkToBool(deserializedPicture)); |
| 1383 | REPORTER_ASSERT(r, deserializedPicture->cullRect().left() == 1); |
| 1384 | REPORTER_ASSERT(r, deserializedPicture->cullRect().top() == 2); |
| 1385 | REPORTER_ASSERT(r, deserializedPicture->cullRect().right() == 3); |
| 1386 | REPORTER_ASSERT(r, deserializedPicture->cullRect().bottom() == 4); |
| 1387 | } |