junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "Test.h" |
| 9 | #include "SkBitmap.h" |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 10 | #include "SkBitmapProcShader.h" |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 11 | #include "SkDeferredCanvas.h" |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 14 | #include "SkShader.h" |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 15 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 16 | static const int gWidth = 2; |
| 17 | static const int gHeight = 2; |
| 18 | |
| 19 | static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
| 20 | bm->setConfig(config, gWidth, gHeight); |
| 21 | bm->allocPixels(); |
| 22 | bm->eraseColor(color); |
| 23 | } |
| 24 | |
| 25 | static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) { |
| 26 | SkBitmap store; |
| 27 | |
| 28 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 29 | SkDevice device(store); |
| 30 | SkDeferredCanvas canvas(&device); |
| 31 | |
| 32 | canvas.clear(0x00000000); |
| 33 | |
| 34 | SkAutoLockPixels alp(store); |
| 35 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred |
| 36 | SkBitmap accessed = canvas.getDevice()->accessBitmap(false); |
| 37 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed |
| 38 | REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef()); |
| 39 | } |
| 40 | |
| 41 | static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { |
| 42 | SkBitmap store; |
| 43 | |
| 44 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 45 | SkDevice device(store); |
| 46 | SkDeferredCanvas canvas(&device); |
| 47 | |
| 48 | canvas.clear(0x00000000); |
| 49 | |
| 50 | SkAutoLockPixels alp(store); |
| 51 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred |
| 52 | canvas.flush(); |
| 53 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed |
| 54 | } |
| 55 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 56 | static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) { |
| 57 | SkBitmap store; |
| 58 | SkRect fullRect; |
| 59 | fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), |
| 60 | SkIntToScalar(gHeight)); |
| 61 | SkRect partialRect; |
junov@chromium.org | b1e218e | 2012-02-13 22:27:58 +0000 | [diff] [blame] | 62 | partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 63 | SkIntToScalar(1), SkIntToScalar(1)); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 64 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 65 | SkDevice device(store); |
| 66 | SkDeferredCanvas canvas(&device); |
| 67 | |
| 68 | // verify that frame is intially fresh |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 70 | // no clearing op since last call to isFreshFrame -> not fresh |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 72 | |
| 73 | // Verify that clear triggers a fresh frame |
| 74 | canvas.clear(0x00000000); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 76 | |
| 77 | // Verify that clear with saved state triggers a fresh frame |
| 78 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 79 | canvas.clear(0x00000000); |
| 80 | canvas.restore(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 82 | |
| 83 | // Verify that clear within a layer does NOT trigger a fresh frame |
| 84 | canvas.saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 85 | canvas.clear(0x00000000); |
| 86 | canvas.restore(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 88 | |
| 89 | // Verify that a clear with clipping triggers a fresh frame |
| 90 | // (clear is not affected by clipping) |
| 91 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 92 | canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); |
| 93 | canvas.clear(0x00000000); |
| 94 | canvas.restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 96 | |
| 97 | // Verify that full frame rects with different forms of opaque paint |
| 98 | // trigger frames to be marked as fresh |
| 99 | { |
| 100 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 101 | paint.setStyle(SkPaint::kFill_Style); |
| 102 | paint.setAlpha(255); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 103 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 105 | } |
skia.committer@gmail.com | 5b6f916 | 2012-10-12 02:01:15 +0000 | [diff] [blame] | 106 | { |
junov@chromium.org | 8cef67a | 2012-10-11 20:19:15 +0000 | [diff] [blame] | 107 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 108 | paint.setStyle(SkPaint::kFill_Style); |
| 109 | paint.setAlpha(255); |
junov@chromium.org | 8cef67a | 2012-10-11 20:19:15 +0000 | [diff] [blame] | 110 | paint.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 111 | canvas.drawRect(fullRect, paint); |
| 112 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
| 113 | } |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 114 | { |
| 115 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 116 | paint.setStyle(SkPaint::kFill_Style); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 117 | SkBitmap bmp; |
| 118 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 119 | bmp.setIsOpaque(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 121 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 122 | paint.setShader(shader)->unref(); |
| 123 | canvas.drawRect(fullRect, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Verify that full frame rects with different forms of non-opaque paint |
| 128 | // do not trigger frames to be marked as fresh |
| 129 | { |
| 130 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 131 | paint.setStyle(SkPaint::kFill_Style); |
| 132 | paint.setAlpha(254); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 133 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 134 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 135 | } |
| 136 | { |
| 137 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 138 | paint.setStyle(SkPaint::kFill_Style); |
| 139 | // Defining a cone that partially overlaps the canvas |
| 140 | const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0)); |
| 141 | const SkScalar r1 = SkIntToScalar(1); |
| 142 | const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0)); |
| 143 | const SkScalar r2 = SkIntToScalar(5); |
| 144 | const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE}; |
| 145 | const SkScalar pos[2] = {0, SK_Scalar1}; |
| 146 | SkShader* shader = SkGradientShader::CreateTwoPointConical( |
| 147 | pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL); |
| 148 | paint.setShader(shader)->unref(); |
| 149 | canvas.drawRect(fullRect, paint); |
| 150 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
| 151 | } |
| 152 | { |
| 153 | SkPaint paint; |
| 154 | paint.setStyle(SkPaint::kFill_Style); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 155 | SkBitmap bmp; |
| 156 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 157 | bmp.setIsOpaque(false); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 158 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 159 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 160 | paint.setShader(shader)->unref(); |
| 161 | canvas.drawRect(fullRect, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Verify that incomplete coverage does not trigger a fresh frame |
| 166 | { |
| 167 | SkPaint paint; |
| 168 | paint.setStyle(SkPaint::kFill_Style); |
| 169 | paint.setAlpha(255); |
| 170 | canvas.drawRect(partialRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Verify that incomplete coverage due to clipping does not trigger a fresh |
| 175 | // frame |
| 176 | { |
| 177 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 178 | canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); |
| 179 | SkPaint paint; |
| 180 | paint.setStyle(SkPaint::kFill_Style); |
| 181 | paint.setAlpha(255); |
| 182 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 41e850f | 2012-12-10 21:24:38 +0000 | [diff] [blame] | 183 | canvas.restore(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 184 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 185 | } |
junov@chromium.org | 8f0ca06 | 2012-12-13 16:30:39 +0000 | [diff] [blame] | 186 | { |
| 187 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 188 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 189 | paint.setStyle(SkPaint::kFill_Style); |
| 190 | paint.setAlpha(255); |
junov@chromium.org | 8f0ca06 | 2012-12-13 16:30:39 +0000 | [diff] [blame] | 191 | SkPath path; |
| 192 | path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2)); |
| 193 | canvas.clipPath(path, SkRegion::kIntersect_Op, false); |
| 194 | canvas.drawRect(fullRect, paint); |
| 195 | canvas.restore(); |
| 196 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
| 197 | } |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 198 | |
| 199 | // Verify that stroked rect does not trigger a fresh frame |
| 200 | { |
| 201 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 202 | paint.setStyle(SkPaint::kStroke_Style); |
| 203 | paint.setAlpha(255); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 204 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 205 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 206 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 207 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 208 | // Verify kSrcMode triggers a fresh frame even with transparent color |
| 209 | { |
| 210 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 211 | paint.setStyle(SkPaint::kFill_Style); |
| 212 | paint.setAlpha(100); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 213 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 214 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 41e850f | 2012-12-10 21:24:38 +0000 | [diff] [blame] | 215 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 219 | class MockDevice : public SkDevice { |
| 220 | public: |
| 221 | MockDevice(const SkBitmap& bm) : SkDevice(bm) { |
| 222 | fDrawBitmapCallCount = 0; |
| 223 | } |
| 224 | virtual void drawBitmap(const SkDraw&, const SkBitmap&, |
| 225 | const SkIRect*, |
| 226 | const SkMatrix&, const SkPaint&) { |
| 227 | fDrawBitmapCallCount++; |
| 228 | } |
| 229 | |
| 230 | int fDrawBitmapCallCount; |
| 231 | }; |
| 232 | |
| 233 | // Verifies that the deferred canvas triggers a flush when its memory |
| 234 | // limit is exceeded |
| 235 | static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) { |
| 236 | SkBitmap store; |
| 237 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 238 | store.allocPixels(); |
| 239 | MockDevice mockDevice(store); |
| 240 | SkDeferredCanvas canvas(&mockDevice); |
| 241 | canvas.setMaxRecordingStorage(160000); |
| 242 | |
| 243 | SkBitmap sourceImage; |
| 244 | // 100 by 100 image, takes 40,000 bytes in memory |
| 245 | sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 246 | sourceImage.allocPixels(); |
| 247 | |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 248 | for (int i = 0; i < 5; i++) { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 249 | sourceImage.notifyPixelsChanged(); // to force re-serialization |
| 250 | canvas.drawBitmap(sourceImage, 0, 0, NULL); |
| 251 | } |
| 252 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 253 | REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 254 | } |
| 255 | |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 256 | class NotificationCounter : public SkDeferredCanvas::NotificationClient { |
| 257 | public: |
| 258 | NotificationCounter() { |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 259 | fPrepareForDrawCount = fStorageAllocatedChangedCount = |
| 260 | fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | virtual void prepareForDraw() SK_OVERRIDE { |
| 264 | fPrepareForDrawCount++; |
| 265 | } |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 266 | virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE { |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 267 | fStorageAllocatedChangedCount++; |
| 268 | } |
| 269 | virtual void flushedDrawCommands() SK_OVERRIDE { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 270 | fFlushedDrawCommandsCount++; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 271 | } |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 272 | virtual void skippedPendingDrawCommands() SK_OVERRIDE { |
| 273 | fSkippedPendingDrawCommandsCount++; |
| 274 | } |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 275 | |
| 276 | int fPrepareForDrawCount; |
| 277 | int fStorageAllocatedChangedCount; |
| 278 | int fFlushedDrawCommandsCount; |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 279 | int fSkippedPendingDrawCommandsCount; |
robertphillips@google.com | 5990397 | 2013-02-07 21:02:23 +0000 | [diff] [blame] | 280 | |
| 281 | private: |
| 282 | typedef SkDeferredCanvas::NotificationClient INHERITED; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 285 | static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { |
| 286 | SkBitmap store; |
| 287 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 288 | store.allocPixels(); |
| 289 | SkDevice device(store); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 290 | NotificationCounter notificationCounter; |
junov@chromium.org | d433c4e | 2012-08-17 14:50:16 +0000 | [diff] [blame] | 291 | SkDeferredCanvas canvas(&device); |
| 292 | canvas.setNotificationClient(¬ificationCounter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 293 | |
| 294 | const int imageCount = 2; |
| 295 | SkBitmap sourceImages[imageCount]; |
| 296 | for (int i = 0; i < imageCount; i++) |
| 297 | { |
| 298 | sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 299 | sourceImages[i].allocPixels(); |
| 300 | } |
| 301 | |
| 302 | size_t bitmapSize = sourceImages[0].getSize(); |
| 303 | |
| 304 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 306 | // stored bitmap + drawBitmap command |
| 307 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > bitmapSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 308 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 309 | // verify that nothing can be freed at this point |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 310 | REPORTER_ASSERT(reporter, 0 == canvas.freeMemoryIfPossible(~0U)); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 311 | |
| 312 | // verify that flush leaves image in cache |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount); |
| 314 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 315 | canvas.flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 316 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
| 317 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 318 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() >= bitmapSize); |
| 319 | |
| 320 | // verify that after a flush, cached image can be freed |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 321 | REPORTER_ASSERT(reporter, canvas.freeMemoryIfPossible(~0U) >= bitmapSize); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 322 | |
| 323 | // Verify that caching works for avoiding multiple copies of the same bitmap |
| 324 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 325 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 326 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 327 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount); |
| 328 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 329 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < 2 * bitmapSize); |
| 330 | |
| 331 | // Verify partial eviction based on bytesToFree |
| 332 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 333 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 334 | canvas.flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 335 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 336 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2 * bitmapSize); |
| 337 | size_t bytesFreed = canvas.freeMemoryIfPossible(1); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 338 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 339 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 340 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 341 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 342 | // Verifiy that partial purge works, image zero is in cache but not reffed by |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 343 | // a pending draw, while image 1 is locked-in. |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 344 | canvas.freeMemoryIfPossible(~0U); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 345 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 346 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
| 347 | canvas.flush(); |
| 348 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 349 | bytesFreed = canvas.freeMemoryIfPossible(~0U); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 350 | // only one bitmap should have been freed. |
| 351 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 352 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 353 | // Clear for next test |
| 354 | canvas.flush(); |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 355 | canvas.freeMemoryIfPossible(~0U); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 356 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < bitmapSize); |
| 357 | |
| 358 | // Verify the image cache is sensitive to genID bumps |
| 359 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 360 | sourceImages[1].notifyPixelsChanged(); |
| 361 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 362 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2*bitmapSize); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 363 | |
| 364 | // Verify that nothing in this test caused commands to be skipped |
| 365 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 366 | } |
| 367 | |
| 368 | static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) { |
| 369 | SkBitmap store; |
| 370 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 371 | store.allocPixels(); |
| 372 | SkDevice device(store); |
| 373 | NotificationCounter notificationCounter; |
| 374 | SkDeferredCanvas canvas(&device); |
| 375 | canvas.setNotificationClient(¬ificationCounter); |
| 376 | canvas.clear(0x0); |
| 377 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 378 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount); |
| 379 | canvas.flush(); |
| 380 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 381 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
| 382 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 383 | } |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 384 | |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 385 | static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) { |
| 386 | // This is a regression test for crbug.com/155875 |
| 387 | // This test covers a code path that inserts bitmaps into the bitmap heap through the |
| 388 | // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through |
| 389 | // the flattening and unflattening of the shader. |
| 390 | SkBitmap store; |
| 391 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 392 | store.allocPixels(); |
| 393 | SkDevice device(store); |
| 394 | SkDeferredCanvas canvas(&device); |
| 395 | // test will fail if nbIterations is not in sync with |
| 396 | // BITMAPS_TO_KEEP in SkGPipeWrite.cpp |
| 397 | const int nbIterations = 5; |
| 398 | size_t bytesAllocated = 0; |
| 399 | for(int pass = 0; pass < 2; ++pass) { |
| 400 | for(int i = 0; i < nbIterations; ++i) { |
| 401 | SkPaint paint; |
| 402 | SkBitmap paintPattern; |
| 403 | paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); |
| 404 | paintPattern.allocPixels(); |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 405 | paint.setShader(SkNEW_ARGS(SkBitmapProcShader, |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 406 | (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref(); |
| 407 | canvas.drawPaint(paint); |
| 408 | canvas.flush(); |
| 409 | |
| 410 | // In the first pass, memory allocation should be monotonically increasing as |
| 411 | // the bitmap heap slots fill up. In the second pass memory allocation should be |
| 412 | // stable as bitmap heap slots get recycled. |
| 413 | size_t newBytesAllocated = canvas.storageAllocatedForRecording(); |
| 414 | if (pass == 0) { |
| 415 | REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated); |
| 416 | bytesAllocated = newBytesAllocated; |
| 417 | } else { |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 418 | REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 422 | // All cached resources should be evictable since last canvas call was flush() |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 423 | canvas.freeMemoryIfPossible(~0U); |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 424 | REPORTER_ASSERT(reporter, 0 == canvas.storageAllocatedForRecording()); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 425 | } |
| 426 | |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 427 | static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) { |
| 428 | SkBitmap store; |
| 429 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 430 | store.allocPixels(); |
skia.committer@gmail.com | 1c9c0d3 | 2012-11-22 02:02:41 +0000 | [diff] [blame] | 431 | |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 432 | SkBitmap sourceImage; |
| 433 | // 100 by 100 image, takes 40,000 bytes in memory |
| 434 | sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 435 | sourceImage.allocPixels(); |
| 436 | |
| 437 | // 1 under : should not store the image |
| 438 | { |
| 439 | SkDevice device(store); |
| 440 | SkDeferredCanvas canvas(&device); |
| 441 | canvas.setBitmapSizeThreshold(39999); |
| 442 | canvas.drawBitmap(sourceImage, 0, 0, NULL); |
| 443 | size_t newBytesAllocated = canvas.storageAllocatedForRecording(); |
| 444 | REPORTER_ASSERT(reporter, newBytesAllocated == 0); |
| 445 | } |
| 446 | |
| 447 | // exact value : should store the image |
| 448 | { |
| 449 | SkDevice device(store); |
| 450 | SkDeferredCanvas canvas(&device); |
| 451 | canvas.setBitmapSizeThreshold(40000); |
| 452 | canvas.drawBitmap(sourceImage, 0, 0, NULL); |
| 453 | size_t newBytesAllocated = canvas.storageAllocatedForRecording(); |
| 454 | REPORTER_ASSERT(reporter, newBytesAllocated > 0); |
| 455 | } |
| 456 | |
| 457 | // 1 over : should still store the image |
| 458 | { |
| 459 | SkDevice device(store); |
| 460 | SkDeferredCanvas canvas(&device); |
| 461 | canvas.setBitmapSizeThreshold(40001); |
| 462 | canvas.drawBitmap(sourceImage, 0, 0, NULL); |
| 463 | size_t newBytesAllocated = canvas.storageAllocatedForRecording(); |
| 464 | REPORTER_ASSERT(reporter, newBytesAllocated > 0); |
| 465 | } |
| 466 | } |
| 467 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 468 | static void TestDeferredCanvas(skiatest::Reporter* reporter) { |
| 469 | TestDeferredCanvasBitmapAccess(reporter); |
| 470 | TestDeferredCanvasFlush(reporter); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 471 | TestDeferredCanvasFreshFrame(reporter); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 472 | TestDeferredCanvasMemoryLimit(reporter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 473 | TestDeferredCanvasBitmapCaching(reporter); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 474 | TestDeferredCanvasSkip(reporter); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 475 | TestDeferredCanvasBitmapShaderNoLeak(reporter); |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 476 | TestDeferredCanvasBitmapSizeThreshold(reporter); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | #include "TestClassDef.h" |
| 480 | DEFINE_TESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas) |