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