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