blob: 28abf9804c1f3dc43b14f486f4d47f31150210c3 [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
74static 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);
reed9594da12014-09-12 12:12:27 -070078 SkData* data = SkData::NewUninitialized(size);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000079
mtkleinbbb61d72014-11-24 13:09:39 -080080 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +000081 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
mtkleinbbb61d72014-11-24 13:09:39 -080082 REPORTER_ASSERT(reporter, !data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +000083 image->unref();
mtkleinbbb61d72014-11-24 13:09:39 -080084 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +000085 data->unref();
86}
87
reed67f2eb42014-12-10 06:54:06 -080088static SkImage* createImage(ImageType imageType, GrContext* context, SkColor color) {
reed@google.com4f7c6152014-02-06 14:11:56 +000089 const SkPMColor pmcolor = SkPreMultiplyColor(color);
90 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
91 const size_t rowBytes = info.minRowBytes();
reede5ea5002014-09-03 11:54:58 -070092 const size_t size = rowBytes * info.height();
reed@google.com4f7c6152014-02-06 14:11:56 +000093
reed9594da12014-09-12 12:12:27 -070094 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
95 void* addr = data->writable_data();
reed@google.com4f7c6152014-02-06 14:11:56 +000096 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
reed@google.com4f7c6152014-02-06 14:11:56 +000097
98 switch (imageType) {
99 case kRasterCopy_ImageType:
100 return SkImage::NewRasterCopy(info, addr, rowBytes);
101 case kRasterData_ImageType:
102 return SkImage::NewRasterData(info, data, rowBytes);
reed67f2eb42014-12-10 06:54:06 -0800103 case kGpu_ImageType: {
104 SkAutoTUnref<SkSurface> surf(SkSurface::NewRenderTarget(context, info, 0));
105 surf->getCanvas()->clear(color);
106 return surf->newImageSnapshot();
107 }
reed@google.com4f7c6152014-02-06 14:11:56 +0000108 case kCodec_ImageType: {
109 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000110 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000111 SkAutoTUnref<SkData> src(
reed67f2eb42014-12-10 06:54:06 -0800112 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
piotaixr0d276f72014-09-18 11:55:14 -0700113 return SkImage::NewFromGenerator(
reed67f2eb42014-12-10 06:54:06 -0800114 SkDecodingImageGenerator::Create(src, SkDecodingImageGenerator::Options()));
reed@google.com4f7c6152014-02-06 14:11:56 +0000115 }
116 }
117 SkASSERT(false);
118 return NULL;
119}
120
reed96472de2014-12-10 09:53:42 -0800121static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
122 sk_memset32(pixels, color, count);
123}
124static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
125 for (int i = 0; i < count; ++i) {
126 if (pixels[i] != expected) {
127 return false;
128 }
129 }
130 return true;
131}
132
133static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image,
134 SkPMColor expected) {
135 const SkPMColor notExpected = ~expected;
136
137 const int w = 2, h = 2;
138 const size_t rowBytes = w * sizeof(SkPMColor);
139 SkPMColor pixels[w*h];
140
141 SkImageInfo info;
142
143 info = SkImageInfo::MakeUnknown(w, h);
144 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
145
146 // out-of-bounds should fail
147 info = SkImageInfo::MakeN32Premul(w, h);
148 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
149 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
150 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
151 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
152
153 // top-left should succeed
154 set_pixels(pixels, w*h, notExpected);
155 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
156 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
157
158 // bottom-right should succeed
159 set_pixels(pixels, w*h, notExpected);
160 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
161 image->width() - w, image->height() - h));
162 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
163
164 // partial top-left should succeed
165 set_pixels(pixels, w*h, notExpected);
166 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
167 REPORTER_ASSERT(reporter, pixels[3] == expected);
168 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
169
170 // partial bottom-right should succeed
171 set_pixels(pixels, w*h, notExpected);
172 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
173 image->width() - 1, image->height() - 1));
174 REPORTER_ASSERT(reporter, pixels[0] == expected);
175 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
176}
177
reed67f2eb42014-12-10 06:54:06 -0800178static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000179 static const struct {
180 ImageType fType;
181 bool fPeekShouldSucceed;
reed67f2eb42014-12-10 06:54:06 -0800182 const char* fName;
reed@google.com4f7c6152014-02-06 14:11:56 +0000183 } gRec[] = {
reed67f2eb42014-12-10 06:54:06 -0800184 { kRasterCopy_ImageType, true, "RasterCopy" },
185 { kRasterData_ImageType, true, "RasterData" },
186 { kGpu_ImageType, false, "Gpu" },
187 { kCodec_ImageType, false, "Codec" },
reed@google.com4f7c6152014-02-06 14:11:56 +0000188 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000189
reed@google.com4f7c6152014-02-06 14:11:56 +0000190 const SkColor color = SK_ColorRED;
191 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000192
reed67f2eb42014-12-10 06:54:06 -0800193 GrContext* ctx = NULL;
194#if SK_SUPPORT_GPU
195 ctx = factory->get(GrContextFactory::kNative_GLContextType);
196#endif
197
reed@google.com4f7c6152014-02-06 14:11:56 +0000198 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
199 SkImageInfo info;
200 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000201
reed67f2eb42014-12-10 06:54:06 -0800202 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, ctx, color));
reed@google.com4f7c6152014-02-06 14:11:56 +0000203 if (!image.get()) {
reed67f2eb42014-12-10 06:54:06 -0800204 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
reed@google.com4f7c6152014-02-06 14:11:56 +0000205 continue; // gpu may not be enabled
206 }
207 const void* addr = image->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700208 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000209 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
210 if (success) {
reede5ea5002014-09-03 11:54:58 -0700211 REPORTER_ASSERT(reporter, 10 == info.width());
212 REPORTER_ASSERT(reporter, 10 == info.height());
213 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
214 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
215 kOpaque_SkAlphaType == info.alphaType());
reed@google.com4f7c6152014-02-06 14:11:56 +0000216 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
217 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
218 }
reed96472de2014-12-10 09:53:42 -0800219
220 test_image_readpixels(reporter, image, pmcolor);
reed@google.com4f7c6152014-02-06 14:11:56 +0000221 }
222}
223
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000224static void test_canvaspeek(skiatest::Reporter* reporter,
225 GrContextFactory* factory) {
226 static const struct {
227 SurfaceType fType;
228 bool fPeekShouldSucceed;
229 } gRec[] = {
230 { kRaster_SurfaceType, true },
231 { kRasterDirect_SurfaceType, true },
232#if SK_SUPPORT_GPU
233 { kGpu_SurfaceType, false },
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000234 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000235#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000236 };
237
238 const SkColor color = SK_ColorRED;
239 const SkPMColor pmcolor = SkPreMultiplyColor(color);
240
bsalomone904c092014-07-17 10:50:59 -0700241 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000242#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700243 cnt = GrContextFactory::kGLContextTypeCnt;
244#else
245 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000246#endif
247
bsalomone904c092014-07-17 10:50:59 -0700248 for (int i= 0; i < cnt; ++i) {
249 GrContext* context = NULL;
250#if SK_SUPPORT_GPU
251 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
252 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
253 continue;
254 }
255 context = factory->get(glCtxType);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000256
bsalomone904c092014-07-17 10:50:59 -0700257 if (NULL == context) {
258 continue;
259 }
260#endif
261 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
262 SkImageInfo info, requestInfo;
263 size_t rowBytes;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000264
bsalomone904c092014-07-17 10:50:59 -0700265 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context,
266 &requestInfo));
267 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000268
bsalomone904c092014-07-17 10:50:59 -0700269 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700270 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700271 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000272
bsalomone904c092014-07-17 10:50:59 -0700273 SkImageInfo info2;
274 size_t rb2;
275 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000276
bsalomone904c092014-07-17 10:50:59 -0700277 if (success) {
278 REPORTER_ASSERT(reporter, requestInfo == info);
279 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
280 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
281
282 REPORTER_ASSERT(reporter, addr2 == addr);
283 REPORTER_ASSERT(reporter, info2 == info);
284 REPORTER_ASSERT(reporter, rb2 == rowBytes);
285 } else {
286 REPORTER_ASSERT(reporter, NULL == addr2);
287 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000288 }
289 }
290}
291
junov@chromium.org995beb62013-03-28 13:49:22 +0000292static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
293 GrContext* context) {
294 // Verify that the right canvas commands trigger a copy on write
295 SkSurface* surface = createSurface(surfaceType, context);
296 SkAutoTUnref<SkSurface> aur_surface(surface);
297 SkCanvas* canvas = surface->getCanvas();
298
299 const SkRect testRect =
300 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
301 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000302 SkPath testPath;
303 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
304 SkIntToScalar(2), SkIntToScalar(1)));
305
306 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
307
308 SkRegion testRegion;
309 testRegion.setRect(testIRect);
310
311
312 const SkColor testColor = 0x01020304;
313 const SkPaint testPaint;
314 const SkPoint testPoints[3] = {
315 {SkIntToScalar(0), SkIntToScalar(0)},
316 {SkIntToScalar(2), SkIntToScalar(1)},
317 {SkIntToScalar(0), SkIntToScalar(2)}
318 };
319 const size_t testPointCount = 3;
320
321 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000322 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000323 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000324
325 SkRRect testRRect;
326 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
327
328 SkString testText("Hello World");
329 const SkPoint testPoints2[] = {
330 { SkIntToScalar(0), SkIntToScalar(1) },
331 { SkIntToScalar(1), SkIntToScalar(1) },
332 { SkIntToScalar(2), SkIntToScalar(1) },
333 { SkIntToScalar(3), SkIntToScalar(1) },
334 { SkIntToScalar(4), SkIntToScalar(1) },
335 { SkIntToScalar(5), SkIntToScalar(1) },
336 { SkIntToScalar(6), SkIntToScalar(1) },
337 { SkIntToScalar(7), SkIntToScalar(1) },
338 { SkIntToScalar(8), SkIntToScalar(1) },
339 { SkIntToScalar(9), SkIntToScalar(1) },
340 { SkIntToScalar(10), SkIntToScalar(1) },
341 };
342
343#define EXPECT_COPY_ON_WRITE(command) \
344 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000345 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000346 SkAutoTUnref<SkImage> aur_before(imageBefore); \
347 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000348 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000349 SkAutoTUnref<SkImage> aur_after(imageAfter); \
350 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
351 }
352
353 EXPECT_COPY_ON_WRITE(clear(testColor))
354 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
355 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
356 testPaint))
357 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
358 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
359 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
360 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
361 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
362 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
junov@chromium.org995beb62013-03-28 13:49:22 +0000363 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
364 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
365 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
366 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
367 testPaint))
368 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
369 testPaint))
370}
371
junov@chromium.orgaf058352013-04-03 15:03:26 +0000372static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
373 SurfaceType surfaceType,
374 GrContext* context) {
375 // This test succeeds by not triggering an assertion.
376 // The test verifies that the surface remains writable (usable) after
377 // acquiring and releasing a snapshot without triggering a copy on write.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000378 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000379 SkCanvas* canvas = surface->getCanvas();
380 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000381 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000382 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000383}
junov@chromium.orgda904742013-05-01 22:38:16 +0000384
junov@chromium.orgb516a412013-05-01 22:49:59 +0000385#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000386static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000387 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000388 GrContext* context) {
389 // This is a regression test for crbug.com/263329
390 // Bug was caused by onCopyOnWrite releasing the old surface texture
391 // back to the scratch texture pool even though the texture is used
392 // by and active SkImage_Gpu.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000393 SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context));
394 SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000395 SkCanvas* canvas1 = surface1->getCanvas();
396 SkCanvas* canvas2 = surface2->getCanvas();
397 canvas1->clear(1);
398 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
399 // Trigger copy on write, new backing is a scratch texture
400 canvas1->clear(2);
401 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
402 // Trigger copy on write, old backing should not be returned to scratch
403 // pool because it is held by image2
404 canvas1->clear(3);
405
406 canvas2->clear(4);
407 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
408 // Trigger copy on write on surface2. The new backing store should not
409 // be recycling a texture that is held by an existing image.
410 canvas2->clear(5);
411 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
412 REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture());
413 // The following assertion checks crbug.com/263329
414 REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture());
415 REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture());
416 REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture());
417 REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture());
418 REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture());
419}
420
junov@chromium.orgda904742013-05-01 22:38:16 +0000421static void TestGetTexture(skiatest::Reporter* reporter,
422 SurfaceType surfaceType,
423 GrContext* context) {
424 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
425 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
426 GrTexture* texture = image->getTexture();
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000427 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700428 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000429 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
430 } else {
431 REPORTER_ASSERT(reporter, NULL == texture);
432 }
433 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
434 REPORTER_ASSERT(reporter, image->getTexture() == texture);
435}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000436#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000437
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000438static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
439 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000440 GrContext* context,
441 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000442 // Verifies the robustness of SkSurface for handling use cases where calls
443 // are made before a canvas is created.
444 {
445 // Test passes by not asserting
446 SkSurface* surface = createSurface(surfaceType, context);
447 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000448 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000449 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000450 }
451 {
452 SkSurface* surface = createSurface(surfaceType, context);
453 SkAutoTUnref<SkSurface> aur_surface(surface);
454 SkImage* image1 = surface->newImageSnapshot();
455 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000456 SkDEBUGCODE(image1->validate();)
457 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000458 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000459 SkDEBUGCODE(image1->validate();)
460 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000461 SkImage* image2 = surface->newImageSnapshot();
462 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000463 SkDEBUGCODE(image2->validate();)
464 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000465 REPORTER_ASSERT(reporter, image1 != image2);
466 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000467
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000468}
junov@chromium.org995beb62013-03-28 13:49:22 +0000469
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000470DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000471 test_image(reporter);
472
junov@chromium.orgaf058352013-04-03 15:03:26 +0000473 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000474 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000475 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
476 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000477
reed67f2eb42014-12-10 06:54:06 -0800478 test_imagepeek(reporter, factory);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000479 test_canvaspeek(reporter, factory);
480
junov@chromium.orgb516a412013-05-01 22:49:59 +0000481#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000482 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700483 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700484 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
485 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
486 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
487 continue;
488 }
489 GrContext* context = factory->get(glCtxType);
bsalomon49f085d2014-09-05 13:34:00 -0700490 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700491 Test_crbug263329(reporter, kGpu_SurfaceType, context);
492 Test_crbug263329(reporter, kGpuScratch_SurfaceType, context);
493 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
494 TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context);
495 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
496 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context);
497 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
498 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
499 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
500 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
501 TestGetTexture(reporter, kGpu_SurfaceType, context);
502 TestGetTexture(reporter, kGpuScratch_SurfaceType, context);
503 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000504 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000505 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000506#endif
507}