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