blob: d8c6d9d76caa5d883b2edf97977538fd229360b1 [file] [log] [blame]
junov@chromium.org995beb62013-03-28 13:49:22 +00001/*
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.org4ee16bf2014-01-10 22:08:27 +00007
junov@chromium.org995beb62013-03-28 13:49:22 +00008#include "SkCanvas.h"
reed@google.com4f7c6152014-02-06 14:11:56 +00009#include "SkData.h"
piotaixr0d276f72014-09-18 11:55:14 -070010#include "SkDecodingImageGenerator.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000011#include "SkImageEncoder.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000012#include "SkRRect.h"
13#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000014#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000015#include "Test.h"
16
17#if SK_SUPPORT_GPU
18#include "GrContextFactory.h"
19#else
20class GrContextFactory;
21class GrContext;
22#endif
23
24enum SurfaceType {
25 kRaster_SurfaceType,
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000026 kRasterDirect_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000027 kGpu_SurfaceType,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000028 kGpuScratch_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000029};
30
reed982542d2014-06-27 06:48:14 -070031static void release_storage(void* pixels, void* context) {
32 SkASSERT(pixels == context);
33 sk_free(pixels);
34}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000035
36static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context,
37 SkImageInfo* requestedInfo = NULL) {
reed982542d2014-06-27 06:48:14 -070038 static const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000039
40 if (requestedInfo) {
41 *requestedInfo = info;
42 }
junov@chromium.org995beb62013-03-28 13:49:22 +000043
44 switch (surfaceType) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000045 case kRaster_SurfaceType:
46 return SkSurface::NewRaster(info);
reed982542d2014-06-27 06:48:14 -070047 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.orgc3bd8af2014-02-13 17:14:46 +000053 case kGpu_SurfaceType:
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000054#if SK_SUPPORT_GPU
reed4a8126e2014-09-22 07:29:03 -070055 return context ? SkSurface::NewRenderTarget(context, info, 0, NULL) : NULL;
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000056#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000057 break;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000058 case kGpuScratch_SurfaceType:
59#if SK_SUPPORT_GPU
60 return context ? SkSurface::NewScratchRenderTarget(context, info) : NULL;
61#endif
62 break;
junov@chromium.org995beb62013-03-28 13:49:22 +000063 }
junov@chromium.org995beb62013-03-28 13:49:22 +000064 return NULL;
65}
66
reed@google.com4f7c6152014-02-06 14:11:56 +000067enum ImageType {
68 kRasterCopy_ImageType,
69 kRasterData_ImageType,
70 kGpu_ImageType,
reed@google.com4f7c6152014-02-06 14:11:56 +000071 kCodec_ImageType,
72};
reed@google.com999da9c2014-02-06 13:43:07 +000073
reedb2497c22014-12-31 12:31:43 -080074#include "SkImageGenerator.h"
75
76class EmptyGenerator : public SkImageGenerator {
77protected:
78 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
79 *info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
80 return true;
81 }
82};
83
84static 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
92static 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.com999da9c2014-02-06 13:43:07 +0000103static 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);
reed9594da12014-09-12 12:12:27 -0700107 SkData* data = SkData::NewUninitialized(size);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000108
mtkleinbbb61d72014-11-24 13:09:39 -0800109 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000110 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
mtkleinbbb61d72014-11-24 13:09:39 -0800111 REPORTER_ASSERT(reporter, !data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000112 image->unref();
mtkleinbbb61d72014-11-24 13:09:39 -0800113 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000114 data->unref();
115}
116
reed67f2eb42014-12-10 06:54:06 -0800117static SkImage* createImage(ImageType imageType, GrContext* context, SkColor color) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000118 const SkPMColor pmcolor = SkPreMultiplyColor(color);
119 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
120 const size_t rowBytes = info.minRowBytes();
reede5ea5002014-09-03 11:54:58 -0700121 const size_t size = rowBytes * info.height();
reed@google.com4f7c6152014-02-06 14:11:56 +0000122
reed9594da12014-09-12 12:12:27 -0700123 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
124 void* addr = data->writable_data();
reed@google.com4f7c6152014-02-06 14:11:56 +0000125 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
reed@google.com4f7c6152014-02-06 14:11:56 +0000126
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);
reed67f2eb42014-12-10 06:54:06 -0800132 case kGpu_ImageType: {
133 SkAutoTUnref<SkSurface> surf(SkSurface::NewRenderTarget(context, info, 0));
134 surf->getCanvas()->clear(color);
135 return surf->newImageSnapshot();
136 }
reed@google.com4f7c6152014-02-06 14:11:56 +0000137 case kCodec_ImageType: {
138 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000139 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000140 SkAutoTUnref<SkData> src(
reed67f2eb42014-12-10 06:54:06 -0800141 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
piotaixr0d276f72014-09-18 11:55:14 -0700142 return SkImage::NewFromGenerator(
reed67f2eb42014-12-10 06:54:06 -0800143 SkDecodingImageGenerator::Create(src, SkDecodingImageGenerator::Options()));
reed@google.com4f7c6152014-02-06 14:11:56 +0000144 }
145 }
146 SkASSERT(false);
147 return NULL;
148}
149
reed96472de2014-12-10 09:53:42 -0800150static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
151 sk_memset32(pixels, color, count);
152}
153static 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
162static 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
reed67f2eb42014-12-10 06:54:06 -0800207static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000208 static const struct {
209 ImageType fType;
210 bool fPeekShouldSucceed;
reed67f2eb42014-12-10 06:54:06 -0800211 const char* fName;
reed@google.com4f7c6152014-02-06 14:11:56 +0000212 } gRec[] = {
reed67f2eb42014-12-10 06:54:06 -0800213 { kRasterCopy_ImageType, true, "RasterCopy" },
214 { kRasterData_ImageType, true, "RasterData" },
215 { kGpu_ImageType, false, "Gpu" },
216 { kCodec_ImageType, false, "Codec" },
reed@google.com4f7c6152014-02-06 14:11:56 +0000217 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000218
reed@google.com4f7c6152014-02-06 14:11:56 +0000219 const SkColor color = SK_ColorRED;
220 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000221
reed67f2eb42014-12-10 06:54:06 -0800222 GrContext* ctx = NULL;
223#if SK_SUPPORT_GPU
224 ctx = factory->get(GrContextFactory::kNative_GLContextType);
225#endif
226
reed@google.com4f7c6152014-02-06 14:11:56 +0000227 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
228 SkImageInfo info;
229 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000230
reed67f2eb42014-12-10 06:54:06 -0800231 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, ctx, color));
reed@google.com4f7c6152014-02-06 14:11:56 +0000232 if (!image.get()) {
reed67f2eb42014-12-10 06:54:06 -0800233 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
reed@google.com4f7c6152014-02-06 14:11:56 +0000234 continue; // gpu may not be enabled
235 }
236 const void* addr = image->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700237 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000238 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
239 if (success) {
reede5ea5002014-09-03 11:54:58 -0700240 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.com4f7c6152014-02-06 14:11:56 +0000245 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
246 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
247 }
reed96472de2014-12-10 09:53:42 -0800248
249 test_image_readpixels(reporter, image, pmcolor);
reed@google.com4f7c6152014-02-06 14:11:56 +0000250 }
251}
252
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000253static 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.orgd8a57af2014-03-19 21:19:16 +0000263 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000264#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000265 };
266
267 const SkColor color = SK_ColorRED;
268 const SkPMColor pmcolor = SkPreMultiplyColor(color);
269
bsalomone904c092014-07-17 10:50:59 -0700270 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000271#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700272 cnt = GrContextFactory::kGLContextTypeCnt;
273#else
274 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000275#endif
276
bsalomone904c092014-07-17 10:50:59 -0700277 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.orgc3bd8af2014-02-13 17:14:46 +0000285
bsalomone904c092014-07-17 10:50:59 -0700286 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.orgc3bd8af2014-02-13 17:14:46 +0000293
bsalomone904c092014-07-17 10:50:59 -0700294 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context,
295 &requestInfo));
296 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000297
bsalomone904c092014-07-17 10:50:59 -0700298 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700299 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700300 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000301
bsalomone904c092014-07-17 10:50:59 -0700302 SkImageInfo info2;
303 size_t rb2;
304 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000305
bsalomone904c092014-07-17 10:50:59 -0700306 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.orgc3bd8af2014-02-13 17:14:46 +0000317 }
318 }
319}
320
junov@chromium.org995beb62013-03-28 13:49:22 +0000321static 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.org995beb62013-03-28 13:49:22 +0000331 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.orgdeee4962014-02-13 14:41:43 +0000351 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000352 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000353
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.org5ee449a2013-04-12 20:20:50 +0000374 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000375 SkAutoTUnref<SkImage> aur_before(imageBefore); \
376 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000377 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000378 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.org995beb62013-03-28 13:49:22 +0000392 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.orgaf058352013-04-03 15:03:26 +0000401static 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.org4d24b742013-07-25 23:29:40 +0000407 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000408 SkCanvas* canvas = surface->getCanvas();
409 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000410 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000411 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000412}
junov@chromium.orgda904742013-05-01 22:38:16 +0000413
junov@chromium.orgb516a412013-05-01 22:49:59 +0000414#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000415static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000416 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000417 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.orgd8a57af2014-03-19 21:19:16 +0000422 SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context));
423 SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000424 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.orgda904742013-05-01 22:38:16 +0000450static 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.orgd8a57af2014-03-19 21:19:16 +0000456 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700457 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000458 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.orgb516a412013-05-01 22:49:59 +0000465#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000466
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000467static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
468 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000469 GrContext* context,
470 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000471 // 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.orgc4c98702013-04-22 14:28:01 +0000477 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000478 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000479 }
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.com03087072013-10-02 16:42:21 +0000485 SkDEBUGCODE(image1->validate();)
486 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000487 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000488 SkDEBUGCODE(image1->validate();)
489 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000490 SkImage* image2 = surface->newImageSnapshot();
491 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000492 SkDEBUGCODE(image2->validate();)
493 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000494 REPORTER_ASSERT(reporter, image1 != image2);
495 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000496
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000497}
junov@chromium.org995beb62013-03-28 13:49:22 +0000498
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000499DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000500 test_image(reporter);
501
junov@chromium.orgaf058352013-04-03 15:03:26 +0000502 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000503 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000504 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
505 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000506
reedb2497c22014-12-31 12:31:43 -0800507 test_empty_image(reporter);
508 test_empty_surface(reporter, NULL);
509
reed67f2eb42014-12-10 06:54:06 -0800510 test_imagepeek(reporter, factory);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000511 test_canvaspeek(reporter, factory);
512
junov@chromium.orgb516a412013-05-01 22:49:59 +0000513#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000514 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700515 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700516 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);
bsalomon49f085d2014-09-05 13:34:00 -0700522 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700523 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);
reedb2497c22014-12-31 12:31:43 -0800535 test_empty_surface(reporter, context);
bsalomone904c092014-07-17 10:50:59 -0700536 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000537 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000538 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000539#endif
540}