blob: 65c0c3b53d03ece7734a165ed7eccaf7c2a8ad76 [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"
reed41e010c2015-06-09 12:16:53 -070010#include "SkDevice.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000011#include "SkImageEncoder.h"
bsalomon55812362015-06-10 08:49:28 -070012#include "SkImage_Base.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000013#include "SkRRect.h"
14#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000015#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000016#include "Test.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContextFactory.h"
20#else
21class GrContextFactory;
22class GrContext;
23#endif
24
25enum SurfaceType {
26 kRaster_SurfaceType,
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000027 kRasterDirect_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000028 kGpu_SurfaceType,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000029 kGpuScratch_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000030};
31
reed982542d2014-06-27 06:48:14 -070032static void release_storage(void* pixels, void* context) {
33 SkASSERT(pixels == context);
34 sk_free(pixels);
35}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000036
37static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context,
38 SkImageInfo* requestedInfo = NULL) {
reed982542d2014-06-27 06:48:14 -070039 static const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000040
41 if (requestedInfo) {
42 *requestedInfo = info;
43 }
junov@chromium.org995beb62013-03-28 13:49:22 +000044
45 switch (surfaceType) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000046 case kRaster_SurfaceType:
47 return SkSurface::NewRaster(info);
reed982542d2014-06-27 06:48:14 -070048 case kRasterDirect_SurfaceType: {
49 const size_t rowBytes = info.minRowBytes();
50 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
51 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes,
52 release_storage, storage);
53 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000054 case kGpu_SurfaceType:
bsalomonafe30052015-01-16 07:32:33 -080055 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, NULL);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000056 case kGpuScratch_SurfaceType:
bsalomonafe30052015-01-16 07:32:33 -080057 return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, NULL);
junov@chromium.org995beb62013-03-28 13:49:22 +000058 }
junov@chromium.org995beb62013-03-28 13:49:22 +000059 return NULL;
60}
61
reed@google.com4f7c6152014-02-06 14:11:56 +000062enum ImageType {
63 kRasterCopy_ImageType,
64 kRasterData_ImageType,
65 kGpu_ImageType,
reed@google.com4f7c6152014-02-06 14:11:56 +000066 kCodec_ImageType,
67};
reed@google.com999da9c2014-02-06 13:43:07 +000068
reedb2497c22014-12-31 12:31:43 -080069#include "SkImageGenerator.h"
70
71class EmptyGenerator : public SkImageGenerator {
reed3ef71e32015-03-19 08:31:14 -070072public:
73 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
reedb2497c22014-12-31 12:31:43 -080074};
75
76static void test_empty_image(skiatest::Reporter* reporter) {
77 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
78
79 REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterCopy(info, NULL, 0));
80 REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterData(info, NULL, 0));
81 REPORTER_ASSERT(reporter, NULL == SkImage::NewFromGenerator(SkNEW(EmptyGenerator)));
82}
83
84static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) {
85 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
86
87 REPORTER_ASSERT(reporter, NULL == SkSurface::NewRaster(info));
88 REPORTER_ASSERT(reporter, NULL == SkSurface::NewRasterDirect(info, NULL, 0));
89 if (ctx) {
bsalomonafe30052015-01-16 07:32:33 -080090 REPORTER_ASSERT(reporter, NULL ==
91 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, NULL));
reedb2497c22014-12-31 12:31:43 -080092 }
93}
94
bsalomone4579ad2015-04-08 08:38:40 -070095#if SK_SUPPORT_GPU
96static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext* ctx) {
97 if (NULL == ctx) {
98 return;
99 }
100 // Test the wrapped factory for SkSurface by creating a texture using ctx and then treat it as
101 // an external texture and wrap it in a SkSurface.
102
103 GrSurfaceDesc texDesc;
104 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
105 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
106 texDesc.fWidth = texDesc.fHeight = 100;
107 texDesc.fSampleCnt = 0;
108 texDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomond309e7a2015-04-30 14:18:54 -0700109 SkAutoTUnref<GrSurface> dummySurface(ctx->textureProvider()->createTexture(texDesc, false));
bsalomone4579ad2015-04-08 08:38:40 -0700110
111 REPORTER_ASSERT(reporter, dummySurface && dummySurface->asTexture() &&
112 dummySurface->asRenderTarget());
113 if (!dummySurface || !dummySurface->asTexture() || !dummySurface->asRenderTarget()) {
114 return;
115 }
116
117 GrBackendObject textureHandle = dummySurface->asTexture()->getTextureHandle();
118
119 GrBackendTextureDesc wrappedDesc;
120 wrappedDesc.fConfig = dummySurface->config();
121 wrappedDesc.fWidth = dummySurface->width();
122 wrappedDesc.fHeight = dummySurface->height();
123 wrappedDesc.fOrigin = dummySurface->origin();
124 wrappedDesc.fSampleCnt = dummySurface->asRenderTarget()->numSamples();
125 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
126 wrappedDesc.fTextureHandle = textureHandle;
127
128 SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrappedDesc, NULL));
129 REPORTER_ASSERT(reporter, surface);
130}
131#endif
132
133
reed@google.com999da9c2014-02-06 13:43:07 +0000134static void test_image(skiatest::Reporter* reporter) {
135 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
136 size_t rowBytes = info.minRowBytes();
137 size_t size = info.getSafeSize(rowBytes);
reed9594da12014-09-12 12:12:27 -0700138 SkData* data = SkData::NewUninitialized(size);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000139
mtkleinbbb61d72014-11-24 13:09:39 -0800140 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000141 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
mtkleinbbb61d72014-11-24 13:09:39 -0800142 REPORTER_ASSERT(reporter, !data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000143 image->unref();
mtkleinbbb61d72014-11-24 13:09:39 -0800144 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000145 data->unref();
146}
147
reed67f2eb42014-12-10 06:54:06 -0800148static SkImage* createImage(ImageType imageType, GrContext* context, SkColor color) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000149 const SkPMColor pmcolor = SkPreMultiplyColor(color);
150 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
151 const size_t rowBytes = info.minRowBytes();
reede5ea5002014-09-03 11:54:58 -0700152 const size_t size = rowBytes * info.height();
reed@google.com4f7c6152014-02-06 14:11:56 +0000153
reed9594da12014-09-12 12:12:27 -0700154 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
155 void* addr = data->writable_data();
reed@google.com4f7c6152014-02-06 14:11:56 +0000156 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
reed@google.com4f7c6152014-02-06 14:11:56 +0000157
158 switch (imageType) {
159 case kRasterCopy_ImageType:
160 return SkImage::NewRasterCopy(info, addr, rowBytes);
161 case kRasterData_ImageType:
162 return SkImage::NewRasterData(info, data, rowBytes);
reed67f2eb42014-12-10 06:54:06 -0800163 case kGpu_ImageType: {
bsalomonafe30052015-01-16 07:32:33 -0800164 SkAutoTUnref<SkSurface> surf(
165 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0));
reed67f2eb42014-12-10 06:54:06 -0800166 surf->getCanvas()->clear(color);
167 return surf->newImageSnapshot();
168 }
reed@google.com4f7c6152014-02-06 14:11:56 +0000169 case kCodec_ImageType: {
170 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000171 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000172 SkAutoTUnref<SkData> src(
reed67f2eb42014-12-10 06:54:06 -0800173 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
reed5965c8a2015-01-07 18:04:45 -0800174 return SkImage::NewFromData(src);
reed@google.com4f7c6152014-02-06 14:11:56 +0000175 }
176 }
177 SkASSERT(false);
178 return NULL;
179}
180
reed96472de2014-12-10 09:53:42 -0800181static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
182 sk_memset32(pixels, color, count);
183}
184static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
185 for (int i = 0; i < count; ++i) {
186 if (pixels[i] != expected) {
187 return false;
188 }
189 }
190 return true;
191}
192
193static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image,
194 SkPMColor expected) {
195 const SkPMColor notExpected = ~expected;
196
197 const int w = 2, h = 2;
198 const size_t rowBytes = w * sizeof(SkPMColor);
199 SkPMColor pixels[w*h];
200
201 SkImageInfo info;
202
203 info = SkImageInfo::MakeUnknown(w, h);
204 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
205
206 // out-of-bounds should fail
207 info = SkImageInfo::MakeN32Premul(w, h);
208 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
209 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
210 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
211 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
212
213 // top-left should succeed
214 set_pixels(pixels, w*h, notExpected);
215 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
216 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
217
218 // bottom-right should succeed
219 set_pixels(pixels, w*h, notExpected);
220 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
221 image->width() - w, image->height() - h));
222 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
223
224 // partial top-left should succeed
225 set_pixels(pixels, w*h, notExpected);
226 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
227 REPORTER_ASSERT(reporter, pixels[3] == expected);
228 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
229
230 // partial bottom-right should succeed
231 set_pixels(pixels, w*h, notExpected);
232 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
233 image->width() - 1, image->height() - 1));
234 REPORTER_ASSERT(reporter, pixels[0] == expected);
235 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
236}
237
reed67f2eb42014-12-10 06:54:06 -0800238static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000239 static const struct {
240 ImageType fType;
241 bool fPeekShouldSucceed;
reed67f2eb42014-12-10 06:54:06 -0800242 const char* fName;
reed@google.com4f7c6152014-02-06 14:11:56 +0000243 } gRec[] = {
reed67f2eb42014-12-10 06:54:06 -0800244 { kRasterCopy_ImageType, true, "RasterCopy" },
245 { kRasterData_ImageType, true, "RasterData" },
246 { kGpu_ImageType, false, "Gpu" },
247 { kCodec_ImageType, false, "Codec" },
reed@google.com4f7c6152014-02-06 14:11:56 +0000248 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000249
reed@google.com4f7c6152014-02-06 14:11:56 +0000250 const SkColor color = SK_ColorRED;
251 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000252
reed67f2eb42014-12-10 06:54:06 -0800253 GrContext* ctx = NULL;
254#if SK_SUPPORT_GPU
255 ctx = factory->get(GrContextFactory::kNative_GLContextType);
senorblancoc8e93402015-04-21 07:20:36 -0700256 if (NULL == ctx) {
senorblanco84bfd392015-04-21 06:59:17 -0700257 return;
258 }
reed67f2eb42014-12-10 06:54:06 -0800259#endif
260
reed@google.com4f7c6152014-02-06 14:11:56 +0000261 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
262 SkImageInfo info;
263 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000264
reed67f2eb42014-12-10 06:54:06 -0800265 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, ctx, color));
reed@google.com4f7c6152014-02-06 14:11:56 +0000266 if (!image.get()) {
reed67f2eb42014-12-10 06:54:06 -0800267 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
reed@google.com4f7c6152014-02-06 14:11:56 +0000268 continue; // gpu may not be enabled
269 }
270 const void* addr = image->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700271 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000272 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
273 if (success) {
reede5ea5002014-09-03 11:54:58 -0700274 REPORTER_ASSERT(reporter, 10 == info.width());
275 REPORTER_ASSERT(reporter, 10 == info.height());
276 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
277 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
278 kOpaque_SkAlphaType == info.alphaType());
reed@google.com4f7c6152014-02-06 14:11:56 +0000279 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
280 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
281 }
reed96472de2014-12-10 09:53:42 -0800282
283 test_image_readpixels(reporter, image, pmcolor);
reed@google.com4f7c6152014-02-06 14:11:56 +0000284 }
285}
286
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000287static void test_canvaspeek(skiatest::Reporter* reporter,
288 GrContextFactory* factory) {
289 static const struct {
290 SurfaceType fType;
291 bool fPeekShouldSucceed;
292 } gRec[] = {
293 { kRaster_SurfaceType, true },
294 { kRasterDirect_SurfaceType, true },
295#if SK_SUPPORT_GPU
296 { kGpu_SurfaceType, false },
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000297 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000298#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000299 };
300
301 const SkColor color = SK_ColorRED;
302 const SkPMColor pmcolor = SkPreMultiplyColor(color);
303
bsalomone904c092014-07-17 10:50:59 -0700304 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000305#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700306 cnt = GrContextFactory::kGLContextTypeCnt;
307#else
308 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000309#endif
310
bsalomone904c092014-07-17 10:50:59 -0700311 for (int i= 0; i < cnt; ++i) {
312 GrContext* context = NULL;
313#if SK_SUPPORT_GPU
314 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
315 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
316 continue;
317 }
318 context = factory->get(glCtxType);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000319
bsalomone904c092014-07-17 10:50:59 -0700320 if (NULL == context) {
321 continue;
322 }
323#endif
324 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
325 SkImageInfo info, requestInfo;
326 size_t rowBytes;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000327
bsalomone904c092014-07-17 10:50:59 -0700328 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context,
329 &requestInfo));
330 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000331
bsalomone904c092014-07-17 10:50:59 -0700332 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700333 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700334 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000335
bsalomone904c092014-07-17 10:50:59 -0700336 SkImageInfo info2;
337 size_t rb2;
338 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000339
bsalomone904c092014-07-17 10:50:59 -0700340 if (success) {
341 REPORTER_ASSERT(reporter, requestInfo == info);
342 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
343 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
344
345 REPORTER_ASSERT(reporter, addr2 == addr);
346 REPORTER_ASSERT(reporter, info2 == info);
347 REPORTER_ASSERT(reporter, rb2 == rowBytes);
348 } else {
349 REPORTER_ASSERT(reporter, NULL == addr2);
350 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000351 }
352 }
353}
354
reed41e010c2015-06-09 12:16:53 -0700355// For compatibility with clients that still call accessBitmap(), we need to ensure that we bump
356// the bitmap's genID when we draw to it, else they won't know it has new values. When they are
357// exclusively using surface/image, and we can hide accessBitmap from device, we can remove this
358// test.
359static void test_accessPixels(skiatest::Reporter* reporter, GrContextFactory* factory) {
360 static const struct {
361 SurfaceType fType;
362 bool fPeekShouldSucceed;
363 } gRec[] = {
364 { kRaster_SurfaceType, true },
365 { kRasterDirect_SurfaceType, true },
366#if SK_SUPPORT_GPU
367 { kGpu_SurfaceType, false },
368 { kGpuScratch_SurfaceType, false },
369#endif
370 };
371
372 int cnt;
373#if SK_SUPPORT_GPU
374 cnt = GrContextFactory::kGLContextTypeCnt;
375#else
376 cnt = 1;
377#endif
378
379 for (int i= 0; i < cnt; ++i) {
380 GrContext* context = NULL;
381#if SK_SUPPORT_GPU
382 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
383 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
384 continue;
385 }
386 context = factory->get(glCtxType);
387
388 if (NULL == context) {
389 continue;
390 }
391#endif
392 for (size_t j = 0; j < SK_ARRAY_COUNT(gRec); ++j) {
393 SkImageInfo info, requestInfo;
394
395 SkAutoTUnref<SkSurface> surface(createSurface(gRec[j].fType, context,
396 &requestInfo));
397 SkCanvas* canvas = surface->getCanvas();
398 canvas->clear(0);
399
400 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
401 SkBitmap bm = device->accessBitmap(false);
402 uint32_t genID0 = bm.getGenerationID();
403 // Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write)
404 canvas->drawColor(SK_ColorBLUE);
405 // Now check that we get a different genID
406 uint32_t genID1 = bm.getGenerationID();
407 REPORTER_ASSERT(reporter, genID0 != genID1);
408 }
409 }
410}
411
junov@chromium.org995beb62013-03-28 13:49:22 +0000412static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
413 GrContext* context) {
414 // Verify that the right canvas commands trigger a copy on write
415 SkSurface* surface = createSurface(surfaceType, context);
416 SkAutoTUnref<SkSurface> aur_surface(surface);
417 SkCanvas* canvas = surface->getCanvas();
418
419 const SkRect testRect =
420 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
421 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000422 SkPath testPath;
423 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
424 SkIntToScalar(2), SkIntToScalar(1)));
425
426 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
427
428 SkRegion testRegion;
429 testRegion.setRect(testIRect);
430
431
432 const SkColor testColor = 0x01020304;
433 const SkPaint testPaint;
434 const SkPoint testPoints[3] = {
435 {SkIntToScalar(0), SkIntToScalar(0)},
436 {SkIntToScalar(2), SkIntToScalar(1)},
437 {SkIntToScalar(0), SkIntToScalar(2)}
438 };
439 const size_t testPointCount = 3;
440
441 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000442 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000443 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000444
445 SkRRect testRRect;
446 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
447
448 SkString testText("Hello World");
449 const SkPoint testPoints2[] = {
450 { SkIntToScalar(0), SkIntToScalar(1) },
451 { SkIntToScalar(1), SkIntToScalar(1) },
452 { SkIntToScalar(2), SkIntToScalar(1) },
453 { SkIntToScalar(3), SkIntToScalar(1) },
454 { SkIntToScalar(4), SkIntToScalar(1) },
455 { SkIntToScalar(5), SkIntToScalar(1) },
456 { SkIntToScalar(6), SkIntToScalar(1) },
457 { SkIntToScalar(7), SkIntToScalar(1) },
458 { SkIntToScalar(8), SkIntToScalar(1) },
459 { SkIntToScalar(9), SkIntToScalar(1) },
460 { SkIntToScalar(10), SkIntToScalar(1) },
461 };
462
463#define EXPECT_COPY_ON_WRITE(command) \
464 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000465 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000466 SkAutoTUnref<SkImage> aur_before(imageBefore); \
467 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000468 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000469 SkAutoTUnref<SkImage> aur_after(imageAfter); \
470 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
471 }
472
473 EXPECT_COPY_ON_WRITE(clear(testColor))
474 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
475 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
476 testPaint))
477 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
478 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
479 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
480 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
481 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
482 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
junov@chromium.org995beb62013-03-28 13:49:22 +0000483 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
484 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
485 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
486 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
487 testPaint))
488 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
489 testPaint))
490}
491
junov@chromium.orgaf058352013-04-03 15:03:26 +0000492static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
493 SurfaceType surfaceType,
494 GrContext* context) {
495 // This test succeeds by not triggering an assertion.
496 // The test verifies that the surface remains writable (usable) after
497 // acquiring and releasing a snapshot without triggering a copy on write.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000498 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000499 SkCanvas* canvas = surface->getCanvas();
500 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000501 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000502 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000503}
junov@chromium.orgda904742013-05-01 22:38:16 +0000504
junov@chromium.orgb516a412013-05-01 22:49:59 +0000505#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000506static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000507 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000508 GrContext* context) {
509 // This is a regression test for crbug.com/263329
510 // Bug was caused by onCopyOnWrite releasing the old surface texture
511 // back to the scratch texture pool even though the texture is used
512 // by and active SkImage_Gpu.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000513 SkAutoTUnref<SkSurface> surface1(createSurface(surfaceType, context));
514 SkAutoTUnref<SkSurface> surface2(createSurface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000515 SkCanvas* canvas1 = surface1->getCanvas();
516 SkCanvas* canvas2 = surface2->getCanvas();
517 canvas1->clear(1);
518 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
519 // Trigger copy on write, new backing is a scratch texture
520 canvas1->clear(2);
521 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
522 // Trigger copy on write, old backing should not be returned to scratch
523 // pool because it is held by image2
524 canvas1->clear(3);
525
526 canvas2->clear(4);
527 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
528 // Trigger copy on write on surface2. The new backing store should not
529 // be recycling a texture that is held by an existing image.
530 canvas2->clear(5);
531 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
bsalomon55812362015-06-10 08:49:28 -0700532 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000533 // The following assertion checks crbug.com/263329
bsalomon55812362015-06-10 08:49:28 -0700534 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
535 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
536 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
537 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
538 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000539}
540
junov@chromium.orgda904742013-05-01 22:38:16 +0000541static void TestGetTexture(skiatest::Reporter* reporter,
542 SurfaceType surfaceType,
543 GrContext* context) {
544 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
545 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
bsalomon55812362015-06-10 08:49:28 -0700546 GrTexture* texture = as_IB(image)->getTexture();
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000547 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700548 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000549 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
550 } else {
551 REPORTER_ASSERT(reporter, NULL == texture);
552 }
553 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon55812362015-06-10 08:49:28 -0700554 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000555}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800556
bsalomon3582d3e2015-02-13 14:20:05 -0800557#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800558#include "SkGpuDevice.h"
559#include "SkImage_Gpu.h"
560#include "SkSurface_Gpu.h"
561
562SkSurface::Budgeted is_budgeted(SkSurface* surf) {
bsalomon3582d3e2015-02-13 14:20:05 -0800563 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800564 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
565}
566
567SkSurface::Budgeted is_budgeted(SkImage* image) {
bsalomon3582d3e2015-02-13 14:20:05 -0800568 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800569 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
570}
571
572static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context) {
573 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
574 for (int i = 0; i < 2; ++i) {
575 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
576 for (int j = 0; j < 2; ++j) {
577 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
578 SkAutoTUnref<SkSurface>
579 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0));
580 SkASSERT(surface);
581 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
582
mtklein31ff2982015-01-24 11:27:27 -0800583 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800584
585 // Initially the image shares a texture with the surface, and the surface decides
586 // whether it is budgeted or not.
587 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
588 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
589
590 // Now trigger copy-on-write
591 surface->getCanvas()->clear(SK_ColorBLUE);
592
593 // They don't share a texture anymore. They should each have made their own budget
594 // decision.
595 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
596 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
597 }
598 }
599}
600
junov@chromium.orgb516a412013-05-01 22:49:59 +0000601#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000602
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000603static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
604 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000605 GrContext* context,
606 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000607 // Verifies the robustness of SkSurface for handling use cases where calls
608 // are made before a canvas is created.
609 {
610 // Test passes by not asserting
611 SkSurface* surface = createSurface(surfaceType, context);
612 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000613 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000614 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000615 }
616 {
617 SkSurface* surface = createSurface(surfaceType, context);
618 SkAutoTUnref<SkSurface> aur_surface(surface);
619 SkImage* image1 = surface->newImageSnapshot();
620 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000621 SkDEBUGCODE(image1->validate();)
622 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000623 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000624 SkDEBUGCODE(image1->validate();)
625 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000626 SkImage* image2 = surface->newImageSnapshot();
627 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000628 SkDEBUGCODE(image2->validate();)
629 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000630 REPORTER_ASSERT(reporter, image1 != image2);
631 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000632
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000633}
junov@chromium.org995beb62013-03-28 13:49:22 +0000634
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000635DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000636 test_image(reporter);
637
junov@chromium.orgaf058352013-04-03 15:03:26 +0000638 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000639 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000640 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
641 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000642
reedb2497c22014-12-31 12:31:43 -0800643 test_empty_image(reporter);
644 test_empty_surface(reporter, NULL);
645
reed67f2eb42014-12-10 06:54:06 -0800646 test_imagepeek(reporter, factory);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000647 test_canvaspeek(reporter, factory);
648
reed41e010c2015-06-09 12:16:53 -0700649 test_accessPixels(reporter, factory);
650
junov@chromium.orgb516a412013-05-01 22:49:59 +0000651#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000652 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700653 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700654 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
655 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
656 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
657 continue;
658 }
659 GrContext* context = factory->get(glCtxType);
bsalomon49f085d2014-09-05 13:34:00 -0700660 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700661 Test_crbug263329(reporter, kGpu_SurfaceType, context);
662 Test_crbug263329(reporter, kGpuScratch_SurfaceType, context);
663 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
664 TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context);
665 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
666 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context);
667 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
668 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
669 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
670 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
671 TestGetTexture(reporter, kGpu_SurfaceType, context);
672 TestGetTexture(reporter, kGpuScratch_SurfaceType, context);
reedb2497c22014-12-31 12:31:43 -0800673 test_empty_surface(reporter, context);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800674 test_surface_budget(reporter, context);
bsalomone4579ad2015-04-08 08:38:40 -0700675 test_wrapped_texture_surface(reporter, context);
bsalomone904c092014-07-17 10:50:59 -0700676 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000677 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000678 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000679#endif
680}
reed8b26b992015-05-07 15:36:17 -0700681
682#if SK_SUPPORT_GPU
683static SkImage* make_desc_image(GrContext* ctx, int w, int h, GrBackendObject texID, bool doCopy) {
684 GrBackendTextureDesc desc;
685 desc.fConfig = kSkia8888_GrPixelConfig;
686 // need to be a rendertarget for now...
687 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
688 desc.fWidth = w;
689 desc.fHeight = h;
690 desc.fSampleCnt = 0;
691 desc.fTextureHandle = texID;
692 return doCopy ? SkImage::NewFromTextureCopy(ctx, desc) : SkImage::NewFromTexture(ctx, desc);
693}
694
695static void test_image_color(skiatest::Reporter* reporter, SkImage* image, SkPMColor expected) {
696 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
697 SkPMColor pixel;
698 REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0, 0));
699 REPORTER_ASSERT(reporter, pixel == expected);
700}
701
702DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) {
703 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
704 if (!ctx) {
705 REPORTER_ASSERT(reporter, false);
706 return;
707 }
708 GrTextureProvider* provider = ctx->textureProvider();
709
710 const int w = 10;
711 const int h = 10;
712 SkPMColor storage[w * h];
713 const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED);
714 sk_memset32(storage, expected0, w * h);
715
716 GrSurfaceDesc desc;
717 desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget for readpixels();
718 desc.fOrigin = kDefault_GrSurfaceOrigin;
719 desc.fWidth = w;
720 desc.fHeight = h;
721 desc.fConfig = kSkia8888_GrPixelConfig;
722 desc.fSampleCnt = 0;
723
724 SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w * 4));
725 if (!tex) {
726 REPORTER_ASSERT(reporter, false);
727 return;
728 }
729
730 GrBackendObject srcTex = tex->getTextureHandle();
731 SkAutoTUnref<SkImage> refImg(make_desc_image(ctx, w, h, srcTex, false));
732 SkAutoTUnref<SkImage> cpyImg(make_desc_image(ctx, w, h, srcTex, true));
733
734 test_image_color(reporter, refImg, expected0);
735 test_image_color(reporter, cpyImg, expected0);
736
737 // Now lets jam new colors into our "external" texture, and see if the images notice
738 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE);
739 sk_memset32(storage, expected1, w * h);
740 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kFlushWrites_PixelOp);
741
742 // We expect the ref'd image to see the new color, but cpy'd one should still see the old color
743 test_image_color(reporter, refImg, expected1);
744 test_image_color(reporter, cpyImg, expected0);
745}
746#endif