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