blob: 5b7325e9af201bf681dd87a08aa0ebd7f45d4226 [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"
bsalomon55812362015-06-10 08:49:28 -070011#include "SkImage_Base.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.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
kkinnunen179a8f52015-11-20 13:32:24 -080019#include "GrContext.h"
20#include "GrGpu.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000021#endif
22
kkinnunen179a8f52015-11-20 13:32:24 -080023#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070024
kkinnunen179a8f52015-11-20 13:32:24 -080025static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070026 SkASSERT(pixels == context);
27 sk_free(pixels);
28}
kkinnunen179a8f52015-11-20 13:32:24 -080029static SkSurface* create_surface(SkAlphaType at = kPremul_SkAlphaType,
halcanary96fcdcc2015-08-27 07:41:13 -070030 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070031 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000032 if (requestedInfo) {
33 *requestedInfo = info;
34 }
kkinnunen179a8f52015-11-20 13:32:24 -080035 return SkSurface::NewRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000036}
kkinnunen179a8f52015-11-20 13:32:24 -080037static SkSurface* create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
38 SkImageInfo* requestedInfo = nullptr) {
39 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
40 if (requestedInfo) {
41 *requestedInfo = info;
42 }
43 const size_t rowBytes = info.minRowBytes();
44 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
45 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes,
46 release_direct_surface_storage,
47 storage);
48}
49#if SK_SUPPORT_GPU
50static SkSurface* create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
51 SkImageInfo* requestedInfo = nullptr) {
52 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
53 if (requestedInfo) {
54 *requestedInfo = info;
55 }
56 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr);
57}
58static SkSurface* create_gpu_scratch_surface(GrContext* context,
59 SkAlphaType at = kPremul_SkAlphaType,
60 SkImageInfo* requestedInfo = nullptr) {
61 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
62 if (requestedInfo) {
63 *requestedInfo = info;
64 }
65 return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, nullptr);
66}
67#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000068
kkinnunen179a8f52015-11-20 13:32:24 -080069DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080070 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
halcanary96fcdcc2015-08-27 07:41:13 -070071 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRaster(info));
72 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080073
reedb2497c22014-12-31 12:31:43 -080074}
kkinnunen179a8f52015-11-20 13:32:24 -080075#if SK_SUPPORT_GPU
76DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, context) {
77 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
78 REPORTER_ASSERT(reporter, nullptr ==
79 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr));
80}
81#endif
reedb2497c22014-12-31 12:31:43 -080082
bsalomone4579ad2015-04-08 08:38:40 -070083#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080084DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedTexture, reporter, context) {
85 const GrGpu* gpu = context->getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -070086 if (!gpu) {
bsalomone4579ad2015-04-08 08:38:40 -070087 return;
88 }
bsalomon7a617932015-06-16 08:07:16 -070089
jvanverth672bb7f2015-07-13 07:19:57 -070090 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
joshualitt81793412015-07-08 12:54:04 -070091 // a SkSurface.
joshualitt81793412015-07-08 12:54:04 -070092 static const int kW = 100;
93 static const int kH = 100;
94 static const uint32_t kOrigColor = 0xFFAABBCC;
95 SkAutoTArray<uint32_t> pixels(kW * kH);
96 sk_memset32(pixels.get(), kOrigColor, kW * kH);
bsalomon091f60c2015-11-10 11:54:56 -080097 GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
98 kRGBA_8888_GrPixelConfig);
bsalomone4579ad2015-04-08 08:38:40 -070099
joshualitt81793412015-07-08 12:54:04 -0700100 GrBackendTextureDesc wrappedDesc;
101 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
102 wrappedDesc.fWidth = kW;
103 wrappedDesc.fHeight = kH;
104 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
105 wrappedDesc.fSampleCnt = 0;
106 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
bsalomon091f60c2015-11-10 11:54:56 -0800107 wrappedDesc.fTextureHandle = texHandle;
bsalomone4579ad2015-04-08 08:38:40 -0700108
kkinnunen179a8f52015-11-20 13:32:24 -0800109 SkAutoTUnref<SkSurface> surface(
110 SkSurface::NewWrappedRenderTarget(context, wrappedDesc, nullptr));
joshualitt81793412015-07-08 12:54:04 -0700111 REPORTER_ASSERT(reporter, surface);
112 if (surface) {
113 // Validate that we can draw to the canvas and that the original texture color is preserved
114 // in pixels that aren't rendered to via the surface.
115 SkPaint paint;
116 static const SkColor kRectColor = ~kOrigColor | 0xFF000000;
117 paint.setColor(kRectColor);
118 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
119 paint);
120 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
121 surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0);
122 bool stop = false;
123 SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF),
124 (kOrigColor >> 0 & 0xFF),
125 (kOrigColor >> 8 & 0xFF),
126 (kOrigColor >> 16 & 0xFF));
127 SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
128 (kRectColor >> 16 & 0xFF),
129 (kRectColor >> 8 & 0xFF),
130 (kRectColor >> 0 & 0xFF));
131 for (int y = 0; y < kH/2 && !stop; ++y) {
132 for (int x = 0; x < kW && !stop; ++x) {
133 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
134 if (rectColorPM != pixels[x + y * kW]) {
135 stop = true;
bsalomon7a617932015-06-16 08:07:16 -0700136 }
137 }
138 }
joshualitt81793412015-07-08 12:54:04 -0700139 stop = false;
140 for (int y = kH/2; y < kH && !stop; ++y) {
141 for (int x = 0; x < kW && !stop; ++x) {
142 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
143 if (origColorPM != pixels[x + y * kW]) {
144 stop = true;
145 }
146 }
bsalomon7a617932015-06-16 08:07:16 -0700147 }
148 }
bsalomon67d76202015-11-11 12:40:42 -0800149 gpu->deleteTestingOnlyBackendTexture(texHandle);
bsalomone4579ad2015-04-08 08:38:40 -0700150}
151#endif
152
kkinnunen179a8f52015-11-20 13:32:24 -0800153static void test_canvas_peek(skiatest::Reporter* reporter,
154 SkSurface* surface,
155 const SkImageInfo& requestInfo,
156 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000157 const SkColor color = SK_ColorRED;
158 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800159 SkImageInfo info;
160 size_t rowBytes;
161 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000162
kkinnunen179a8f52015-11-20 13:32:24 -0800163 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
164 bool success = SkToBool(addr);
165 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000166
kkinnunen179a8f52015-11-20 13:32:24 -0800167 SkImageInfo info2;
168 size_t rb2;
169 const void* addr2 = surface->peekPixels(&info2, &rb2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000170
kkinnunen179a8f52015-11-20 13:32:24 -0800171 if (success) {
172 REPORTER_ASSERT(reporter, requestInfo == info);
173 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
174 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000175
kkinnunen179a8f52015-11-20 13:32:24 -0800176 REPORTER_ASSERT(reporter, addr2 == addr);
177 REPORTER_ASSERT(reporter, info2 == info);
178 REPORTER_ASSERT(reporter, rb2 == rowBytes);
179 } else {
180 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000181 }
182}
kkinnunen179a8f52015-11-20 13:32:24 -0800183DEF_TEST(SurfaceCanvasPeek, reporter) {
184 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
185 SkImageInfo requestInfo;
186 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, &requestInfo));
187 test_canvas_peek(reporter, surface, requestInfo, true);
188 }
189}
190#if SK_SUPPORT_GPU
191DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, context) {
192 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
193 SkImageInfo requestInfo;
194 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, &requestInfo));
195 test_canvas_peek(reporter, surface, requestInfo, false);
196 }
197}
198#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000199
reed41e010c2015-06-09 12:16:53 -0700200// For compatibility with clients that still call accessBitmap(), we need to ensure that we bump
201// the bitmap's genID when we draw to it, else they won't know it has new values. When they are
202// exclusively using surface/image, and we can hide accessBitmap from device, we can remove this
203// test.
kkinnunen179a8f52015-11-20 13:32:24 -0800204void test_access_pixels(skiatest::Reporter* reporter, SkSurface* surface) {
205 SkCanvas* canvas = surface->getCanvas();
206 canvas->clear(0);
reed41e010c2015-06-09 12:16:53 -0700207
kkinnunen179a8f52015-11-20 13:32:24 -0800208 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
209 SkBitmap bm = device->accessBitmap(false);
210 uint32_t genID0 = bm.getGenerationID();
211 // Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write)
212 canvas->drawColor(SK_ColorBLUE);
213 // Now check that we get a different genID
214 uint32_t genID1 = bm.getGenerationID();
215 REPORTER_ASSERT(reporter, genID0 != genID1);
216}
217DEF_TEST(SurfaceAccessPixels, reporter) {
218 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
219 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, nullptr));
220 test_access_pixels(reporter, surface);
221 }
222}
223#if SK_SUPPORT_GPU
224DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAccessPixels_Gpu, reporter, context) {
225 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
226 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
227 test_access_pixels(reporter, surface);
228 }
229}
230#endif
231
232static void test_snapshot_alphatype(skiatest::Reporter* reporter, SkSurface* surface,
233 bool expectOpaque) {
234 REPORTER_ASSERT(reporter, surface);
235 if (surface) {
236 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
237 REPORTER_ASSERT(reporter, image);
238 if (image) {
239 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(expectOpaque));
reed41e010c2015-06-09 12:16:53 -0700240 }
241 }
242}
kkinnunen179a8f52015-11-20 13:32:24 -0800243DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
244 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
245 for (auto& isOpaque : { true, false }) {
246 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
247 SkAutoTUnref<SkSurface> surface(surface_func(alphaType, nullptr));
248 test_snapshot_alphatype(reporter, surface, isOpaque);
bsalomon74f681d2015-06-23 14:38:48 -0700249 }
250 }
251}
kkinnunen179a8f52015-11-20 13:32:24 -0800252#if SK_SUPPORT_GPU
253DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, context) {
254 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
255 for (auto& isOpaque : { true, false }) {
256 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
257 SkAutoTUnref<SkSurface> surface(surface_func(context, alphaType, nullptr));
258 test_snapshot_alphatype(reporter, surface, isOpaque);
259 }
260 }
261}
262#endif
bsalomon74f681d2015-06-23 14:38:48 -0700263
kkinnunen179a8f52015-11-20 13:32:24 -0800264static GrBackendObject get_surface_backend_texture_handle(
265 SkSurface* s, SkSurface::BackendHandleAccess a) {
266 return s->getTextureHandle(a);
267}
268static GrBackendObject get_surface_backend_render_target_handle(
269 SkSurface* s, SkSurface::BackendHandleAccess a) {
270 GrBackendObject result;
271 if (!s->getRenderTargetHandle(&result, a)) {
272 return 0;
273 }
274 return result;
275}
276
277static void test_backend_handle_access_copy_on_write(
278 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
279 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700280 GrBackendObject obj1 = func(surface, mode);
281 SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot());
282
283 GrBackendObject obj2 = func(surface, mode);
284 SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot());
285
286 // If the access mode triggers CoW, then the backend objects should reflect it.
287 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
288}
kkinnunen179a8f52015-11-20 13:32:24 -0800289DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
290 const SkSurface::BackendHandleAccess accessModes[] = {
291 SkSurface::kFlushRead_BackendHandleAccess,
292 SkSurface::kFlushWrite_BackendHandleAccess,
293 SkSurface::kDiscardWrite_BackendHandleAccess,
294 };
295 for (auto& handle_access_func :
296 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
297 for (auto& accessMode : accessModes) {
298 SkAutoTUnref<SkSurface> surface(create_surface());
299 test_backend_handle_access_copy_on_write(reporter, surface, accessMode,
300 handle_access_func);
301 }
302 }
303}
304#if SK_SUPPORT_GPU
305DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, context) {
306 const SkSurface::BackendHandleAccess accessModes[] = {
307 SkSurface::kFlushRead_BackendHandleAccess,
308 SkSurface::kFlushWrite_BackendHandleAccess,
309 SkSurface::kDiscardWrite_BackendHandleAccess,
310 };
311 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
312 for (auto& handle_access_func :
313 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
314 for (auto& accessMode : accessModes) {
315 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType,
316 nullptr));
317 test_backend_handle_access_copy_on_write(reporter, surface, accessMode,
318 handle_access_func);
319 }
320 }
321 }
322}
323#endif
fmalitae2639082015-08-06 07:04:51 -0700324
kkinnunen179a8f52015-11-20 13:32:24 -0800325#if SK_SUPPORT_GPU
326// May we (soon) eliminate the need to keep testing this, by hiding the bloody device!
327static uint32_t get_legacy_gen_id(SkSurface* surface) {
328 SkBaseDevice* device =
329 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
330 return device->accessBitmap(false).getGenerationID();
331}
332/*
333 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
334 * texture handle for writing.
335 *
336 * Note: this needs to be tested separately from checking newImageSnapshot, as calling that
337 * can also incidentally bump the genID (when a new backing surface is created).
338 */
339static void test_backend_handle_gen_id(
340 skiatest::Reporter* reporter, SkSurface* surface,
341 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
342 const uint32_t gen0 = get_legacy_gen_id(surface);
343 func(surface, SkSurface::kFlushRead_BackendHandleAccess);
344 const uint32_t gen1 = get_legacy_gen_id(surface);
345 REPORTER_ASSERT(reporter, gen0 == gen1);
346
347 func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
348 const uint32_t gen2 = get_legacy_gen_id(surface);
349 REPORTER_ASSERT(reporter, gen0 != gen2);
350
351 func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
352 const uint32_t gen3 = get_legacy_gen_id(surface);
353 REPORTER_ASSERT(reporter, gen0 != gen3);
354 REPORTER_ASSERT(reporter, gen2 != gen3);
355}
356static void test_backend_handle_unique_id(
357 skiatest::Reporter* reporter, SkSurface* surface,
358 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
359 SkAutoTUnref<SkImage> image0(surface->newImageSnapshot());
360 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
361 REPORTER_ASSERT(reporter, obj != 0);
362 SkAutoTUnref<SkImage> image1(surface->newImageSnapshot());
363 // just read access should not affect the snapshot
364 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
365
366 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
367 REPORTER_ASSERT(reporter, obj != 0);
368 SkAutoTUnref<SkImage> image2(surface->newImageSnapshot());
369 // expect a new image, since we claimed we would write
370 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
371
372 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
373 REPORTER_ASSERT(reporter, obj != 0);
374 SkAutoTUnref<SkImage> image3(surface->newImageSnapshot());
375 // expect a new(er) image, since we claimed we would write
376 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
377 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
378}
379// No CPU test.
380DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, context) {
381 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
382 for (auto& test_func : { &test_backend_handle_unique_id, &test_backend_handle_gen_id }) {
383 for (auto& handle_access_func :
384 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
385 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType,
386 nullptr));
387 test_func(reporter, surface, handle_access_func);
388 }
389 }
390 }
391}
392#endif
393
394// Verify that the right canvas commands trigger a copy on write.
395static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000396 SkCanvas* canvas = surface->getCanvas();
397
398 const SkRect testRect =
399 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
400 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000401 SkPath testPath;
402 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
403 SkIntToScalar(2), SkIntToScalar(1)));
404
405 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
406
407 SkRegion testRegion;
408 testRegion.setRect(testIRect);
409
410
411 const SkColor testColor = 0x01020304;
412 const SkPaint testPaint;
413 const SkPoint testPoints[3] = {
414 {SkIntToScalar(0), SkIntToScalar(0)},
415 {SkIntToScalar(2), SkIntToScalar(1)},
416 {SkIntToScalar(0), SkIntToScalar(2)}
417 };
418 const size_t testPointCount = 3;
419
420 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000421 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000422 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000423
424 SkRRect testRRect;
425 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
426
427 SkString testText("Hello World");
428 const SkPoint testPoints2[] = {
429 { SkIntToScalar(0), SkIntToScalar(1) },
430 { SkIntToScalar(1), SkIntToScalar(1) },
431 { SkIntToScalar(2), SkIntToScalar(1) },
432 { SkIntToScalar(3), SkIntToScalar(1) },
433 { SkIntToScalar(4), SkIntToScalar(1) },
434 { SkIntToScalar(5), SkIntToScalar(1) },
435 { SkIntToScalar(6), SkIntToScalar(1) },
436 { SkIntToScalar(7), SkIntToScalar(1) },
437 { SkIntToScalar(8), SkIntToScalar(1) },
438 { SkIntToScalar(9), SkIntToScalar(1) },
439 { SkIntToScalar(10), SkIntToScalar(1) },
440 };
441
442#define EXPECT_COPY_ON_WRITE(command) \
443 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000444 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000445 SkAutoTUnref<SkImage> aur_before(imageBefore); \
446 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000447 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000448 SkAutoTUnref<SkImage> aur_after(imageAfter); \
449 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
450 }
451
452 EXPECT_COPY_ON_WRITE(clear(testColor))
453 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
454 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
455 testPaint))
456 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
457 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
458 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
459 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
460 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700461 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700462 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000463 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
464 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
465 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700466 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000467 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800468}
469DEF_TEST(SurfaceCopyOnWrite, reporter) {
470 SkAutoTUnref<SkSurface> surface(create_surface());
471 test_copy_on_write(reporter, surface);
472}
473#if SK_SUPPORT_GPU
474DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, context) {
475 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
476 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
477 test_copy_on_write(reporter, surface);
fmalitae2639082015-08-06 07:04:51 -0700478 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000479}
kkinnunen179a8f52015-11-20 13:32:24 -0800480#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000481
kkinnunen179a8f52015-11-20 13:32:24 -0800482static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
483 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000484 // This test succeeds by not triggering an assertion.
485 // The test verifies that the surface remains writable (usable) after
486 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000487 SkCanvas* canvas = surface->getCanvas();
488 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000489 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000490 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000491}
kkinnunen179a8f52015-11-20 13:32:24 -0800492DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
493 SkAutoTUnref<SkSurface> surface(create_surface());
494 test_writable_after_snapshot_release(reporter, surface);
495}
496#if SK_SUPPORT_GPU
497DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, context) {
498 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
499 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
500 test_writable_after_snapshot_release(reporter, surface);
501 }
502}
503#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000504
junov@chromium.orgb516a412013-05-01 22:49:59 +0000505#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800506static void test_crbug263329(skiatest::Reporter* reporter,
507 SkSurface* surface1,
508 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000509 // 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.org4d24b742013-07-25 23:29:40 +0000513 SkCanvas* canvas1 = surface1->getCanvas();
514 SkCanvas* canvas2 = surface2->getCanvas();
515 canvas1->clear(1);
516 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
517 // Trigger copy on write, new backing is a scratch texture
518 canvas1->clear(2);
519 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
520 // Trigger copy on write, old backing should not be returned to scratch
521 // pool because it is held by image2
522 canvas1->clear(3);
523
524 canvas2->clear(4);
525 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
526 // Trigger copy on write on surface2. The new backing store should not
527 // be recycling a texture that is held by an existing image.
528 canvas2->clear(5);
529 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
bsalomon55812362015-06-10 08:49:28 -0700530 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000531 // The following assertion checks crbug.com/263329
bsalomon55812362015-06-10 08:49:28 -0700532 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
533 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
534 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
535 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
536 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000537}
kkinnunen179a8f52015-11-20 13:32:24 -0800538DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, context) {
539 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
540 SkAutoTUnref<SkSurface> surface1(surface_func(context, kPremul_SkAlphaType, nullptr));
541 SkAutoTUnref<SkSurface> surface2(surface_func(context, kPremul_SkAlphaType, nullptr));
542 test_crbug263329(reporter, surface1, surface2);
543 }
544}
545#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000546
kkinnunen179a8f52015-11-20 13:32:24 -0800547DEF_TEST(SurfaceGetTexture, reporter) {
548 SkAutoTUnref<SkSurface> surface(create_surface());
junov@chromium.orgda904742013-05-01 22:38:16 +0000549 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800550 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == nullptr);
551 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
552 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == nullptr);
553}
554#if SK_SUPPORT_GPU
555DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceGetTexture_Gpu, reporter, context) {
556 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
557 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
558 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
559 GrTexture* texture = as_IB(image)->getTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700560 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000561 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800562 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
563 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000564 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000565}
kkinnunen179a8f52015-11-20 13:32:24 -0800566#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800567
kkinnunen179a8f52015-11-20 13:32:24 -0800568#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800569#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800570#include "SkGpuDevice.h"
571#include "SkImage_Gpu.h"
572#include "SkSurface_Gpu.h"
573
kkinnunen179a8f52015-11-20 13:32:24 -0800574static SkSurface::Budgeted is_budgeted(SkSurface* surf) {
bsalomon3582d3e2015-02-13 14:20:05 -0800575 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800576 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
577}
578
kkinnunen179a8f52015-11-20 13:32:24 -0800579static SkSurface::Budgeted is_budgeted(SkImage* image) {
bsalomon3582d3e2015-02-13 14:20:05 -0800580 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800581 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
582}
583
kkinnunen179a8f52015-11-20 13:32:24 -0800584DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, context) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800585 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
586 for (int i = 0; i < 2; ++i) {
587 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
588 for (int j = 0; j < 2; ++j) {
589 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
590 SkAutoTUnref<SkSurface>
591 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0));
592 SkASSERT(surface);
593 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
594
mtklein31ff2982015-01-24 11:27:27 -0800595 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800596
597 // Initially the image shares a texture with the surface, and the surface decides
598 // whether it is budgeted or not.
599 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
600 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
601
602 // Now trigger copy-on-write
603 surface->getCanvas()->clear(SK_ColorBLUE);
604
605 // They don't share a texture anymore. They should each have made their own budget
606 // decision.
607 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
608 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
609 }
610 }
611}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000612#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000613
kkinnunen179a8f52015-11-20 13:32:24 -0800614static void test_no_canvas1(skiatest::Reporter* reporter,
615 SkSurface* surface,
616 SkSurface::ContentChangeMode mode) {
617 // Test passes by not asserting
618 surface->notifyContentWillChange(mode);
619 SkDEBUGCODE(surface->validate();)
620}
621static void test_no_canvas2(skiatest::Reporter* reporter,
622 SkSurface* surface,
623 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000624 // Verifies the robustness of SkSurface for handling use cases where calls
625 // are made before a canvas is created.
kkinnunen179a8f52015-11-20 13:32:24 -0800626 SkImage* image1 = surface->newImageSnapshot();
627 SkAutoTUnref<SkImage> aur_image1(image1);
628 SkDEBUGCODE(image1->validate();)
629 SkDEBUGCODE(surface->validate();)
630 surface->notifyContentWillChange(mode);
631 SkDEBUGCODE(image1->validate();)
632 SkDEBUGCODE(surface->validate();)
633 SkImage* image2 = surface->newImageSnapshot();
634 SkAutoTUnref<SkImage> aur_image2(image2);
635 SkDEBUGCODE(image2->validate();)
636 SkDEBUGCODE(surface->validate();)
637 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000638}
kkinnunen179a8f52015-11-20 13:32:24 -0800639DEF_TEST(SurfaceNoCanvas, reporter) {
640 SkSurface::ContentChangeMode modes[] =
641 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
642 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
643 for (auto& mode : modes) {
644 SkAutoTUnref<SkSurface> surface(create_surface());
645 test_func(reporter, surface, mode);
646 }
647 }
648}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000649#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800650DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, context) {
651 SkSurface::ContentChangeMode modes[] =
652 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
653 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
654 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
655 for (auto& mode : modes) {
656 SkAutoTUnref<SkSurface> surface(
657 surface_func(context, kPremul_SkAlphaType, nullptr));
658 test_func(reporter, surface, mode);
bsalomone904c092014-07-17 10:50:59 -0700659 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000660 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000661 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000662}
kkinnunen179a8f52015-11-20 13:32:24 -0800663#endif
reed9cd016e2016-01-30 10:01:06 -0800664
665static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
666 SkImageInfo info;
667 size_t rowBytes;
668 REPORTER_ASSERT(reporter, surface->peekPixels(&info, &rowBytes));
669
670 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
671 SkImageInfo im_info;
672 size_t im_rowbytes;
673 REPORTER_ASSERT(reporter, image->peekPixels(&im_info, &im_rowbytes));
674
675 REPORTER_ASSERT(reporter, rowBytes == im_rowbytes);
676
677 // trigger a copy-on-write
678 surface->getCanvas()->drawPaint(SkPaint());
679 SkAutoTUnref<SkImage> image2(surface->newImageSnapshot());
680 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
681
682 SkImageInfo im_info2;
683 size_t im_rowbytes2;
684 REPORTER_ASSERT(reporter, image2->peekPixels(&im_info2, &im_rowbytes2));
685
686 REPORTER_ASSERT(reporter, im_rowbytes2 == im_rowbytes);
687}
688
689DEF_TEST(surface_rowbytes, reporter) {
690 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
691
692 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRaster(info));
693 check_rowbytes_remain_consistent(surf0, reporter);
694
695 // specify a larger rowbytes
696 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, 500, nullptr));
697 check_rowbytes_remain_consistent(surf1, reporter);
698
699 // Try some illegal rowByte values
700 SkSurface* s = SkSurface::NewRaster(info, 396, nullptr); // needs to be at least 400
701 REPORTER_ASSERT(reporter, nullptr == s);
702 s = SkSurface::NewRaster(info, 1 << 30, nullptr); // allocation to large
703 REPORTER_ASSERT(reporter, nullptr == s);
704}