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 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame^] | 74 | #include "SkImageGenerator.h" |
| 75 | |
| 76 | class EmptyGenerator : public SkImageGenerator { |
| 77 | protected: |
| 78 | bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { |
| 79 | *info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
| 80 | return true; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | static void test_empty_image(skiatest::Reporter* reporter) { |
| 85 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
| 86 | |
| 87 | REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterCopy(info, NULL, 0)); |
| 88 | REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterData(info, NULL, 0)); |
| 89 | REPORTER_ASSERT(reporter, NULL == SkImage::NewFromGenerator(SkNEW(EmptyGenerator))); |
| 90 | } |
| 91 | |
| 92 | static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) { |
| 93 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
| 94 | |
| 95 | REPORTER_ASSERT(reporter, NULL == SkSurface::NewRaster(info)); |
| 96 | REPORTER_ASSERT(reporter, NULL == SkSurface::NewRasterDirect(info, NULL, 0)); |
| 97 | if (ctx) { |
| 98 | REPORTER_ASSERT(reporter, NULL == SkSurface::NewRenderTarget(ctx, info, 0, NULL)); |
| 99 | REPORTER_ASSERT(reporter, NULL == SkSurface::NewScratchRenderTarget(ctx, info, 0, NULL)); |
| 100 | } |
| 101 | } |
| 102 | |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 103 | static void test_image(skiatest::Reporter* reporter) { |
| 104 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 105 | size_t rowBytes = info.minRowBytes(); |
| 106 | size_t size = info.getSafeSize(rowBytes); |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 107 | SkData* data = SkData::NewUninitialized(size); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 108 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, data->unique()); |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 110 | SkImage* image = SkImage::NewRasterData(info, data, rowBytes); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, !data->unique()); |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 112 | image->unref(); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, data->unique()); |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 114 | data->unref(); |
| 115 | } |
| 116 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 117 | static SkImage* createImage(ImageType imageType, GrContext* context, SkColor color) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 118 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 119 | const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 120 | const size_t rowBytes = info.minRowBytes(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 121 | const size_t size = rowBytes * info.height(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 122 | |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 123 | SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
| 124 | void* addr = data->writable_data(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 125 | sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 126 | |
| 127 | switch (imageType) { |
| 128 | case kRasterCopy_ImageType: |
| 129 | return SkImage::NewRasterCopy(info, addr, rowBytes); |
| 130 | case kRasterData_ImageType: |
| 131 | return SkImage::NewRasterData(info, data, rowBytes); |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 132 | case kGpu_ImageType: { |
| 133 | SkAutoTUnref<SkSurface> surf(SkSurface::NewRenderTarget(context, info, 0)); |
| 134 | surf->getCanvas()->clear(color); |
| 135 | return surf->newImageSnapshot(); |
| 136 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 137 | case kCodec_ImageType: { |
| 138 | SkBitmap bitmap; |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 139 | bitmap.installPixels(info, addr, rowBytes); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 140 | SkAutoTUnref<SkData> src( |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 141 | SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100)); |
piotaixr | 0d276f7 | 2014-09-18 11:55:14 -0700 | [diff] [blame] | 142 | return SkImage::NewFromGenerator( |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 143 | SkDecodingImageGenerator::Create(src, SkDecodingImageGenerator::Options())); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | SkASSERT(false); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 150 | static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) { |
| 151 | sk_memset32(pixels, color, count); |
| 152 | } |
| 153 | static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) { |
| 154 | for (int i = 0; i < count; ++i) { |
| 155 | if (pixels[i] != expected) { |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image, |
| 163 | SkPMColor expected) { |
| 164 | const SkPMColor notExpected = ~expected; |
| 165 | |
| 166 | const int w = 2, h = 2; |
| 167 | const size_t rowBytes = w * sizeof(SkPMColor); |
| 168 | SkPMColor pixels[w*h]; |
| 169 | |
| 170 | SkImageInfo info; |
| 171 | |
| 172 | info = SkImageInfo::MakeUnknown(w, h); |
| 173 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 174 | |
| 175 | // out-of-bounds should fail |
| 176 | info = SkImageInfo::MakeN32Premul(w, h); |
| 177 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0)); |
| 178 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h)); |
| 179 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0)); |
| 180 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height())); |
| 181 | |
| 182 | // top-left should succeed |
| 183 | set_pixels(pixels, w*h, notExpected); |
| 184 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 185 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 186 | |
| 187 | // bottom-right should succeed |
| 188 | set_pixels(pixels, w*h, notExpected); |
| 189 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 190 | image->width() - w, image->height() - h)); |
| 191 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 192 | |
| 193 | // partial top-left should succeed |
| 194 | set_pixels(pixels, w*h, notExpected); |
| 195 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1)); |
| 196 | REPORTER_ASSERT(reporter, pixels[3] == expected); |
| 197 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected)); |
| 198 | |
| 199 | // partial bottom-right should succeed |
| 200 | set_pixels(pixels, w*h, notExpected); |
| 201 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 202 | image->width() - 1, image->height() - 1)); |
| 203 | REPORTER_ASSERT(reporter, pixels[0] == expected); |
| 204 | REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected)); |
| 205 | } |
| 206 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 207 | static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 208 | static const struct { |
| 209 | ImageType fType; |
| 210 | bool fPeekShouldSucceed; |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 211 | const char* fName; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 212 | } gRec[] = { |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 213 | { kRasterCopy_ImageType, true, "RasterCopy" }, |
| 214 | { kRasterData_ImageType, true, "RasterData" }, |
| 215 | { kGpu_ImageType, false, "Gpu" }, |
| 216 | { kCodec_ImageType, false, "Codec" }, |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 217 | }; |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 218 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 219 | const SkColor color = SK_ColorRED; |
| 220 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 221 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 222 | GrContext* ctx = NULL; |
| 223 | #if SK_SUPPORT_GPU |
| 224 | ctx = factory->get(GrContextFactory::kNative_GLContextType); |
| 225 | #endif |
| 226 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 227 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 228 | SkImageInfo info; |
| 229 | size_t rowBytes; |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 230 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 231 | SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, ctx, color)); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 232 | if (!image.get()) { |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 233 | SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 234 | continue; // gpu may not be enabled |
| 235 | } |
| 236 | const void* addr = image->peekPixels(&info, &rowBytes); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 237 | bool success = SkToBool(addr); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 238 | REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
| 239 | if (success) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 240 | REPORTER_ASSERT(reporter, 10 == info.width()); |
| 241 | REPORTER_ASSERT(reporter, 10 == info.height()); |
| 242 | REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType()); |
| 243 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() || |
| 244 | kOpaque_SkAlphaType == info.alphaType()); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 245 | REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); |
| 246 | REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
| 247 | } |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 248 | |
| 249 | test_image_readpixels(reporter, image, pmcolor); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 253 | static void test_canvaspeek(skiatest::Reporter* reporter, |
| 254 | GrContextFactory* factory) { |
| 255 | static const struct { |
| 256 | SurfaceType fType; |
| 257 | bool fPeekShouldSucceed; |
| 258 | } gRec[] = { |
| 259 | { kRaster_SurfaceType, true }, |
| 260 | { kRasterDirect_SurfaceType, true }, |
| 261 | #if SK_SUPPORT_GPU |
| 262 | { kGpu_SurfaceType, false }, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 263 | { kGpuScratch_SurfaceType, false }, |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 264 | #endif |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | const SkColor color = SK_ColorRED; |
| 268 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 269 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 270 | int cnt; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 271 | #if SK_SUPPORT_GPU |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 272 | cnt = GrContextFactory::kGLContextTypeCnt; |
| 273 | #else |
| 274 | cnt = 1; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 275 | #endif |
| 276 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 277 | for (int i= 0; i < cnt; ++i) { |
| 278 | GrContext* context = NULL; |
| 279 | #if SK_SUPPORT_GPU |
| 280 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| 281 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 282 | continue; |
| 283 | } |
| 284 | context = factory->get(glCtxType); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 285 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 286 | if (NULL == context) { |
| 287 | continue; |
| 288 | } |
| 289 | #endif |
| 290 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 291 | SkImageInfo info, requestInfo; |
| 292 | size_t rowBytes; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 293 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 294 | SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context, |
| 295 | &requestInfo)); |
| 296 | surface->getCanvas()->clear(color); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 297 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 298 | const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 299 | bool success = SkToBool(addr); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 300 | REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 301 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 302 | SkImageInfo info2; |
| 303 | size_t rb2; |
| 304 | const void* addr2 = surface->peekPixels(&info2, &rb2); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 305 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 306 | if (success) { |
| 307 | REPORTER_ASSERT(reporter, requestInfo == info); |
| 308 | REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes); |
| 309 | REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
| 310 | |
| 311 | REPORTER_ASSERT(reporter, addr2 == addr); |
| 312 | REPORTER_ASSERT(reporter, info2 == info); |
| 313 | REPORTER_ASSERT(reporter, rb2 == rowBytes); |
| 314 | } else { |
| 315 | REPORTER_ASSERT(reporter, NULL == addr2); |
| 316 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 321 | static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType, |
| 322 | GrContext* context) { |
| 323 | // Verify that the right canvas commands trigger a copy on write |
| 324 | SkSurface* surface = createSurface(surfaceType, context); |
| 325 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 326 | SkCanvas* canvas = surface->getCanvas(); |
| 327 | |
| 328 | const SkRect testRect = |
| 329 | SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 330 | SkIntToScalar(4), SkIntToScalar(5)); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 331 | SkPath testPath; |
| 332 | testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 333 | SkIntToScalar(2), SkIntToScalar(1))); |
| 334 | |
| 335 | const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 336 | |
| 337 | SkRegion testRegion; |
| 338 | testRegion.setRect(testIRect); |
| 339 | |
| 340 | |
| 341 | const SkColor testColor = 0x01020304; |
| 342 | const SkPaint testPaint; |
| 343 | const SkPoint testPoints[3] = { |
| 344 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 345 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 346 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 347 | }; |
| 348 | const size_t testPointCount = 3; |
| 349 | |
| 350 | SkBitmap testBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 351 | testBitmap.allocN32Pixels(10, 10); |
robertphillips@google.com | d1ce77d | 2013-10-09 12:51:09 +0000 | [diff] [blame] | 352 | testBitmap.eraseColor(0); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 353 | |
| 354 | SkRRect testRRect; |
| 355 | testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); |
| 356 | |
| 357 | SkString testText("Hello World"); |
| 358 | const SkPoint testPoints2[] = { |
| 359 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 360 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 361 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 362 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 363 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 364 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 365 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 366 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 367 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 368 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 369 | { SkIntToScalar(10), SkIntToScalar(1) }, |
| 370 | }; |
| 371 | |
| 372 | #define EXPECT_COPY_ON_WRITE(command) \ |
| 373 | { \ |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 374 | SkImage* imageBefore = surface->newImageSnapshot(); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 375 | SkAutoTUnref<SkImage> aur_before(imageBefore); \ |
| 376 | canvas-> command ; \ |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 377 | SkImage* imageAfter = surface->newImageSnapshot(); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 378 | SkAutoTUnref<SkImage> aur_after(imageAfter); \ |
| 379 | REPORTER_ASSERT(reporter, imageBefore != imageAfter); \ |
| 380 | } |
| 381 | |
| 382 | EXPECT_COPY_ON_WRITE(clear(testColor)) |
| 383 | EXPECT_COPY_ON_WRITE(drawPaint(testPaint)) |
| 384 | EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ |
| 385 | testPaint)) |
| 386 | EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint)) |
| 387 | EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint)) |
| 388 | EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint)) |
| 389 | EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
| 390 | EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
| 391 | EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect)) |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 392 | EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL)) |
| 393 | EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL)) |
| 394 | EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint)) |
| 395 | EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \ |
| 396 | testPaint)) |
| 397 | EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \ |
| 398 | testPaint)) |
| 399 | } |
| 400 | |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 401 | static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter, |
| 402 | SurfaceType surfaceType, |
| 403 | GrContext* context) { |
| 404 | // This test succeeds by not triggering an assertion. |
| 405 | // The test verifies that the surface remains writable (usable) after |
| 406 | // 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] | 407 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 408 | SkCanvas* canvas = surface->getCanvas(); |
| 409 | canvas->clear(1); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 410 | surface->newImageSnapshot()->unref(); // Create and destroy SkImage |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 411 | canvas->clear(2); // Must not assert internally |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 412 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 413 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 414 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 415 | static void Test_crbug263329(skiatest::Reporter* reporter, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 416 | SurfaceType surfaceType, |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 417 | GrContext* context) { |
| 418 | // This is a regression test for crbug.com/263329 |
| 419 | // Bug was caused by onCopyOnWrite releasing the old surface texture |
| 420 | // back to the scratch texture pool even though the texture is used |
| 421 | // by and active SkImage_Gpu. |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 422 | SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context)); |
| 423 | SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context)); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 424 | SkCanvas* canvas1 = surface1->getCanvas(); |
| 425 | SkCanvas* canvas2 = surface2->getCanvas(); |
| 426 | canvas1->clear(1); |
| 427 | SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); |
| 428 | // Trigger copy on write, new backing is a scratch texture |
| 429 | canvas1->clear(2); |
| 430 | SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); |
| 431 | // Trigger copy on write, old backing should not be returned to scratch |
| 432 | // pool because it is held by image2 |
| 433 | canvas1->clear(3); |
| 434 | |
| 435 | canvas2->clear(4); |
| 436 | SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); |
| 437 | // Trigger copy on write on surface2. The new backing store should not |
| 438 | // be recycling a texture that is held by an existing image. |
| 439 | canvas2->clear(5); |
| 440 | SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); |
| 441 | REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture()); |
| 442 | // The following assertion checks crbug.com/263329 |
| 443 | REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture()); |
| 444 | REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture()); |
| 445 | REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture()); |
| 446 | REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture()); |
| 447 | REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture()); |
| 448 | } |
| 449 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 450 | static void TestGetTexture(skiatest::Reporter* reporter, |
| 451 | SurfaceType surfaceType, |
| 452 | GrContext* context) { |
| 453 | SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context)); |
| 454 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 455 | GrTexture* texture = image->getTexture(); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 456 | if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 457 | REPORTER_ASSERT(reporter, texture); |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 458 | REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); |
| 459 | } else { |
| 460 | REPORTER_ASSERT(reporter, NULL == texture); |
| 461 | } |
| 462 | surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
| 463 | REPORTER_ASSERT(reporter, image->getTexture() == texture); |
| 464 | } |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 465 | #endif |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 466 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 467 | static void TestSurfaceNoCanvas(skiatest::Reporter* reporter, |
| 468 | SurfaceType surfaceType, |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 469 | GrContext* context, |
| 470 | SkSurface::ContentChangeMode mode) { |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 471 | // Verifies the robustness of SkSurface for handling use cases where calls |
| 472 | // are made before a canvas is created. |
| 473 | { |
| 474 | // Test passes by not asserting |
| 475 | SkSurface* surface = createSurface(surfaceType, context); |
| 476 | SkAutoTUnref<SkSurface> aur_surface(surface); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 477 | surface->notifyContentWillChange(mode); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 478 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 479 | } |
| 480 | { |
| 481 | SkSurface* surface = createSurface(surfaceType, context); |
| 482 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 483 | SkImage* image1 = surface->newImageSnapshot(); |
| 484 | SkAutoTUnref<SkImage> aur_image1(image1); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 485 | SkDEBUGCODE(image1->validate();) |
| 486 | SkDEBUGCODE(surface->validate();) |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 487 | surface->notifyContentWillChange(mode); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 488 | SkDEBUGCODE(image1->validate();) |
| 489 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 490 | SkImage* image2 = surface->newImageSnapshot(); |
| 491 | SkAutoTUnref<SkImage> aur_image2(image2); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 492 | SkDEBUGCODE(image2->validate();) |
| 493 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 494 | REPORTER_ASSERT(reporter, image1 != image2); |
| 495 | } |
skia.committer@gmail.com | 45fb8b6 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 496 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 497 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 498 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 499 | DEF_GPUTEST(Surface, reporter, factory) { |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 500 | test_image(reporter); |
| 501 | |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 502 | TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 503 | TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 504 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode); |
| 505 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 506 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame^] | 507 | test_empty_image(reporter); |
| 508 | test_empty_surface(reporter, NULL); |
| 509 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 510 | test_imagepeek(reporter, factory); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 511 | test_canvaspeek(reporter, factory); |
| 512 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 513 | #if SK_SUPPORT_GPU |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 514 | TestGetTexture(reporter, kRaster_SurfaceType, NULL); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 515 | if (factory) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 516 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 517 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| 518 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 519 | continue; |
| 520 | } |
| 521 | GrContext* context = factory->get(glCtxType); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 522 | if (context) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 523 | Test_crbug263329(reporter, kGpu_SurfaceType, context); |
| 524 | Test_crbug263329(reporter, kGpuScratch_SurfaceType, context); |
| 525 | TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); |
| 526 | TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context); |
| 527 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); |
| 528 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context); |
| 529 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
| 530 | TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
| 531 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
| 532 | TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
| 533 | TestGetTexture(reporter, kGpu_SurfaceType, context); |
| 534 | TestGetTexture(reporter, kGpuScratch_SurfaceType, context); |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame^] | 535 | test_empty_surface(reporter, context); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 536 | } |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 537 | } |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 538 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 539 | #endif |
| 540 | } |