blob: 69c8b845ece6227761d900a1944b9d08f3a8aee0 [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
reed3716fd02014-09-21 09:39:55 -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
Mike Klein874a62a2014-07-09 09:04:07 -040080 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
reed@google.com999da9c2014-02-06 13:43:07 +000081 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
Mike Klein874a62a2014-07-09 09:04:07 -040082 REPORTER_ASSERT(reporter, 2 == data->getRefCnt());
reed@google.com999da9c2014-02-06 13:43:07 +000083 image->unref();
Mike Klein874a62a2014-07-09 09:04:07 -040084 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
reed@google.com999da9c2014-02-06 13:43:07 +000085 data->unref();
86}
87
reed@google.com4f7c6152014-02-06 14:11:56 +000088static 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();
reede5ea5002014-09-03 11:54:58 -070093 const size_t size = rowBytes * info.height();
reed@google.com4f7c6152014-02-06 14:11:56 +000094
reed9594da12014-09-12 12:12:27 -070095 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
96 void* addr = data->writable_data();
reed@google.com4f7c6152014-02-06 14:11:56 +000097 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
reed@google.com4f7c6152014-02-06 14:11:56 +000098
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.com4f7c6152014-02-06 14:11:56 +0000106 case kCodec_ImageType: {
107 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000108 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000109 SkAutoTUnref<SkData> src(
110 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type,
111 100));
piotaixr0d276f72014-09-18 11:55:14 -0700112 return SkImage::NewFromGenerator(
113 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
reed@google.com4f7c6152014-02-06 14:11:56 +0000114 }
115 }
116 SkASSERT(false);
117 return NULL;
118}
119
120static 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.orgd8a57af2014-03-19 21:19:16 +0000127 { kGpu_ImageType, false },
reed@google.com4f7c6152014-02-06 14:11:56 +0000128 { kCodec_ImageType, false },
129 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000130
reed@google.com4f7c6152014-02-06 14:11:56 +0000131 const SkColor color = SK_ColorRED;
132 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000133
reed@google.com4f7c6152014-02-06 14:11:56 +0000134 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
135 SkImageInfo info;
136 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000137
reed@google.com4f7c6152014-02-06 14:11:56 +0000138 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);
bsalomon49f085d2014-09-05 13:34:00 -0700143 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000144 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
145 if (success) {
reede5ea5002014-09-03 11:54:58 -0700146 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.com4f7c6152014-02-06 14:11:56 +0000151 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
152 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
153 }
154 }
155}
156
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000157static 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.orgd8a57af2014-03-19 21:19:16 +0000167 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000168#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000169 };
170
171 const SkColor color = SK_ColorRED;
172 const SkPMColor pmcolor = SkPreMultiplyColor(color);
173
bsalomone904c092014-07-17 10:50:59 -0700174 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000175#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700176 cnt = GrContextFactory::kGLContextTypeCnt;
177#else
178 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000179#endif
180
bsalomone904c092014-07-17 10:50:59 -0700181 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.orgc3bd8af2014-02-13 17:14:46 +0000189
bsalomone904c092014-07-17 10:50:59 -0700190 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.orgc3bd8af2014-02-13 17:14:46 +0000197
bsalomone904c092014-07-17 10:50:59 -0700198 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context,
199 &requestInfo));
200 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000201
bsalomone904c092014-07-17 10:50:59 -0700202 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700203 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700204 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000205
bsalomone904c092014-07-17 10:50:59 -0700206 SkImageInfo info2;
207 size_t rb2;
208 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000209
bsalomone904c092014-07-17 10:50:59 -0700210 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.orgc3bd8af2014-02-13 17:14:46 +0000221 }
222 }
223}
224
junov@chromium.org995beb62013-03-28 13:49:22 +0000225static 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.orgdeee4962014-02-13 14:41:43 +0000259 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000260 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000261
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.org5ee449a2013-04-12 20:20:50 +0000282 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000283 SkAutoTUnref<SkImage> aur_before(imageBefore); \
284 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000285 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000286 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.orgaf058352013-04-03 15:03:26 +0000310static 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.org4d24b742013-07-25 23:29:40 +0000316 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000317 SkCanvas* canvas = surface->getCanvas();
318 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000319 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000320 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000321}
junov@chromium.orgda904742013-05-01 22:38:16 +0000322
junov@chromium.orgb516a412013-05-01 22:49:59 +0000323#if SK_SUPPORT_GPU
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000324static void TestSurfaceInCache(skiatest::Reporter* reporter,
325 SurfaceType surfaceType,
326 GrContext* context) {
327 context->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000328 int resourceCount;
329
330 context->getResourceCacheUsage(&resourceCount, NULL);
331 REPORTER_ASSERT(reporter, 0 == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000332 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.org95c20032014-05-09 14:29:32 +0000336 context->getResourceCacheUsage(&resourceCount, NULL);
337 REPORTER_ASSERT(reporter, expectedCachedResources == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000338
339 // Verify that all the cached resources are locked in cache.
skia.committer@gmail.com99e5b522014-03-21 03:02:42 +0000340 context->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000341 context->getResourceCacheUsage(&resourceCount, NULL);
342 REPORTER_ASSERT(reporter, expectedCachedResources == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000343
344 // Verify that all the cached resources are unlocked upon surface release
345 surface.reset(0);
346 context->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000347 context->getResourceCacheUsage(&resourceCount, NULL);
348 REPORTER_ASSERT(reporter, 0 == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000349}
350
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000351static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000352 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000353 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.orgd8a57af2014-03-19 21:19:16 +0000358 SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context));
359 SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000360 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.orgda904742013-05-01 22:38:16 +0000386static 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.orgd8a57af2014-03-19 21:19:16 +0000392 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700393 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000394 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.orgb516a412013-05-01 22:49:59 +0000401#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000402
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000403static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
404 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000405 GrContext* context,
406 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000407 // 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.orgc4c98702013-04-22 14:28:01 +0000413 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000414 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000415 }
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.com03087072013-10-02 16:42:21 +0000421 SkDEBUGCODE(image1->validate();)
422 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000423 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000424 SkDEBUGCODE(image1->validate();)
425 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000426 SkImage* image2 = surface->newImageSnapshot();
427 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000428 SkDEBUGCODE(image2->validate();)
429 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000430 REPORTER_ASSERT(reporter, image1 != image2);
431 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000432
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000433}
junov@chromium.org995beb62013-03-28 13:49:22 +0000434
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000435DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000436 test_image(reporter);
437
junov@chromium.orgaf058352013-04-03 15:03:26 +0000438 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000439 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000440 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
441 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000442
reed@google.com4f7c6152014-02-06 14:11:56 +0000443 test_imagepeek(reporter);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000444 test_canvaspeek(reporter, factory);
445
junov@chromium.orgb516a412013-05-01 22:49:59 +0000446#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000447 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700448 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700449 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);
bsalomon49f085d2014-09-05 13:34:00 -0700455 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700456 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.com3bddb382013-11-12 13:51:03 +0000471 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000472 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000473#endif
474}