blob: dcf9a859dd9639a6ef837e415c3cabd330403b5d [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"
10#include "SkImageEncoder.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000011#include "SkRRect.h"
12#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000013#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000014#include "Test.h"
15
16#if SK_SUPPORT_GPU
17#include "GrContextFactory.h"
18#else
19class GrContextFactory;
20class GrContext;
21#endif
22
23enum SurfaceType {
24 kRaster_SurfaceType,
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000025 kRasterDirect_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000026 kGpu_SurfaceType,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000027 kGpuScratch_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000028};
29
reed982542d2014-06-27 06:48:14 -070030static void release_storage(void* pixels, void* context) {
31 SkASSERT(pixels == context);
32 sk_free(pixels);
33}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000034
35static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context,
36 SkImageInfo* requestedInfo = NULL) {
reed982542d2014-06-27 06:48:14 -070037 static const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000038
39 if (requestedInfo) {
40 *requestedInfo = info;
41 }
junov@chromium.org995beb62013-03-28 13:49:22 +000042
43 switch (surfaceType) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000044 case kRaster_SurfaceType:
45 return SkSurface::NewRaster(info);
reed982542d2014-06-27 06:48:14 -070046 case kRasterDirect_SurfaceType: {
47 const size_t rowBytes = info.minRowBytes();
48 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
49 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes,
50 release_storage, storage);
51 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000052 case kGpu_SurfaceType:
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000053#if SK_SUPPORT_GPU
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000054 return context ? SkSurface::NewRenderTarget(context, info) : NULL;
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000055#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000056 break;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000057 case kGpuScratch_SurfaceType:
58#if SK_SUPPORT_GPU
59 return context ? SkSurface::NewScratchRenderTarget(context, info) : NULL;
60#endif
61 break;
junov@chromium.org995beb62013-03-28 13:49:22 +000062 }
junov@chromium.org995beb62013-03-28 13:49:22 +000063 return NULL;
64}
65
reed@google.com4f7c6152014-02-06 14:11:56 +000066enum ImageType {
67 kRasterCopy_ImageType,
68 kRasterData_ImageType,
69 kGpu_ImageType,
reed@google.com4f7c6152014-02-06 14:11:56 +000070 kCodec_ImageType,
71};
reed@google.com999da9c2014-02-06 13:43:07 +000072
73static void test_image(skiatest::Reporter* reporter) {
74 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
75 size_t rowBytes = info.minRowBytes();
76 size_t size = info.getSafeSize(rowBytes);
reed9594da12014-09-12 12:12:27 -070077 SkData* data = SkData::NewUninitialized(size);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000078
Mike Klein874a62a2014-07-09 09:04:07 -040079 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
reed@google.com999da9c2014-02-06 13:43:07 +000080 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
Mike Klein874a62a2014-07-09 09:04:07 -040081 REPORTER_ASSERT(reporter, 2 == data->getRefCnt());
reed@google.com999da9c2014-02-06 13:43:07 +000082 image->unref();
Mike Klein874a62a2014-07-09 09:04:07 -040083 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
reed@google.com999da9c2014-02-06 13:43:07 +000084 data->unref();
85}
86
reed@google.com4f7c6152014-02-06 14:11:56 +000087static SkImage* createImage(ImageType imageType, GrContext* context,
88 SkColor color) {
89 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);
103 case kGpu_ImageType:
104 return NULL; // TODO
reed@google.com4f7c6152014-02-06 14:11:56 +0000105 case kCodec_ImageType: {
106 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000107 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000108 SkAutoTUnref<SkData> src(
109 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type,
110 100));
111 return SkImage::NewEncodedData(src);
112 }
113 }
114 SkASSERT(false);
115 return NULL;
116}
117
118static void test_imagepeek(skiatest::Reporter* reporter) {
119 static const struct {
120 ImageType fType;
121 bool fPeekShouldSucceed;
122 } gRec[] = {
123 { kRasterCopy_ImageType, true },
124 { kRasterData_ImageType, true },
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000125 { kGpu_ImageType, false },
reed@google.com4f7c6152014-02-06 14:11:56 +0000126 { kCodec_ImageType, false },
127 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000128
reed@google.com4f7c6152014-02-06 14:11:56 +0000129 const SkColor color = SK_ColorRED;
130 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000131
reed@google.com4f7c6152014-02-06 14:11:56 +0000132 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
133 SkImageInfo info;
134 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000135
reed@google.com4f7c6152014-02-06 14:11:56 +0000136 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color));
137 if (!image.get()) {
138 continue; // gpu may not be enabled
139 }
140 const void* addr = image->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700141 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000142 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
143 if (success) {
reede5ea5002014-09-03 11:54:58 -0700144 REPORTER_ASSERT(reporter, 10 == info.width());
145 REPORTER_ASSERT(reporter, 10 == info.height());
146 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
147 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
148 kOpaque_SkAlphaType == info.alphaType());
reed@google.com4f7c6152014-02-06 14:11:56 +0000149 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
150 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
151 }
152 }
153}
154
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000155static void test_canvaspeek(skiatest::Reporter* reporter,
156 GrContextFactory* factory) {
157 static const struct {
158 SurfaceType fType;
159 bool fPeekShouldSucceed;
160 } gRec[] = {
161 { kRaster_SurfaceType, true },
162 { kRasterDirect_SurfaceType, true },
163#if SK_SUPPORT_GPU
164 { kGpu_SurfaceType, false },
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000165 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000166#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000167 };
168
169 const SkColor color = SK_ColorRED;
170 const SkPMColor pmcolor = SkPreMultiplyColor(color);
171
bsalomone904c092014-07-17 10:50:59 -0700172 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000173#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700174 cnt = GrContextFactory::kGLContextTypeCnt;
175#else
176 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000177#endif
178
bsalomone904c092014-07-17 10:50:59 -0700179 for (int i= 0; i < cnt; ++i) {
180 GrContext* context = NULL;
181#if SK_SUPPORT_GPU
182 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
183 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
184 continue;
185 }
186 context = factory->get(glCtxType);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000187
bsalomone904c092014-07-17 10:50:59 -0700188 if (NULL == context) {
189 continue;
190 }
191#endif
192 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
193 SkImageInfo info, requestInfo;
194 size_t rowBytes;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000195
bsalomone904c092014-07-17 10:50:59 -0700196 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context,
197 &requestInfo));
198 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000199
bsalomone904c092014-07-17 10:50:59 -0700200 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700201 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700202 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000203
bsalomone904c092014-07-17 10:50:59 -0700204 SkImageInfo info2;
205 size_t rb2;
206 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000207
bsalomone904c092014-07-17 10:50:59 -0700208 if (success) {
209 REPORTER_ASSERT(reporter, requestInfo == info);
210 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
211 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
212
213 REPORTER_ASSERT(reporter, addr2 == addr);
214 REPORTER_ASSERT(reporter, info2 == info);
215 REPORTER_ASSERT(reporter, rb2 == rowBytes);
216 } else {
217 REPORTER_ASSERT(reporter, NULL == addr2);
218 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000219 }
220 }
221}
222
junov@chromium.org995beb62013-03-28 13:49:22 +0000223static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
224 GrContext* context) {
225 // Verify that the right canvas commands trigger a copy on write
226 SkSurface* surface = createSurface(surfaceType, context);
227 SkAutoTUnref<SkSurface> aur_surface(surface);
228 SkCanvas* canvas = surface->getCanvas();
229
230 const SkRect testRect =
231 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
232 SkIntToScalar(4), SkIntToScalar(5));
233 SkMatrix testMatrix;
234 testMatrix.reset();
235 testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
236
237 SkPath testPath;
238 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
239 SkIntToScalar(2), SkIntToScalar(1)));
240
241 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
242
243 SkRegion testRegion;
244 testRegion.setRect(testIRect);
245
246
247 const SkColor testColor = 0x01020304;
248 const SkPaint testPaint;
249 const SkPoint testPoints[3] = {
250 {SkIntToScalar(0), SkIntToScalar(0)},
251 {SkIntToScalar(2), SkIntToScalar(1)},
252 {SkIntToScalar(0), SkIntToScalar(2)}
253 };
254 const size_t testPointCount = 3;
255
256 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000257 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000258 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000259
260 SkRRect testRRect;
261 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
262
263 SkString testText("Hello World");
264 const SkPoint testPoints2[] = {
265 { SkIntToScalar(0), SkIntToScalar(1) },
266 { SkIntToScalar(1), SkIntToScalar(1) },
267 { SkIntToScalar(2), SkIntToScalar(1) },
268 { SkIntToScalar(3), SkIntToScalar(1) },
269 { SkIntToScalar(4), SkIntToScalar(1) },
270 { SkIntToScalar(5), SkIntToScalar(1) },
271 { SkIntToScalar(6), SkIntToScalar(1) },
272 { SkIntToScalar(7), SkIntToScalar(1) },
273 { SkIntToScalar(8), SkIntToScalar(1) },
274 { SkIntToScalar(9), SkIntToScalar(1) },
275 { SkIntToScalar(10), SkIntToScalar(1) },
276 };
277
278#define EXPECT_COPY_ON_WRITE(command) \
279 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000280 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000281 SkAutoTUnref<SkImage> aur_before(imageBefore); \
282 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000283 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000284 SkAutoTUnref<SkImage> aur_after(imageAfter); \
285 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
286 }
287
288 EXPECT_COPY_ON_WRITE(clear(testColor))
289 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
290 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
291 testPaint))
292 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
293 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
294 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
295 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
296 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
297 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
298 EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL))
299 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
300 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
301 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
302 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
303 testPaint))
304 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
305 testPaint))
306}
307
junov@chromium.orgaf058352013-04-03 15:03:26 +0000308static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
309 SurfaceType surfaceType,
310 GrContext* context) {
311 // This test succeeds by not triggering an assertion.
312 // The test verifies that the surface remains writable (usable) after
313 // acquiring and releasing a snapshot without triggering a copy on write.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000314 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000315 SkCanvas* canvas = surface->getCanvas();
316 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000317 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000318 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000319}
junov@chromium.orgda904742013-05-01 22:38:16 +0000320
junov@chromium.orgb516a412013-05-01 22:49:59 +0000321#if SK_SUPPORT_GPU
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000322static void TestSurfaceInCache(skiatest::Reporter* reporter,
323 SurfaceType surfaceType,
324 GrContext* context) {
325 context->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000326 int resourceCount;
327
328 context->getResourceCacheUsage(&resourceCount, NULL);
329 REPORTER_ASSERT(reporter, 0 == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000330 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
331 // Note: the stencil buffer is always cached, so kGpu_SurfaceType uses
332 // one cached resource, and kGpuScratch_SurfaceType uses two.
333 int expectedCachedResources = surfaceType == kGpuScratch_SurfaceType ? 2 : 1;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000334 context->getResourceCacheUsage(&resourceCount, NULL);
335 REPORTER_ASSERT(reporter, expectedCachedResources == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000336
337 // Verify that all the cached resources are locked in cache.
skia.committer@gmail.com99e5b522014-03-21 03:02:42 +0000338 context->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000339 context->getResourceCacheUsage(&resourceCount, NULL);
340 REPORTER_ASSERT(reporter, expectedCachedResources == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000341
342 // Verify that all the cached resources are unlocked upon surface release
343 surface.reset(0);
344 context->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000345 context->getResourceCacheUsage(&resourceCount, NULL);
346 REPORTER_ASSERT(reporter, 0 == resourceCount);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000347}
348
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000349static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000350 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000351 GrContext* context) {
352 // This is a regression test for crbug.com/263329
353 // Bug was caused by onCopyOnWrite releasing the old surface texture
354 // back to the scratch texture pool even though the texture is used
355 // by and active SkImage_Gpu.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000356 SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context));
357 SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000358 SkCanvas* canvas1 = surface1->getCanvas();
359 SkCanvas* canvas2 = surface2->getCanvas();
360 canvas1->clear(1);
361 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
362 // Trigger copy on write, new backing is a scratch texture
363 canvas1->clear(2);
364 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
365 // Trigger copy on write, old backing should not be returned to scratch
366 // pool because it is held by image2
367 canvas1->clear(3);
368
369 canvas2->clear(4);
370 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
371 // Trigger copy on write on surface2. The new backing store should not
372 // be recycling a texture that is held by an existing image.
373 canvas2->clear(5);
374 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
375 REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture());
376 // The following assertion checks crbug.com/263329
377 REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture());
378 REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture());
379 REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture());
380 REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture());
381 REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture());
382}
383
junov@chromium.orgda904742013-05-01 22:38:16 +0000384static void TestGetTexture(skiatest::Reporter* reporter,
385 SurfaceType surfaceType,
386 GrContext* context) {
387 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
388 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
389 GrTexture* texture = image->getTexture();
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000390 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700391 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000392 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
393 } else {
394 REPORTER_ASSERT(reporter, NULL == texture);
395 }
396 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
397 REPORTER_ASSERT(reporter, image->getTexture() == texture);
398}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000399#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000400
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000401static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
402 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000403 GrContext* context,
404 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000405 // Verifies the robustness of SkSurface for handling use cases where calls
406 // are made before a canvas is created.
407 {
408 // Test passes by not asserting
409 SkSurface* surface = createSurface(surfaceType, context);
410 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000411 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000412 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000413 }
414 {
415 SkSurface* surface = createSurface(surfaceType, context);
416 SkAutoTUnref<SkSurface> aur_surface(surface);
417 SkImage* image1 = surface->newImageSnapshot();
418 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000419 SkDEBUGCODE(image1->validate();)
420 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000421 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000422 SkDEBUGCODE(image1->validate();)
423 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000424 SkImage* image2 = surface->newImageSnapshot();
425 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000426 SkDEBUGCODE(image2->validate();)
427 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000428 REPORTER_ASSERT(reporter, image1 != image2);
429 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000430
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000431}
junov@chromium.org995beb62013-03-28 13:49:22 +0000432
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000433DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000434 test_image(reporter);
435
junov@chromium.orgaf058352013-04-03 15:03:26 +0000436 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000437 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000438 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
439 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000440
reed@google.com4f7c6152014-02-06 14:11:56 +0000441 test_imagepeek(reporter);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000442 test_canvaspeek(reporter, factory);
443
junov@chromium.orgb516a412013-05-01 22:49:59 +0000444#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000445 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700446 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700447 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
448 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
449 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
450 continue;
451 }
452 GrContext* context = factory->get(glCtxType);
bsalomon49f085d2014-09-05 13:34:00 -0700453 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700454 TestSurfaceInCache(reporter, kGpu_SurfaceType, context);
455 TestSurfaceInCache(reporter, kGpuScratch_SurfaceType, context);
456 Test_crbug263329(reporter, kGpu_SurfaceType, context);
457 Test_crbug263329(reporter, kGpuScratch_SurfaceType, context);
458 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
459 TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context);
460 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
461 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context);
462 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
463 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
464 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
465 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
466 TestGetTexture(reporter, kGpu_SurfaceType, context);
467 TestGetTexture(reporter, kGpuScratch_SurfaceType, context);
468 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000469 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000470 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000471#endif
472}