blob: fac283d5b13a2559d75875577361c41a75ff4086 [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"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000014#include "SkRRect.h"
15#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000016#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000017#include "Test.h"
18
19#if SK_SUPPORT_GPU
20#include "GrContextFactory.h"
bsalomon7a617932015-06-16 08:07:16 -070021#include "GrTest.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000022#else
23class GrContextFactory;
24class GrContext;
25#endif
26
27enum SurfaceType {
28 kRaster_SurfaceType,
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000029 kRasterDirect_SurfaceType,
junov@chromium.org995beb62013-03-28 13:49:22 +000030 kGpu_SurfaceType,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000031 kGpuScratch_SurfaceType,
bsalomon74f681d2015-06-23 14:38:48 -070032
33 kLastSurfaceType = kGpuScratch_SurfaceType
junov@chromium.org995beb62013-03-28 13:49:22 +000034};
bsalomon74f681d2015-06-23 14:38:48 -070035static const int kSurfaceTypeCnt = kLastSurfaceType + 1;
junov@chromium.org995beb62013-03-28 13:49:22 +000036
reed982542d2014-06-27 06:48:14 -070037static void release_storage(void* pixels, void* context) {
38 SkASSERT(pixels == context);
39 sk_free(pixels);
40}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000041
bsalomon74f681d2015-06-23 14:38:48 -070042static SkSurface* create_surface(SurfaceType surfaceType, GrContext* context,
43 SkAlphaType at = kPremul_SkAlphaType,
44 SkImageInfo* requestedInfo = NULL) {
45 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000046
47 if (requestedInfo) {
48 *requestedInfo = info;
49 }
junov@chromium.org995beb62013-03-28 13:49:22 +000050
51 switch (surfaceType) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000052 case kRaster_SurfaceType:
53 return SkSurface::NewRaster(info);
reed982542d2014-06-27 06:48:14 -070054 case kRasterDirect_SurfaceType: {
55 const size_t rowBytes = info.minRowBytes();
56 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
57 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes,
58 release_storage, storage);
59 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000060 case kGpu_SurfaceType:
bsalomonafe30052015-01-16 07:32:33 -080061 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, NULL);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000062 case kGpuScratch_SurfaceType:
bsalomonafe30052015-01-16 07:32:33 -080063 return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, NULL);
junov@chromium.org995beb62013-03-28 13:49:22 +000064 }
junov@chromium.org995beb62013-03-28 13:49:22 +000065 return NULL;
66}
67
reed@google.com4f7c6152014-02-06 14:11:56 +000068enum ImageType {
69 kRasterCopy_ImageType,
70 kRasterData_ImageType,
reedde499882015-06-18 13:41:40 -070071 kRasterProc_ImageType,
reed@google.com4f7c6152014-02-06 14:11:56 +000072 kGpu_ImageType,
reed@google.com4f7c6152014-02-06 14:11:56 +000073 kCodec_ImageType,
74};
reed@google.com999da9c2014-02-06 13:43:07 +000075
reedb2497c22014-12-31 12:31:43 -080076#include "SkImageGenerator.h"
77
78class EmptyGenerator : public SkImageGenerator {
reed3ef71e32015-03-19 08:31:14 -070079public:
80 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
reedb2497c22014-12-31 12:31:43 -080081};
82
83static void test_empty_image(skiatest::Reporter* reporter) {
84 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
85
86 REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterCopy(info, NULL, 0));
87 REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterData(info, NULL, 0));
reedde499882015-06-18 13:41:40 -070088 REPORTER_ASSERT(reporter, NULL == SkImage::NewFromRaster(info, NULL, 0, NULL, NULL));
reedb2497c22014-12-31 12:31:43 -080089 REPORTER_ASSERT(reporter, NULL == SkImage::NewFromGenerator(SkNEW(EmptyGenerator)));
90}
91
92static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) {
93 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
94
95 REPORTER_ASSERT(reporter, NULL == SkSurface::NewRaster(info));
96 REPORTER_ASSERT(reporter, NULL == SkSurface::NewRasterDirect(info, NULL, 0));
97 if (ctx) {
bsalomonafe30052015-01-16 07:32:33 -080098 REPORTER_ASSERT(reporter, NULL ==
99 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, NULL));
reedb2497c22014-12-31 12:31:43 -0800100 }
101}
102
bsalomone4579ad2015-04-08 08:38:40 -0700103#if SK_SUPPORT_GPU
joshualitt81793412015-07-08 12:54:04 -0700104static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext* ctx) {
bsalomone4579ad2015-04-08 08:38:40 -0700105 if (NULL == ctx) {
106 return;
107 }
bsalomone4579ad2015-04-08 08:38:40 -0700108
jvanverth672bb7f2015-07-13 07:19:57 -0700109 const GrGpu* gpu = ctx->getGpu();
110 if (!gpu) {
bsalomone4579ad2015-04-08 08:38:40 -0700111 return;
112 }
bsalomon7a617932015-06-16 08:07:16 -0700113
jvanverth672bb7f2015-07-13 07:19:57 -0700114 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
joshualitt81793412015-07-08 12:54:04 -0700115 // a SkSurface.
joshualitt81793412015-07-08 12:54:04 -0700116 static const int kW = 100;
117 static const int kH = 100;
118 static const uint32_t kOrigColor = 0xFFAABBCC;
119 SkAutoTArray<uint32_t> pixels(kW * kH);
120 sk_memset32(pixels.get(), kOrigColor, kW * kH);
jvanverth88957922015-07-14 11:02:52 -0700121 GrBackendObject texID = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
122 kRGBA_8888_GrPixelConfig);
bsalomone4579ad2015-04-08 08:38:40 -0700123
joshualitt81793412015-07-08 12:54:04 -0700124 GrBackendTextureDesc wrappedDesc;
125 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
126 wrappedDesc.fWidth = kW;
127 wrappedDesc.fHeight = kH;
128 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
129 wrappedDesc.fSampleCnt = 0;
130 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
131 wrappedDesc.fTextureHandle = texID;
bsalomone4579ad2015-04-08 08:38:40 -0700132
joshualitt81793412015-07-08 12:54:04 -0700133 SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrappedDesc, NULL));
134 REPORTER_ASSERT(reporter, surface);
135 if (surface) {
136 // Validate that we can draw to the canvas and that the original texture color is preserved
137 // in pixels that aren't rendered to via the surface.
138 SkPaint paint;
139 static const SkColor kRectColor = ~kOrigColor | 0xFF000000;
140 paint.setColor(kRectColor);
141 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
142 paint);
143 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
144 surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0);
145 bool stop = false;
146 SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF),
147 (kOrigColor >> 0 & 0xFF),
148 (kOrigColor >> 8 & 0xFF),
149 (kOrigColor >> 16 & 0xFF));
150 SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
151 (kRectColor >> 16 & 0xFF),
152 (kRectColor >> 8 & 0xFF),
153 (kRectColor >> 0 & 0xFF));
154 for (int y = 0; y < kH/2 && !stop; ++y) {
155 for (int x = 0; x < kW && !stop; ++x) {
156 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
157 if (rectColorPM != pixels[x + y * kW]) {
158 stop = true;
bsalomon7a617932015-06-16 08:07:16 -0700159 }
160 }
161 }
joshualitt81793412015-07-08 12:54:04 -0700162 stop = false;
163 for (int y = kH/2; y < kH && !stop; ++y) {
164 for (int x = 0; x < kW && !stop; ++x) {
165 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
166 if (origColorPM != pixels[x + y * kW]) {
167 stop = true;
168 }
169 }
bsalomon7a617932015-06-16 08:07:16 -0700170 }
171 }
bsalomone4579ad2015-04-08 08:38:40 -0700172}
173#endif
174
175
reed@google.com999da9c2014-02-06 13:43:07 +0000176static void test_image(skiatest::Reporter* reporter) {
177 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
178 size_t rowBytes = info.minRowBytes();
179 size_t size = info.getSafeSize(rowBytes);
reed9594da12014-09-12 12:12:27 -0700180 SkData* data = SkData::NewUninitialized(size);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000181
mtkleinbbb61d72014-11-24 13:09:39 -0800182 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000183 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
mtkleinbbb61d72014-11-24 13:09:39 -0800184 REPORTER_ASSERT(reporter, !data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000185 image->unref();
mtkleinbbb61d72014-11-24 13:09:39 -0800186 REPORTER_ASSERT(reporter, data->unique());
reed@google.com999da9c2014-02-06 13:43:07 +0000187 data->unref();
188}
189
reedde499882015-06-18 13:41:40 -0700190// Want to ensure that our Release is called when the owning image is destroyed
191struct ReleaseDataContext {
192 skiatest::Reporter* fReporter;
193 SkData* fData;
194
195 static void Release(const void* pixels, void* context) {
196 ReleaseDataContext* state = (ReleaseDataContext*)context;
197 REPORTER_ASSERT(state->fReporter, state->fData);
198 state->fData->unref();
199 state->fData = NULL;
200 }
201};
202
reed09b2c932015-06-29 14:09:41 -0700203// May we (soon) eliminate the need to keep testing this, by hiding the bloody device!
204#include "SkDevice.h"
205static uint32_t get_legacy_gen_id(SkSurface* surf) {
206 SkBaseDevice* device = surf->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
207 return device->accessBitmap(false).getGenerationID();
208}
209
reeddca20ce2015-07-03 10:43:43 -0700210/*
211 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
212 * texture handle for writing.
213 *
214 * Note: this needs to be tested separately from checking newImageSnapshot, as calling that
215 * can also incidentally bump the genID (when a new backing surface is created).
216 */
joshualitt81793412015-07-08 12:54:04 -0700217template <class F>
218static void test_texture_handle_genID(skiatest::Reporter* reporter, SkSurface* surf, F f) {
reeddca20ce2015-07-03 10:43:43 -0700219 const uint32_t gen0 = get_legacy_gen_id(surf);
joshualitt81793412015-07-08 12:54:04 -0700220 f(surf, SkSurface::kFlushRead_BackendHandleAccess);
reeddca20ce2015-07-03 10:43:43 -0700221 const uint32_t gen1 = get_legacy_gen_id(surf);
222 REPORTER_ASSERT(reporter, gen0 == gen1);
223
joshualitt81793412015-07-08 12:54:04 -0700224 f(surf, SkSurface::kFlushWrite_BackendHandleAccess);
reeddca20ce2015-07-03 10:43:43 -0700225 const uint32_t gen2 = get_legacy_gen_id(surf);
226 REPORTER_ASSERT(reporter, gen0 != gen2);
227
joshualitt81793412015-07-08 12:54:04 -0700228 f(surf, SkSurface::kDiscardWrite_BackendHandleAccess);
reeddca20ce2015-07-03 10:43:43 -0700229 const uint32_t gen3 = get_legacy_gen_id(surf);
230 REPORTER_ASSERT(reporter, gen0 != gen3);
231 REPORTER_ASSERT(reporter, gen2 != gen3);
232}
233
joshualitt81793412015-07-08 12:54:04 -0700234template <class F>
235static void test_backend_handle(skiatest::Reporter* reporter, SkSurface* surf, F f) {
reedfa5e68e2015-06-29 07:37:01 -0700236 SkAutoTUnref<SkImage> image0(surf->newImageSnapshot());
joshualitt81793412015-07-08 12:54:04 -0700237 GrBackendObject obj = f(surf, SkSurface::kFlushRead_BackendHandleAccess);
reedfa5e68e2015-06-29 07:37:01 -0700238 REPORTER_ASSERT(reporter, obj != 0);
239 SkAutoTUnref<SkImage> image1(surf->newImageSnapshot());
240 // just read access should not affect the snapshot
241 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
242
joshualitt81793412015-07-08 12:54:04 -0700243 obj = f(surf, SkSurface::kFlushWrite_BackendHandleAccess);
reedfa5e68e2015-06-29 07:37:01 -0700244 REPORTER_ASSERT(reporter, obj != 0);
245 SkAutoTUnref<SkImage> image2(surf->newImageSnapshot());
246 // expect a new image, since we claimed we would write
247 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
248
joshualitt81793412015-07-08 12:54:04 -0700249 obj = f(surf, SkSurface::kDiscardWrite_BackendHandleAccess);
reedfa5e68e2015-06-29 07:37:01 -0700250 REPORTER_ASSERT(reporter, obj != 0);
251 SkAutoTUnref<SkImage> image3(surf->newImageSnapshot());
252 // expect a new(er) image, since we claimed we would write
253 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
254 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
255}
256
257static SkImage* create_image(skiatest::Reporter* reporter,
258 ImageType imageType, GrContext* context, SkColor color,
bsalomon74f681d2015-06-23 14:38:48 -0700259 ReleaseDataContext* releaseContext) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000260 const SkPMColor pmcolor = SkPreMultiplyColor(color);
261 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
262 const size_t rowBytes = info.minRowBytes();
reede5ea5002014-09-03 11:54:58 -0700263 const size_t size = rowBytes * info.height();
reed@google.com4f7c6152014-02-06 14:11:56 +0000264
reed9594da12014-09-12 12:12:27 -0700265 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
266 void* addr = data->writable_data();
reed@google.com4f7c6152014-02-06 14:11:56 +0000267 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
reed@google.com4f7c6152014-02-06 14:11:56 +0000268
269 switch (imageType) {
270 case kRasterCopy_ImageType:
271 return SkImage::NewRasterCopy(info, addr, rowBytes);
272 case kRasterData_ImageType:
273 return SkImage::NewRasterData(info, data, rowBytes);
reedde499882015-06-18 13:41:40 -0700274 case kRasterProc_ImageType:
275 SkASSERT(releaseContext);
276 releaseContext->fData = SkRef(data.get());
277 return SkImage::NewFromRaster(info, addr, rowBytes,
278 ReleaseDataContext::Release, releaseContext);
reed67f2eb42014-12-10 06:54:06 -0800279 case kGpu_ImageType: {
bsalomonafe30052015-01-16 07:32:33 -0800280 SkAutoTUnref<SkSurface> surf(
281 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0));
reed67f2eb42014-12-10 06:54:06 -0800282 surf->getCanvas()->clear(color);
joshualitt81793412015-07-08 12:54:04 -0700283 // test our backing texture / rendertarget while were here...
284 auto textureAccessorFunc =
285 [](SkSurface* surf, SkSurface::BackendHandleAccess access) -> GrBackendObject {
286 return surf->getTextureHandle(access); };
287 auto renderTargetAccessorFunc =
288 [](SkSurface* surf, SkSurface::BackendHandleAccess access) -> GrBackendObject {
289 GrBackendObject obj;
290 SkAssertResult(surf->getRenderTargetHandle(&obj, access));
291 return obj; };
292 test_backend_handle(reporter, surf, textureAccessorFunc);
293 test_backend_handle(reporter, surf, renderTargetAccessorFunc);
294 test_texture_handle_genID(reporter, surf, textureAccessorFunc);
295 test_texture_handle_genID(reporter, surf, renderTargetAccessorFunc);
296
reedfa5e68e2015-06-29 07:37:01 -0700297 // redraw so our returned image looks as expected.
298 surf->getCanvas()->clear(color);
reed67f2eb42014-12-10 06:54:06 -0800299 return surf->newImageSnapshot();
300 }
reed@google.com4f7c6152014-02-06 14:11:56 +0000301 case kCodec_ImageType: {
302 SkBitmap bitmap;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000303 bitmap.installPixels(info, addr, rowBytes);
reed@google.com4f7c6152014-02-06 14:11:56 +0000304 SkAutoTUnref<SkData> src(
reed67f2eb42014-12-10 06:54:06 -0800305 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
reed871872f2015-06-22 12:48:26 -0700306 return SkImage::NewFromEncoded(src);
reed@google.com4f7c6152014-02-06 14:11:56 +0000307 }
308 }
309 SkASSERT(false);
310 return NULL;
311}
312
reed96472de2014-12-10 09:53:42 -0800313static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
314 sk_memset32(pixels, color, count);
315}
316static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
317 for (int i = 0; i < count; ++i) {
318 if (pixels[i] != expected) {
319 return false;
320 }
321 }
322 return true;
323}
324
325static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image,
326 SkPMColor expected) {
327 const SkPMColor notExpected = ~expected;
328
329 const int w = 2, h = 2;
330 const size_t rowBytes = w * sizeof(SkPMColor);
331 SkPMColor pixels[w*h];
332
333 SkImageInfo info;
334
335 info = SkImageInfo::MakeUnknown(w, h);
336 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
337
338 // out-of-bounds should fail
339 info = SkImageInfo::MakeN32Premul(w, h);
340 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
341 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
342 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
343 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
344
345 // top-left should succeed
346 set_pixels(pixels, w*h, notExpected);
347 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
348 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
349
350 // bottom-right should succeed
351 set_pixels(pixels, w*h, notExpected);
352 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
353 image->width() - w, image->height() - h));
354 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
355
356 // partial top-left should succeed
357 set_pixels(pixels, w*h, notExpected);
358 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
359 REPORTER_ASSERT(reporter, pixels[3] == expected);
360 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
361
362 // partial bottom-right should succeed
363 set_pixels(pixels, w*h, notExpected);
364 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
365 image->width() - 1, image->height() - 1));
366 REPORTER_ASSERT(reporter, pixels[0] == expected);
367 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
368}
369
fmalitaea561bf2015-07-09 17:10:31 -0700370static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image,
371 const SkBitmap& bitmap, SkImage::LegacyBitmapMode mode) {
372 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
373 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
374 REPORTER_ASSERT(reporter, image->isOpaque() == bitmap.isOpaque());
375
376 if (SkImage::kRO_LegacyBitmapMode == mode) {
377 REPORTER_ASSERT(reporter, bitmap.isImmutable());
378 }
379
380 SkAutoLockPixels alp(bitmap);
381 REPORTER_ASSERT(reporter, bitmap.getPixels());
382
383 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
384 SkPMColor imageColor;
385 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0));
386 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
387}
388
reed3c065112015-07-08 12:46:22 -0700389static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) {
390 const SkImage::LegacyBitmapMode modes[] = {
391 SkImage::kRO_LegacyBitmapMode,
392 SkImage::kRW_LegacyBitmapMode,
393 };
394 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
395 SkBitmap bitmap;
396 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, modes[i]));
fmalitaea561bf2015-07-09 17:10:31 -0700397 check_legacy_bitmap(reporter, image, bitmap, modes[i]);
reed3c065112015-07-08 12:46:22 -0700398
fmalitaea561bf2015-07-09 17:10:31 -0700399 // Test subsetting to exercise the rowBytes logic.
400 SkBitmap tmp;
401 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
402 image->height() / 2)));
403 SkAutoTUnref<SkImage> subsetImage(SkImage::NewFromBitmap(tmp));
404 REPORTER_ASSERT(reporter, subsetImage);
reed3c065112015-07-08 12:46:22 -0700405
fmalitaea561bf2015-07-09 17:10:31 -0700406 SkBitmap subsetBitmap;
407 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, modes[i]));
408 check_legacy_bitmap(reporter, subsetImage, subsetBitmap, modes[i]);
reed3c065112015-07-08 12:46:22 -0700409 }
410}
411
reed67f2eb42014-12-10 06:54:06 -0800412static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) {
reed@google.com4f7c6152014-02-06 14:11:56 +0000413 static const struct {
414 ImageType fType;
415 bool fPeekShouldSucceed;
reed67f2eb42014-12-10 06:54:06 -0800416 const char* fName;
reed@google.com4f7c6152014-02-06 14:11:56 +0000417 } gRec[] = {
reed67f2eb42014-12-10 06:54:06 -0800418 { kRasterCopy_ImageType, true, "RasterCopy" },
419 { kRasterData_ImageType, true, "RasterData" },
reedde499882015-06-18 13:41:40 -0700420 { kRasterProc_ImageType, true, "RasterProc" },
reed67f2eb42014-12-10 06:54:06 -0800421 { kGpu_ImageType, false, "Gpu" },
422 { kCodec_ImageType, false, "Codec" },
reed@google.com4f7c6152014-02-06 14:11:56 +0000423 };
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000424
reed@google.com4f7c6152014-02-06 14:11:56 +0000425 const SkColor color = SK_ColorRED;
426 const SkPMColor pmcolor = SkPreMultiplyColor(color);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000427
reed67f2eb42014-12-10 06:54:06 -0800428 GrContext* ctx = NULL;
429#if SK_SUPPORT_GPU
430 ctx = factory->get(GrContextFactory::kNative_GLContextType);
senorblancoc8e93402015-04-21 07:20:36 -0700431 if (NULL == ctx) {
senorblanco84bfd392015-04-21 06:59:17 -0700432 return;
433 }
reed67f2eb42014-12-10 06:54:06 -0800434#endif
435
reedde499882015-06-18 13:41:40 -0700436 ReleaseDataContext releaseCtx;
437 releaseCtx.fReporter = reporter;
438
reed@google.com4f7c6152014-02-06 14:11:56 +0000439 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
440 SkImageInfo info;
441 size_t rowBytes;
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000442
reedde499882015-06-18 13:41:40 -0700443 releaseCtx.fData = NULL;
reedfa5e68e2015-06-29 07:37:01 -0700444 SkAutoTUnref<SkImage> image(create_image(reporter, gRec[i].fType, ctx, color, &releaseCtx));
reed@google.com4f7c6152014-02-06 14:11:56 +0000445 if (!image.get()) {
reed67f2eb42014-12-10 06:54:06 -0800446 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
reed@google.com4f7c6152014-02-06 14:11:56 +0000447 continue; // gpu may not be enabled
448 }
reedde499882015-06-18 13:41:40 -0700449 if (kRasterProc_ImageType == gRec[i].fType) {
450 REPORTER_ASSERT(reporter, NULL != releaseCtx.fData); // we are tracking the data
451 } else {
452 REPORTER_ASSERT(reporter, NULL == releaseCtx.fData); // we ignored the context
453 }
454
reed3c065112015-07-08 12:46:22 -0700455 test_legacy_bitmap(reporter, image);
456
reed@google.com4f7c6152014-02-06 14:11:56 +0000457 const void* addr = image->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700458 bool success = SkToBool(addr);
reed@google.com4f7c6152014-02-06 14:11:56 +0000459 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
460 if (success) {
reede5ea5002014-09-03 11:54:58 -0700461 REPORTER_ASSERT(reporter, 10 == info.width());
462 REPORTER_ASSERT(reporter, 10 == info.height());
463 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
464 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
465 kOpaque_SkAlphaType == info.alphaType());
reed@google.com4f7c6152014-02-06 14:11:56 +0000466 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
467 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
468 }
reed96472de2014-12-10 09:53:42 -0800469
470 test_image_readpixels(reporter, image, pmcolor);
reed@google.com4f7c6152014-02-06 14:11:56 +0000471 }
reedde499882015-06-18 13:41:40 -0700472 REPORTER_ASSERT(reporter, NULL == releaseCtx.fData); // we released the data
reed@google.com4f7c6152014-02-06 14:11:56 +0000473}
474
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000475static void test_canvaspeek(skiatest::Reporter* reporter,
476 GrContextFactory* factory) {
477 static const struct {
478 SurfaceType fType;
479 bool fPeekShouldSucceed;
480 } gRec[] = {
481 { kRaster_SurfaceType, true },
482 { kRasterDirect_SurfaceType, true },
483#if SK_SUPPORT_GPU
484 { kGpu_SurfaceType, false },
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000485 { kGpuScratch_SurfaceType, false },
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000486#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000487 };
488
489 const SkColor color = SK_ColorRED;
490 const SkPMColor pmcolor = SkPreMultiplyColor(color);
491
bsalomone904c092014-07-17 10:50:59 -0700492 int cnt;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000493#if SK_SUPPORT_GPU
bsalomone904c092014-07-17 10:50:59 -0700494 cnt = GrContextFactory::kGLContextTypeCnt;
495#else
496 cnt = 1;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000497#endif
498
bsalomone904c092014-07-17 10:50:59 -0700499 for (int i= 0; i < cnt; ++i) {
500 GrContext* context = NULL;
501#if SK_SUPPORT_GPU
502 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
503 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
504 continue;
505 }
506 context = factory->get(glCtxType);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000507
bsalomone904c092014-07-17 10:50:59 -0700508 if (NULL == context) {
509 continue;
510 }
511#endif
512 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
513 SkImageInfo info, requestInfo;
514 size_t rowBytes;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000515
bsalomon74f681d2015-06-23 14:38:48 -0700516 SkAutoTUnref<SkSurface> surface(create_surface(gRec[i].fType, context,
517 kPremul_SkAlphaType, &requestInfo));
bsalomone904c092014-07-17 10:50:59 -0700518 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000519
bsalomone904c092014-07-17 10:50:59 -0700520 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700521 bool success = SkToBool(addr);
bsalomone904c092014-07-17 10:50:59 -0700522 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000523
bsalomone904c092014-07-17 10:50:59 -0700524 SkImageInfo info2;
525 size_t rb2;
526 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000527
bsalomone904c092014-07-17 10:50:59 -0700528 if (success) {
529 REPORTER_ASSERT(reporter, requestInfo == info);
530 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
531 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
532
533 REPORTER_ASSERT(reporter, addr2 == addr);
534 REPORTER_ASSERT(reporter, info2 == info);
535 REPORTER_ASSERT(reporter, rb2 == rowBytes);
536 } else {
537 REPORTER_ASSERT(reporter, NULL == addr2);
538 }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000539 }
540 }
541}
542
reed41e010c2015-06-09 12:16:53 -0700543// For compatibility with clients that still call accessBitmap(), we need to ensure that we bump
544// the bitmap's genID when we draw to it, else they won't know it has new values. When they are
545// exclusively using surface/image, and we can hide accessBitmap from device, we can remove this
546// test.
547static void test_accessPixels(skiatest::Reporter* reporter, GrContextFactory* factory) {
548 static const struct {
549 SurfaceType fType;
550 bool fPeekShouldSucceed;
551 } gRec[] = {
552 { kRaster_SurfaceType, true },
553 { kRasterDirect_SurfaceType, true },
554#if SK_SUPPORT_GPU
555 { kGpu_SurfaceType, false },
556 { kGpuScratch_SurfaceType, false },
557#endif
558 };
559
560 int cnt;
561#if SK_SUPPORT_GPU
562 cnt = GrContextFactory::kGLContextTypeCnt;
563#else
564 cnt = 1;
565#endif
566
567 for (int i= 0; i < cnt; ++i) {
568 GrContext* context = NULL;
569#if SK_SUPPORT_GPU
570 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
571 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
572 continue;
573 }
574 context = factory->get(glCtxType);
575
576 if (NULL == context) {
577 continue;
578 }
579#endif
580 for (size_t j = 0; j < SK_ARRAY_COUNT(gRec); ++j) {
581 SkImageInfo info, requestInfo;
582
bsalomon74f681d2015-06-23 14:38:48 -0700583 SkAutoTUnref<SkSurface> surface(create_surface(gRec[j].fType, context,
584 kPremul_SkAlphaType, &requestInfo));
reed41e010c2015-06-09 12:16:53 -0700585 SkCanvas* canvas = surface->getCanvas();
586 canvas->clear(0);
587
588 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
589 SkBitmap bm = device->accessBitmap(false);
590 uint32_t genID0 = bm.getGenerationID();
591 // Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write)
592 canvas->drawColor(SK_ColorBLUE);
593 // Now check that we get a different genID
594 uint32_t genID1 = bm.getGenerationID();
595 REPORTER_ASSERT(reporter, genID0 != genID1);
596 }
597 }
598}
599
bsalomon74f681d2015-06-23 14:38:48 -0700600static void test_snap_alphatype(skiatest::Reporter* reporter, GrContextFactory* factory) {
601 GrContext* context = NULL;
602#if SK_SUPPORT_GPU
603 context = factory->get(GrContextFactory::kNative_GLContextType);
604 if (NULL == context) {
605 return;
606 }
607#endif
608 for (int opaque = 0; opaque < 2; ++opaque) {
609 SkAlphaType atype = SkToBool(opaque) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
610 for (int st = 0; st < kSurfaceTypeCnt; ++st) {
611 SurfaceType stype = (SurfaceType)st;
612 SkAutoTUnref<SkSurface> surface(create_surface(stype, context, atype));
613 REPORTER_ASSERT(reporter, surface);
614 if (surface) {
615 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
616 REPORTER_ASSERT(reporter, image);
617 if (image) {
618 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(opaque));
619 }
620 }
621 }
622 }
623}
624
fmalitae2639082015-08-06 07:04:51 -0700625static void test_backend_cow(skiatest::Reporter* reporter, SkSurface* surface,
626 SkSurface::BackendHandleAccess mode,
627 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
628 GrBackendObject obj1 = func(surface, mode);
629 SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot());
630
631 GrBackendObject obj2 = func(surface, mode);
632 SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot());
633
634 // If the access mode triggers CoW, then the backend objects should reflect it.
635 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
636}
637
junov@chromium.org995beb62013-03-28 13:49:22 +0000638static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
639 GrContext* context) {
640 // Verify that the right canvas commands trigger a copy on write
bsalomon74f681d2015-06-23 14:38:48 -0700641 SkSurface* surface = create_surface(surfaceType, context);
junov@chromium.org995beb62013-03-28 13:49:22 +0000642 SkAutoTUnref<SkSurface> aur_surface(surface);
643 SkCanvas* canvas = surface->getCanvas();
644
645 const SkRect testRect =
646 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
647 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000648 SkPath testPath;
649 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
650 SkIntToScalar(2), SkIntToScalar(1)));
651
652 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
653
654 SkRegion testRegion;
655 testRegion.setRect(testIRect);
656
657
658 const SkColor testColor = 0x01020304;
659 const SkPaint testPaint;
660 const SkPoint testPoints[3] = {
661 {SkIntToScalar(0), SkIntToScalar(0)},
662 {SkIntToScalar(2), SkIntToScalar(1)},
663 {SkIntToScalar(0), SkIntToScalar(2)}
664 };
665 const size_t testPointCount = 3;
666
667 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000668 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000669 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000670
671 SkRRect testRRect;
672 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
673
674 SkString testText("Hello World");
675 const SkPoint testPoints2[] = {
676 { SkIntToScalar(0), SkIntToScalar(1) },
677 { SkIntToScalar(1), SkIntToScalar(1) },
678 { SkIntToScalar(2), SkIntToScalar(1) },
679 { SkIntToScalar(3), SkIntToScalar(1) },
680 { SkIntToScalar(4), SkIntToScalar(1) },
681 { SkIntToScalar(5), SkIntToScalar(1) },
682 { SkIntToScalar(6), SkIntToScalar(1) },
683 { SkIntToScalar(7), SkIntToScalar(1) },
684 { SkIntToScalar(8), SkIntToScalar(1) },
685 { SkIntToScalar(9), SkIntToScalar(1) },
686 { SkIntToScalar(10), SkIntToScalar(1) },
687 };
688
689#define EXPECT_COPY_ON_WRITE(command) \
690 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000691 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000692 SkAutoTUnref<SkImage> aur_before(imageBefore); \
693 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000694 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000695 SkAutoTUnref<SkImage> aur_after(imageAfter); \
696 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
697 }
698
699 EXPECT_COPY_ON_WRITE(clear(testColor))
700 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
701 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
702 testPaint))
703 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
704 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
705 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
706 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
707 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700708 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000709 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
710 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
711 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
712 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
713 testPaint))
714 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
715 testPaint))
fmalitae2639082015-08-06 07:04:51 -0700716
717 const SkSurface::BackendHandleAccess accessModes[] = {
718 SkSurface::kFlushRead_BackendHandleAccess,
719 SkSurface::kFlushWrite_BackendHandleAccess,
720 SkSurface::kDiscardWrite_BackendHandleAccess,
721 };
722
723 for (auto access : accessModes) {
724 test_backend_cow(reporter, surface, access,
725 [](SkSurface* s, SkSurface::BackendHandleAccess a) -> GrBackendObject {
726 return s->getTextureHandle(a);
727 });
728
729 test_backend_cow(reporter, surface, access,
730 [](SkSurface* s, SkSurface::BackendHandleAccess a) -> GrBackendObject {
731 GrBackendObject result;
732 if (!s->getRenderTargetHandle(&result, a)) {
733 return 0;
734 }
735 return result;
736 });
737 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000738}
739
junov@chromium.orgaf058352013-04-03 15:03:26 +0000740static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
741 SurfaceType surfaceType,
742 GrContext* context) {
743 // This test succeeds by not triggering an assertion.
744 // The test verifies that the surface remains writable (usable) after
745 // acquiring and releasing a snapshot without triggering a copy on write.
bsalomon74f681d2015-06-23 14:38:48 -0700746 SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000747 SkCanvas* canvas = surface->getCanvas();
748 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000749 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000750 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000751}
junov@chromium.orgda904742013-05-01 22:38:16 +0000752
junov@chromium.orgb516a412013-05-01 22:49:59 +0000753#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000754static void Test_crbug263329(skiatest::Reporter* reporter,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000755 SurfaceType surfaceType,
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000756 GrContext* context) {
757 // This is a regression test for crbug.com/263329
758 // Bug was caused by onCopyOnWrite releasing the old surface texture
759 // back to the scratch texture pool even though the texture is used
760 // by and active SkImage_Gpu.
bsalomon74f681d2015-06-23 14:38:48 -0700761 SkAutoTUnref<SkSurface> surface1(create_surface(surfaceType, context));
762 SkAutoTUnref<SkSurface> surface2(create_surface(surfaceType, context));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000763 SkCanvas* canvas1 = surface1->getCanvas();
764 SkCanvas* canvas2 = surface2->getCanvas();
765 canvas1->clear(1);
766 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
767 // Trigger copy on write, new backing is a scratch texture
768 canvas1->clear(2);
769 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
770 // Trigger copy on write, old backing should not be returned to scratch
771 // pool because it is held by image2
772 canvas1->clear(3);
773
774 canvas2->clear(4);
775 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
776 // Trigger copy on write on surface2. The new backing store should not
777 // be recycling a texture that is held by an existing image.
778 canvas2->clear(5);
779 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
bsalomon55812362015-06-10 08:49:28 -0700780 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000781 // The following assertion checks crbug.com/263329
bsalomon55812362015-06-10 08:49:28 -0700782 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
783 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
784 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
785 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
786 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000787}
788
junov@chromium.orgda904742013-05-01 22:38:16 +0000789static void TestGetTexture(skiatest::Reporter* reporter,
790 SurfaceType surfaceType,
791 GrContext* context) {
bsalomon74f681d2015-06-23 14:38:48 -0700792 SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context));
junov@chromium.orgda904742013-05-01 22:38:16 +0000793 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
bsalomon55812362015-06-10 08:49:28 -0700794 GrTexture* texture = as_IB(image)->getTexture();
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000795 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
bsalomon49f085d2014-09-05 13:34:00 -0700796 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000797 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
798 } else {
799 REPORTER_ASSERT(reporter, NULL == texture);
800 }
801 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon55812362015-06-10 08:49:28 -0700802 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000803}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800804
bsalomon3582d3e2015-02-13 14:20:05 -0800805#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800806#include "SkGpuDevice.h"
807#include "SkImage_Gpu.h"
808#include "SkSurface_Gpu.h"
809
810SkSurface::Budgeted is_budgeted(SkSurface* surf) {
bsalomon3582d3e2015-02-13 14:20:05 -0800811 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800812 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
813}
814
815SkSurface::Budgeted is_budgeted(SkImage* image) {
bsalomon3582d3e2015-02-13 14:20:05 -0800816 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800817 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
818}
819
820static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context) {
821 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
822 for (int i = 0; i < 2; ++i) {
823 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
824 for (int j = 0; j < 2; ++j) {
825 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
826 SkAutoTUnref<SkSurface>
827 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0));
828 SkASSERT(surface);
829 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
830
mtklein31ff2982015-01-24 11:27:27 -0800831 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800832
833 // Initially the image shares a texture with the surface, and the surface decides
834 // whether it is budgeted or not.
835 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
836 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
837
838 // Now trigger copy-on-write
839 surface->getCanvas()->clear(SK_ColorBLUE);
840
841 // They don't share a texture anymore. They should each have made their own budget
842 // decision.
843 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
844 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
845 }
846 }
847}
848
junov@chromium.orgb516a412013-05-01 22:49:59 +0000849#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000850
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000851static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
852 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000853 GrContext* context,
854 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000855 // Verifies the robustness of SkSurface for handling use cases where calls
856 // are made before a canvas is created.
857 {
858 // Test passes by not asserting
bsalomon74f681d2015-06-23 14:38:48 -0700859 SkSurface* surface = create_surface(surfaceType, context);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000860 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000861 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000862 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000863 }
864 {
bsalomon74f681d2015-06-23 14:38:48 -0700865 SkSurface* surface = create_surface(surfaceType, context);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000866 SkAutoTUnref<SkSurface> aur_surface(surface);
867 SkImage* image1 = surface->newImageSnapshot();
868 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000869 SkDEBUGCODE(image1->validate();)
870 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000871 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000872 SkDEBUGCODE(image1->validate();)
873 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000874 SkImage* image2 = surface->newImageSnapshot();
875 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000876 SkDEBUGCODE(image2->validate();)
877 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000878 REPORTER_ASSERT(reporter, image1 != image2);
879 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000880
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000881}
junov@chromium.org995beb62013-03-28 13:49:22 +0000882
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000883DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000884 test_image(reporter);
885
junov@chromium.orgaf058352013-04-03 15:03:26 +0000886 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000887 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000888 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
889 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000890
reedb2497c22014-12-31 12:31:43 -0800891 test_empty_image(reporter);
892 test_empty_surface(reporter, NULL);
893
reed67f2eb42014-12-10 06:54:06 -0800894 test_imagepeek(reporter, factory);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000895 test_canvaspeek(reporter, factory);
896
reed41e010c2015-06-09 12:16:53 -0700897 test_accessPixels(reporter, factory);
898
bsalomon74f681d2015-06-23 14:38:48 -0700899 test_snap_alphatype(reporter, factory);
900
junov@chromium.orgb516a412013-05-01 22:49:59 +0000901#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000902 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
bsalomon49f085d2014-09-05 13:34:00 -0700903 if (factory) {
bsalomone904c092014-07-17 10:50:59 -0700904 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
905 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
906 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
907 continue;
908 }
909 GrContext* context = factory->get(glCtxType);
bsalomon49f085d2014-09-05 13:34:00 -0700910 if (context) {
bsalomone904c092014-07-17 10:50:59 -0700911 Test_crbug263329(reporter, kGpu_SurfaceType, context);
912 Test_crbug263329(reporter, kGpuScratch_SurfaceType, context);
913 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
914 TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context);
915 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
916 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context);
917 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
918 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
919 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
920 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
921 TestGetTexture(reporter, kGpu_SurfaceType, context);
922 TestGetTexture(reporter, kGpuScratch_SurfaceType, context);
reedb2497c22014-12-31 12:31:43 -0800923 test_empty_surface(reporter, context);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800924 test_surface_budget(reporter, context);
joshualitt81793412015-07-08 12:54:04 -0700925 test_wrapped_texture_surface(reporter, context);
bsalomone904c092014-07-17 10:50:59 -0700926 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000927 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000928 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000929#endif
930}
reed8b26b992015-05-07 15:36:17 -0700931
932#if SK_SUPPORT_GPU
reedde499882015-06-18 13:41:40 -0700933
934struct ReleaseTextureContext {
935 ReleaseTextureContext(skiatest::Reporter* reporter) {
936 fReporter = reporter;
937 fIsReleased = false;
938 }
939
940 skiatest::Reporter* fReporter;
941 bool fIsReleased;
942
943 void doRelease() {
944 REPORTER_ASSERT(fReporter, false == fIsReleased);
945 fIsReleased = true;
946 }
947
948 static void ReleaseProc(void* context) {
949 ((ReleaseTextureContext*)context)->doRelease();
950 }
951};
952
953static SkImage* make_desc_image(GrContext* ctx, int w, int h, GrBackendObject texID,
954 ReleaseTextureContext* releaseContext) {
reed8b26b992015-05-07 15:36:17 -0700955 GrBackendTextureDesc desc;
956 desc.fConfig = kSkia8888_GrPixelConfig;
957 // need to be a rendertarget for now...
958 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
959 desc.fWidth = w;
960 desc.fHeight = h;
961 desc.fSampleCnt = 0;
962 desc.fTextureHandle = texID;
reedde499882015-06-18 13:41:40 -0700963 return releaseContext
964 ? SkImage::NewFromTexture(ctx, desc, kPremul_SkAlphaType,
965 ReleaseTextureContext::ReleaseProc, releaseContext)
966 : SkImage::NewFromTextureCopy(ctx, desc, kPremul_SkAlphaType);
reed8b26b992015-05-07 15:36:17 -0700967}
968
969static void test_image_color(skiatest::Reporter* reporter, SkImage* image, SkPMColor expected) {
970 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
971 SkPMColor pixel;
972 REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0, 0));
973 REPORTER_ASSERT(reporter, pixel == expected);
974}
975
976DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) {
977 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
978 if (!ctx) {
979 REPORTER_ASSERT(reporter, false);
980 return;
981 }
982 GrTextureProvider* provider = ctx->textureProvider();
983
984 const int w = 10;
985 const int h = 10;
986 SkPMColor storage[w * h];
987 const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED);
988 sk_memset32(storage, expected0, w * h);
989
990 GrSurfaceDesc desc;
bsalomonbea01502015-07-16 10:00:28 -0700991 desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget for readpixels();
reed8b26b992015-05-07 15:36:17 -0700992 desc.fOrigin = kDefault_GrSurfaceOrigin;
993 desc.fWidth = w;
994 desc.fHeight = h;
995 desc.fConfig = kSkia8888_GrPixelConfig;
996 desc.fSampleCnt = 0;
997
998 SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w * 4));
999 if (!tex) {
1000 REPORTER_ASSERT(reporter, false);
1001 return;
1002 }
reedde499882015-06-18 13:41:40 -07001003
reed8b26b992015-05-07 15:36:17 -07001004 GrBackendObject srcTex = tex->getTextureHandle();
reedde499882015-06-18 13:41:40 -07001005 ReleaseTextureContext releaseCtx(reporter);
1006
1007 SkAutoTUnref<SkImage> refImg(make_desc_image(ctx, w, h, srcTex, &releaseCtx));
1008 SkAutoTUnref<SkImage> cpyImg(make_desc_image(ctx, w, h, srcTex, NULL));
reed8b26b992015-05-07 15:36:17 -07001009
1010 test_image_color(reporter, refImg, expected0);
1011 test_image_color(reporter, cpyImg, expected0);
1012
1013 // Now lets jam new colors into our "external" texture, and see if the images notice
1014 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE);
1015 sk_memset32(storage, expected1, w * h);
1016 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kFlushWrites_PixelOp);
1017
bsalomona4497792015-07-23 12:22:19 -07001018 // The cpy'd one should still see the old color
1019#if 0
1020 // There is no guarantee that refImg sees the new color. We are free to have made a copy. Our
1021 // write pixels call violated the contract with refImg and refImg is now undefined.
reed8b26b992015-05-07 15:36:17 -07001022 test_image_color(reporter, refImg, expected1);
bsalomona4497792015-07-23 12:22:19 -07001023#endif
reed8b26b992015-05-07 15:36:17 -07001024 test_image_color(reporter, cpyImg, expected0);
reedde499882015-06-18 13:41:40 -07001025
1026 // Now exercise the release proc
1027 REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased);
1028 refImg.reset(NULL); // force a release of the image
1029 REPORTER_ASSERT(reporter, releaseCtx.fIsReleased);
reed8b26b992015-05-07 15:36:17 -07001030}
1031#endif