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(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +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 | } |
| 104 | { |
| 105 | SkPaint paint; |
| 106 | paint.setStyle( SkPaint::kFill_Style ); |
| 107 | SkBitmap bmp; |
| 108 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 109 | bmp.setIsOpaque(true); |
| 110 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
| 111 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 112 | paint.setShader(shader)->unref(); |
| 113 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 114 | REPORTER_ASSERT(reporter, canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Verify that full frame rects with different forms of non-opaque paint |
| 118 | // do not trigger frames to be marked as fresh |
| 119 | { |
| 120 | SkPaint paint; |
| 121 | paint.setStyle( SkPaint::kFill_Style ); |
| 122 | paint.setAlpha( 254 ); |
| 123 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +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 | SkPaint paint; |
| 128 | paint.setStyle( SkPaint::kFill_Style ); |
| 129 | SkBitmap bmp; |
| 130 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
| 131 | bmp.setIsOpaque(false); |
| 132 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
| 133 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 134 | paint.setShader(shader)->unref(); |
| 135 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 136 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Verify that incomplete coverage does not trigger a fresh frame |
| 140 | { |
| 141 | SkPaint paint; |
| 142 | paint.setStyle(SkPaint::kFill_Style); |
| 143 | paint.setAlpha(255); |
| 144 | canvas.drawRect(partialRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +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 due to clipping does not trigger a fresh |
| 149 | // frame |
| 150 | { |
| 151 | canvas.save(SkCanvas::kMatrixClip_SaveFlag); |
| 152 | canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); |
| 153 | SkPaint paint; |
| 154 | paint.setStyle(SkPaint::kFill_Style); |
| 155 | paint.setAlpha(255); |
| 156 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 157 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Verify that stroked rect does not trigger a fresh frame |
| 161 | { |
| 162 | SkPaint paint; |
| 163 | paint.setStyle( SkPaint::kStroke_Style ); |
| 164 | paint.setAlpha( 255 ); |
| 165 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 166 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Verify kSrcMode triggers a fresh frame even with transparent color |
| 170 | { |
| 171 | SkPaint paint; |
| 172 | paint.setStyle( SkPaint::kFill_Style ); |
| 173 | paint.setAlpha( 100 ); |
| 174 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 175 | canvas.drawRect(fullRect, paint); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 176 | REPORTER_ASSERT(reporter, !canvas.isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 180 | class MockDevice : public SkDevice { |
| 181 | public: |
| 182 | MockDevice(const SkBitmap& bm) : SkDevice(bm) { |
| 183 | fDrawBitmapCallCount = 0; |
| 184 | } |
| 185 | virtual void drawBitmap(const SkDraw&, const SkBitmap&, |
| 186 | const SkIRect*, |
| 187 | const SkMatrix&, const SkPaint&) { |
| 188 | fDrawBitmapCallCount++; |
| 189 | } |
| 190 | |
| 191 | int fDrawBitmapCallCount; |
| 192 | }; |
| 193 | |
| 194 | // Verifies that the deferred canvas triggers a flush when its memory |
| 195 | // limit is exceeded |
| 196 | static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) { |
| 197 | SkBitmap store; |
| 198 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 199 | store.allocPixels(); |
| 200 | MockDevice mockDevice(store); |
| 201 | SkDeferredCanvas canvas(&mockDevice); |
| 202 | canvas.setMaxRecordingStorage(160000); |
| 203 | |
| 204 | SkBitmap sourceImage; |
| 205 | // 100 by 100 image, takes 40,000 bytes in memory |
| 206 | sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 207 | sourceImage.allocPixels(); |
| 208 | |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 209 | for (int i = 0; i < 5; i++) { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 210 | sourceImage.notifyPixelsChanged(); // to force re-serialization |
| 211 | canvas.drawBitmap(sourceImage, 0, 0, NULL); |
| 212 | } |
| 213 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 215 | } |
| 216 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 217 | static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { |
| 218 | SkBitmap store; |
| 219 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 220 | store.allocPixels(); |
| 221 | SkDevice device(store); |
| 222 | SkDeferredCanvas canvas(&device); |
| 223 | |
| 224 | const int imageCount = 2; |
| 225 | SkBitmap sourceImages[imageCount]; |
| 226 | for (int i = 0; i < imageCount; i++) |
| 227 | { |
| 228 | sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 229 | sourceImages[i].allocPixels(); |
| 230 | } |
| 231 | |
| 232 | size_t bitmapSize = sourceImages[0].getSize(); |
| 233 | |
| 234 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
| 235 | // stored bitmap + drawBitmap command |
| 236 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > bitmapSize); |
| 237 | |
| 238 | // verify that nothing can be freed at this point |
| 239 | REPORTER_ASSERT(reporter, 0 == canvas.freeMemoryIfPossible(~0)); |
| 240 | |
| 241 | // verify that flush leaves image in cache |
| 242 | canvas.flush(); |
| 243 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() >= bitmapSize); |
| 244 | |
| 245 | // verify that after a flush, cached image can be freed |
| 246 | REPORTER_ASSERT(reporter, canvas.freeMemoryIfPossible(~0) >= bitmapSize); |
| 247 | |
| 248 | // Verify that caching works for avoiding multiple copies of the same bitmap |
| 249 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
| 250 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
| 251 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < 2 * bitmapSize); |
| 252 | |
| 253 | // Verify partial eviction based on bytesToFree |
| 254 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 255 | canvas.flush(); |
| 256 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2 * bitmapSize); |
| 257 | size_t bytesFreed = canvas.freeMemoryIfPossible(1); |
| 258 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 259 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 260 | |
| 261 | // Verifiy that partial purge works, image zero is in cache but not reffed by |
| 262 | // a pending draw, while image 1 is locked-in. |
| 263 | canvas.freeMemoryIfPossible(~0); |
| 264 | canvas.drawBitmap(sourceImages[0], 0, 0, NULL); |
| 265 | canvas.flush(); |
| 266 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 267 | bytesFreed = canvas.freeMemoryIfPossible(~0); |
| 268 | // only one bitmap should have been freed. |
| 269 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 270 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 271 | // Clear for next test |
| 272 | canvas.flush(); |
| 273 | canvas.freeMemoryIfPossible(~0); |
| 274 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() < bitmapSize); |
| 275 | |
| 276 | // Verify the image cache is sensitive to genID bumps |
| 277 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 278 | sourceImages[1].notifyPixelsChanged(); |
| 279 | canvas.drawBitmap(sourceImages[1], 0, 0, NULL); |
| 280 | REPORTER_ASSERT(reporter, canvas.storageAllocatedForRecording() > 2*bitmapSize); |
| 281 | } |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 282 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 283 | static void TestDeferredCanvas(skiatest::Reporter* reporter) { |
| 284 | TestDeferredCanvasBitmapAccess(reporter); |
| 285 | TestDeferredCanvasFlush(reporter); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 286 | TestDeferredCanvasFreshFrame(reporter); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 287 | TestDeferredCanvasMemoryLimit(reporter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 288 | TestDeferredCanvasBitmapCaching(reporter); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | #include "TestClassDef.h" |
| 292 | DEFINE_TESTCLASS("DeferredCanvas", TestDeferredCanvasClass, TestDeferredCanvas) |