junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 7 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 11 | #include "SkBitmapDevice.h" |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 12 | #include "SkBitmapProcShader.h" |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 13 | #include "SkDeferredCanvas.h" |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 15 | #include "SkShader.h" |
junov@chromium.org | 44324fa | 2013-08-02 15:36:02 +0000 | [diff] [blame] | 16 | #include "../src/image/SkSurface_Base.h" |
| 17 | #include "../src/image/SkImagePriv.h" |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContextFactory.h" |
| 20 | #else |
| 21 | class GrContextFactory; |
| 22 | #endif |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 23 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 24 | static const int gWidth = 2; |
| 25 | static const int gHeight = 2; |
| 26 | |
| 27 | static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) { |
| 28 | bm->setConfig(config, gWidth, gHeight); |
| 29 | bm->allocPixels(); |
| 30 | bm->eraseColor(color); |
| 31 | } |
| 32 | |
| 33 | static void TestDeferredCanvasBitmapAccess(skiatest::Reporter* reporter) { |
| 34 | SkBitmap store; |
| 35 | |
| 36 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 37 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 38 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 39 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 40 | canvas->clear(0x00000000); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 41 | |
| 42 | SkAutoLockPixels alp(store); |
| 43 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 44 | SkBitmap accessed = canvas->getDevice()->accessBitmap(false); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed |
| 46 | REPORTER_ASSERT(reporter, accessed.pixelRef() == store.pixelRef()); |
| 47 | } |
| 48 | |
junov@chromium.org | 44324fa | 2013-08-02 15:36:02 +0000 | [diff] [blame] | 49 | class MockSurface : public SkSurface_Base { |
| 50 | public: |
| 51 | MockSurface(int width, int height) : SkSurface_Base(width, height) { |
| 52 | clearCounts(); |
| 53 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 54 | fBitmap.allocPixels(); |
| 55 | } |
| 56 | |
| 57 | virtual SkCanvas* onNewCanvas() SK_OVERRIDE { |
| 58 | return SkNEW_ARGS(SkCanvas, (fBitmap)); |
| 59 | } |
| 60 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 61 | virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE { |
junov@chromium.org | 44324fa | 2013-08-02 15:36:02 +0000 | [diff] [blame] | 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | virtual SkImage* onNewImageSnapshot() SK_OVERRIDE { |
| 66 | return SkNewImageFromBitmap(fBitmap, true); |
| 67 | } |
| 68 | |
| 69 | virtual void onCopyOnWrite(ContentChangeMode mode) SK_OVERRIDE { |
| 70 | if (mode == SkSurface::kDiscard_ContentChangeMode) { |
| 71 | fDiscardCount++; |
| 72 | } else { |
| 73 | fRetainCount++; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void clearCounts() { |
| 78 | fDiscardCount = 0; |
skia.committer@gmail.com | ea4b797 | 2013-08-06 07:01:27 +0000 | [diff] [blame] | 79 | fRetainCount = 0; |
junov@chromium.org | 44324fa | 2013-08-02 15:36:02 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int fDiscardCount, fRetainCount; |
| 83 | SkBitmap fBitmap; |
| 84 | }; |
| 85 | |
| 86 | static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) { |
| 87 | SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10))); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 88 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get())); |
junov@chromium.org | 44324fa | 2013-08-02 15:36:02 +0000 | [diff] [blame] | 89 | |
| 90 | SkBitmap srcBitmap; |
| 91 | srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); |
| 92 | srcBitmap.allocPixels(); |
| 93 | srcBitmap.eraseColor(SK_ColorGREEN); |
| 94 | // Tests below depend on this bitmap being recognized as opaque |
| 95 | |
| 96 | // Preliminary sanity check: no copy on write if no active snapshot |
| 97 | surface->clearCounts(); |
| 98 | canvas->clear(SK_ColorWHITE); |
| 99 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 100 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 101 | |
| 102 | surface->clearCounts(); |
| 103 | canvas->flush(); |
| 104 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 105 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 106 | |
| 107 | // Case 1: Discard notification happens upon flushing |
| 108 | // with an Image attached. |
| 109 | surface->clearCounts(); |
| 110 | SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot()); |
| 111 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 112 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 113 | |
| 114 | surface->clearCounts(); |
| 115 | canvas->clear(SK_ColorWHITE); |
| 116 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 117 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 118 | |
| 119 | surface->clearCounts(); |
| 120 | canvas->flush(); |
| 121 | REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
| 122 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 123 | |
| 124 | // Case 2: Opaque writePixels |
| 125 | surface->clearCounts(); |
| 126 | SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot()); |
| 127 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 128 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 129 | |
| 130 | surface->clearCounts(); |
| 131 | canvas->writePixels(srcBitmap, 0, 0); |
| 132 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 133 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 134 | |
| 135 | surface->clearCounts(); |
| 136 | canvas->flush(); |
| 137 | REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
| 138 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 139 | |
| 140 | // Case 3: writePixels that partially covers the canvas |
| 141 | surface->clearCounts(); |
| 142 | SkAutoTUnref<SkImage> image3(canvas->newImageSnapshot()); |
| 143 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 144 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 145 | |
| 146 | surface->clearCounts(); |
| 147 | canvas->writePixels(srcBitmap, 5, 0); |
| 148 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 149 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 150 | |
| 151 | surface->clearCounts(); |
| 152 | canvas->flush(); |
| 153 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 154 | REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
| 155 | |
| 156 | // Case 4: unpremultiplied opaque writePixels that entirely |
| 157 | // covers the canvas |
| 158 | surface->clearCounts(); |
| 159 | SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot()); |
| 160 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 161 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 162 | |
| 163 | surface->clearCounts(); |
| 164 | canvas->writePixels(srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888); |
| 165 | REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
| 166 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 167 | |
| 168 | surface->clearCounts(); |
| 169 | canvas->flush(); |
| 170 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 171 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 172 | |
| 173 | // Case 5: unpremultiplied opaque writePixels that partially |
| 174 | // covers the canvas |
| 175 | surface->clearCounts(); |
| 176 | SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot()); |
| 177 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 178 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 179 | |
| 180 | surface->clearCounts(); |
| 181 | canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); |
| 182 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 183 | REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
| 184 | |
| 185 | surface->clearCounts(); |
| 186 | canvas->flush(); |
| 187 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 188 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 189 | |
| 190 | // Case 6: unpremultiplied opaque writePixels that entirely |
| 191 | // covers the canvas, preceded by clear |
| 192 | surface->clearCounts(); |
| 193 | SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot()); |
| 194 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 195 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 196 | |
| 197 | surface->clearCounts(); |
| 198 | canvas->clear(SK_ColorWHITE); |
| 199 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 200 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 201 | |
| 202 | surface->clearCounts(); |
| 203 | canvas->writePixels(srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888); |
| 204 | REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
| 205 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 206 | |
| 207 | surface->clearCounts(); |
| 208 | canvas->flush(); |
| 209 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 210 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 211 | |
| 212 | // Case 7: unpremultiplied opaque writePixels that partially |
| 213 | // covers the canvas, preceeded by a clear |
| 214 | surface->clearCounts(); |
| 215 | SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot()); |
| 216 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 217 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 218 | |
| 219 | surface->clearCounts(); |
| 220 | canvas->clear(SK_ColorWHITE); |
| 221 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 222 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 223 | |
| 224 | surface->clearCounts(); |
| 225 | canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); |
| 226 | REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear |
| 227 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 228 | |
| 229 | surface->clearCounts(); |
| 230 | canvas->flush(); |
| 231 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 232 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 233 | |
| 234 | // Case 8: unpremultiplied opaque writePixels that partially |
| 235 | // covers the canvas, preceeded by a drawREct that partially |
| 236 | // covers the canvas |
| 237 | surface->clearCounts(); |
| 238 | SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot()); |
| 239 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 240 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 241 | |
| 242 | surface->clearCounts(); |
| 243 | SkPaint paint; |
| 244 | canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint); |
| 245 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 246 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 247 | |
| 248 | surface->clearCounts(); |
| 249 | canvas->writePixels(srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); |
| 250 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 251 | REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
| 252 | |
| 253 | surface->clearCounts(); |
| 254 | canvas->flush(); |
| 255 | REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
| 256 | REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
| 257 | } |
| 258 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 259 | static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { |
| 260 | SkBitmap store; |
| 261 | |
| 262 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 263 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 264 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 265 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 266 | canvas->clear(0x00000000); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 267 | |
| 268 | SkAutoLockPixels alp(store); |
| 269 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0xFFFFFFFF); //verify that clear was deferred |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 270 | canvas->flush(); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, store.getColor(0,0) == 0x00000000); //verify that clear was executed |
| 272 | } |
| 273 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 274 | static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) { |
| 275 | SkBitmap store; |
| 276 | SkRect fullRect; |
| 277 | fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), |
| 278 | SkIntToScalar(gHeight)); |
| 279 | SkRect partialRect; |
junov@chromium.org | b1e218e | 2012-02-13 22:27:58 +0000 | [diff] [blame] | 280 | partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 281 | SkIntToScalar(1), SkIntToScalar(1)); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 282 | create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 283 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 284 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 285 | |
| 286 | // verify that frame is intially fresh |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 287 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 288 | // no clearing op since last call to isFreshFrame -> not fresh |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 290 | |
| 291 | // Verify that clear triggers a fresh frame |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 292 | canvas->clear(0x00000000); |
| 293 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 294 | |
| 295 | // Verify that clear with saved state triggers a fresh frame |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 296 | canvas->save(SkCanvas::kMatrixClip_SaveFlag); |
| 297 | canvas->clear(0x00000000); |
| 298 | canvas->restore(); |
| 299 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 300 | |
| 301 | // Verify that clear within a layer does NOT trigger a fresh frame |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 302 | canvas->saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 303 | canvas->clear(0x00000000); |
| 304 | canvas->restore(); |
| 305 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 306 | |
| 307 | // Verify that a clear with clipping triggers a fresh frame |
| 308 | // (clear is not affected by clipping) |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 309 | canvas->save(SkCanvas::kMatrixClip_SaveFlag); |
| 310 | canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false); |
| 311 | canvas->clear(0x00000000); |
| 312 | canvas->restore(); |
| 313 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 314 | |
| 315 | // Verify that full frame rects with different forms of opaque paint |
| 316 | // trigger frames to be marked as fresh |
| 317 | { |
| 318 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 319 | paint.setStyle(SkPaint::kFill_Style); |
| 320 | paint.setAlpha(255); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 321 | canvas->drawRect(fullRect, paint); |
| 322 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 323 | } |
skia.committer@gmail.com | 5b6f916 | 2012-10-12 02:01:15 +0000 | [diff] [blame] | 324 | { |
junov@chromium.org | 8cef67a | 2012-10-11 20:19:15 +0000 | [diff] [blame] | 325 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 326 | paint.setStyle(SkPaint::kFill_Style); |
| 327 | paint.setAlpha(255); |
junov@chromium.org | 8cef67a | 2012-10-11 20:19:15 +0000 | [diff] [blame] | 328 | paint.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 329 | canvas->drawRect(fullRect, paint); |
| 330 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8cef67a | 2012-10-11 20:19:15 +0000 | [diff] [blame] | 331 | } |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 332 | { |
| 333 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 334 | paint.setStyle(SkPaint::kFill_Style); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 335 | SkBitmap bmp; |
| 336 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 337 | bmp.setAlphaType(kOpaque_SkAlphaType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 338 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 339 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 340 | paint.setShader(shader)->unref(); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 341 | canvas->drawRect(fullRect, paint); |
| 342 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // Verify that full frame rects with different forms of non-opaque paint |
| 346 | // do not trigger frames to be marked as fresh |
| 347 | { |
| 348 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 349 | paint.setStyle(SkPaint::kFill_Style); |
| 350 | paint.setAlpha(254); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 351 | canvas->drawRect(fullRect, paint); |
| 352 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 353 | } |
| 354 | { |
| 355 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 356 | paint.setStyle(SkPaint::kFill_Style); |
| 357 | // Defining a cone that partially overlaps the canvas |
| 358 | const SkPoint pt1 = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0)); |
| 359 | const SkScalar r1 = SkIntToScalar(1); |
| 360 | const SkPoint pt2 = SkPoint::Make(SkIntToScalar(10), SkIntToScalar(0)); |
| 361 | const SkScalar r2 = SkIntToScalar(5); |
| 362 | const SkColor colors[2] = {SK_ColorWHITE, SK_ColorWHITE}; |
| 363 | const SkScalar pos[2] = {0, SK_Scalar1}; |
| 364 | SkShader* shader = SkGradientShader::CreateTwoPointConical( |
| 365 | pt1, r1, pt2, r2, colors, pos, 2, SkShader::kClamp_TileMode, NULL); |
| 366 | paint.setShader(shader)->unref(); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 367 | canvas->drawRect(fullRect, paint); |
| 368 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 369 | } |
| 370 | { |
| 371 | SkPaint paint; |
| 372 | paint.setStyle(SkPaint::kFill_Style); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 373 | SkBitmap bmp; |
| 374 | create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 375 | bmp.setAlphaType(kPremul_SkAlphaType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 376 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 377 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 378 | paint.setShader(shader)->unref(); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 379 | canvas->drawRect(fullRect, paint); |
| 380 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // Verify that incomplete coverage does not trigger a fresh frame |
| 384 | { |
| 385 | SkPaint paint; |
| 386 | paint.setStyle(SkPaint::kFill_Style); |
| 387 | paint.setAlpha(255); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 388 | canvas->drawRect(partialRect, paint); |
| 389 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Verify that incomplete coverage due to clipping does not trigger a fresh |
| 393 | // frame |
| 394 | { |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 395 | canvas->save(SkCanvas::kMatrixClip_SaveFlag); |
| 396 | canvas->clipRect(partialRect, SkRegion::kIntersect_Op, false); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 397 | SkPaint paint; |
| 398 | paint.setStyle(SkPaint::kFill_Style); |
| 399 | paint.setAlpha(255); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 400 | canvas->drawRect(fullRect, paint); |
| 401 | canvas->restore(); |
| 402 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 403 | } |
junov@chromium.org | 8f0ca06 | 2012-12-13 16:30:39 +0000 | [diff] [blame] | 404 | { |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 405 | canvas->save(SkCanvas::kMatrixClip_SaveFlag); |
junov@chromium.org | 8f0ca06 | 2012-12-13 16:30:39 +0000 | [diff] [blame] | 406 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 407 | paint.setStyle(SkPaint::kFill_Style); |
| 408 | paint.setAlpha(255); |
junov@chromium.org | 8f0ca06 | 2012-12-13 16:30:39 +0000 | [diff] [blame] | 409 | SkPath path; |
| 410 | path.addCircle(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(2)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 411 | canvas->clipPath(path, SkRegion::kIntersect_Op, false); |
| 412 | canvas->drawRect(fullRect, paint); |
| 413 | canvas->restore(); |
| 414 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f0ca06 | 2012-12-13 16:30:39 +0000 | [diff] [blame] | 415 | } |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 416 | |
| 417 | // Verify that stroked rect does not trigger a fresh frame |
| 418 | { |
| 419 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 420 | paint.setStyle(SkPaint::kStroke_Style); |
| 421 | paint.setAlpha(255); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 422 | canvas->drawRect(fullRect, paint); |
| 423 | REPORTER_ASSERT(reporter, !canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 424 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 425 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 426 | // Verify kSrcMode triggers a fresh frame even with transparent color |
| 427 | { |
| 428 | SkPaint paint; |
commit-bot@chromium.org | 3fbab82 | 2013-03-20 00:49:57 +0000 | [diff] [blame] | 429 | paint.setStyle(SkPaint::kFill_Style); |
| 430 | paint.setAlpha(100); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 431 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 432 | canvas->drawRect(fullRect, paint); |
| 433 | REPORTER_ASSERT(reporter, canvas->isFreshFrame()); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 437 | class MockDevice : public SkBitmapDevice { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 438 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 439 | MockDevice(const SkBitmap& bm) : SkBitmapDevice(bm) { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 440 | fDrawBitmapCallCount = 0; |
| 441 | } |
| 442 | virtual void drawBitmap(const SkDraw&, const SkBitmap&, |
commit-bot@chromium.org | 3e2ea25 | 2013-07-23 11:28:45 +0000 | [diff] [blame] | 443 | const SkMatrix&, const SkPaint&) SK_OVERRIDE { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 444 | fDrawBitmapCallCount++; |
| 445 | } |
| 446 | |
| 447 | int fDrawBitmapCallCount; |
| 448 | }; |
| 449 | |
| 450 | // Verifies that the deferred canvas triggers a flush when its memory |
| 451 | // limit is exceeded |
| 452 | static void TestDeferredCanvasMemoryLimit(skiatest::Reporter* reporter) { |
| 453 | SkBitmap store; |
| 454 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 455 | store.allocPixels(); |
| 456 | MockDevice mockDevice(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 457 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&mockDevice)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 458 | canvas->setMaxRecordingStorage(160000); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 459 | |
| 460 | SkBitmap sourceImage; |
| 461 | // 100 by 100 image, takes 40,000 bytes in memory |
| 462 | sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 463 | sourceImage.allocPixels(); |
| 464 | |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 465 | for (int i = 0; i < 5; i++) { |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 466 | sourceImage.notifyPixelsChanged(); // to force re-serialization |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 467 | canvas->drawBitmap(sourceImage, 0, 0, NULL); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 468 | } |
| 469 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 470 | REPORTER_ASSERT(reporter, mockDevice.fDrawBitmapCallCount == 4); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 471 | } |
| 472 | |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 473 | class NotificationCounter : public SkDeferredCanvas::NotificationClient { |
| 474 | public: |
| 475 | NotificationCounter() { |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 476 | fPrepareForDrawCount = fStorageAllocatedChangedCount = |
| 477 | fFlushedDrawCommandsCount = fSkippedPendingDrawCommandsCount = 0; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | virtual void prepareForDraw() SK_OVERRIDE { |
| 481 | fPrepareForDrawCount++; |
| 482 | } |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 483 | virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE { |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 484 | fStorageAllocatedChangedCount++; |
| 485 | } |
| 486 | virtual void flushedDrawCommands() SK_OVERRIDE { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 487 | fFlushedDrawCommandsCount++; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 488 | } |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 489 | virtual void skippedPendingDrawCommands() SK_OVERRIDE { |
| 490 | fSkippedPendingDrawCommandsCount++; |
| 491 | } |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 492 | |
| 493 | int fPrepareForDrawCount; |
| 494 | int fStorageAllocatedChangedCount; |
| 495 | int fFlushedDrawCommandsCount; |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 496 | int fSkippedPendingDrawCommandsCount; |
robertphillips@google.com | 5990397 | 2013-02-07 21:02:23 +0000 | [diff] [blame] | 497 | |
| 498 | private: |
| 499 | typedef SkDeferredCanvas::NotificationClient INHERITED; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 500 | }; |
| 501 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 502 | static void TestDeferredCanvasBitmapCaching(skiatest::Reporter* reporter) { |
| 503 | SkBitmap store; |
| 504 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 505 | store.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 506 | SkBitmapDevice device(store); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 507 | NotificationCounter notificationCounter; |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 508 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 509 | canvas->setNotificationClient(¬ificationCounter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 510 | |
| 511 | const int imageCount = 2; |
| 512 | SkBitmap sourceImages[imageCount]; |
| 513 | for (int i = 0; i < imageCount; i++) |
| 514 | { |
| 515 | sourceImages[i].setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 516 | sourceImages[i].allocPixels(); |
| 517 | } |
| 518 | |
| 519 | size_t bitmapSize = sourceImages[0].getSize(); |
| 520 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 521 | canvas->drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 522 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fStorageAllocatedChangedCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 523 | // stored bitmap + drawBitmap command |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 524 | REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > bitmapSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 525 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 526 | // verify that nothing can be freed at this point |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 527 | REPORTER_ASSERT(reporter, 0 == canvas->freeMemoryIfPossible(~0U)); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 528 | |
| 529 | // verify that flush leaves image in cache |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 530 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount); |
| 531 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fPrepareForDrawCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 532 | canvas->flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 533 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
| 534 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fPrepareForDrawCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 535 | REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() >= bitmapSize); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 536 | |
| 537 | // verify that after a flush, cached image can be freed |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 538 | REPORTER_ASSERT(reporter, canvas->freeMemoryIfPossible(~0U) >= bitmapSize); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 539 | |
| 540 | // Verify that caching works for avoiding multiple copies of the same bitmap |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 541 | canvas->drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 542 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 543 | canvas->drawBitmap(sourceImages[0], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 544 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fStorageAllocatedChangedCount); |
| 545 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 546 | REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < 2 * bitmapSize); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 547 | |
| 548 | // Verify partial eviction based on bytesToFree |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 549 | canvas->drawBitmap(sourceImages[1], 0, 0, NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 550 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 551 | canvas->flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 552 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 553 | REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2 * bitmapSize); |
| 554 | size_t bytesFreed = canvas->freeMemoryIfPossible(1); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 555 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 556 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 557 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 558 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 559 | // 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] | 560 | // a pending draw, while image 1 is locked-in. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 561 | canvas->freeMemoryIfPossible(~0U); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 562 | REPORTER_ASSERT(reporter, 2 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 563 | canvas->drawBitmap(sourceImages[0], 0, 0, NULL); |
| 564 | canvas->flush(); |
| 565 | canvas->drawBitmap(sourceImages[1], 0, 0, NULL); |
| 566 | bytesFreed = canvas->freeMemoryIfPossible(~0U); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 567 | // only one bitmap should have been freed. |
| 568 | REPORTER_ASSERT(reporter, bytesFreed >= bitmapSize); |
| 569 | REPORTER_ASSERT(reporter, bytesFreed < 2*bitmapSize); |
| 570 | // Clear for next test |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 571 | canvas->flush(); |
| 572 | canvas->freeMemoryIfPossible(~0U); |
| 573 | REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() < bitmapSize); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 574 | |
| 575 | // Verify the image cache is sensitive to genID bumps |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 576 | canvas->drawBitmap(sourceImages[1], 0, 0, NULL); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 577 | sourceImages[1].notifyPixelsChanged(); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 578 | canvas->drawBitmap(sourceImages[1], 0, 0, NULL); |
| 579 | REPORTER_ASSERT(reporter, canvas->storageAllocatedForRecording() > 2*bitmapSize); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 580 | |
| 581 | // Verify that nothing in this test caused commands to be skipped |
| 582 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 583 | } |
| 584 | |
| 585 | static void TestDeferredCanvasSkip(skiatest::Reporter* reporter) { |
| 586 | SkBitmap store; |
| 587 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 588 | store.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 589 | SkBitmapDevice device(store); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 590 | NotificationCounter notificationCounter; |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 591 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 592 | canvas->setNotificationClient(¬ificationCounter); |
| 593 | canvas->clear(0x0); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 594 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 595 | REPORTER_ASSERT(reporter, 0 == notificationCounter.fFlushedDrawCommandsCount); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 596 | canvas->flush(); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 597 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fSkippedPendingDrawCommandsCount); |
| 598 | REPORTER_ASSERT(reporter, 1 == notificationCounter.fFlushedDrawCommandsCount); |
| 599 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 600 | } |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 601 | |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 602 | static void TestDeferredCanvasBitmapShaderNoLeak(skiatest::Reporter* reporter) { |
| 603 | // This is a regression test for crbug.com/155875 |
| 604 | // This test covers a code path that inserts bitmaps into the bitmap heap through the |
| 605 | // flattening of SkBitmapProcShaders. The refcount in the bitmap heap is maintained through |
| 606 | // the flattening and unflattening of the shader. |
| 607 | SkBitmap store; |
| 608 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 609 | store.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 610 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 611 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 612 | // test will fail if nbIterations is not in sync with |
| 613 | // BITMAPS_TO_KEEP in SkGPipeWrite.cpp |
| 614 | const int nbIterations = 5; |
| 615 | size_t bytesAllocated = 0; |
| 616 | for(int pass = 0; pass < 2; ++pass) { |
| 617 | for(int i = 0; i < nbIterations; ++i) { |
| 618 | SkPaint paint; |
| 619 | SkBitmap paintPattern; |
| 620 | paintPattern.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); |
| 621 | paintPattern.allocPixels(); |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 622 | paint.setShader(SkNEW_ARGS(SkBitmapProcShader, |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 623 | (paintPattern, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)))->unref(); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 624 | canvas->drawPaint(paint); |
| 625 | canvas->flush(); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 626 | |
| 627 | // In the first pass, memory allocation should be monotonically increasing as |
| 628 | // the bitmap heap slots fill up. In the second pass memory allocation should be |
| 629 | // stable as bitmap heap slots get recycled. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 630 | size_t newBytesAllocated = canvas->storageAllocatedForRecording(); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 631 | if (pass == 0) { |
| 632 | REPORTER_ASSERT(reporter, newBytesAllocated > bytesAllocated); |
| 633 | bytesAllocated = newBytesAllocated; |
| 634 | } else { |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 635 | REPORTER_ASSERT(reporter, newBytesAllocated == bytesAllocated); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | } |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 639 | // All cached resources should be evictable since last canvas call was flush() |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 640 | canvas->freeMemoryIfPossible(~0U); |
| 641 | REPORTER_ASSERT(reporter, 0 == canvas->storageAllocatedForRecording()); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 642 | } |
| 643 | |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 644 | static void TestDeferredCanvasBitmapSizeThreshold(skiatest::Reporter* reporter) { |
| 645 | SkBitmap store; |
| 646 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 647 | store.allocPixels(); |
skia.committer@gmail.com | 1c9c0d3 | 2012-11-22 02:02:41 +0000 | [diff] [blame] | 648 | |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 649 | SkBitmap sourceImage; |
| 650 | // 100 by 100 image, takes 40,000 bytes in memory |
| 651 | sourceImage.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 652 | sourceImage.allocPixels(); |
| 653 | |
| 654 | // 1 under : should not store the image |
| 655 | { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 656 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 657 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 658 | canvas->setBitmapSizeThreshold(39999); |
| 659 | canvas->drawBitmap(sourceImage, 0, 0, NULL); |
| 660 | size_t newBytesAllocated = canvas->storageAllocatedForRecording(); |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 661 | REPORTER_ASSERT(reporter, newBytesAllocated == 0); |
| 662 | } |
| 663 | |
| 664 | // exact value : should store the image |
| 665 | { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 666 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 667 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 668 | canvas->setBitmapSizeThreshold(40000); |
| 669 | canvas->drawBitmap(sourceImage, 0, 0, NULL); |
| 670 | size_t newBytesAllocated = canvas->storageAllocatedForRecording(); |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 671 | REPORTER_ASSERT(reporter, newBytesAllocated > 0); |
| 672 | } |
| 673 | |
| 674 | // 1 over : should still store the image |
| 675 | { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 676 | SkBitmapDevice device(store); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 677 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 678 | canvas->setBitmapSizeThreshold(40001); |
| 679 | canvas->drawBitmap(sourceImage, 0, 0, NULL); |
| 680 | size_t newBytesAllocated = canvas->storageAllocatedForRecording(); |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 681 | REPORTER_ASSERT(reporter, newBytesAllocated > 0); |
| 682 | } |
| 683 | } |
| 684 | |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 685 | |
| 686 | typedef void* PixelPtr; |
| 687 | // Returns an opaque pointer which, either points to a GrTexture or RAM pixel |
| 688 | // buffer. Used to test pointer equality do determine whether a surface points |
| 689 | // to the same pixel data storage as before. |
junov@chromium.org | 3c5ec8d | 2013-04-12 13:34:47 +0000 | [diff] [blame] | 690 | static PixelPtr getSurfacePixelPtr(SkSurface* surface, bool useGpu) { |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 691 | return useGpu ? surface->getCanvas()->getDevice()->accessBitmap(false).getTexture() : |
| 692 | surface->getCanvas()->getDevice()->accessBitmap(false).getPixels(); |
| 693 | } |
| 694 | |
| 695 | static void TestDeferredCanvasSurface(skiatest::Reporter* reporter, GrContextFactory* factory) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 696 | SkImageInfo imageSpec = { |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 697 | 10, // width |
| 698 | 10, // height |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 699 | kPMColor_SkColorType, |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 700 | kPremul_SkAlphaType |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 701 | }; |
| 702 | SkSurface* surface; |
| 703 | bool useGpu = NULL != factory; |
| 704 | #if SK_SUPPORT_GPU |
| 705 | if (useGpu) { |
| 706 | GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
robertphillips@google.com | 5fa42d1 | 2013-11-12 17:33:02 +0000 | [diff] [blame] | 707 | if (NULL == context) { |
| 708 | return; |
| 709 | } |
| 710 | |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 711 | surface = SkSurface::NewRenderTarget(context, imageSpec); |
| 712 | } else { |
| 713 | surface = SkSurface::NewRaster(imageSpec); |
| 714 | } |
| 715 | #else |
| 716 | SkASSERT(!useGpu); |
| 717 | surface = SkSurface::NewRaster(imageSpec); |
| 718 | #endif |
| 719 | SkASSERT(NULL != surface); |
| 720 | SkAutoTUnref<SkSurface> aur(surface); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 721 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface)); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 722 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 723 | SkImage* image1 = canvas->newImageSnapshot(); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 724 | SkAutoTUnref<SkImage> aur_i1(image1); |
| 725 | PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu); |
| 726 | // The following clear would normally trigger a copy on write, but |
| 727 | // it won't because rendering is deferred. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 728 | canvas->clear(SK_ColorBLACK); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 729 | // Obtaining a snapshot directly from the surface (as opposed to the |
| 730 | // SkDeferredCanvas) will not trigger a flush of deferred draw operations |
| 731 | // and will therefore return the same image as the previous snapshot. |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 732 | SkImage* image2 = surface->newImageSnapshot(); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 733 | SkAutoTUnref<SkImage> aur_i2(image2); |
| 734 | // Images identical because of deferral |
| 735 | REPORTER_ASSERT(reporter, image1->uniqueID() == image2->uniqueID()); |
| 736 | // Now we obtain a snpshot via the deferred canvas, which triggers a flush. |
| 737 | // Because there is a pending clear, this will generate a different image. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 738 | SkImage* image3 = canvas->newImageSnapshot(); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 739 | SkAutoTUnref<SkImage> aur_i3(image3); |
| 740 | REPORTER_ASSERT(reporter, image1->uniqueID() != image3->uniqueID()); |
| 741 | // Verify that backing store is now a different buffer because of copy on |
| 742 | // write |
| 743 | PixelPtr pixels2 = getSurfacePixelPtr(surface, useGpu); |
| 744 | REPORTER_ASSERT(reporter, pixels1 != pixels2); |
junov@chromium.org | 9becf00 | 2013-04-15 18:15:23 +0000 | [diff] [blame] | 745 | // Verify copy-on write with a draw operation that gets deferred by |
| 746 | // the in order draw buffer. |
| 747 | SkPaint paint; |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 748 | canvas->drawPaint(paint); |
| 749 | SkImage* image4 = canvas->newImageSnapshot(); // implicit flush |
junov@chromium.org | 9becf00 | 2013-04-15 18:15:23 +0000 | [diff] [blame] | 750 | SkAutoTUnref<SkImage> aur_i4(image4); |
| 751 | REPORTER_ASSERT(reporter, image4->uniqueID() != image3->uniqueID()); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 752 | PixelPtr pixels3 = getSurfacePixelPtr(surface, useGpu); |
junov@chromium.org | 9becf00 | 2013-04-15 18:15:23 +0000 | [diff] [blame] | 753 | REPORTER_ASSERT(reporter, pixels2 != pixels3); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 754 | // Verify that a direct canvas flush with a pending draw does not trigger |
| 755 | // a copy on write when the surface is not sharing its buffer with an |
| 756 | // SkImage. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 757 | canvas->clear(SK_ColorWHITE); |
| 758 | canvas->flush(); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 759 | PixelPtr pixels4 = getSurfacePixelPtr(surface, useGpu); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 760 | canvas->drawPaint(paint); |
| 761 | canvas->flush(); |
junov@chromium.org | 9becf00 | 2013-04-15 18:15:23 +0000 | [diff] [blame] | 762 | PixelPtr pixels5 = getSurfacePixelPtr(surface, useGpu); |
| 763 | REPORTER_ASSERT(reporter, pixels4 == pixels5); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 764 | } |
| 765 | |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 766 | static void TestDeferredCanvasSetSurface(skiatest::Reporter* reporter, GrContextFactory* factory) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 767 | SkImageInfo imageSpec = { |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 768 | 10, // width |
| 769 | 10, // height |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 770 | kPMColor_SkColorType, |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 771 | kPremul_SkAlphaType |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 772 | }; |
| 773 | SkSurface* surface; |
| 774 | SkSurface* alternateSurface; |
| 775 | bool useGpu = NULL != factory; |
| 776 | #if SK_SUPPORT_GPU |
| 777 | if (useGpu) { |
| 778 | GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
robertphillips@google.com | e293005 | 2013-11-13 14:26:52 +0000 | [diff] [blame] | 779 | if (NULL == context) { |
| 780 | return; |
| 781 | } |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 782 | surface = SkSurface::NewRenderTarget(context, imageSpec); |
| 783 | alternateSurface = SkSurface::NewRenderTarget(context, imageSpec); |
| 784 | } else { |
| 785 | surface = SkSurface::NewRaster(imageSpec); |
| 786 | alternateSurface = SkSurface::NewRaster(imageSpec); |
| 787 | } |
| 788 | #else |
| 789 | SkASSERT(!useGpu); |
| 790 | surface = SkSurface::NewRaster(imageSpec); |
| 791 | alternateSurface = SkSurface::NewRaster(imageSpec); |
| 792 | #endif |
| 793 | SkASSERT(NULL != surface); |
| 794 | SkASSERT(NULL != alternateSurface); |
| 795 | SkAutoTUnref<SkSurface> aur1(surface); |
| 796 | SkAutoTUnref<SkSurface> aur2(alternateSurface); |
| 797 | PixelPtr pixels1 = getSurfacePixelPtr(surface, useGpu); |
| 798 | PixelPtr pixels2 = getSurfacePixelPtr(alternateSurface, useGpu); |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 799 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 800 | SkAutoTUnref<SkImage> image1(canvas->newImageSnapshot()); |
| 801 | canvas->setSurface(alternateSurface); |
| 802 | SkAutoTUnref<SkImage> image2(canvas->newImageSnapshot()); |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 803 | REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID()); |
| 804 | // Verify that none of the above operations triggered a surface copy on write. |
| 805 | REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1); |
| 806 | REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) == pixels2); |
| 807 | // Verify that a flushed draw command will trigger a copy on write on alternateSurface. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 808 | canvas->clear(SK_ColorWHITE); |
| 809 | canvas->flush(); |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 810 | REPORTER_ASSERT(reporter, getSurfacePixelPtr(surface, useGpu) == pixels1); |
| 811 | REPORTER_ASSERT(reporter, getSurfacePixelPtr(alternateSurface, useGpu) != pixels2); |
| 812 | } |
| 813 | |
junov@chromium.org | b1c725a | 2013-05-21 20:16:17 +0000 | [diff] [blame] | 814 | static void TestDeferredCanvasCreateCompatibleDevice(skiatest::Reporter* reporter) { |
| 815 | SkBitmap store; |
| 816 | store.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 817 | store.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 818 | SkBitmapDevice device(store); |
junov@chromium.org | b1c725a | 2013-05-21 20:16:17 +0000 | [diff] [blame] | 819 | NotificationCounter notificationCounter; |
commit-bot@chromium.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 820 | SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(&device)); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 821 | canvas->setNotificationClient(¬ificationCounter); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 822 | SkAutoTUnref<SkBaseDevice> secondaryDevice(canvas->createCompatibleDevice( |
junov@chromium.org | b1c725a | 2013-05-21 20:16:17 +0000 | [diff] [blame] | 823 | SkBitmap::kARGB_8888_Config, 10, 10, device.isOpaque())); |
| 824 | SkCanvas secondaryCanvas(secondaryDevice.get()); |
| 825 | SkRect rect = SkRect::MakeWH(5, 5); |
| 826 | SkPaint paint; |
| 827 | // After spawning a compatible canvas: |
| 828 | // 1) Verify that secondary canvas is usable and does not report to the notification client. |
| 829 | secondaryCanvas.drawRect(rect, paint); |
| 830 | REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 0); |
| 831 | // 2) Verify that original canvas is usable and still reports to the notification client. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 832 | canvas->drawRect(rect, paint); |
junov@chromium.org | b1c725a | 2013-05-21 20:16:17 +0000 | [diff] [blame] | 833 | REPORTER_ASSERT(reporter, notificationCounter.fStorageAllocatedChangedCount == 1); |
| 834 | } |
| 835 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 836 | DEF_GPUTEST(DeferredCanvas, reporter, factory) { |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 837 | TestDeferredCanvasBitmapAccess(reporter); |
| 838 | TestDeferredCanvasFlush(reporter); |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 839 | TestDeferredCanvasFreshFrame(reporter); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 840 | TestDeferredCanvasMemoryLimit(reporter); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 841 | TestDeferredCanvasBitmapCaching(reporter); |
junov@google.com | 52a00ca | 2012-10-01 15:27:14 +0000 | [diff] [blame] | 842 | TestDeferredCanvasSkip(reporter); |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 843 | TestDeferredCanvasBitmapShaderNoLeak(reporter); |
sugoi@google.com | 7775fd5 | 2012-11-21 15:47:04 +0000 | [diff] [blame] | 844 | TestDeferredCanvasBitmapSizeThreshold(reporter); |
junov@chromium.org | b1c725a | 2013-05-21 20:16:17 +0000 | [diff] [blame] | 845 | TestDeferredCanvasCreateCompatibleDevice(reporter); |
junov@chromium.org | 44324fa | 2013-08-02 15:36:02 +0000 | [diff] [blame] | 846 | TestDeferredCanvasWritePixelsToSurface(reporter); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 847 | TestDeferredCanvasSurface(reporter, NULL); |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 848 | TestDeferredCanvasSetSurface(reporter, NULL); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 849 | if (NULL != factory) { |
| 850 | TestDeferredCanvasSurface(reporter, factory); |
junov@chromium.org | 7070f76 | 2013-05-24 17:13:00 +0000 | [diff] [blame] | 851 | TestDeferredCanvasSetSurface(reporter, factory); |
junov@chromium.org | 67d7422 | 2013-04-12 13:33:01 +0000 | [diff] [blame] | 852 | } |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 853 | } |