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