| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
| 9 | #include "SkRRect.h" |
| 10 | #include "SkSurface.h" |
| 11 | #include "Test.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | #include "GrContextFactory.h" |
| 15 | #else |
| 16 | class GrContextFactory; |
| 17 | class GrContext; |
| 18 | #endif |
| 19 | |
| 20 | enum SurfaceType { |
| 21 | kRaster_SurfaceType, |
| 22 | kGpu_SurfaceType, |
| 23 | kPicture_SurfaceType |
| 24 | }; |
| 25 | |
| 26 | static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) { |
| reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 27 | static const SkImageInfo imageSpec = { |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 28 | 10, // width |
| 29 | 10, // height |
| reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 30 | kPMColor_SkColorType, |
| reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 31 | kPremul_SkAlphaType |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | switch (surfaceType) { |
| 35 | case kRaster_SurfaceType: |
| 36 | return SkSurface::NewRaster(imageSpec); |
| 37 | case kGpu_SurfaceType: |
| junov@chromium.org | 8bc9edc | 2013-04-03 15:25:46 +0000 | [diff] [blame] | 38 | #if SK_SUPPORT_GPU |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 39 | SkASSERT(NULL != context); |
| 40 | return SkSurface::NewRenderTarget(context, imageSpec); |
| junov@chromium.org | 8bc9edc | 2013-04-03 15:25:46 +0000 | [diff] [blame] | 41 | #else |
| 42 | SkASSERT(0); |
| 43 | #endif |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 44 | case kPicture_SurfaceType: |
| 45 | return SkSurface::NewPicture(10, 10); |
| 46 | } |
| 47 | SkASSERT(0); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame^] | 51 | #include "SkData.h" |
| 52 | |
| 53 | static void test_image(skiatest::Reporter* reporter) { |
| 54 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 55 | size_t rowBytes = info.minRowBytes(); |
| 56 | size_t size = info.getSafeSize(rowBytes); |
| 57 | void* addr = sk_malloc_throw(size); |
| 58 | SkData* data = SkData::NewFromMalloc(addr, size); |
| 59 | |
| 60 | REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); |
| 61 | SkImage* image = SkImage::NewRasterData(info, data, rowBytes); |
| 62 | REPORTER_ASSERT(reporter, 2 == data->getRefCnt()); |
| 63 | image->unref(); |
| 64 | REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); |
| 65 | data->unref(); |
| 66 | } |
| 67 | |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 68 | static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType, |
| 69 | GrContext* context) { |
| 70 | // Verify that the right canvas commands trigger a copy on write |
| 71 | SkSurface* surface = createSurface(surfaceType, context); |
| 72 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 73 | SkCanvas* canvas = surface->getCanvas(); |
| 74 | |
| 75 | const SkRect testRect = |
| 76 | SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 77 | SkIntToScalar(4), SkIntToScalar(5)); |
| 78 | SkMatrix testMatrix; |
| 79 | testMatrix.reset(); |
| 80 | testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 81 | |
| 82 | SkPath testPath; |
| 83 | testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 84 | SkIntToScalar(2), SkIntToScalar(1))); |
| 85 | |
| 86 | const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 87 | |
| 88 | SkRegion testRegion; |
| 89 | testRegion.setRect(testIRect); |
| 90 | |
| 91 | |
| 92 | const SkColor testColor = 0x01020304; |
| 93 | const SkPaint testPaint; |
| 94 | const SkPoint testPoints[3] = { |
| 95 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 96 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 97 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 98 | }; |
| 99 | const size_t testPointCount = 3; |
| 100 | |
| 101 | SkBitmap testBitmap; |
| 102 | testBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); |
| 103 | testBitmap.allocPixels(); |
| robertphillips@google.com | d1ce77d | 2013-10-09 12:51:09 +0000 | [diff] [blame] | 104 | testBitmap.eraseColor(0); |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 105 | |
| 106 | SkRRect testRRect; |
| 107 | testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); |
| 108 | |
| 109 | SkString testText("Hello World"); |
| 110 | const SkPoint testPoints2[] = { |
| 111 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 112 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 113 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 114 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 115 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 116 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 117 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 118 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 119 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 120 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 121 | { SkIntToScalar(10), SkIntToScalar(1) }, |
| 122 | }; |
| 123 | |
| 124 | #define EXPECT_COPY_ON_WRITE(command) \ |
| 125 | { \ |
| junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 126 | SkImage* imageBefore = surface->newImageSnapshot(); \ |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 127 | SkAutoTUnref<SkImage> aur_before(imageBefore); \ |
| 128 | canvas-> command ; \ |
| junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 129 | SkImage* imageAfter = surface->newImageSnapshot(); \ |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 130 | SkAutoTUnref<SkImage> aur_after(imageAfter); \ |
| 131 | REPORTER_ASSERT(reporter, imageBefore != imageAfter); \ |
| 132 | } |
| 133 | |
| 134 | EXPECT_COPY_ON_WRITE(clear(testColor)) |
| 135 | EXPECT_COPY_ON_WRITE(drawPaint(testPaint)) |
| 136 | EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ |
| 137 | testPaint)) |
| 138 | EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint)) |
| 139 | EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint)) |
| 140 | EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint)) |
| 141 | EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
| 142 | EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
| 143 | EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect)) |
| 144 | EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL)) |
| 145 | EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL)) |
| 146 | EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL)) |
| 147 | EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint)) |
| 148 | EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \ |
| 149 | testPaint)) |
| 150 | EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \ |
| 151 | testPaint)) |
| 152 | } |
| 153 | |
| junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 154 | static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter, |
| 155 | SurfaceType surfaceType, |
| 156 | GrContext* context) { |
| 157 | // This test succeeds by not triggering an assertion. |
| 158 | // The test verifies that the surface remains writable (usable) after |
| 159 | // acquiring and releasing a snapshot without triggering a copy on write. |
| commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 160 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
| junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 161 | SkCanvas* canvas = surface->getCanvas(); |
| 162 | canvas->clear(1); |
| junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 163 | surface->newImageSnapshot()->unref(); // Create and destroy SkImage |
| commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 164 | canvas->clear(2); // Must not assert internally |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 165 | } |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 166 | |
| junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 167 | #if SK_SUPPORT_GPU |
| commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 168 | static void Test_crbug263329(skiatest::Reporter* reporter, |
| 169 | GrContext* context) { |
| 170 | // This is a regression test for crbug.com/263329 |
| 171 | // Bug was caused by onCopyOnWrite releasing the old surface texture |
| 172 | // back to the scratch texture pool even though the texture is used |
| 173 | // by and active SkImage_Gpu. |
| 174 | SkAutoTUnref<SkSurface> surface1(createSurface(kGpu_SurfaceType, context)); |
| 175 | SkAutoTUnref<SkSurface> surface2(createSurface(kGpu_SurfaceType, context)); |
| 176 | SkCanvas* canvas1 = surface1->getCanvas(); |
| 177 | SkCanvas* canvas2 = surface2->getCanvas(); |
| 178 | canvas1->clear(1); |
| 179 | SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); |
| 180 | // Trigger copy on write, new backing is a scratch texture |
| 181 | canvas1->clear(2); |
| 182 | SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); |
| 183 | // Trigger copy on write, old backing should not be returned to scratch |
| 184 | // pool because it is held by image2 |
| 185 | canvas1->clear(3); |
| 186 | |
| 187 | canvas2->clear(4); |
| 188 | SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); |
| 189 | // Trigger copy on write on surface2. The new backing store should not |
| 190 | // be recycling a texture that is held by an existing image. |
| 191 | canvas2->clear(5); |
| 192 | SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); |
| 193 | REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture()); |
| 194 | // The following assertion checks crbug.com/263329 |
| 195 | REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture()); |
| 196 | REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture()); |
| 197 | REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture()); |
| 198 | REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture()); |
| 199 | REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture()); |
| 200 | } |
| 201 | |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 202 | static void TestGetTexture(skiatest::Reporter* reporter, |
| 203 | SurfaceType surfaceType, |
| 204 | GrContext* context) { |
| 205 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
| 206 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 207 | GrTexture* texture = image->getTexture(); |
| 208 | if (surfaceType == kGpu_SurfaceType) { |
| 209 | REPORTER_ASSERT(reporter, NULL != texture); |
| 210 | REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); |
| 211 | } else { |
| 212 | REPORTER_ASSERT(reporter, NULL == texture); |
| 213 | } |
| 214 | surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
| 215 | REPORTER_ASSERT(reporter, image->getTexture() == texture); |
| 216 | } |
| junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 217 | #endif |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 218 | |
| junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 219 | static void TestSurfaceNoCanvas(skiatest::Reporter* reporter, |
| 220 | SurfaceType surfaceType, |
| commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 221 | GrContext* context, |
| 222 | SkSurface::ContentChangeMode mode) { |
| junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 223 | // Verifies the robustness of SkSurface for handling use cases where calls |
| 224 | // are made before a canvas is created. |
| 225 | { |
| 226 | // Test passes by not asserting |
| 227 | SkSurface* surface = createSurface(surfaceType, context); |
| 228 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 229 | surface->notifyContentWillChange(mode); |
| robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 230 | SkDEBUGCODE(surface->validate();) |
| junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 231 | } |
| 232 | { |
| 233 | SkSurface* surface = createSurface(surfaceType, context); |
| 234 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 235 | SkImage* image1 = surface->newImageSnapshot(); |
| 236 | SkAutoTUnref<SkImage> aur_image1(image1); |
| robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 237 | SkDEBUGCODE(image1->validate();) |
| 238 | SkDEBUGCODE(surface->validate();) |
| commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 239 | surface->notifyContentWillChange(mode); |
| robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 240 | SkDEBUGCODE(image1->validate();) |
| 241 | SkDEBUGCODE(surface->validate();) |
| junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 242 | SkImage* image2 = surface->newImageSnapshot(); |
| 243 | SkAutoTUnref<SkImage> aur_image2(image2); |
| robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 244 | SkDEBUGCODE(image2->validate();) |
| 245 | SkDEBUGCODE(surface->validate();) |
| junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 246 | REPORTER_ASSERT(reporter, image1 != image2); |
| 247 | } |
| skia.committer@gmail.com | 45fb8b6 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 248 | |
| junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 249 | } |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 250 | |
| tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 251 | DEF_GPUTEST(Surface, reporter, factory) { |
| reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame^] | 252 | test_image(reporter); |
| 253 | |
| junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 254 | TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); |
| 255 | TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL); |
| 256 | TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL); |
| 257 | TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL); |
| commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 258 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode); |
| 259 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode); |
| junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 260 | #if SK_SUPPORT_GPU |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 261 | TestGetTexture(reporter, kRaster_SurfaceType, NULL); |
| 262 | TestGetTexture(reporter, kPicture_SurfaceType, NULL); |
| junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 263 | if (NULL != factory) { |
| 264 | GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
| robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 265 | if (NULL != context) { |
| 266 | Test_crbug263329(reporter, context); |
| 267 | TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); |
| 268 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); |
| 269 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
| 270 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
| 271 | TestGetTexture(reporter, kGpu_SurfaceType, context); |
| 272 | } |
| junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 273 | } |
| junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 274 | #endif |
| 275 | } |