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" |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
| 10 | #include "SkImageEncoder.h" |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 11 | #include "SkRRect.h" |
| 12 | #include "SkSurface.h" |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 13 | #include "SkUtils.h" |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 14 | #include "Test.h" |
| 15 | |
| 16 | #if SK_SUPPORT_GPU |
| 17 | #include "GrContextFactory.h" |
| 18 | #else |
| 19 | class GrContextFactory; |
| 20 | class GrContext; |
| 21 | #endif |
| 22 | |
| 23 | enum SurfaceType { |
| 24 | kRaster_SurfaceType, |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 25 | kRasterDirect_SurfaceType, |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 26 | kGpu_SurfaceType, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 27 | kGpuScratch_SurfaceType, |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 30 | static const int gSurfaceSize = 10; |
| 31 | static SkPMColor gSurfaceStorage[gSurfaceSize * gSurfaceSize]; |
| 32 | |
| 33 | static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context, |
| 34 | SkImageInfo* requestedInfo = NULL) { |
| 35 | static const SkImageInfo info = SkImageInfo::MakeN32Premul(gSurfaceSize, |
| 36 | gSurfaceSize); |
| 37 | |
| 38 | if (requestedInfo) { |
| 39 | *requestedInfo = info; |
| 40 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 41 | |
| 42 | switch (surfaceType) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 43 | case kRaster_SurfaceType: |
| 44 | return SkSurface::NewRaster(info); |
| 45 | case kRasterDirect_SurfaceType: |
| 46 | return SkSurface::NewRasterDirect(info, gSurfaceStorage, |
| 47 | info.minRowBytes()); |
| 48 | case kGpu_SurfaceType: |
junov@chromium.org | 8bc9edc | 2013-04-03 15:25:46 +0000 | [diff] [blame] | 49 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 50 | return context ? SkSurface::NewRenderTarget(context, info) : NULL; |
junov@chromium.org | 8bc9edc | 2013-04-03 15:25:46 +0000 | [diff] [blame] | 51 | #endif |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 52 | break; |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 53 | case kGpuScratch_SurfaceType: |
| 54 | #if SK_SUPPORT_GPU |
| 55 | return context ? SkSurface::NewScratchRenderTarget(context, info) : NULL; |
| 56 | #endif |
| 57 | break; |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 58 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
| 61 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 62 | enum ImageType { |
| 63 | kRasterCopy_ImageType, |
| 64 | kRasterData_ImageType, |
| 65 | kGpu_ImageType, |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 66 | kCodec_ImageType, |
| 67 | }; |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 68 | |
| 69 | static void test_image(skiatest::Reporter* reporter) { |
| 70 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 71 | size_t rowBytes = info.minRowBytes(); |
| 72 | size_t size = info.getSafeSize(rowBytes); |
| 73 | void* addr = sk_malloc_throw(size); |
| 74 | SkData* data = SkData::NewFromMalloc(addr, size); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 75 | |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); |
| 77 | SkImage* image = SkImage::NewRasterData(info, data, rowBytes); |
| 78 | REPORTER_ASSERT(reporter, 2 == data->getRefCnt()); |
| 79 | image->unref(); |
| 80 | REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); |
| 81 | data->unref(); |
| 82 | } |
| 83 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 84 | static SkImage* createImage(ImageType imageType, GrContext* context, |
| 85 | SkColor color) { |
| 86 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 87 | const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 88 | const size_t rowBytes = info.minRowBytes(); |
| 89 | const size_t size = rowBytes * info.fHeight; |
| 90 | |
| 91 | void* addr = sk_malloc_throw(size); |
| 92 | sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); |
| 93 | SkAutoTUnref<SkData> data(SkData::NewFromMalloc(addr, size)); |
| 94 | |
| 95 | switch (imageType) { |
| 96 | case kRasterCopy_ImageType: |
| 97 | return SkImage::NewRasterCopy(info, addr, rowBytes); |
| 98 | case kRasterData_ImageType: |
| 99 | return SkImage::NewRasterData(info, data, rowBytes); |
| 100 | case kGpu_ImageType: |
| 101 | return NULL; // TODO |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 102 | case kCodec_ImageType: { |
| 103 | SkBitmap bitmap; |
| 104 | bitmap.installPixels(info, addr, rowBytes, NULL, NULL); |
| 105 | SkAutoTUnref<SkData> src( |
| 106 | SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, |
| 107 | 100)); |
| 108 | return SkImage::NewEncodedData(src); |
| 109 | } |
| 110 | } |
| 111 | SkASSERT(false); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | static void test_imagepeek(skiatest::Reporter* reporter) { |
| 116 | static const struct { |
| 117 | ImageType fType; |
| 118 | bool fPeekShouldSucceed; |
| 119 | } gRec[] = { |
| 120 | { kRasterCopy_ImageType, true }, |
| 121 | { kRasterData_ImageType, true }, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 122 | { kGpu_ImageType, false }, |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 123 | { kCodec_ImageType, false }, |
| 124 | }; |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 125 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 126 | const SkColor color = SK_ColorRED; |
| 127 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 128 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 129 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 130 | SkImageInfo info; |
| 131 | size_t rowBytes; |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 132 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 133 | SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color)); |
| 134 | if (!image.get()) { |
| 135 | continue; // gpu may not be enabled |
| 136 | } |
| 137 | const void* addr = image->peekPixels(&info, &rowBytes); |
| 138 | bool success = (NULL != addr); |
| 139 | REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
| 140 | if (success) { |
| 141 | REPORTER_ASSERT(reporter, 10 == info.fWidth); |
| 142 | REPORTER_ASSERT(reporter, 10 == info.fHeight); |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, kN32_SkColorType == info.fColorType); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 144 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.fAlphaType || |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 145 | kOpaque_SkAlphaType == info.fAlphaType); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 146 | REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); |
| 147 | REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 152 | static void test_canvaspeek(skiatest::Reporter* reporter, |
| 153 | GrContextFactory* factory) { |
| 154 | static const struct { |
| 155 | SurfaceType fType; |
| 156 | bool fPeekShouldSucceed; |
| 157 | } gRec[] = { |
| 158 | { kRaster_SurfaceType, true }, |
| 159 | { kRasterDirect_SurfaceType, true }, |
| 160 | #if SK_SUPPORT_GPU |
| 161 | { kGpu_SurfaceType, false }, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 162 | { kGpuScratch_SurfaceType, false }, |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 163 | #endif |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | const SkColor color = SK_ColorRED; |
| 167 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 168 | |
| 169 | GrContext* context = NULL; |
| 170 | #if SK_SUPPORT_GPU |
| 171 | context = factory->get(GrContextFactory::kNative_GLContextType); |
| 172 | #endif |
| 173 | |
| 174 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 175 | SkImageInfo info, requestInfo; |
| 176 | size_t rowBytes; |
| 177 | |
| 178 | SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context, |
| 179 | &requestInfo)); |
| 180 | surface->getCanvas()->clear(color); |
| 181 | |
| 182 | const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes); |
| 183 | bool success = (NULL != addr); |
| 184 | REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
| 185 | |
| 186 | SkImageInfo info2; |
| 187 | size_t rb2; |
| 188 | const void* addr2 = surface->peekPixels(&info2, &rb2); |
| 189 | |
| 190 | if (success) { |
| 191 | REPORTER_ASSERT(reporter, requestInfo == info); |
| 192 | REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes); |
| 193 | REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
| 194 | |
| 195 | REPORTER_ASSERT(reporter, addr2 == addr); |
| 196 | REPORTER_ASSERT(reporter, info2 == info); |
| 197 | REPORTER_ASSERT(reporter, rb2 == rowBytes); |
| 198 | } else { |
| 199 | REPORTER_ASSERT(reporter, NULL == addr2); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 204 | static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType, |
| 205 | GrContext* context) { |
| 206 | // Verify that the right canvas commands trigger a copy on write |
| 207 | SkSurface* surface = createSurface(surfaceType, context); |
| 208 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 209 | SkCanvas* canvas = surface->getCanvas(); |
| 210 | |
| 211 | const SkRect testRect = |
| 212 | SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 213 | SkIntToScalar(4), SkIntToScalar(5)); |
| 214 | SkMatrix testMatrix; |
| 215 | testMatrix.reset(); |
| 216 | testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 217 | |
| 218 | SkPath testPath; |
| 219 | testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 220 | SkIntToScalar(2), SkIntToScalar(1))); |
| 221 | |
| 222 | const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 223 | |
| 224 | SkRegion testRegion; |
| 225 | testRegion.setRect(testIRect); |
| 226 | |
| 227 | |
| 228 | const SkColor testColor = 0x01020304; |
| 229 | const SkPaint testPaint; |
| 230 | const SkPoint testPoints[3] = { |
| 231 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 232 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 233 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 234 | }; |
| 235 | const size_t testPointCount = 3; |
| 236 | |
| 237 | SkBitmap testBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 238 | testBitmap.allocN32Pixels(10, 10); |
robertphillips@google.com | d1ce77d | 2013-10-09 12:51:09 +0000 | [diff] [blame] | 239 | testBitmap.eraseColor(0); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 240 | |
| 241 | SkRRect testRRect; |
| 242 | testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); |
| 243 | |
| 244 | SkString testText("Hello World"); |
| 245 | const SkPoint testPoints2[] = { |
| 246 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 247 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 248 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 249 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 250 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 251 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 252 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 253 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 254 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 255 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 256 | { SkIntToScalar(10), SkIntToScalar(1) }, |
| 257 | }; |
| 258 | |
| 259 | #define EXPECT_COPY_ON_WRITE(command) \ |
| 260 | { \ |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 261 | SkImage* imageBefore = surface->newImageSnapshot(); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 262 | SkAutoTUnref<SkImage> aur_before(imageBefore); \ |
| 263 | canvas-> command ; \ |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 264 | SkImage* imageAfter = surface->newImageSnapshot(); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 265 | SkAutoTUnref<SkImage> aur_after(imageAfter); \ |
| 266 | REPORTER_ASSERT(reporter, imageBefore != imageAfter); \ |
| 267 | } |
| 268 | |
| 269 | EXPECT_COPY_ON_WRITE(clear(testColor)) |
| 270 | EXPECT_COPY_ON_WRITE(drawPaint(testPaint)) |
| 271 | EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ |
| 272 | testPaint)) |
| 273 | EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint)) |
| 274 | EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint)) |
| 275 | EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint)) |
| 276 | EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
| 277 | EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
| 278 | EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect)) |
| 279 | EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL)) |
| 280 | EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL)) |
| 281 | EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL)) |
| 282 | EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint)) |
| 283 | EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \ |
| 284 | testPaint)) |
| 285 | EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \ |
| 286 | testPaint)) |
| 287 | } |
| 288 | |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 289 | static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter, |
| 290 | SurfaceType surfaceType, |
| 291 | GrContext* context) { |
| 292 | // This test succeeds by not triggering an assertion. |
| 293 | // The test verifies that the surface remains writable (usable) after |
| 294 | // 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] | 295 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 296 | SkCanvas* canvas = surface->getCanvas(); |
| 297 | canvas->clear(1); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 298 | surface->newImageSnapshot()->unref(); // Create and destroy SkImage |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 299 | canvas->clear(2); // Must not assert internally |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 300 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 301 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 302 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 303 | static void TestSurfaceInCache(skiatest::Reporter* reporter, |
| 304 | SurfaceType surfaceType, |
| 305 | GrContext* context) { |
| 306 | context->freeGpuResources(); |
| 307 | REPORTER_ASSERT(reporter, 0 == context->getGpuTextureCacheResourceCount()); |
| 308 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
| 309 | // Note: the stencil buffer is always cached, so kGpu_SurfaceType uses |
| 310 | // one cached resource, and kGpuScratch_SurfaceType uses two. |
| 311 | int expectedCachedResources = surfaceType == kGpuScratch_SurfaceType ? 2 : 1; |
| 312 | REPORTER_ASSERT(reporter, expectedCachedResources == context->getGpuTextureCacheResourceCount()); |
| 313 | |
| 314 | // Verify that all the cached resources are locked in cache. |
skia.committer@gmail.com | 99e5b52 | 2014-03-21 03:02:42 +0000 | [diff] [blame] | 315 | context->freeGpuResources(); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 316 | REPORTER_ASSERT(reporter, expectedCachedResources == context->getGpuTextureCacheResourceCount()); |
| 317 | |
| 318 | // Verify that all the cached resources are unlocked upon surface release |
| 319 | surface.reset(0); |
| 320 | context->freeGpuResources(); |
skia.committer@gmail.com | 99e5b52 | 2014-03-21 03:02:42 +0000 | [diff] [blame] | 321 | REPORTER_ASSERT(reporter, 0 == context->getGpuTextureCacheResourceCount()); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 322 | } |
| 323 | |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 324 | static void Test_crbug263329(skiatest::Reporter* reporter, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 325 | SurfaceType surfaceType, |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 326 | GrContext* context) { |
| 327 | // This is a regression test for crbug.com/263329 |
| 328 | // Bug was caused by onCopyOnWrite releasing the old surface texture |
| 329 | // back to the scratch texture pool even though the texture is used |
| 330 | // by and active SkImage_Gpu. |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 331 | SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context)); |
| 332 | SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context)); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 333 | SkCanvas* canvas1 = surface1->getCanvas(); |
| 334 | SkCanvas* canvas2 = surface2->getCanvas(); |
| 335 | canvas1->clear(1); |
| 336 | SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); |
| 337 | // Trigger copy on write, new backing is a scratch texture |
| 338 | canvas1->clear(2); |
| 339 | SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); |
| 340 | // Trigger copy on write, old backing should not be returned to scratch |
| 341 | // pool because it is held by image2 |
| 342 | canvas1->clear(3); |
| 343 | |
| 344 | canvas2->clear(4); |
| 345 | SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); |
| 346 | // Trigger copy on write on surface2. The new backing store should not |
| 347 | // be recycling a texture that is held by an existing image. |
| 348 | canvas2->clear(5); |
| 349 | SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); |
| 350 | REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture()); |
| 351 | // The following assertion checks crbug.com/263329 |
| 352 | REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture()); |
| 353 | REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture()); |
| 354 | REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture()); |
| 355 | REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture()); |
| 356 | REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture()); |
| 357 | } |
| 358 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 359 | static void TestGetTexture(skiatest::Reporter* reporter, |
| 360 | SurfaceType surfaceType, |
| 361 | GrContext* context) { |
| 362 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
| 363 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 364 | GrTexture* texture = image->getTexture(); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 365 | if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) { |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 366 | REPORTER_ASSERT(reporter, NULL != texture); |
| 367 | REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); |
| 368 | } else { |
| 369 | REPORTER_ASSERT(reporter, NULL == texture); |
| 370 | } |
| 371 | surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
| 372 | REPORTER_ASSERT(reporter, image->getTexture() == texture); |
| 373 | } |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 374 | #endif |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 375 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 376 | static void TestSurfaceNoCanvas(skiatest::Reporter* reporter, |
| 377 | SurfaceType surfaceType, |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 378 | GrContext* context, |
| 379 | SkSurface::ContentChangeMode mode) { |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 380 | // Verifies the robustness of SkSurface for handling use cases where calls |
| 381 | // are made before a canvas is created. |
| 382 | { |
| 383 | // Test passes by not asserting |
| 384 | SkSurface* surface = createSurface(surfaceType, context); |
| 385 | SkAutoTUnref<SkSurface> aur_surface(surface); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 386 | surface->notifyContentWillChange(mode); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 387 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 388 | } |
| 389 | { |
| 390 | SkSurface* surface = createSurface(surfaceType, context); |
| 391 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 392 | SkImage* image1 = surface->newImageSnapshot(); |
| 393 | SkAutoTUnref<SkImage> aur_image1(image1); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 394 | SkDEBUGCODE(image1->validate();) |
| 395 | SkDEBUGCODE(surface->validate();) |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 396 | surface->notifyContentWillChange(mode); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 397 | SkDEBUGCODE(image1->validate();) |
| 398 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 399 | SkImage* image2 = surface->newImageSnapshot(); |
| 400 | SkAutoTUnref<SkImage> aur_image2(image2); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 401 | SkDEBUGCODE(image2->validate();) |
| 402 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 403 | REPORTER_ASSERT(reporter, image1 != image2); |
| 404 | } |
skia.committer@gmail.com | 45fb8b6 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 405 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 406 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 407 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 408 | DEF_GPUTEST(Surface, reporter, factory) { |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 409 | test_image(reporter); |
| 410 | |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 411 | TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 412 | TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 413 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode); |
| 414 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 415 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 416 | test_imagepeek(reporter); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 417 | test_canvaspeek(reporter, factory); |
| 418 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 419 | #if SK_SUPPORT_GPU |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 420 | TestGetTexture(reporter, kRaster_SurfaceType, NULL); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 421 | if (NULL != factory) { |
| 422 | GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 423 | if (NULL != context) { |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 424 | TestSurfaceInCache(reporter, kGpu_SurfaceType, context); |
| 425 | TestSurfaceInCache(reporter, kGpuScratch_SurfaceType, context); |
| 426 | Test_crbug263329(reporter, kGpu_SurfaceType, context); |
| 427 | Test_crbug263329(reporter, kGpuScratch_SurfaceType, context); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 428 | TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 429 | TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 430 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 431 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 432 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 433 | TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 434 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 435 | TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 436 | TestGetTexture(reporter, kGpu_SurfaceType, context); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 437 | TestGetTexture(reporter, kGpuScratch_SurfaceType, context); |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 438 | } |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 439 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 440 | #endif |
| 441 | } |