blob: a11d60f9633d5234f8b89bcb66a05329ed8955dd [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:
bsalomonafe30052015-01-16 07:32:33 -080053 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, NULL);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000054 case kGpuScratch_SurfaceType:
bsalomonafe30052015-01-16 07:32:33 -080055 return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, NULL);
junov@chromium.org995beb62013-03-28 13:49:22 +000056 }
junov@chromium.org995beb62013-03-28 13:49:22 +000057 return NULL;
58}
59
reed@google.com4f7c6152014-02-06 14:11:56 +000060enum ImageType {
61 kRasterCopy_ImageType,
62 kRasterData_ImageType,
63 kGpu_ImageType,
reed@google.com4f7c6152014-02-06 14:11:56 +000064 kCodec_ImageType,
65};
reed@google.com999da9c2014-02-06 13:43:07 +000066
reedb2497c22014-12-31 12:31:43 -080067#include "SkImageGenerator.h"
68
69class EmptyGenerator : public SkImageGenerator {
reed3ef71e32015-03-19 08:31:14 -070070public:
71 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
reedb2497c22014-12-31 12:31:43 -080072};
73
74static void test_empty_image(skiatest::Reporter* reporter) {
75 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
76
77 REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterCopy(info, NULL, 0));
78 REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterData(info, NULL, 0));
79 REPORTER_ASSERT(reporter, NULL == SkImage::NewFromGenerator(SkNEW(EmptyGenerator)));
80}
81
82static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) {
83 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
84
85 REPORTER_ASSERT(reporter, NULL == SkSurface::NewRaster(info));
86 REPORTER_ASSERT(reporter, NULL == SkSurface::NewRasterDirect(info, NULL, 0));
87 if (ctx) {
bsalomonafe30052015-01-16 07:32:33 -080088 REPORTER_ASSERT(reporter, NULL ==
89 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, NULL));
reedb2497c22014-12-31 12:31:43 -080090 }
91}
92
bsalomone4579ad2015-04-08 08:38:40 -070093#if SK_SUPPORT_GPU
94static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext* ctx) {
95 if (NULL == ctx) {
96 return;
97 }
98 // Test the wrapped factory for SkSurface by creating a texture using ctx and then treat it as
99 // an external texture and wrap it in a SkSurface.
100
101 GrSurfaceDesc texDesc;
102 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
103 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
104 texDesc.fWidth = texDesc.fHeight = 100;
105 texDesc.fSampleCnt = 0;
106 texDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
107 SkAutoTUnref<GrSurface> dummySurface(ctx->createTexture(texDesc, false));
108
109 REPORTER_ASSERT(reporter, dummySurface && dummySurface->asTexture() &&
110 dummySurface->asRenderTarget());
111 if (!dummySurface || !dummySurface->asTexture() || !dummySurface->asRenderTarget()) {
112 return;
113 }
114
115 GrBackendObject textureHandle = dummySurface->asTexture()->getTextureHandle();
116
117 GrBackendTextureDesc wrappedDesc;
118 wrappedDesc.fConfig = dummySurface->config();
119 wrappedDesc.fWidth = dummySurface->width();
120 wrappedDesc.fHeight = dummySurface->height();
121 wrappedDesc.fOrigin = dummySurface->origin();
122 wrappedDesc.fSampleCnt = dummySurface->asRenderTarget()->numSamples();
123 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
124 wrappedDesc.fTextureHandle = textureHandle;
125
126 SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrappedDesc, NULL));
127 REPORTER_ASSERT(reporter, surface);
128}
129#endif
130
131
reed@google.com999da9c2014-02-06 13:43:07 +0000132static void test_image(skiatest::Reporter* reporter) {
133 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
134 size_t rowBytes = info.minRowBytes();
135 size_t size = info.getSafeSize(rowBytes);
reed9594da12014-09-12 12:12:27 -0700136 SkData* data = SkData::NewUninitialized(size);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000137
mtkleinbbb61d72014-11-24 13:09:39 -0800138 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000139 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
mtkleinbbb61d72014-11-24 13:09:39 -0800140 REPORTER_ASSERT(reporter, !data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000141 image->unref();
mtkleinbbb61d72014-11-24 13:09:39 -0800142 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000143 data->unref();
144}
145
reed67f2eb42014-12-10 06:54:06 -0800146static SkImage* createImage(ImageType imageType, GrContext* context, SkColor color) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000147 const SkPMColor pmcolor = SkPreMultiplyColor(color);
148 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
149 const size_t rowBytes = info.minRowBytes();
reede5ea5002014-09-03 11:54:58 -0700150 const size_t size = rowBytes * info.height();
reed@google.com4f7c6152014-02-06 14:11:56 +0000151
reed9594da12014-09-12 12:12:27 -0700152 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
153 void* addr = data->writable_data();
reed@google.com4f7c6152014-02-06 14:11:56 +0000154 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
reed@google.com4f7c6152014-02-06 14:11:56 +0000155
156 switch (imageType) {
157 case kRasterCopy_ImageType:
158 return SkImage::NewRasterCopy(info, addr, rowBytes);
159 case kRasterData_ImageType:
160 return SkImage::NewRasterData(info, data, rowBytes);
reed67f2eb42014-12-10 06:54:06 -0800161 case kGpu_ImageType: {
bsalomonafe30052015-01-16 07:32:33 -0800162 SkAutoTUnref<SkSurface> surf(
163 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0));
reed67f2eb42014-12-10 06:54:06 -0800164 surf->getCanvas()->clear(color);
165 return surf->newImageSnapshot();
166 }
reed@google.com4f7c6152014-02-06 14:11:56 +0000167 case kCodec_ImageType: {
168 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000169 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000170 SkAutoTUnref<SkData> src(
reed67f2eb42014-12-10 06:54:06 -0800171 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
reed5965c8a2015-01-07 18:04:45 -0800172 return SkImage::NewFromData(src);
reed@google.com4f7c6152014-02-06 14:11:56 +0000173 }
174 }
175 SkASSERT(false);
176 return NULL;
177}
178
reed96472de2014-12-10 09:53:42 -0800179static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
180 sk_memset32(pixels, color, count);
181}
182static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
183 for (int i = 0; i < count; ++i) {
184 if (pixels[i] != expected) {
185 return false;
186 }
187 }
188 return true;
189}
190
191static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image,
192 SkPMColor expected) {
193 const SkPMColor notExpected = ~expected;
194
195 const int w = 2, h = 2;
196 const size_t rowBytes = w * sizeof(SkPMColor);
197 SkPMColor pixels[w*h];
198
199 SkImageInfo info;
200
201 info = SkImageInfo::MakeUnknown(w, h);
202 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
203
204 // out-of-bounds should fail
205 info = SkImageInfo::MakeN32Premul(w, h);
206 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
207 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
208 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
209 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
210
211 // top-left should succeed
212 set_pixels(pixels, w*h, notExpected);
213 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
214 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
215
216 // bottom-right should succeed
217 set_pixels(pixels, w*h, notExpected);
218 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
219 image->width() - w, image->height() - h));
220 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
221
222 // partial top-left should succeed
223 set_pixels(pixels, w*h, notExpected);
224 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
225 REPORTER_ASSERT(reporter, pixels[3] == expected);
226 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
227
228 // partial bottom-right should succeed
229 set_pixels(pixels, w*h, notExpected);
230 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
231 image->width() - 1, image->height() - 1));
232 REPORTER_ASSERT(reporter, pixels[0] == expected);
233 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
234}
235
reed67f2eb42014-12-10 06:54:06 -0800236static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000237 static const struct {
238 ImageType fType;
239 bool fPeekShouldSucceed;
reed67f2eb42014-12-10 06:54:06 -0800240 const char* fName;
reed@google.com4f7c6152014-02-06 14:11:56 +0000241 } gRec[] = {
reed67f2eb42014-12-10 06:54:06 -0800242 { kRasterCopy_ImageType, true, "RasterCopy" },
243 { kRasterData_ImageType, true, "RasterData" },
244 { kGpu_ImageType, false, "Gpu" },
245 { kCodec_ImageType, false, "Codec" },
reed@google.com4f7c6152014-02-06 14:11:56 +0000246 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000247
reed@google.com4f7c6152014-02-06 14:11:56 +0000248 const SkColor color = SK_ColorRED;
249 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000250
reed67f2eb42014-12-10 06:54:06 -0800251 GrContext* ctx = NULL;
252#if SK_SUPPORT_GPU
253 ctx = factory->get(GrContextFactory::kNative_GLContextType);
254#endif
255
reed@google.com4f7c6152014-02-06 14:11:56 +0000256 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
257 SkImageInfo info;
258 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000259
reed67f2eb42014-12-10 06:54:06 -0800260 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, ctx, color));
reed@google.com4f7c6152014-02-06 14:11:56 +0000261 if (!image.get()) {
reed67f2eb42014-12-10 06:54:06 -0800262 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
reed@google.com4f7c6152014-02-06 14:11:56 +0000263 continue; // gpu may not be enabled
264 }
265 const void* addr = image->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700266 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000267 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
268 if (success) {
reede5ea5002014-09-03 11:54:58 -0700269 REPORTER_ASSERT(reporter, 10 == info.width());
270 REPORTER_ASSERT(reporter, 10 == info.height());
271 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
272 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
273 kOpaque_SkAlphaType == info.alphaType());
reed@google.com4f7c6152014-02-06 14:11:56 +0000274 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
275 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
276 }
reed96472de2014-12-10 09:53:42 -0800277
278 test_image_readpixels(reporter, image, pmcolor);
reed@google.com4f7c6152014-02-06 14:11:56 +0000279 }
280}
281
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000282static void test_canvaspeek(skiatest::Reporter* reporter,
283 GrContextFactory* factory) {
284 static const struct {
285 SurfaceType fType;
286 bool fPeekShouldSucceed;
287 } gRec[] = {
288 { kRaster_SurfaceType, true },
289 { kRasterDirect_SurfaceType, true },
290#if SK_SUPPORT_GPU
291 { kGpu_SurfaceType, false },
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000292 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000293#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000294 };
295
296 const SkColor color = SK_ColorRED;
297 const SkPMColor pmcolor = SkPreMultiplyColor(color);
298
bsalomone904c092014-07-17 10:50:59 -0700299 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000300#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700301 cnt = GrContextFactory::kGLContextTypeCnt;
302#else
303 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000304#endif
305
bsalomone904c092014-07-17 10:50:59 -0700306 for (int i= 0; i < cnt; ++i) {
307 GrContext* context = NULL;
308#if SK_SUPPORT_GPU
309 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
310 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
311 continue;
312 }
313 context = factory->get(glCtxType);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000314
bsalomone904c092014-07-17 10:50:59 -0700315 if (NULL == context) {
316 continue;
317 }
318#endif
319 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
320 SkImageInfo info, requestInfo;
321 size_t rowBytes;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000322
bsalomone904c092014-07-17 10:50:59 -0700323 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context,
324 &requestInfo));
325 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000326
bsalomone904c092014-07-17 10:50:59 -0700327 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700328 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700329 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000330
bsalomone904c092014-07-17 10:50:59 -0700331 SkImageInfo info2;
332 size_t rb2;
333 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000334
bsalomone904c092014-07-17 10:50:59 -0700335 if (success) {
336 REPORTER_ASSERT(reporter, requestInfo == info);
337 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
338 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
339
340 REPORTER_ASSERT(reporter, addr2 == addr);
341 REPORTER_ASSERT(reporter, info2 == info);
342 REPORTER_ASSERT(reporter, rb2 == rowBytes);
343 } else {
344 REPORTER_ASSERT(reporter, NULL == addr2);
345 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000346 }
347 }
348}
349
junov@chromium.org995beb62013-03-28 13:49:22 +0000350static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
351 GrContext* context) {
352 // Verify that the right canvas commands trigger a copy on write
353 SkSurface* surface = createSurface(surfaceType, context);
354 SkAutoTUnref<SkSurface> aur_surface(surface);
355 SkCanvas* canvas = surface->getCanvas();
356
357 const SkRect testRect =
358 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
359 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000360 SkPath testPath;
361 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
362 SkIntToScalar(2), SkIntToScalar(1)));
363
364 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
365
366 SkRegion testRegion;
367 testRegion.setRect(testIRect);
368
369
370 const SkColor testColor = 0x01020304;
371 const SkPaint testPaint;
372 const SkPoint testPoints[3] = {
373 {SkIntToScalar(0), SkIntToScalar(0)},
374 {SkIntToScalar(2), SkIntToScalar(1)},
375 {SkIntToScalar(0), SkIntToScalar(2)}
376 };
377 const size_t testPointCount = 3;
378
379 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000380 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000381 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000382
383 SkRRect testRRect;
384 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
385
386 SkString testText("Hello World");
387 const SkPoint testPoints2[] = {
388 { SkIntToScalar(0), SkIntToScalar(1) },
389 { SkIntToScalar(1), SkIntToScalar(1) },
390 { SkIntToScalar(2), SkIntToScalar(1) },
391 { SkIntToScalar(3), SkIntToScalar(1) },
392 { SkIntToScalar(4), SkIntToScalar(1) },
393 { SkIntToScalar(5), SkIntToScalar(1) },
394 { SkIntToScalar(6), SkIntToScalar(1) },
395 { SkIntToScalar(7), SkIntToScalar(1) },
396 { SkIntToScalar(8), SkIntToScalar(1) },
397 { SkIntToScalar(9), SkIntToScalar(1) },
398 { SkIntToScalar(10), SkIntToScalar(1) },
399 };
400
401#define EXPECT_COPY_ON_WRITE(command) \
402 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000403 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000404 SkAutoTUnref<SkImage> aur_before(imageBefore); \
405 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000406 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000407 SkAutoTUnref<SkImage> aur_after(imageAfter); \
408 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
409 }
410
411 EXPECT_COPY_ON_WRITE(clear(testColor))
412 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
413 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
414 testPaint))
415 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
416 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
417 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
418 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
419 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
420 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
junov@chromium.org995beb62013-03-28 13:49:22 +0000421 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
422 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
423 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
424 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
425 testPaint))
426 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
427 testPaint))
428}
429
junov@chromium.orgaf058352013-04-03 15:03:26 +0000430static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
431 SurfaceType surfaceType,
432 GrContext* context) {
433 // This test succeeds by not triggering an assertion.
434 // The test verifies that the surface remains writable (usable) after
435 // acquiring and releasing a snapshot without triggering a copy on write.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000436 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000437 SkCanvas* canvas = surface->getCanvas();
438 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000439 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000440 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000441}
junov@chromium.orgda904742013-05-01 22:38:16 +0000442
junov@chromium.orgb516a412013-05-01 22:49:59 +0000443#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000444static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000445 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000446 GrContext* context) {
447 // This is a regression test for crbug.com/263329
448 // Bug was caused by onCopyOnWrite releasing the old surface texture
449 // back to the scratch texture pool even though the texture is used
450 // by and active SkImage_Gpu.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000451 SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context));
452 SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000453 SkCanvas* canvas1 = surface1->getCanvas();
454 SkCanvas* canvas2 = surface2->getCanvas();
455 canvas1->clear(1);
456 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
457 // Trigger copy on write, new backing is a scratch texture
458 canvas1->clear(2);
459 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
460 // Trigger copy on write, old backing should not be returned to scratch
461 // pool because it is held by image2
462 canvas1->clear(3);
463
464 canvas2->clear(4);
465 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
466 // Trigger copy on write on surface2. The new backing store should not
467 // be recycling a texture that is held by an existing image.
468 canvas2->clear(5);
469 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
470 REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture());
471 // The following assertion checks crbug.com/263329
472 REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture());
473 REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture());
474 REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture());
475 REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture());
476 REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture());
477}
478
junov@chromium.orgda904742013-05-01 22:38:16 +0000479static void TestGetTexture(skiatest::Reporter* reporter,
480 SurfaceType surfaceType,
481 GrContext* context) {
482 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
483 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
484 GrTexture* texture = image->getTexture();
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000485 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700486 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000487 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
488 } else {
489 REPORTER_ASSERT(reporter, NULL == texture);
490 }
491 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
492 REPORTER_ASSERT(reporter, image->getTexture() == texture);
493}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800494
bsalomon3582d3e2015-02-13 14:20:05 -0800495#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800496#include "SkGpuDevice.h"
497#include "SkImage_Gpu.h"
498#include "SkSurface_Gpu.h"
499
500SkSurface::Budgeted is_budgeted(SkSurface* surf) {
bsalomon3582d3e2015-02-13 14:20:05 -0800501 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800502 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
503}
504
505SkSurface::Budgeted is_budgeted(SkImage* image) {
bsalomon3582d3e2015-02-13 14:20:05 -0800506 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800507 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
508}
509
510static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context) {
511 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
512 for (int i = 0; i < 2; ++i) {
513 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
514 for (int j = 0; j < 2; ++j) {
515 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
516 SkAutoTUnref<SkSurface>
517 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0));
518 SkASSERT(surface);
519 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
520
mtklein31ff2982015-01-24 11:27:27 -0800521 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800522
523 // Initially the image shares a texture with the surface, and the surface decides
524 // whether it is budgeted or not.
525 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
526 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
527
528 // Now trigger copy-on-write
529 surface->getCanvas()->clear(SK_ColorBLUE);
530
531 // They don't share a texture anymore. They should each have made their own budget
532 // decision.
533 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
534 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
535 }
536 }
537}
538
junov@chromium.orgb516a412013-05-01 22:49:59 +0000539#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000540
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000541static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
542 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000543 GrContext* context,
544 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000545 // Verifies the robustness of SkSurface for handling use cases where calls
546 // are made before a canvas is created.
547 {
548 // Test passes by not asserting
549 SkSurface* surface = createSurface(surfaceType, context);
550 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000551 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000552 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000553 }
554 {
555 SkSurface* surface = createSurface(surfaceType, context);
556 SkAutoTUnref<SkSurface> aur_surface(surface);
557 SkImage* image1 = surface->newImageSnapshot();
558 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000559 SkDEBUGCODE(image1->validate();)
560 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000561 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000562 SkDEBUGCODE(image1->validate();)
563 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000564 SkImage* image2 = surface->newImageSnapshot();
565 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000566 SkDEBUGCODE(image2->validate();)
567 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000568 REPORTER_ASSERT(reporter, image1 != image2);
569 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000570
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000571}
junov@chromium.org995beb62013-03-28 13:49:22 +0000572
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000573DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000574 test_image(reporter);
575
junov@chromium.orgaf058352013-04-03 15:03:26 +0000576 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000577 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000578 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
579 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000580
reedb2497c22014-12-31 12:31:43 -0800581 test_empty_image(reporter);
582 test_empty_surface(reporter, NULL);
583
reed67f2eb42014-12-10 06:54:06 -0800584 test_imagepeek(reporter, factory);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000585 test_canvaspeek(reporter, factory);
586
junov@chromium.orgb516a412013-05-01 22:49:59 +0000587#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000588 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700589 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700590 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
591 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
592 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
593 continue;
594 }
595 GrContext* context = factory->get(glCtxType);
bsalomon49f085d2014-09-05 13:34:00 -0700596 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700597 Test_crbug263329(reporter, kGpu_SurfaceType, context);
598 Test_crbug263329(reporter, kGpuScratch_SurfaceType, context);
599 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
600 TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context);
601 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
602 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context);
603 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
604 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
605 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
606 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
607 TestGetTexture(reporter, kGpu_SurfaceType, context);
608 TestGetTexture(reporter, kGpuScratch_SurfaceType, context);
reedb2497c22014-12-31 12:31:43 -0800609 test_empty_surface(reporter, context);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800610 test_surface_budget(reporter, context);
bsalomone4579ad2015-04-08 08:38:40 -0700611 test_wrapped_texture_surface(reporter, context);
bsalomone904c092014-07-17 10:50:59 -0700612 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000613 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000614 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000615#endif
616}