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