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