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