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