blob: 2ac8d29b3c0ef0951a1e0d375487218efd455a66 [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"
brianosman0e22eb82016-08-30 07:07:59 -070010#include "SkColorSpace_Base.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000011#include "SkData.h"
reed41e010c2015-06-09 12:16:53 -070012#include "SkDevice.h"
bsalomon55812362015-06-10 08:49:28 -070013#include "SkImage_Base.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000015#include "SkRRect.h"
16#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000017#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000018#include "Test.h"
19
20#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080021#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040022#include "GrRenderTargetContext.h"
kkinnunen179a8f52015-11-20 13:32:24 -080023#include "GrGpu.h"
ericrkc4025182016-05-04 12:01:58 -070024#include "GrResourceProvider.h"
brianosman0e22eb82016-08-30 07:07:59 -070025#include <vector>
junov@chromium.org995beb62013-03-28 13:49:22 +000026#endif
27
kkinnunen179a8f52015-11-20 13:32:24 -080028#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070029
kkinnunen179a8f52015-11-20 13:32:24 -080030static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070031 SkASSERT(pixels == context);
32 sk_free(pixels);
33}
reede8f30622016-03-23 18:59:25 -070034static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
35 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070036 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000037 if (requestedInfo) {
38 *requestedInfo = info;
39 }
reede8f30622016-03-23 18:59:25 -070040 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000041}
reede8f30622016-03-23 18:59:25 -070042static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
43 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080044 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
45 if (requestedInfo) {
46 *requestedInfo = info;
47 }
48 const size_t rowBytes = info.minRowBytes();
49 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070050 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
51 release_direct_surface_storage,
52 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080053}
54#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070055static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
56 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080057 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
58 if (requestedInfo) {
59 *requestedInfo = info;
60 }
robertphillips7e922762016-07-26 11:38:17 -070061 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080062}
reede8f30622016-03-23 18:59:25 -070063static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
64 SkAlphaType at = kPremul_SkAlphaType,
65 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080066 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
67 if (requestedInfo) {
68 *requestedInfo = info;
69 }
robertphillips7e922762016-07-26 11:38:17 -070070 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080071}
72#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000073
kkinnunen179a8f52015-11-20 13:32:24 -080074DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080075 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070076 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
77 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080078
reedb2497c22014-12-31 12:31:43 -080079}
kkinnunen179a8f52015-11-20 13:32:24 -080080#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070081DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080082 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
83 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070084 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080085}
86#endif
reedb2497c22014-12-31 12:31:43 -080087
kkinnunen179a8f52015-11-20 13:32:24 -080088static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070089 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080090 const SkImageInfo& requestInfo,
91 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000092 const SkColor color = SK_ColorRED;
93 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080094 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000095
reed6ceeebd2016-03-09 14:26:26 -080096 SkPixmap pmap;
97 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -080098 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000099
reed6ceeebd2016-03-09 14:26:26 -0800100 SkPixmap pmap2;
101 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000102
kkinnunen179a8f52015-11-20 13:32:24 -0800103 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800104 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
105 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
106 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000107
reed6ceeebd2016-03-09 14:26:26 -0800108 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
109 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
110 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800111 } else {
112 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000113 }
114}
kkinnunen179a8f52015-11-20 13:32:24 -0800115DEF_TEST(SurfaceCanvasPeek, reporter) {
116 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
117 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700118 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800119 test_canvas_peek(reporter, surface, requestInfo, true);
120 }
121}
122#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700123DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800124 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
125 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700126 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800127 test_canvas_peek(reporter, surface, requestInfo, false);
128 }
129}
130#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000131
reede8f30622016-03-23 18:59:25 -0700132static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700133 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800134 REPORTER_ASSERT(reporter, surface);
135 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700136 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800137 REPORTER_ASSERT(reporter, image);
138 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700139 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700140 }
141 }
142}
kkinnunen179a8f52015-11-20 13:32:24 -0800143DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
144 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700145 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
146 auto surface(surface_func(at, nullptr));
147 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700148 }
149 }
150}
kkinnunen179a8f52015-11-20 13:32:24 -0800151#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700152DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800153 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700154 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
155 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
156 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
157 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800158 }
159 }
160}
161#endif
bsalomon74f681d2015-06-23 14:38:48 -0700162
kkinnunen179a8f52015-11-20 13:32:24 -0800163static GrBackendObject get_surface_backend_texture_handle(
164 SkSurface* s, SkSurface::BackendHandleAccess a) {
165 return s->getTextureHandle(a);
166}
167static GrBackendObject get_surface_backend_render_target_handle(
168 SkSurface* s, SkSurface::BackendHandleAccess a) {
169 GrBackendObject result;
170 if (!s->getRenderTargetHandle(&result, a)) {
171 return 0;
172 }
173 return result;
174}
175
176static void test_backend_handle_access_copy_on_write(
177 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
178 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700179 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700180 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700181
182 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700183 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700184
185 // If the access mode triggers CoW, then the backend objects should reflect it.
186 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
187}
kkinnunen179a8f52015-11-20 13:32:24 -0800188DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
189 const SkSurface::BackendHandleAccess accessModes[] = {
190 SkSurface::kFlushRead_BackendHandleAccess,
191 SkSurface::kFlushWrite_BackendHandleAccess,
192 SkSurface::kDiscardWrite_BackendHandleAccess,
193 };
194 for (auto& handle_access_func :
195 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
196 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700197 auto surface(create_surface());
198 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800199 handle_access_func);
200 }
201 }
202}
203#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700204DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800205 const SkSurface::BackendHandleAccess accessModes[] = {
206 SkSurface::kFlushRead_BackendHandleAccess,
207 SkSurface::kFlushWrite_BackendHandleAccess,
208 SkSurface::kDiscardWrite_BackendHandleAccess,
209 };
210 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
211 for (auto& handle_access_func :
212 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
213 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700214 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700215 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800216 handle_access_func);
217 }
218 }
219 }
220}
221#endif
fmalitae2639082015-08-06 07:04:51 -0700222
bsalomonf47b9a32016-02-22 11:02:58 -0800223static bool same_image(SkImage* a, SkImage* b,
224 std::function<intptr_t(SkImage*)> getImageBackingStore) {
225 return getImageBackingStore(a) == getImageBackingStore(b);
226}
227
228static bool same_image_surf(SkImage* a, SkSurface* b,
229 std::function<intptr_t(SkImage*)> getImageBackingStore,
230 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
231 return getImageBackingStore(a) == getSurfaceBackingStore(b);
232}
233
234static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
235 bool surfaceIsDirect,
236 std::function<intptr_t(SkImage*)> imageBackingStore,
237 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
238 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
239 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800240 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800241 {
reed9ce9d672016-03-17 10:51:11 -0700242 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
243 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800244 REPORTER_ASSERT(reporter, image->unique());
245 }
246 {
reed9ce9d672016-03-17 10:51:11 -0700247 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
248 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800249 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700250 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
251 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
252 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800253 REPORTER_ASSERT(reporter, image2->unique());
254 }
255 {
reed9ce9d672016-03-17 10:51:11 -0700256 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
257 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
258 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
259 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800260 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700261 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800262 // If the surface is not direct then images 1 and 3 should alias the surface's
263 // store.
reed9ce9d672016-03-17 10:51:11 -0700264 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800265 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700266 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
267 !same_image(image3.get(), image2.get(), ibs) &&
268 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800269 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700270 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800271 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700272 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
273 !same_image(image3.get(), image4.get(), ibs));
274 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800275 REPORTER_ASSERT(reporter, image4->unique());
276 }
277}
278
279DEF_TEST(UniqueImageSnapshot, reporter) {
280 auto getImageBackingStore = [reporter](SkImage* image) {
281 SkPixmap pm;
282 bool success = image->peekPixels(&pm);
283 REPORTER_ASSERT(reporter, success);
284 return reinterpret_cast<intptr_t>(pm.addr());
285 };
286 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800287 SkPixmap pmap;
288 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800289 REPORTER_ASSERT(reporter, pixels);
290 return reinterpret_cast<intptr_t>(pixels);
291 };
292
reede8f30622016-03-23 18:59:25 -0700293 auto surface(create_surface());
294 test_unique_image_snap(reporter, surface.get(), false, getImageBackingStore,
295 getSufaceBackingStore);
296 surface = create_direct_surface();
297 test_unique_image_snap(reporter, surface.get(), true, getImageBackingStore,
298 getSufaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800299}
300
301#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700302DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700303 GrContext* context = ctxInfo.grContext();
bsalomonf47b9a32016-02-22 11:02:58 -0800304 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700305 auto surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
bsalomonf47b9a32016-02-22 11:02:58 -0800306
307 auto imageBackingStore = [reporter](SkImage* image) {
308 GrTexture* texture = as_IB(image)->peekTexture();
309 if (!texture) {
310 ERRORF(reporter, "Not texture backed.");
311 return static_cast<intptr_t>(0);
312 }
Robert Phillips294870f2016-11-11 12:38:40 -0500313 return static_cast<intptr_t>(texture->uniqueID().asUInt());
bsalomonf47b9a32016-02-22 11:02:58 -0800314 };
315
316 auto surfaceBackingStore = [reporter](SkSurface* surface) {
Brian Osman11052242016-10-27 14:47:55 -0400317 GrRenderTargetContext* rtc =
318 surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
319 GrRenderTarget* rt = rtc->accessRenderTarget();
bsalomonf47b9a32016-02-22 11:02:58 -0800320 if (!rt) {
321 ERRORF(reporter, "Not render target backed.");
322 return static_cast<intptr_t>(0);
323 }
Robert Phillips294870f2016-11-11 12:38:40 -0500324 return static_cast<intptr_t>(rt->uniqueID().asUInt());
bsalomonf47b9a32016-02-22 11:02:58 -0800325 };
326
reede8f30622016-03-23 18:59:25 -0700327 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
328 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800329
330 // Test again with a "direct" render target;
331 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
egdaniel0a3a7f72016-06-24 09:22:31 -0700332 10, 10, kRGBA_8888_GrPixelConfig, true);
bsalomonf47b9a32016-02-22 11:02:58 -0800333 GrBackendTextureDesc desc;
334 desc.fConfig = kRGBA_8888_GrPixelConfig;
335 desc.fWidth = 10;
336 desc.fHeight = 10;
337 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
338 desc.fTextureHandle = textureObject;
robertphillips7e922762016-07-26 11:38:17 -0700339
bsalomonf47b9a32016-02-22 11:02:58 -0800340 {
robertphillips7e922762016-07-26 11:38:17 -0700341 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTexture(context, desc, nullptr));
reede8f30622016-03-23 18:59:25 -0700342 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800343 surfaceBackingStore);
344 }
robertphillips7e922762016-07-26 11:38:17 -0700345
bsalomonf47b9a32016-02-22 11:02:58 -0800346 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
347 }
348}
349#endif
350
kkinnunen179a8f52015-11-20 13:32:24 -0800351#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800352
kkinnunen179a8f52015-11-20 13:32:24 -0800353static void test_backend_handle_unique_id(
354 skiatest::Reporter* reporter, SkSurface* surface,
355 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800357 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
358 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700359 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800360 // just read access should not affect the snapshot
361 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
362
363 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
364 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700365 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800366 // expect a new image, since we claimed we would write
367 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
368
369 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
370 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700371 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800372 // expect a new(er) image, since we claimed we would write
373 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
374 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
375}
376// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700377DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800378 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700379 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800380 for (auto& handle_access_func :
381 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700382 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700383 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800384 }
385 }
386 }
387}
388#endif
389
390// Verify that the right canvas commands trigger a copy on write.
391static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000392 SkCanvas* canvas = surface->getCanvas();
393
394 const SkRect testRect =
395 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
396 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000397 SkPath testPath;
398 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
399 SkIntToScalar(2), SkIntToScalar(1)));
400
401 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
402
403 SkRegion testRegion;
404 testRegion.setRect(testIRect);
405
406
407 const SkColor testColor = 0x01020304;
408 const SkPaint testPaint;
409 const SkPoint testPoints[3] = {
410 {SkIntToScalar(0), SkIntToScalar(0)},
411 {SkIntToScalar(2), SkIntToScalar(1)},
412 {SkIntToScalar(0), SkIntToScalar(2)}
413 };
414 const size_t testPointCount = 3;
415
416 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000417 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000418 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000419
420 SkRRect testRRect;
421 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
422
423 SkString testText("Hello World");
424 const SkPoint testPoints2[] = {
425 { SkIntToScalar(0), SkIntToScalar(1) },
426 { SkIntToScalar(1), SkIntToScalar(1) },
427 { SkIntToScalar(2), SkIntToScalar(1) },
428 { SkIntToScalar(3), SkIntToScalar(1) },
429 { SkIntToScalar(4), SkIntToScalar(1) },
430 { SkIntToScalar(5), SkIntToScalar(1) },
431 { SkIntToScalar(6), SkIntToScalar(1) },
432 { SkIntToScalar(7), SkIntToScalar(1) },
433 { SkIntToScalar(8), SkIntToScalar(1) },
434 { SkIntToScalar(9), SkIntToScalar(1) },
435 { SkIntToScalar(10), SkIntToScalar(1) },
436 };
437
438#define EXPECT_COPY_ON_WRITE(command) \
439 { \
reed9ce9d672016-03-17 10:51:11 -0700440 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
441 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000442 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700443 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
444 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000445 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
446 }
447
448 EXPECT_COPY_ON_WRITE(clear(testColor))
449 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
450 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
451 testPaint))
452 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
453 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
454 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
455 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
456 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700457 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700458 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000459 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
460 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
461 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700462 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000463 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800464}
465DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700466 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800467}
468#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700469DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800470 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700471 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700472 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700473 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000474}
kkinnunen179a8f52015-11-20 13:32:24 -0800475#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000476
kkinnunen179a8f52015-11-20 13:32:24 -0800477static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
478 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000479 // This test succeeds by not triggering an assertion.
480 // The test verifies that the surface remains writable (usable) after
481 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000482 SkCanvas* canvas = surface->getCanvas();
483 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700484 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000485 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000486}
kkinnunen179a8f52015-11-20 13:32:24 -0800487DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700488 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800489}
490#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700491DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800492 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700493 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700494 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800495 }
496}
497#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000498
junov@chromium.orgb516a412013-05-01 22:49:59 +0000499#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800500static void test_crbug263329(skiatest::Reporter* reporter,
501 SkSurface* surface1,
502 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000503 // This is a regression test for crbug.com/263329
504 // Bug was caused by onCopyOnWrite releasing the old surface texture
505 // back to the scratch texture pool even though the texture is used
506 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000507 SkCanvas* canvas1 = surface1->getCanvas();
508 SkCanvas* canvas2 = surface2->getCanvas();
509 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700510 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000511 // Trigger copy on write, new backing is a scratch texture
512 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700513 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000514 // Trigger copy on write, old backing should not be returned to scratch
515 // pool because it is held by image2
516 canvas1->clear(3);
517
518 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700519 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000520 // Trigger copy on write on surface2. The new backing store should not
521 // be recycling a texture that is held by an existing image.
522 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700523 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800524 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000525 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800526 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
527 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
528 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
529 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
530 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000531}
egdanielab527a52016-06-28 08:07:26 -0700532DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800533 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700534 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
535 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700536 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800537 }
538}
539#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000540
kkinnunen179a8f52015-11-20 13:32:24 -0800541DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700542 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700543 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800544 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800545 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800546 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800547}
548#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700549DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800550 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700551 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700552 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800553 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700554 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000555 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800556 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800557 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000558 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000559}
kkinnunen179a8f52015-11-20 13:32:24 -0800560#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800561
kkinnunen179a8f52015-11-20 13:32:24 -0800562#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800563#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800564#include "SkGpuDevice.h"
565#include "SkImage_Gpu.h"
566#include "SkSurface_Gpu.h"
567
reede8f30622016-03-23 18:59:25 -0700568static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
569 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Brian Osman11052242016-10-27 14:47:55 -0400570 return gsurf->getDevice()->accessRenderTargetContext()
571 ->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800572}
573
bsalomon5ec26ae2016-02-25 08:33:02 -0800574static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800575 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800576}
577
reed9ce9d672016-03-17 10:51:11 -0700578static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
579 return is_budgeted(image.get());
580}
581
egdanielab527a52016-06-28 08:07:26 -0700582DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800583 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800584 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
585 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700586 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800587 SkASSERT(surface);
588 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
589
reed9ce9d672016-03-17 10:51:11 -0700590 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800591
592 // Initially the image shares a texture with the surface, and the surface decides
593 // whether it is budgeted or not.
594 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
595 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
596
597 // Now trigger copy-on-write
598 surface->getCanvas()->clear(SK_ColorBLUE);
599
600 // They don't share a texture anymore. They should each have made their own budget
601 // decision.
602 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
603 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
604 }
605 }
606}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000607#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000608
kkinnunen179a8f52015-11-20 13:32:24 -0800609static void test_no_canvas1(skiatest::Reporter* reporter,
610 SkSurface* surface,
611 SkSurface::ContentChangeMode mode) {
612 // Test passes by not asserting
613 surface->notifyContentWillChange(mode);
614 SkDEBUGCODE(surface->validate();)
615}
616static void test_no_canvas2(skiatest::Reporter* reporter,
617 SkSurface* surface,
618 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000619 // Verifies the robustness of SkSurface for handling use cases where calls
620 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700621 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
622 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800623 SkDEBUGCODE(image1->validate();)
624 SkDEBUGCODE(surface->validate();)
625 surface->notifyContentWillChange(mode);
626 SkDEBUGCODE(image1->validate();)
627 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700628 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
629 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800630 SkDEBUGCODE(image2->validate();)
631 SkDEBUGCODE(surface->validate();)
632 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000633}
kkinnunen179a8f52015-11-20 13:32:24 -0800634DEF_TEST(SurfaceNoCanvas, reporter) {
635 SkSurface::ContentChangeMode modes[] =
636 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
637 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
638 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700639 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800640 }
641 }
642}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000643#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700644DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800645 SkSurface::ContentChangeMode modes[] =
646 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
647 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
648 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
649 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700650 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700651 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700652 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000653 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000654 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000655}
kkinnunen179a8f52015-11-20 13:32:24 -0800656#endif
reed9cd016e2016-01-30 10:01:06 -0800657
658static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800659 SkPixmap surfacePM;
660 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800661
reed9ce9d672016-03-17 10:51:11 -0700662 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800663 SkPixmap pm;
664 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800665
reed6ceeebd2016-03-09 14:26:26 -0800666 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800667
668 // trigger a copy-on-write
669 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700670 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800671 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
672
reed6ceeebd2016-03-09 14:26:26 -0800673 SkPixmap pm2;
674 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
675 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800676}
677
678DEF_TEST(surface_rowbytes, reporter) {
679 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
680
reede8f30622016-03-23 18:59:25 -0700681 auto surf0(SkSurface::MakeRaster(info));
682 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800683
684 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700685 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
686 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800687
688 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700689 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800690 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700691 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800692 REPORTER_ASSERT(reporter, nullptr == s);
693}
bsalomone63ffef2016-02-05 07:17:34 -0800694
fmalita03912f12016-07-06 06:22:06 -0700695DEF_TEST(surface_raster_zeroinitialized, reporter) {
696 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
697 SkPixmap pixmap;
698 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
699
700 for (int i = 0; i < pixmap.info().width(); ++i) {
701 for (int j = 0; j < pixmap.info().height(); ++j) {
702 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
703 }
704 }
705}
706
bsalomone63ffef2016-02-05 07:17:34 -0800707#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700708static sk_sp<SkSurface> create_gpu_surface_backend_texture(
709 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
710 const int kWidth = 10;
711 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400712 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700713 sk_memset32(pixels.get(), color, kWidth * kHeight);
714 GrBackendTextureDesc desc;
715 desc.fConfig = kRGBA_8888_GrPixelConfig;
716 desc.fWidth = kWidth;
717 desc.fHeight = kHeight;
718 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
719 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700720 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700721 desc.fSampleCnt = sampleCnt;
722 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
723 if (!surface) {
724 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
725 return nullptr;
726 }
727 *outTexture = desc.fTextureHandle;
728 return surface;
729}
bsalomone63ffef2016-02-05 07:17:34 -0800730
ericrkc4025182016-05-04 12:01:58 -0700731static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
732 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
733 const int kWidth = 10;
734 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400735 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700736 sk_memset32(pixels.get(), color, kWidth * kHeight);
737 GrBackendTextureDesc desc;
738 desc.fConfig = kRGBA_8888_GrPixelConfig;
739 desc.fWidth = kWidth;
740 desc.fHeight = kHeight;
741 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
742 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700743 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700744 desc.fSampleCnt = sampleCnt;
745 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
746 nullptr);
747 if (!surface) {
748 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
749 return nullptr;
750 }
751 *outTexture = desc.fTextureHandle;
752 return surface;
753}
754
755static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
756 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
757 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800758 if (!surface) {
759 ERRORF(reporter, "Could not create GPU SkSurface.");
760 return;
761 }
762 int w = surface->width();
763 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400764 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700765 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800766
Hal Canary342b7ac2016-11-04 11:49:42 -0400767 sk_sp<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800768 if (!grSurface) {
769 ERRORF(reporter, "Could access render target of GPU SkSurface.");
770 return;
771 }
bsalomon2fba8092016-02-05 13:47:06 -0800772 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800773 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
774 for (int y = 0; y < h; ++y) {
775 for (int x = 0; x < w; ++x) {
776 uint32_t pixel = pixels.get()[y * w + x];
777 if (pixel != expectedValue) {
778 SkString msg;
779 if (expectedValue) {
780 msg = "SkSurface should have left render target unmodified";
781 } else {
782 msg = "SkSurface should have cleared the render target";
783 }
784 ERRORF(reporter,
785 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
786 expectedValue, x, y);
787 return;
788 }
789 }
790 }
791}
792
bsalomon758586c2016-04-06 14:02:39 -0700793DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700794 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700795
bsalomone63ffef2016-02-05 07:17:34 -0800796 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700797 [] (SkSurface* s){
Brian Osman11052242016-10-27 14:47:55 -0400798 GrRenderTargetContext* rtc =
799 s->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
800 return rtc->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700801 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
ericrkc4025182016-05-04 12:01:58 -0700802 return as_IB(i)->peekTexture(); }
bsalomone63ffef2016-02-05 07:17:34 -0800803 };
ericrkc4025182016-05-04 12:01:58 -0700804
bsalomone63ffef2016-02-05 07:17:34 -0800805 for (auto grSurfaceGetter : grSurfaceGetters) {
ericrkc4025182016-05-04 12:01:58 -0700806 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800807 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700808 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800809 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
810 }
811 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700812 const uint32_t kOrigColor = 0xABABABAB;
813 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
814 &create_gpu_surface_backend_texture_as_render_target}) {
815 GrBackendObject textureObject;
816 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
817 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
818 surface.reset();
819 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
820 }
821 }
822}
bsalomone63ffef2016-02-05 07:17:34 -0800823
ericrkc4025182016-05-04 12:01:58 -0700824static void test_surface_draw_partially(
825 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
826 const int kW = surface->width();
827 const int kH = surface->height();
828 SkPaint paint;
829 const SkColor kRectColor = ~origColor | 0xFF000000;
830 paint.setColor(kRectColor);
831 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
832 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400833 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700834 sk_memset32(pixels.get(), ~origColor, kW * kH);
835 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
836 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
837 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
838 bool stop = false;
839 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
840 (origColor >> 0 & 0xFF),
841 (origColor >> 8 & 0xFF),
842 (origColor >> 16 & 0xFF));
843 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
844 (kRectColor >> 16 & 0xFF),
845 (kRectColor >> 8 & 0xFF),
846 (kRectColor >> 0 & 0xFF));
847 for (int y = 0; y < kH/2 && !stop; ++y) {
848 for (int x = 0; x < kW && !stop; ++x) {
849 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
850 if (rectColorPM != pixels[x + y * kW]) {
851 stop = true;
852 }
853 }
854 }
855 stop = false;
856 for (int y = kH/2; y < kH && !stop; ++y) {
857 for (int x = 0; x < kW && !stop; ++x) {
858 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
859 if (origColorPM != pixels[x + y * kW]) {
860 stop = true;
861 }
862 }
863 }
864}
bsalomone63ffef2016-02-05 07:17:34 -0800865
egdanielab527a52016-06-28 08:07:26 -0700866DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700867 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700868 if (!gpu) {
869 return;
870 }
871 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800872
ericrkc4025182016-05-04 12:01:58 -0700873 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
874 &create_gpu_surface_backend_texture_as_render_target}) {
875 // Validate that we can draw to the canvas and that the original texture color is
876 // preserved in pixels that aren't rendered to via the surface.
877 // This works only for non-multisampled case.
878 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700879 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700880 if (surface) {
881 test_surface_draw_partially(reporter, surface, kOrigColor);
882 surface.reset();
883 gpu->deleteTestingOnlyBackendTexture(textureObject);
884 }
885 }
886}
887
888
889DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700890 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700891 if (!gpu) {
892 return;
893 }
894 static const uint32_t kOrigColor = 0xFFAABBCC;
895
896 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
897 &create_gpu_surface_backend_texture_as_render_target}) {
898 for (int sampleCnt : {0, 4, 8}) {
899 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700900 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700901
902 if (!surface && sampleCnt > 0) {
903 // Certain platforms don't support MSAA, skip these.
904 continue;
905 }
906
907 // Validate that we can attach a stencil buffer to an SkSurface created by either of
908 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400909 GrRenderTarget* rt = surface->getCanvas()
910 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700911 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700912 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700913 gpu->deleteTestingOnlyBackendTexture(textureObject);
914 }
bsalomone63ffef2016-02-05 07:17:34 -0800915 }
916}
917#endif
brianosman0e22eb82016-08-30 07:07:59 -0700918
919static void test_surface_creation_and_snapshot_with_color_space(
920 skiatest::Reporter* reporter,
921 const char* prefix,
922 bool f16Support,
923 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
924
Brian Osman526972e2016-10-24 09:24:02 -0400925 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
926 auto adobeColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700927 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
928 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400929 SkColorSpaceTransferFn oddGamma;
930 oddGamma.fA = 1.0f;
931 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
932 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400933 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
934 auto linearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosman0e22eb82016-08-30 07:07:59 -0700935
936 const struct {
937 SkColorType fColorType;
938 sk_sp<SkColorSpace> fColorSpace;
939 bool fShouldWork;
940 const char* fDescription;
941 } testConfigs[] = {
942 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
943 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
944 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
945 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
946 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Brian Osmaneb21ef62016-11-01 16:30:21 -0400947 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700948 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
949 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
950 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
951 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
952 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
953 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
954 };
955
956 for (auto& testConfig : testConfigs) {
957 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
958 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
959 testConfig.fColorSpace);
960
961 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
962 // any surface of that type:
963 bool shouldWork = testConfig.fShouldWork &&
964 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
965
966 auto surface(surfaceMaker(info));
967 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
968
969 if (shouldWork && surface) {
970 sk_sp<SkImage> image(surface->makeImageSnapshot());
971 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
972 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
973 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
974 fullTestName.c_str());
975 }
976 }
977}
978
979DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
980 auto surfaceMaker = [](const SkImageInfo& info) {
981 return SkSurface::MakeRaster(info);
982 };
983
984 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
985}
986
987#if SK_SUPPORT_GPU
988DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
989 GrContext* context = ctxInfo.grContext();
990 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
991 auto surfaceMaker = [context](const SkImageInfo& info) {
992 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
993 };
994
995 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
996
997 std::vector<GrBackendObject> textureHandles;
998 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
999 GrBackendTextureDesc desc;
1000 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
1001 desc.fWidth = 10;
1002 desc.fHeight = 10;
1003 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
1004 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
1005 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
1006
1007 if (!desc.fTextureHandle) {
1008 return sk_sp<SkSurface>(nullptr);
1009 }
1010 textureHandles.push_back(desc.fTextureHandle);
1011
1012 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
1013 nullptr);
1014 };
1015
1016 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
1017 wrappedSurfaceMaker);
1018
1019 for (auto textureHandle : textureHandles) {
1020 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
1021 }
1022}
1023#endif