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