blob: 89c7765c5601326a1bc6b1ce84cf024d6ece1329 [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}
kkinnunen179a8f52015-11-20 13:32:24 -080030static SkSurface* create_surface(SkAlphaType at = kPremul_SkAlphaType,
halcanary96fcdcc2015-08-27 07:41:13 -070031 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 }
kkinnunen179a8f52015-11-20 13:32:24 -080036 return SkSurface::NewRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000037}
kkinnunen179a8f52015-11-20 13:32:24 -080038static SkSurface* create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
39 SkImageInfo* requestedInfo = nullptr) {
40 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));
46 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes,
47 release_direct_surface_storage,
48 storage);
49}
50#if SK_SUPPORT_GPU
51static SkSurface* create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
52 SkImageInfo* requestedInfo = nullptr) {
53 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
54 if (requestedInfo) {
55 *requestedInfo = info;
56 }
bsalomon5ec26ae2016-02-25 08:33:02 -080057 return SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -080058}
59static SkSurface* create_gpu_scratch_surface(GrContext* context,
60 SkAlphaType at = kPremul_SkAlphaType,
61 SkImageInfo* requestedInfo = nullptr) {
62 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
63 if (requestedInfo) {
64 *requestedInfo = info;
65 }
bsalomon5ec26ae2016-02-25 08:33:02 -080066 return SkSurface::NewRenderTarget(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);
halcanary96fcdcc2015-08-27 07:41:13 -070072 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRaster(info));
73 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRasterDirect(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
77DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, context) {
78 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
79 REPORTER_ASSERT(reporter, nullptr ==
bsalomon5ec26ae2016-02-25 08:33:02 -080080 SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -080081}
82#endif
reedb2497c22014-12-31 12:31:43 -080083
bsalomone4579ad2015-04-08 08:38:40 -070084#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080085DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedTexture, reporter, context) {
bsalomone63ffef2016-02-05 07:17:34 -080086 GrGpu* gpu = context->getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -070087 if (!gpu) {
bsalomone4579ad2015-04-08 08:38:40 -070088 return;
89 }
bsalomon7a617932015-06-16 08:07:16 -070090
jvanverth672bb7f2015-07-13 07:19:57 -070091 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
joshualitt81793412015-07-08 12:54:04 -070092 // a SkSurface.
joshualitt81793412015-07-08 12:54:04 -070093 static const int kW = 100;
94 static const int kH = 100;
95 static const uint32_t kOrigColor = 0xFFAABBCC;
96 SkAutoTArray<uint32_t> pixels(kW * kH);
97 sk_memset32(pixels.get(), kOrigColor, kW * kH);
bsalomon091f60c2015-11-10 11:54:56 -080098 GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
99 kRGBA_8888_GrPixelConfig);
bsalomone4579ad2015-04-08 08:38:40 -0700100
joshualitt81793412015-07-08 12:54:04 -0700101 GrBackendTextureDesc wrappedDesc;
102 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
103 wrappedDesc.fWidth = kW;
104 wrappedDesc.fHeight = kH;
105 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
106 wrappedDesc.fSampleCnt = 0;
107 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
bsalomon091f60c2015-11-10 11:54:56 -0800108 wrappedDesc.fTextureHandle = texHandle;
bsalomone4579ad2015-04-08 08:38:40 -0700109
kkinnunen179a8f52015-11-20 13:32:24 -0800110 SkAutoTUnref<SkSurface> surface(
111 SkSurface::NewWrappedRenderTarget(context, 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,
155 SkSurface* surface,
156 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;
184 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, &requestInfo));
185 test_canvas_peek(reporter, surface, requestInfo, true);
186 }
187}
188#if SK_SUPPORT_GPU
189DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, context) {
190 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
191 SkImageInfo requestInfo;
192 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, &requestInfo));
193 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.
kkinnunen179a8f52015-11-20 13:32:24 -0800202void test_access_pixels(skiatest::Reporter* reporter, SkSurface* surface) {
203 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 }) {
217 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, nullptr));
218 test_access_pixels(reporter, surface);
219 }
220}
221#if SK_SUPPORT_GPU
222DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAccessPixels_Gpu, reporter, context) {
223 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
224 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
225 test_access_pixels(reporter, surface);
226 }
227}
228#endif
229
230static void test_snapshot_alphatype(skiatest::Reporter* reporter, SkSurface* surface,
231 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;
245 SkAutoTUnref<SkSurface> surface(surface_func(alphaType, nullptr));
246 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
251DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, context) {
252 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;
255 SkAutoTUnref<SkSurface> surface(surface_func(context, alphaType, nullptr));
256 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) {
296 SkAutoTUnref<SkSurface> surface(create_surface());
297 test_backend_handle_access_copy_on_write(reporter, surface, accessMode,
298 handle_access_func);
299 }
300 }
301}
302#if SK_SUPPORT_GPU
303DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, context) {
304 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) {
313 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType,
314 nullptr));
315 test_backend_handle_access_copy_on_write(reporter, surface, accessMode,
316 handle_access_func);
317 }
318 }
319 }
320}
321#endif
fmalitae2639082015-08-06 07:04:51 -0700322
bsalomonf47b9a32016-02-22 11:02:58 -0800323static bool same_image(SkImage* a, SkImage* b,
324 std::function<intptr_t(SkImage*)> getImageBackingStore) {
325 return getImageBackingStore(a) == getImageBackingStore(b);
326}
327
328static bool same_image_surf(SkImage* a, SkSurface* b,
329 std::function<intptr_t(SkImage*)> getImageBackingStore,
330 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
331 return getImageBackingStore(a) == getSurfaceBackingStore(b);
332}
333
334static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
335 bool surfaceIsDirect,
336 std::function<intptr_t(SkImage*)> imageBackingStore,
337 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
338 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
339 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800340 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800341 {
reed9ce9d672016-03-17 10:51:11 -0700342 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
343 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800344 REPORTER_ASSERT(reporter, image->unique());
345 }
346 {
reed9ce9d672016-03-17 10:51:11 -0700347 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
348 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800349 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
351 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
352 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800353 REPORTER_ASSERT(reporter, image2->unique());
354 }
355 {
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
357 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
358 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
359 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800360 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700361 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800362 // If the surface is not direct then images 1 and 3 should alias the surface's
363 // store.
reed9ce9d672016-03-17 10:51:11 -0700364 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800365 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700366 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
367 !same_image(image3.get(), image2.get(), ibs) &&
368 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800369 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700370 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800371 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700372 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
373 !same_image(image3.get(), image4.get(), ibs));
374 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800375 REPORTER_ASSERT(reporter, image4->unique());
376 }
377}
378
379DEF_TEST(UniqueImageSnapshot, reporter) {
380 auto getImageBackingStore = [reporter](SkImage* image) {
381 SkPixmap pm;
382 bool success = image->peekPixels(&pm);
383 REPORTER_ASSERT(reporter, success);
384 return reinterpret_cast<intptr_t>(pm.addr());
385 };
386 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800387 SkPixmap pmap;
388 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800389 REPORTER_ASSERT(reporter, pixels);
390 return reinterpret_cast<intptr_t>(pixels);
391 };
392
393 SkAutoTUnref<SkSurface> surface(create_surface());
394 test_unique_image_snap(reporter, surface, false, getImageBackingStore, getSufaceBackingStore);
395 surface.reset(create_direct_surface());
396 test_unique_image_snap(reporter, surface, true, getImageBackingStore, getSufaceBackingStore);
397}
398
399#if SK_SUPPORT_GPU
400DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, context) {
401 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
402 SkAutoTUnref<SkSurface> surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
403
404 auto imageBackingStore = [reporter](SkImage* image) {
405 GrTexture* texture = as_IB(image)->peekTexture();
406 if (!texture) {
407 ERRORF(reporter, "Not texture backed.");
408 return static_cast<intptr_t>(0);
409 }
410 return static_cast<intptr_t>(texture->getUniqueID());
411 };
412
413 auto surfaceBackingStore = [reporter](SkSurface* surface) {
414 GrRenderTarget* rt =
415 surface->getCanvas()->internal_private_accessTopLayerRenderTarget();
416 if (!rt) {
417 ERRORF(reporter, "Not render target backed.");
418 return static_cast<intptr_t>(0);
419 }
420 return static_cast<intptr_t>(rt->getUniqueID());
421 };
422
423 test_unique_image_snap(reporter, surface, false, imageBackingStore, surfaceBackingStore);
424
425 // Test again with a "direct" render target;
426 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
427 10, 10, kRGBA_8888_GrPixelConfig);
428 GrBackendTextureDesc desc;
429 desc.fConfig = kRGBA_8888_GrPixelConfig;
430 desc.fWidth = 10;
431 desc.fHeight = 10;
432 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
433 desc.fTextureHandle = textureObject;
434 GrTexture* texture = context->textureProvider()->wrapBackendTexture(desc);
435 {
436 SkAutoTUnref<SkSurface> surface(
437 SkSurface::NewRenderTargetDirect(texture->asRenderTarget()));
bsalomonb2c01332016-02-26 10:37:26 -0800438 test_unique_image_snap(reporter, surface, true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800439 surfaceBackingStore);
440 }
441 texture->unref();
442 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
443 }
444}
445#endif
446
kkinnunen179a8f52015-11-20 13:32:24 -0800447#if SK_SUPPORT_GPU
448// May we (soon) eliminate the need to keep testing this, by hiding the bloody device!
449static uint32_t get_legacy_gen_id(SkSurface* surface) {
450 SkBaseDevice* device =
451 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
452 return device->accessBitmap(false).getGenerationID();
453}
454/*
455 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
456 * texture handle for writing.
457 *
reed9ce9d672016-03-17 10:51:11 -0700458 * Note: this needs to be tested separately from checking makeImageSnapshot, as calling that
kkinnunen179a8f52015-11-20 13:32:24 -0800459 * can also incidentally bump the genID (when a new backing surface is created).
460 */
461static void test_backend_handle_gen_id(
462 skiatest::Reporter* reporter, SkSurface* surface,
463 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
464 const uint32_t gen0 = get_legacy_gen_id(surface);
465 func(surface, SkSurface::kFlushRead_BackendHandleAccess);
466 const uint32_t gen1 = get_legacy_gen_id(surface);
467 REPORTER_ASSERT(reporter, gen0 == gen1);
468
469 func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
470 const uint32_t gen2 = get_legacy_gen_id(surface);
471 REPORTER_ASSERT(reporter, gen0 != gen2);
472
473 func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
474 const uint32_t gen3 = get_legacy_gen_id(surface);
475 REPORTER_ASSERT(reporter, gen0 != gen3);
476 REPORTER_ASSERT(reporter, gen2 != gen3);
477}
478static void test_backend_handle_unique_id(
479 skiatest::Reporter* reporter, SkSurface* surface,
480 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700481 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800482 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
483 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700484 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800485 // just read access should not affect the snapshot
486 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
487
488 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
489 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700490 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800491 // expect a new image, since we claimed we would write
492 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
493
494 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
495 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700496 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800497 // expect a new(er) image, since we claimed we would write
498 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
499 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
500}
501// No CPU test.
502DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, context) {
503 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
504 for (auto& test_func : { &test_backend_handle_unique_id, &test_backend_handle_gen_id }) {
505 for (auto& handle_access_func :
506 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
507 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType,
508 nullptr));
509 test_func(reporter, surface, handle_access_func);
510 }
511 }
512 }
513}
514#endif
515
516// Verify that the right canvas commands trigger a copy on write.
517static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000518 SkCanvas* canvas = surface->getCanvas();
519
520 const SkRect testRect =
521 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
522 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000523 SkPath testPath;
524 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
525 SkIntToScalar(2), SkIntToScalar(1)));
526
527 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
528
529 SkRegion testRegion;
530 testRegion.setRect(testIRect);
531
532
533 const SkColor testColor = 0x01020304;
534 const SkPaint testPaint;
535 const SkPoint testPoints[3] = {
536 {SkIntToScalar(0), SkIntToScalar(0)},
537 {SkIntToScalar(2), SkIntToScalar(1)},
538 {SkIntToScalar(0), SkIntToScalar(2)}
539 };
540 const size_t testPointCount = 3;
541
542 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000543 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000544 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000545
546 SkRRect testRRect;
547 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
548
549 SkString testText("Hello World");
550 const SkPoint testPoints2[] = {
551 { SkIntToScalar(0), SkIntToScalar(1) },
552 { SkIntToScalar(1), SkIntToScalar(1) },
553 { SkIntToScalar(2), SkIntToScalar(1) },
554 { SkIntToScalar(3), SkIntToScalar(1) },
555 { SkIntToScalar(4), SkIntToScalar(1) },
556 { SkIntToScalar(5), SkIntToScalar(1) },
557 { SkIntToScalar(6), SkIntToScalar(1) },
558 { SkIntToScalar(7), SkIntToScalar(1) },
559 { SkIntToScalar(8), SkIntToScalar(1) },
560 { SkIntToScalar(9), SkIntToScalar(1) },
561 { SkIntToScalar(10), SkIntToScalar(1) },
562 };
563
564#define EXPECT_COPY_ON_WRITE(command) \
565 { \
reed9ce9d672016-03-17 10:51:11 -0700566 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
567 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000568 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700569 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
570 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000571 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
572 }
573
574 EXPECT_COPY_ON_WRITE(clear(testColor))
575 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
576 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
577 testPaint))
578 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
579 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
580 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
581 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
582 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700583 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700584 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000585 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
586 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
587 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700588 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000589 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800590}
591DEF_TEST(SurfaceCopyOnWrite, reporter) {
592 SkAutoTUnref<SkSurface> surface(create_surface());
593 test_copy_on_write(reporter, surface);
594}
595#if SK_SUPPORT_GPU
596DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, context) {
597 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
598 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
599 test_copy_on_write(reporter, surface);
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) {
615 SkAutoTUnref<SkSurface> surface(create_surface());
616 test_writable_after_snapshot_release(reporter, surface);
617}
618#if SK_SUPPORT_GPU
619DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, context) {
620 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
621 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
622 test_writable_after_snapshot_release(reporter, surface);
623 }
624}
625#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000626
junov@chromium.orgb516a412013-05-01 22:49:59 +0000627#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800628static void test_crbug263329(skiatest::Reporter* reporter,
629 SkSurface* surface1,
630 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000631 // This is a regression test for crbug.com/263329
632 // Bug was caused by onCopyOnWrite releasing the old surface texture
633 // back to the scratch texture pool even though the texture is used
634 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000635 SkCanvas* canvas1 = surface1->getCanvas();
636 SkCanvas* canvas2 = surface2->getCanvas();
637 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700638 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000639 // Trigger copy on write, new backing is a scratch texture
640 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700641 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000642 // Trigger copy on write, old backing should not be returned to scratch
643 // pool because it is held by image2
644 canvas1->clear(3);
645
646 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700647 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000648 // Trigger copy on write on surface2. The new backing store should not
649 // be recycling a texture that is held by an existing image.
650 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700651 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800652 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000653 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800654 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
655 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
656 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
657 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
658 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000659}
kkinnunen179a8f52015-11-20 13:32:24 -0800660DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, context) {
661 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
662 SkAutoTUnref<SkSurface> surface1(surface_func(context, kPremul_SkAlphaType, nullptr));
663 SkAutoTUnref<SkSurface> surface2(surface_func(context, kPremul_SkAlphaType, nullptr));
664 test_crbug263329(reporter, surface1, surface2);
665 }
666}
667#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000668
kkinnunen179a8f52015-11-20 13:32:24 -0800669DEF_TEST(SurfaceGetTexture, reporter) {
670 SkAutoTUnref<SkSurface> surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700671 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800672 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800673 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800674 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800675}
676#if SK_SUPPORT_GPU
bsalomon84a4e5a2016-02-29 11:41:52 -0800677DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, context) {
kkinnunen179a8f52015-11-20 13:32:24 -0800678 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
679 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700680 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800681 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700682 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000683 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800684 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800685 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000686 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000687}
kkinnunen179a8f52015-11-20 13:32:24 -0800688#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800689
kkinnunen179a8f52015-11-20 13:32:24 -0800690#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800691#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800692#include "SkGpuDevice.h"
693#include "SkImage_Gpu.h"
694#include "SkSurface_Gpu.h"
695
bsalomon5ec26ae2016-02-25 08:33:02 -0800696static SkBudgeted is_budgeted(SkSurface* surf) {
697 return ((SkSurface_Gpu*)surf)->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
kkinnunen179a8f52015-11-20 13:32:24 -0800708DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, context) {
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 }) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800712 SkAutoTUnref<SkSurface>
713 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0));
714 SkASSERT(surface);
715 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
716
reed9ce9d672016-03-17 10:51:11 -0700717 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800718
719 // Initially the image shares a texture with the surface, and the surface decides
720 // whether it is budgeted or not.
721 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
722 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
723
724 // Now trigger copy-on-write
725 surface->getCanvas()->clear(SK_ColorBLUE);
726
727 // They don't share a texture anymore. They should each have made their own budget
728 // decision.
729 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
730 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
731 }
732 }
733}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000734#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000735
kkinnunen179a8f52015-11-20 13:32:24 -0800736static void test_no_canvas1(skiatest::Reporter* reporter,
737 SkSurface* surface,
738 SkSurface::ContentChangeMode mode) {
739 // Test passes by not asserting
740 surface->notifyContentWillChange(mode);
741 SkDEBUGCODE(surface->validate();)
742}
743static void test_no_canvas2(skiatest::Reporter* reporter,
744 SkSurface* surface,
745 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000746 // Verifies the robustness of SkSurface for handling use cases where calls
747 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700748 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
749 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800750 SkDEBUGCODE(image1->validate();)
751 SkDEBUGCODE(surface->validate();)
752 surface->notifyContentWillChange(mode);
753 SkDEBUGCODE(image1->validate();)
754 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700755 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
756 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800757 SkDEBUGCODE(image2->validate();)
758 SkDEBUGCODE(surface->validate();)
759 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000760}
kkinnunen179a8f52015-11-20 13:32:24 -0800761DEF_TEST(SurfaceNoCanvas, reporter) {
762 SkSurface::ContentChangeMode modes[] =
763 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
764 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
765 for (auto& mode : modes) {
766 SkAutoTUnref<SkSurface> surface(create_surface());
767 test_func(reporter, surface, mode);
768 }
769 }
770}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000771#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800772DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, context) {
773 SkSurface::ContentChangeMode modes[] =
774 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
775 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
776 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
777 for (auto& mode : modes) {
778 SkAutoTUnref<SkSurface> surface(
779 surface_func(context, kPremul_SkAlphaType, nullptr));
780 test_func(reporter, surface, mode);
bsalomone904c092014-07-17 10:50:59 -0700781 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000782 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000783 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000784}
kkinnunen179a8f52015-11-20 13:32:24 -0800785#endif
reed9cd016e2016-01-30 10:01:06 -0800786
787static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800788 SkPixmap surfacePM;
789 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800790
reed9ce9d672016-03-17 10:51:11 -0700791 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800792 SkPixmap pm;
793 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800794
reed6ceeebd2016-03-09 14:26:26 -0800795 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800796
797 // trigger a copy-on-write
798 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700799 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800800 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
801
reed6ceeebd2016-03-09 14:26:26 -0800802 SkPixmap pm2;
803 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
804 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800805}
806
807DEF_TEST(surface_rowbytes, reporter) {
808 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
809
810 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRaster(info));
811 check_rowbytes_remain_consistent(surf0, reporter);
812
813 // specify a larger rowbytes
814 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, 500, nullptr));
815 check_rowbytes_remain_consistent(surf1, reporter);
816
817 // Try some illegal rowByte values
818 SkSurface* s = SkSurface::NewRaster(info, 396, nullptr); // needs to be at least 400
819 REPORTER_ASSERT(reporter, nullptr == s);
820 s = SkSurface::NewRaster(info, 1 << 30, nullptr); // allocation to large
821 REPORTER_ASSERT(reporter, nullptr == s);
822}
bsalomone63ffef2016-02-05 07:17:34 -0800823
824#if SK_SUPPORT_GPU
825
bsalomon2fba8092016-02-05 13:47:06 -0800826void test_surface_clear(skiatest::Reporter* reporter, SkSurface* surfacePtr,
bsalomone63ffef2016-02-05 07:17:34 -0800827 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
828 uint32_t expectedValue) {
bsalomon2fba8092016-02-05 13:47:06 -0800829 SkAutoTUnref<SkSurface> surface(surfacePtr);
bsalomone63ffef2016-02-05 07:17:34 -0800830 if (!surface) {
831 ERRORF(reporter, "Could not create GPU SkSurface.");
832 return;
833 }
834 int w = surface->width();
835 int h = surface->height();
836 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
837 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
838
839 SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface)));
840 if (!grSurface) {
841 ERRORF(reporter, "Could access render target of GPU SkSurface.");
842 return;
843 }
844 SkASSERT(surface->unique());
bsalomon2fba8092016-02-05 13:47:06 -0800845 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800846 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
847 for (int y = 0; y < h; ++y) {
848 for (int x = 0; x < w; ++x) {
849 uint32_t pixel = pixels.get()[y * w + x];
850 if (pixel != expectedValue) {
851 SkString msg;
852 if (expectedValue) {
853 msg = "SkSurface should have left render target unmodified";
854 } else {
855 msg = "SkSurface should have cleared the render target";
856 }
857 ERRORF(reporter,
858 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
859 expectedValue, x, y);
860 return;
861 }
862 }
863 }
864}
865
866DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, context) {
867 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
bsalomon2fba8092016-02-05 13:47:06 -0800868 [] (SkSurface* s){ return s->getCanvas()->internal_private_accessTopLayerRenderTarget(); },
bsalomone63ffef2016-02-05 07:17:34 -0800869 [] (SkSurface* s){
870 SkBaseDevice* d =
871 s->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
872 return d->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700873 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
bsalomone63ffef2016-02-05 07:17:34 -0800874 return as_IB(i)->peekTexture(); },
875 };
876 for (auto grSurfaceGetter : grSurfaceGetters) {
877 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
bsalomon2fba8092016-02-05 13:47:06 -0800878 SkSurface* surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800879 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
880 }
881 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
882 static const int kWidth = 10;
883 static const int kHeight = 10;
884 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
885 memset(pixels.get(), 0xAB, sizeof(uint32_t) * kWidth * kHeight);
886
887 GrBackendObject textureObject =
888 context->getGpu()->createTestingOnlyBackendTexture(pixels.get(), kWidth, kHeight,
889 kRGBA_8888_GrPixelConfig);
890
891 GrBackendTextureDesc desc;
892 desc.fConfig = kRGBA_8888_GrPixelConfig;
893 desc.fWidth = kWidth;
894 desc.fHeight = kHeight;
895 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
896 desc.fTextureHandle = textureObject;
897
898 SkSurface* surface = SkSurface::NewFromBackendTexture(context, desc, nullptr);
899 test_surface_clear(reporter, surface, grSurfaceGetter, 0xABABABAB);
900 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
901 }
902}
903#endif