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" |
| 10 | #include "SkDeferredCanvas.h" |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 12 | #include "SkShader.h" |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 13 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 14 | static const int gWidth = 2; |
| 15 | static const int gHeight = 2; |
| 16 | |
| 17 | static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
| 18 | bm->setConfig(config, gWidth, gHeight); |
| 19 | bm->allocPixels(); |
| 20 | bm->eraseColor(color); |
| 21 | } |
| 22 | |
| 23 | static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) { |
| 24 | SkBitmap store; |
| 25 | |
| 26 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 27 | SkDevice device(store); |
| 28 | SkDeferredCanvas canvas(&device); |
| 29 | |
| 30 | canvas.clear(0x00000000); |
| 31 | |
| 32 | SkAutoLockPixels alp(store); |
| 33 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred |
| 34 | SkBitmap accessed = canvas.getDevice()->accessBitmap(false); |
| 35 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed |
| 36 | REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef()); |
| 37 | } |
| 38 | |
| 39 | static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { |
| 40 | SkBitmap store; |
| 41 | |
| 42 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 43 | SkDevice device(store); |
| 44 | SkDeferredCanvas canvas(&device); |
| 45 | |
| 46 | canvas.clear(0x00000000); |
| 47 | |
| 48 | SkAutoLockPixels alp(store); |
| 49 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred |
| 50 | canvas.flush(); |
| 51 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed |
| 52 | } |
| 53 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 54 | static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) { |
| 55 | SkBitmap store; |
| 56 | SkRect fullRect; |
| 57 | fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), |
| 58 | SkIntToScalar(gHeight)); |
| 59 | SkRect partialRect; |
junov@chromium.org | b1e218e | 2012-02-13 22:27:58 +0000 | [diff] [blame] | 60 | partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 61 | SkIntToScalar(1), SkIntToScalar(1)); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 62 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 63 | SkDevice device(store); |
| 64 | SkDeferredCanvas canvas(&device); |
| 65 | |
| 66 | // verify that frame is intially fresh |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 68 | // no clearing op since last call to isFreshFrame -> not 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 | |
| 71 | // Verify that clear triggers a fresh frame |
| 72 | canvas.clear(0x00000000); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 74 | |
| 75 | // Verify that clear with saved state triggers a fresh frame |
| 76 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 77 | canvas.clear(0x00000000); |
| 78 | canvas.restore(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 80 | |
| 81 | // Verify that clear within a layer does NOT trigger a fresh frame |
| 82 | canvas.saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 83 | canvas.clear(0x00000000); |
| 84 | canvas.restore(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 86 | |
| 87 | // Verify that a clear with clipping triggers a fresh frame |
| 88 | // (clear is not affected by clipping) |
| 89 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 90 | canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); |
| 91 | canvas.clear(0x00000000); |
| 92 | canvas.restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 94 | |
| 95 | // Verify that full frame rects with different forms of opaque paint |
| 96 | // trigger frames to be marked as fresh |
| 97 | { |
| 98 | SkPaint paint; |
| 99 | paint.setStyle( SkPaint::kFill_Style ); |
| 100 | paint.setAlpha( 255 ); |
| 101 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 103 | } |
skia.committer@gmail.com | 5b6f916 | 2012-10-12 02:01:15 +0000 | [diff] [blame^] | 104 | { |
junov@chromium.org | 8cef67a | 2012-10-11 20:19:15 +0000 | [diff] [blame] | 105 | SkPaint paint; |
| 106 | paint.setStyle( SkPaint::kFill_Style ); |
| 107 | paint.setAlpha( 255 ); |
| 108 | paint.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 109 | canvas.drawRect(fullRect, paint); |
| 110 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
| 111 | } |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 112 | { |
| 113 | SkPaint paint; |
| 114 | paint.setStyle( SkPaint::kFill_Style ); |
| 115 | SkBitmap bmp; |
| 116 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 117 | bmp.setIsOpaque(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 118 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 119 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 120 | paint.setShader(shader)->unref(); |
| 121 | canvas.drawRect(fullRect, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Verify that full frame rects with different forms of non-opaque paint |
| 126 | // do not trigger frames to be marked as fresh |
| 127 | { |
| 128 | SkPaint paint; |
| 129 | paint.setStyle( SkPaint::kFill_Style ); |
| 130 | paint.setAlpha( 254 ); |
| 131 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 133 | } |
| 134 | { |
| 135 | SkPaint paint; |
| 136 | paint.setStyle( SkPaint::kFill_Style ); |
| 137 | SkBitmap bmp; |
| 138 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 139 | bmp.setIsOpaque(false); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 140 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 141 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 142 | paint.setShader(shader)->unref(); |
| 143 | canvas.drawRect(fullRect, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 144 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Verify that incomplete coverage does not trigger a fresh frame |
| 148 | { |
| 149 | SkPaint paint; |
| 150 | paint.setStyle(SkPaint::kFill_Style); |
| 151 | paint.setAlpha(255); |
| 152 | canvas.drawRect(partialRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Verify that incomplete coverage due to clipping does not trigger a fresh |
| 157 | // frame |
| 158 | { |
| 159 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 160 | canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); |
| 161 | SkPaint paint; |
| 162 | paint.setStyle(SkPaint::kFill_Style); |
| 163 | paint.setAlpha(255); |
| 164 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Verify that stroked rect does not trigger a fresh frame |
| 169 | { |
| 170 | SkPaint paint; |
| 171 | paint.setStyle( SkPaint::kStroke_Style ); |
| 172 | paint.setAlpha( 255 ); |
| 173 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 174 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 175 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 176 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 177 | // Verify kSrcMode triggers a fresh frame even with transparent color |
| 178 | { |
| 179 | SkPaint paint; |
| 180 | paint.setStyle( SkPaint::kFill_Style ); |
| 181 | paint.setAlpha( 100 ); |
| 182 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 183 | canvas.drawRect(fullRect, paint); |
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 | } |
| 186 | } |
| 187 | |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 188 | class MockDevice : public SkDevice { |
| 189 | public: |
| 190 | MockDevice(const SkBitmap& bm) : SkDevice(bm) { |
| 191 | fDrawBitmapCallCount = 0; |
| 192 | } |
| 193 | virtual void drawBitmap(const SkDraw&, const SkBitmap&, |
| 194 | const SkIRect*, |
| 195 | const SkMatrix&, const SkPaint&) { |
| 196 | fDrawBitmapCallCount++; |
| 197 | } |
| 198 | |
| 199 | int fDrawBitmapCallCount; |
| 200 | }; |
| 201 | |
| 202 | // Verifies that the deferred canvas triggers a flush when its memory |
| 203 | // limit is exceeded |
| 204 | static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) { |
| 205 | SkBitmap store; |
| 206 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 207 | store.allocPixels(); |
| 208 | MockDevice mockDevice(store); |
| 209 | SkDeferredCanvas canvas(&mockDevice); |
| 210 | canvas.setMaxRecordingStorage(160000); |
| 211 | |
| 212 | SkBitmap sourceImage; |
| 213 | // 100 by 100 image, takes 40,000 bytes in memory |
| 214 | sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 215 | sourceImage.allocPixels(); |
| 216 | |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 217 | for (int i = 0; i < 5; i++) { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 218 | sourceImage.notifyPixelsChanged(); // to force re-serialization |
| 219 | canvas.drawBitmap(sourceImage, 0, 0, NULL); |
| 220 | } |
| 221 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 222 | REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 223 | } |
| 224 | |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 225 | class NotificationCounter : public SkDeferredCanvas::NotificationClient { |
| 226 | public: |
| 227 | NotificationCounter() { |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 228 | fPrepareForDrawCount = fStorageAllocatedChangedCount = |
| 229 | fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | virtual void prepareForDraw() SK_OVERRIDE { |
| 233 | fPrepareForDrawCount++; |
| 234 | } |
| 235 | virtual void storageAllocatedForRecordingChanged(size_t size) SK_OVERRIDE { |
| 236 | fStorageAllocatedChangedCount++; |
| 237 | } |
| 238 | virtual void flushedDrawCommands() SK_OVERRIDE { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 239 | fFlushedDrawCommandsCount++; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 240 | } |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 241 | virtual void skippedPendingDrawCommands() SK_OVERRIDE { |
| 242 | fSkippedPendingDrawCommandsCount++; |
| 243 | } |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 244 | |
| 245 | int fPrepareForDrawCount; |
| 246 | int fStorageAllocatedChangedCount; |
| 247 | int fFlushedDrawCommandsCount; |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 248 | int fSkippedPendingDrawCommandsCount; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 251 | static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { |
| 252 | SkBitmap store; |
| 253 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 254 | store.allocPixels(); |
| 255 | SkDevice device(store); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 256 | NotificationCounter notificationCounter; |
junov@chromium.org | d433c4e | 2012-08-17 14:50:16 +0000 | [diff] [blame] | 257 | SkDeferredCanvas canvas(&device); |
| 258 | canvas.setNotificationClient(¬ificationCounter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 259 | |
| 260 | const int imageCount = 2; |
| 261 | SkBitmap sourceImages[imageCount]; |
| 262 | for (int i = 0; i < imageCount; i++) |
| 263 | { |
| 264 | sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 265 | sourceImages[i].allocPixels(); |
| 266 | } |
| 267 | |
| 268 | size_t bitmapSize = sourceImages[0].getSize(); |
| 269 | |
| 270 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 272 | // stored bitmap + drawBitmap command |
| 273 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > bitmapSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 274 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 275 | // verify that nothing can be freed at this point |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 276 | REPORTER_ASSERT(reporter, 0 == canvas.freeMemoryIfPossible(~0U)); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 277 | |
| 278 | // verify that flush leaves image in cache |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount); |
| 280 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 281 | canvas.flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 282 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
| 283 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() >= bitmapSize); |
| 285 | |
| 286 | // verify that after a flush, cached image can be freed |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 287 | REPORTER_ASSERT(reporter, canvas.freeMemoryIfPossible(~0U) >= bitmapSize); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 288 | |
| 289 | // Verify that caching works for avoiding multiple copies of the same bitmap |
| 290 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 291 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 292 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 293 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount); |
| 294 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < 2 * bitmapSize); |
| 296 | |
| 297 | // Verify partial eviction based on bytesToFree |
| 298 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 299 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 300 | canvas.flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 301 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 302 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2 * bitmapSize); |
| 303 | size_t bytesFreed = canvas.freeMemoryIfPossible(1); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 304 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 306 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 307 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 308 | // 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] | 309 | // a pending draw, while image 1 is locked-in. |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 310 | canvas.freeMemoryIfPossible(~0U); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 312 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
| 313 | canvas.flush(); |
| 314 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 315 | bytesFreed = canvas.freeMemoryIfPossible(~0U); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 316 | // only one bitmap should have been freed. |
| 317 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 318 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 319 | // Clear for next test |
| 320 | canvas.flush(); |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 321 | canvas.freeMemoryIfPossible(~0U); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < bitmapSize); |
| 323 | |
| 324 | // Verify the image cache is sensitive to genID bumps |
| 325 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 326 | sourceImages[1].notifyPixelsChanged(); |
| 327 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 328 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2*bitmapSize); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 329 | |
| 330 | // Verify that nothing in this test caused commands to be skipped |
| 331 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 332 | } |
| 333 | |
| 334 | static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) { |
| 335 | SkBitmap store; |
| 336 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 337 | store.allocPixels(); |
| 338 | SkDevice device(store); |
| 339 | NotificationCounter notificationCounter; |
| 340 | SkDeferredCanvas canvas(&device); |
| 341 | canvas.setNotificationClient(¬ificationCounter); |
| 342 | canvas.clear(0x0); |
| 343 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 344 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount); |
| 345 | canvas.flush(); |
| 346 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 347 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
| 348 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 349 | } |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 350 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 351 | static void TestDeferredCanvas(skiatest::Reporter* reporter) { |
| 352 | TestDeferredCanvasBitmapAccess(reporter); |
| 353 | TestDeferredCanvasFlush(reporter); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 354 | TestDeferredCanvasFreshFrame(reporter); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 355 | TestDeferredCanvasMemoryLimit(reporter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 356 | TestDeferredCanvasBitmapCaching(reporter); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 357 | TestDeferredCanvasSkip(reporter); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | #include "TestClassDef.h" |
| 361 | DEFINE_TESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas) |