blob: ba66a2b3bb3f184290c2817497952e30ac0f78ca [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
bsalomone63ffef2016-02-05 07:17:34 -08008#include <functional>
junov@chromium.org995beb62013-03-28 13:49:22 +00009#include "SkCanvas.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000010#include "SkData.h"
reed41e010c2015-06-09 12:16:53 -070011#include "SkDevice.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
kkinnunen179a8f52015-11-20 13:32:24 -080020#include "GrContext.h"
21#include "GrGpu.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000022#endif
23
kkinnunen179a8f52015-11-20 13:32:24 -080024#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070025
kkinnunen179a8f52015-11-20 13:32:24 -080026static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070027 SkASSERT(pixels == context);
28 sk_free(pixels);
29}
reede8f30622016-03-23 18:59:25 -070030static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
31 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070032 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000033 if (requestedInfo) {
34 *requestedInfo = info;
35 }
reede8f30622016-03-23 18:59:25 -070036 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000037}
reede8f30622016-03-23 18:59:25 -070038static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
39 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080040 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
41 if (requestedInfo) {
42 *requestedInfo = info;
43 }
44 const size_t rowBytes = info.minRowBytes();
45 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070046 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
47 release_direct_surface_storage,
48 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080049}
50#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070051static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
52 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080053 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
54 if (requestedInfo) {
55 *requestedInfo = info;
56 }
reede8f30622016-03-23 18:59:25 -070057 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -080058}
reede8f30622016-03-23 18:59:25 -070059static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
60 SkAlphaType at = kPremul_SkAlphaType,
61 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080062 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
63 if (requestedInfo) {
64 *requestedInfo = info;
65 }
reede8f30622016-03-23 18:59:25 -070066 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0, nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -080067}
68#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000069
kkinnunen179a8f52015-11-20 13:32:24 -080070DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080071 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070072 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
73 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080074
reedb2497c22014-12-31 12:31:43 -080075}
kkinnunen179a8f52015-11-20 13:32:24 -080076#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -070077DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080078 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
79 REPORTER_ASSERT(reporter, nullptr ==
bsalomonf2f1c172016-04-05 12:59:06 -070080 SkSurface::MakeRenderTarget(ctxInfo.fGrContext, SkBudgeted::kNo, info, 0,
81 nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -080082}
83#endif
reedb2497c22014-12-31 12:31:43 -080084
bsalomone4579ad2015-04-08 08:38:40 -070085#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -070086DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceWrappedTexture, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -070087 GrGpu* gpu = ctxInfo.fGrContext->getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -070088 if (!gpu) {
bsalomone4579ad2015-04-08 08:38:40 -070089 return;
90 }
bsalomon7a617932015-06-16 08:07:16 -070091
jvanverth672bb7f2015-07-13 07:19:57 -070092 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
joshualitt81793412015-07-08 12:54:04 -070093 // a SkSurface.
joshualitt81793412015-07-08 12:54:04 -070094 static const int kW = 100;
95 static const int kH = 100;
96 static const uint32_t kOrigColor = 0xFFAABBCC;
97 SkAutoTArray<uint32_t> pixels(kW * kH);
98 sk_memset32(pixels.get(), kOrigColor, kW * kH);
bsalomon091f60c2015-11-10 11:54:56 -080099 GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
100 kRGBA_8888_GrPixelConfig);
bsalomone4579ad2015-04-08 08:38:40 -0700101
joshualitt81793412015-07-08 12:54:04 -0700102 GrBackendTextureDesc wrappedDesc;
103 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
104 wrappedDesc.fWidth = kW;
105 wrappedDesc.fHeight = kH;
106 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
107 wrappedDesc.fSampleCnt = 0;
108 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
bsalomon091f60c2015-11-10 11:54:56 -0800109 wrappedDesc.fTextureHandle = texHandle;
bsalomone4579ad2015-04-08 08:38:40 -0700110
bsalomonf2f1c172016-04-05 12:59:06 -0700111 auto surface(SkSurface::MakeFromBackendTexture(ctxInfo.fGrContext, wrappedDesc, nullptr));
joshualitt81793412015-07-08 12:54:04 -0700112 REPORTER_ASSERT(reporter, surface);
113 if (surface) {
114 // Validate that we can draw to the canvas and that the original texture color is preserved
115 // in pixels that aren't rendered to via the surface.
116 SkPaint paint;
117 static const SkColor kRectColor = ~kOrigColor | 0xFF000000;
118 paint.setColor(kRectColor);
119 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
120 paint);
121 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
122 surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0);
123 bool stop = false;
124 SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF),
125 (kOrigColor >> 0 & 0xFF),
126 (kOrigColor >> 8 & 0xFF),
127 (kOrigColor >> 16 & 0xFF));
128 SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
129 (kRectColor >> 16 & 0xFF),
130 (kRectColor >> 8 & 0xFF),
131 (kRectColor >> 0 & 0xFF));
132 for (int y = 0; y < kH/2 && !stop; ++y) {
133 for (int x = 0; x < kW && !stop; ++x) {
134 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
135 if (rectColorPM != pixels[x + y * kW]) {
136 stop = true;
bsalomon7a617932015-06-16 08:07:16 -0700137 }
138 }
139 }
joshualitt81793412015-07-08 12:54:04 -0700140 stop = false;
141 for (int y = kH/2; y < kH && !stop; ++y) {
142 for (int x = 0; x < kW && !stop; ++x) {
143 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
144 if (origColorPM != pixels[x + y * kW]) {
145 stop = true;
146 }
147 }
bsalomon7a617932015-06-16 08:07:16 -0700148 }
149 }
bsalomon67d76202015-11-11 12:40:42 -0800150 gpu->deleteTestingOnlyBackendTexture(texHandle);
bsalomone4579ad2015-04-08 08:38:40 -0700151}
152#endif
153
kkinnunen179a8f52015-11-20 13:32:24 -0800154static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700155 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800156 const SkImageInfo& requestInfo,
157 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000158 const SkColor color = SK_ColorRED;
159 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800160 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000161
reed6ceeebd2016-03-09 14:26:26 -0800162 SkPixmap pmap;
163 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800164 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000165
reed6ceeebd2016-03-09 14:26:26 -0800166 SkPixmap pmap2;
167 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000168
kkinnunen179a8f52015-11-20 13:32:24 -0800169 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800170 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
171 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
172 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000173
reed6ceeebd2016-03-09 14:26:26 -0800174 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
175 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
176 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800177 } else {
178 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000179 }
180}
kkinnunen179a8f52015-11-20 13:32:24 -0800181DEF_TEST(SurfaceCanvasPeek, reporter) {
182 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
183 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700184 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800185 test_canvas_peek(reporter, surface, requestInfo, true);
186 }
187}
188#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700189DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800190 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
191 SkImageInfo requestInfo;
bsalomonf2f1c172016-04-05 12:59:06 -0700192 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800193 test_canvas_peek(reporter, surface, requestInfo, false);
194 }
195}
196#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000197
reed41e010c2015-06-09 12:16:53 -0700198// For compatibility with clients that still call accessBitmap(), we need to ensure that we bump
199// the bitmap's genID when we draw to it, else they won't know it has new values. When they are
200// exclusively using surface/image, and we can hide accessBitmap from device, we can remove this
201// test.
reede8f30622016-03-23 18:59:25 -0700202void test_access_pixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface) {
kkinnunen179a8f52015-11-20 13:32:24 -0800203 SkCanvas* canvas = surface->getCanvas();
204 canvas->clear(0);
reed41e010c2015-06-09 12:16:53 -0700205
kkinnunen179a8f52015-11-20 13:32:24 -0800206 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
207 SkBitmap bm = device->accessBitmap(false);
208 uint32_t genID0 = bm.getGenerationID();
209 // Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write)
210 canvas->drawColor(SK_ColorBLUE);
211 // Now check that we get a different genID
212 uint32_t genID1 = bm.getGenerationID();
213 REPORTER_ASSERT(reporter, genID0 != genID1);
214}
215DEF_TEST(SurfaceAccessPixels, reporter) {
216 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
reede8f30622016-03-23 18:59:25 -0700217 auto surface(surface_func(kPremul_SkAlphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800218 test_access_pixels(reporter, surface);
219 }
220}
221#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700222DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAccessPixels_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800223 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomonf2f1c172016-04-05 12:59:06 -0700224 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800225 test_access_pixels(reporter, surface);
226 }
227}
228#endif
229
reede8f30622016-03-23 18:59:25 -0700230static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800231 bool expectOpaque) {
232 REPORTER_ASSERT(reporter, surface);
233 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700234 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800235 REPORTER_ASSERT(reporter, image);
236 if (image) {
237 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(expectOpaque));
reed41e010c2015-06-09 12:16:53 -0700238 }
239 }
240}
kkinnunen179a8f52015-11-20 13:32:24 -0800241DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
242 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
243 for (auto& isOpaque : { true, false }) {
244 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reede8f30622016-03-23 18:59:25 -0700245 auto surface(surface_func(alphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800246 test_snapshot_alphatype(reporter, surface, isOpaque);
bsalomon74f681d2015-06-23 14:38:48 -0700247 }
248 }
249}
kkinnunen179a8f52015-11-20 13:32:24 -0800250#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700251DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800252 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
253 for (auto& isOpaque : { true, false }) {
254 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
bsalomonf2f1c172016-04-05 12:59:06 -0700255 auto surface(surface_func(ctxInfo.fGrContext, alphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800256 test_snapshot_alphatype(reporter, surface, isOpaque);
257 }
258 }
259}
260#endif
bsalomon74f681d2015-06-23 14:38:48 -0700261
kkinnunen179a8f52015-11-20 13:32:24 -0800262static GrBackendObject get_surface_backend_texture_handle(
263 SkSurface* s, SkSurface::BackendHandleAccess a) {
264 return s->getTextureHandle(a);
265}
266static GrBackendObject get_surface_backend_render_target_handle(
267 SkSurface* s, SkSurface::BackendHandleAccess a) {
268 GrBackendObject result;
269 if (!s->getRenderTargetHandle(&result, a)) {
270 return 0;
271 }
272 return result;
273}
274
275static void test_backend_handle_access_copy_on_write(
276 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
277 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700278 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700279 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700280
281 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700282 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700283
284 // If the access mode triggers CoW, then the backend objects should reflect it.
285 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
286}
kkinnunen179a8f52015-11-20 13:32:24 -0800287DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
288 const SkSurface::BackendHandleAccess accessModes[] = {
289 SkSurface::kFlushRead_BackendHandleAccess,
290 SkSurface::kFlushWrite_BackendHandleAccess,
291 SkSurface::kDiscardWrite_BackendHandleAccess,
292 };
293 for (auto& handle_access_func :
294 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
295 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700296 auto surface(create_surface());
297 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800298 handle_access_func);
299 }
300 }
301}
302#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700303DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800304 const SkSurface::BackendHandleAccess accessModes[] = {
305 SkSurface::kFlushRead_BackendHandleAccess,
306 SkSurface::kFlushWrite_BackendHandleAccess,
307 SkSurface::kDiscardWrite_BackendHandleAccess,
308 };
309 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
310 for (auto& handle_access_func :
311 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
312 for (auto& accessMode : accessModes) {
bsalomonf2f1c172016-04-05 12:59:06 -0700313 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700314 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800315 handle_access_func);
316 }
317 }
318 }
319}
320#endif
fmalitae2639082015-08-06 07:04:51 -0700321
bsalomonf47b9a32016-02-22 11:02:58 -0800322static bool same_image(SkImage* a, SkImage* b,
323 std::function<intptr_t(SkImage*)> getImageBackingStore) {
324 return getImageBackingStore(a) == getImageBackingStore(b);
325}
326
327static bool same_image_surf(SkImage* a, SkSurface* b,
328 std::function<intptr_t(SkImage*)> getImageBackingStore,
329 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
330 return getImageBackingStore(a) == getSurfaceBackingStore(b);
331}
332
333static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
334 bool surfaceIsDirect,
335 std::function<intptr_t(SkImage*)> imageBackingStore,
336 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
337 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
338 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800339 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800340 {
reed9ce9d672016-03-17 10:51:11 -0700341 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
342 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800343 REPORTER_ASSERT(reporter, image->unique());
344 }
345 {
reed9ce9d672016-03-17 10:51:11 -0700346 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
347 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800348 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700349 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
350 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
351 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800352 REPORTER_ASSERT(reporter, image2->unique());
353 }
354 {
reed9ce9d672016-03-17 10:51:11 -0700355 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
356 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
357 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
358 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800359 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700360 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800361 // If the surface is not direct then images 1 and 3 should alias the surface's
362 // store.
reed9ce9d672016-03-17 10:51:11 -0700363 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800364 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700365 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
366 !same_image(image3.get(), image2.get(), ibs) &&
367 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800368 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700369 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800370 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700371 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
372 !same_image(image3.get(), image4.get(), ibs));
373 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800374 REPORTER_ASSERT(reporter, image4->unique());
375 }
376}
377
378DEF_TEST(UniqueImageSnapshot, reporter) {
379 auto getImageBackingStore = [reporter](SkImage* image) {
380 SkPixmap pm;
381 bool success = image->peekPixels(&pm);
382 REPORTER_ASSERT(reporter, success);
383 return reinterpret_cast<intptr_t>(pm.addr());
384 };
385 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800386 SkPixmap pmap;
387 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800388 REPORTER_ASSERT(reporter, pixels);
389 return reinterpret_cast<intptr_t>(pixels);
390 };
391
reede8f30622016-03-23 18:59:25 -0700392 auto surface(create_surface());
393 test_unique_image_snap(reporter, surface.get(), false, getImageBackingStore,
394 getSufaceBackingStore);
395 surface = create_direct_surface();
396 test_unique_image_snap(reporter, surface.get(), true, getImageBackingStore,
397 getSufaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800398}
399
400#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700401DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700402 GrContext* context = ctxInfo.fGrContext;
bsalomonf47b9a32016-02-22 11:02:58 -0800403 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700404 auto surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
bsalomonf47b9a32016-02-22 11:02:58 -0800405
406 auto imageBackingStore = [reporter](SkImage* image) {
407 GrTexture* texture = as_IB(image)->peekTexture();
408 if (!texture) {
409 ERRORF(reporter, "Not texture backed.");
410 return static_cast<intptr_t>(0);
411 }
412 return static_cast<intptr_t>(texture->getUniqueID());
413 };
414
415 auto surfaceBackingStore = [reporter](SkSurface* surface) {
416 GrRenderTarget* rt =
417 surface->getCanvas()->internal_private_accessTopLayerRenderTarget();
418 if (!rt) {
419 ERRORF(reporter, "Not render target backed.");
420 return static_cast<intptr_t>(0);
421 }
422 return static_cast<intptr_t>(rt->getUniqueID());
423 };
424
reede8f30622016-03-23 18:59:25 -0700425 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
426 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800427
428 // Test again with a "direct" render target;
429 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
430 10, 10, kRGBA_8888_GrPixelConfig);
431 GrBackendTextureDesc desc;
432 desc.fConfig = kRGBA_8888_GrPixelConfig;
433 desc.fWidth = 10;
434 desc.fHeight = 10;
435 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
436 desc.fTextureHandle = textureObject;
437 GrTexture* texture = context->textureProvider()->wrapBackendTexture(desc);
438 {
reede8f30622016-03-23 18:59:25 -0700439 auto surface(SkSurface::MakeRenderTargetDirect(texture->asRenderTarget()));
440 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800441 surfaceBackingStore);
442 }
443 texture->unref();
444 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
445 }
446}
447#endif
448
kkinnunen179a8f52015-11-20 13:32:24 -0800449#if SK_SUPPORT_GPU
450// May we (soon) eliminate the need to keep testing this, by hiding the bloody device!
451static uint32_t get_legacy_gen_id(SkSurface* surface) {
452 SkBaseDevice* device =
453 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
454 return device->accessBitmap(false).getGenerationID();
455}
456/*
457 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
458 * texture handle for writing.
459 *
reed9ce9d672016-03-17 10:51:11 -0700460 * Note: this needs to be tested separately from checking makeImageSnapshot, as calling that
kkinnunen179a8f52015-11-20 13:32:24 -0800461 * can also incidentally bump the genID (when a new backing surface is created).
462 */
463static void test_backend_handle_gen_id(
464 skiatest::Reporter* reporter, SkSurface* surface,
465 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
466 const uint32_t gen0 = get_legacy_gen_id(surface);
467 func(surface, SkSurface::kFlushRead_BackendHandleAccess);
468 const uint32_t gen1 = get_legacy_gen_id(surface);
469 REPORTER_ASSERT(reporter, gen0 == gen1);
470
471 func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
472 const uint32_t gen2 = get_legacy_gen_id(surface);
473 REPORTER_ASSERT(reporter, gen0 != gen2);
474
475 func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
476 const uint32_t gen3 = get_legacy_gen_id(surface);
477 REPORTER_ASSERT(reporter, gen0 != gen3);
478 REPORTER_ASSERT(reporter, gen2 != gen3);
479}
480static void test_backend_handle_unique_id(
481 skiatest::Reporter* reporter, SkSurface* surface,
482 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700483 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800484 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
485 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700486 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800487 // just read access should not affect the snapshot
488 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
489
490 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
491 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700492 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800493 // expect a new image, since we claimed we would write
494 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
495
496 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
497 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700498 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800499 // expect a new(er) image, since we claimed we would write
500 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
501 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
502}
503// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700504DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800505 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
506 for (auto& test_func : { &test_backend_handle_unique_id, &test_backend_handle_gen_id }) {
507 for (auto& handle_access_func :
508 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomonf2f1c172016-04-05 12:59:06 -0700509 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700510 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800511 }
512 }
513 }
514}
515#endif
516
517// Verify that the right canvas commands trigger a copy on write.
518static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000519 SkCanvas* canvas = surface->getCanvas();
520
521 const SkRect testRect =
522 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
523 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000524 SkPath testPath;
525 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
526 SkIntToScalar(2), SkIntToScalar(1)));
527
528 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
529
530 SkRegion testRegion;
531 testRegion.setRect(testIRect);
532
533
534 const SkColor testColor = 0x01020304;
535 const SkPaint testPaint;
536 const SkPoint testPoints[3] = {
537 {SkIntToScalar(0), SkIntToScalar(0)},
538 {SkIntToScalar(2), SkIntToScalar(1)},
539 {SkIntToScalar(0), SkIntToScalar(2)}
540 };
541 const size_t testPointCount = 3;
542
543 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000544 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000545 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000546
547 SkRRect testRRect;
548 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
549
550 SkString testText("Hello World");
551 const SkPoint testPoints2[] = {
552 { SkIntToScalar(0), SkIntToScalar(1) },
553 { SkIntToScalar(1), SkIntToScalar(1) },
554 { SkIntToScalar(2), SkIntToScalar(1) },
555 { SkIntToScalar(3), SkIntToScalar(1) },
556 { SkIntToScalar(4), SkIntToScalar(1) },
557 { SkIntToScalar(5), SkIntToScalar(1) },
558 { SkIntToScalar(6), SkIntToScalar(1) },
559 { SkIntToScalar(7), SkIntToScalar(1) },
560 { SkIntToScalar(8), SkIntToScalar(1) },
561 { SkIntToScalar(9), SkIntToScalar(1) },
562 { SkIntToScalar(10), SkIntToScalar(1) },
563 };
564
565#define EXPECT_COPY_ON_WRITE(command) \
566 { \
reed9ce9d672016-03-17 10:51:11 -0700567 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
568 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000569 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700570 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
571 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000572 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
573 }
574
575 EXPECT_COPY_ON_WRITE(clear(testColor))
576 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
577 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
578 testPaint))
579 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
580 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
581 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
582 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
583 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700584 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700585 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000586 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
587 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
588 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700589 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000590 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800591}
592DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700593 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800594}
595#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700596DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800597 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomonf2f1c172016-04-05 12:59:06 -0700598 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700599 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700600 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000601}
kkinnunen179a8f52015-11-20 13:32:24 -0800602#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000603
kkinnunen179a8f52015-11-20 13:32:24 -0800604static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
605 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000606 // This test succeeds by not triggering an assertion.
607 // The test verifies that the surface remains writable (usable) after
608 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000609 SkCanvas* canvas = surface->getCanvas();
610 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700611 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000612 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000613}
kkinnunen179a8f52015-11-20 13:32:24 -0800614DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700615 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800616}
617#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700618DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800619 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomonf2f1c172016-04-05 12:59:06 -0700620 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700621 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800622 }
623}
624#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000625
junov@chromium.orgb516a412013-05-01 22:49:59 +0000626#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800627static void test_crbug263329(skiatest::Reporter* reporter,
628 SkSurface* surface1,
629 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000630 // This is a regression test for crbug.com/263329
631 // Bug was caused by onCopyOnWrite releasing the old surface texture
632 // back to the scratch texture pool even though the texture is used
633 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000634 SkCanvas* canvas1 = surface1->getCanvas();
635 SkCanvas* canvas2 = surface2->getCanvas();
636 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700637 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000638 // Trigger copy on write, new backing is a scratch texture
639 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700640 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000641 // Trigger copy on write, old backing should not be returned to scratch
642 // pool because it is held by image2
643 canvas1->clear(3);
644
645 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700646 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000647 // Trigger copy on write on surface2. The new backing store should not
648 // be recycling a texture that is held by an existing image.
649 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700650 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800651 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000652 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800653 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
654 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
655 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
656 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
657 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000658}
bsalomon758586c2016-04-06 14:02:39 -0700659DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800660 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomonf2f1c172016-04-05 12:59:06 -0700661 auto surface1(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
662 auto surface2(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700663 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800664 }
665}
666#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000667
kkinnunen179a8f52015-11-20 13:32:24 -0800668DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700669 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700670 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800671 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800672 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800673 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800674}
675#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700676DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800677 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomonf2f1c172016-04-05 12:59:06 -0700678 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700679 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800680 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700681 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000682 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800683 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800684 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000685 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000686}
kkinnunen179a8f52015-11-20 13:32:24 -0800687#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800688
kkinnunen179a8f52015-11-20 13:32:24 -0800689#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800690#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800691#include "SkGpuDevice.h"
692#include "SkImage_Gpu.h"
693#include "SkSurface_Gpu.h"
694
reede8f30622016-03-23 18:59:25 -0700695static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
696 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
697 return gsurf->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800698}
699
bsalomon5ec26ae2016-02-25 08:33:02 -0800700static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800701 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800702}
703
reed9ce9d672016-03-17 10:51:11 -0700704static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
705 return is_budgeted(image.get());
706}
707
bsalomon758586c2016-04-06 14:02:39 -0700708DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800709 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800710 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
711 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomonf2f1c172016-04-05 12:59:06 -0700712 auto surface(SkSurface::MakeRenderTarget(ctxInfo.fGrContext, sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800713 SkASSERT(surface);
714 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
715
reed9ce9d672016-03-17 10:51:11 -0700716 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800717
718 // Initially the image shares a texture with the surface, and the surface decides
719 // whether it is budgeted or not.
720 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
721 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
722
723 // Now trigger copy-on-write
724 surface->getCanvas()->clear(SK_ColorBLUE);
725
726 // They don't share a texture anymore. They should each have made their own budget
727 // decision.
728 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
729 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
730 }
731 }
732}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000733#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000734
kkinnunen179a8f52015-11-20 13:32:24 -0800735static void test_no_canvas1(skiatest::Reporter* reporter,
736 SkSurface* surface,
737 SkSurface::ContentChangeMode mode) {
738 // Test passes by not asserting
739 surface->notifyContentWillChange(mode);
740 SkDEBUGCODE(surface->validate();)
741}
742static void test_no_canvas2(skiatest::Reporter* reporter,
743 SkSurface* surface,
744 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000745 // Verifies the robustness of SkSurface for handling use cases where calls
746 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700747 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
748 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800749 SkDEBUGCODE(image1->validate();)
750 SkDEBUGCODE(surface->validate();)
751 surface->notifyContentWillChange(mode);
752 SkDEBUGCODE(image1->validate();)
753 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700754 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
755 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800756 SkDEBUGCODE(image2->validate();)
757 SkDEBUGCODE(surface->validate();)
758 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000759}
kkinnunen179a8f52015-11-20 13:32:24 -0800760DEF_TEST(SurfaceNoCanvas, reporter) {
761 SkSurface::ContentChangeMode modes[] =
762 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
763 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
764 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700765 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800766 }
767 }
768}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000769#if SK_SUPPORT_GPU
bsalomon758586c2016-04-06 14:02:39 -0700770DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800771 SkSurface::ContentChangeMode modes[] =
772 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
773 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
774 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
775 for (auto& mode : modes) {
bsalomonf2f1c172016-04-05 12:59:06 -0700776 auto surface(surface_func(ctxInfo.fGrContext, kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700777 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700778 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000779 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000780 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000781}
kkinnunen179a8f52015-11-20 13:32:24 -0800782#endif
reed9cd016e2016-01-30 10:01:06 -0800783
784static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800785 SkPixmap surfacePM;
786 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800787
reed9ce9d672016-03-17 10:51:11 -0700788 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800789 SkPixmap pm;
790 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800791
reed6ceeebd2016-03-09 14:26:26 -0800792 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800793
794 // trigger a copy-on-write
795 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700796 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800797 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
798
reed6ceeebd2016-03-09 14:26:26 -0800799 SkPixmap pm2;
800 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
801 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800802}
803
804DEF_TEST(surface_rowbytes, reporter) {
805 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
806
reede8f30622016-03-23 18:59:25 -0700807 auto surf0(SkSurface::MakeRaster(info));
808 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800809
810 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700811 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
812 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800813
814 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700815 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800816 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700817 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800818 REPORTER_ASSERT(reporter, nullptr == s);
819}
bsalomone63ffef2016-02-05 07:17:34 -0800820
821#if SK_SUPPORT_GPU
822
reede8f30622016-03-23 18:59:25 -0700823void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
bsalomone63ffef2016-02-05 07:17:34 -0800824 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
825 uint32_t expectedValue) {
826 if (!surface) {
827 ERRORF(reporter, "Could not create GPU SkSurface.");
828 return;
829 }
830 int w = surface->width();
831 int h = surface->height();
832 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
833 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
834
reede8f30622016-03-23 18:59:25 -0700835 SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800836 if (!grSurface) {
837 ERRORF(reporter, "Could access render target of GPU SkSurface.");
838 return;
839 }
bsalomon2fba8092016-02-05 13:47:06 -0800840 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800841 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
842 for (int y = 0; y < h; ++y) {
843 for (int x = 0; x < w; ++x) {
844 uint32_t pixel = pixels.get()[y * w + x];
845 if (pixel != expectedValue) {
846 SkString msg;
847 if (expectedValue) {
848 msg = "SkSurface should have left render target unmodified";
849 } else {
850 msg = "SkSurface should have cleared the render target";
851 }
852 ERRORF(reporter,
853 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
854 expectedValue, x, y);
855 return;
856 }
857 }
858 }
859}
860
bsalomon758586c2016-04-06 14:02:39 -0700861DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700862 GrContext* context = ctxInfo.fGrContext;
bsalomone63ffef2016-02-05 07:17:34 -0800863 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
bsalomon2fba8092016-02-05 13:47:06 -0800864 [] (SkSurface* s){ return s->getCanvas()->internal_private_accessTopLayerRenderTarget(); },
bsalomone63ffef2016-02-05 07:17:34 -0800865 [] (SkSurface* s){
866 SkBaseDevice* d =
867 s->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
868 return d->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700869 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
bsalomone63ffef2016-02-05 07:17:34 -0800870 return as_IB(i)->peekTexture(); },
871 };
872 for (auto grSurfaceGetter : grSurfaceGetters) {
873 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700874 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800875 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
876 }
877 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
878 static const int kWidth = 10;
879 static const int kHeight = 10;
880 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
881 memset(pixels.get(), 0xAB, sizeof(uint32_t) * kWidth * kHeight);
882
883 GrBackendObject textureObject =
884 context->getGpu()->createTestingOnlyBackendTexture(pixels.get(), kWidth, kHeight,
885 kRGBA_8888_GrPixelConfig);
886
887 GrBackendTextureDesc desc;
888 desc.fConfig = kRGBA_8888_GrPixelConfig;
889 desc.fWidth = kWidth;
890 desc.fHeight = kHeight;
891 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
892 desc.fTextureHandle = textureObject;
893
reede8f30622016-03-23 18:59:25 -0700894 auto surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800895 test_surface_clear(reporter, surface, grSurfaceGetter, 0xABABABAB);
896 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
897 }
898}
899#endif