blob: ec8e4a90590b4c9dc761c8add3e168334ddf7971 [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"
Matt Sarette11b6142016-11-28 18:28:07 -050014#include "SkOverdrawCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070015#include "SkPath.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000016#include "SkRRect.h"
17#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000018#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000019#include "Test.h"
20
21#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080022#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040023#include "GrRenderTargetContext.h"
kkinnunen179a8f52015-11-20 13:32:24 -080024#include "GrGpu.h"
ericrkc4025182016-05-04 12:01:58 -070025#include "GrResourceProvider.h"
brianosman0e22eb82016-08-30 07:07:59 -070026#include <vector>
junov@chromium.org995beb62013-03-28 13:49:22 +000027#endif
28
kkinnunen179a8f52015-11-20 13:32:24 -080029#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070030
kkinnunen179a8f52015-11-20 13:32:24 -080031static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070032 SkASSERT(pixels == context);
33 sk_free(pixels);
34}
reede8f30622016-03-23 18:59:25 -070035static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
36 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070037 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000038 if (requestedInfo) {
39 *requestedInfo = info;
40 }
reede8f30622016-03-23 18:59:25 -070041 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000042}
reede8f30622016-03-23 18:59:25 -070043static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
44 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080045 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
46 if (requestedInfo) {
47 *requestedInfo = info;
48 }
49 const size_t rowBytes = info.minRowBytes();
50 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070051 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
52 release_direct_surface_storage,
53 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080054}
55#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070056static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
57 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080058 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
59 if (requestedInfo) {
60 *requestedInfo = info;
61 }
robertphillips7e922762016-07-26 11:38:17 -070062 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080063}
reede8f30622016-03-23 18:59:25 -070064static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
65 SkAlphaType at = kPremul_SkAlphaType,
66 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080067 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
68 if (requestedInfo) {
69 *requestedInfo = info;
70 }
robertphillips7e922762016-07-26 11:38:17 -070071 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080072}
73#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000074
kkinnunen179a8f52015-11-20 13:32:24 -080075DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080076 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070077 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
78 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080079
reedb2497c22014-12-31 12:31:43 -080080}
kkinnunen179a8f52015-11-20 13:32:24 -080081#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070082DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080083 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
84 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070085 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080086}
87#endif
reedb2497c22014-12-31 12:31:43 -080088
kkinnunen179a8f52015-11-20 13:32:24 -080089static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070090 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080091 const SkImageInfo& requestInfo,
92 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000093 const SkColor color = SK_ColorRED;
94 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080095 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000096
reed6ceeebd2016-03-09 14:26:26 -080097 SkPixmap pmap;
98 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -080099 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000100
reed6ceeebd2016-03-09 14:26:26 -0800101 SkPixmap pmap2;
102 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000103
kkinnunen179a8f52015-11-20 13:32:24 -0800104 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800105 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
106 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
107 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000108
reed6ceeebd2016-03-09 14:26:26 -0800109 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
110 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
111 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800112 } else {
113 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000114 }
115}
kkinnunen179a8f52015-11-20 13:32:24 -0800116DEF_TEST(SurfaceCanvasPeek, reporter) {
117 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
118 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700119 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800120 test_canvas_peek(reporter, surface, requestInfo, true);
121 }
122}
123#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700124DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800125 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
126 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700127 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800128 test_canvas_peek(reporter, surface, requestInfo, false);
129 }
130}
131#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000132
reede8f30622016-03-23 18:59:25 -0700133static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700134 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800135 REPORTER_ASSERT(reporter, surface);
136 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700137 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800138 REPORTER_ASSERT(reporter, image);
139 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700140 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700141 }
142 }
143}
kkinnunen179a8f52015-11-20 13:32:24 -0800144DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
145 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700146 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
147 auto surface(surface_func(at, nullptr));
148 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700149 }
150 }
151}
kkinnunen179a8f52015-11-20 13:32:24 -0800152#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700153DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800154 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700155 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
156 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
157 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
158 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800159 }
160 }
161}
162#endif
bsalomon74f681d2015-06-23 14:38:48 -0700163
kkinnunen179a8f52015-11-20 13:32:24 -0800164static GrBackendObject get_surface_backend_texture_handle(
165 SkSurface* s, SkSurface::BackendHandleAccess a) {
166 return s->getTextureHandle(a);
167}
168static GrBackendObject get_surface_backend_render_target_handle(
169 SkSurface* s, SkSurface::BackendHandleAccess a) {
170 GrBackendObject result;
171 if (!s->getRenderTargetHandle(&result, a)) {
172 return 0;
173 }
174 return result;
175}
176
177static void test_backend_handle_access_copy_on_write(
178 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
179 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700180 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700181 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700182
183 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700184 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700185
186 // If the access mode triggers CoW, then the backend objects should reflect it.
187 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
188}
kkinnunen179a8f52015-11-20 13:32:24 -0800189DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
190 const SkSurface::BackendHandleAccess accessModes[] = {
191 SkSurface::kFlushRead_BackendHandleAccess,
192 SkSurface::kFlushWrite_BackendHandleAccess,
193 SkSurface::kDiscardWrite_BackendHandleAccess,
194 };
195 for (auto& handle_access_func :
196 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
197 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700198 auto surface(create_surface());
199 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800200 handle_access_func);
201 }
202 }
203}
204#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700205DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800206 const SkSurface::BackendHandleAccess accessModes[] = {
207 SkSurface::kFlushRead_BackendHandleAccess,
208 SkSurface::kFlushWrite_BackendHandleAccess,
209 SkSurface::kDiscardWrite_BackendHandleAccess,
210 };
211 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
212 for (auto& handle_access_func :
213 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
214 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700215 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700216 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800217 handle_access_func);
218 }
219 }
220 }
221}
222#endif
fmalitae2639082015-08-06 07:04:51 -0700223
bsalomonf47b9a32016-02-22 11:02:58 -0800224static bool same_image(SkImage* a, SkImage* b,
225 std::function<intptr_t(SkImage*)> getImageBackingStore) {
226 return getImageBackingStore(a) == getImageBackingStore(b);
227}
228
229static bool same_image_surf(SkImage* a, SkSurface* b,
230 std::function<intptr_t(SkImage*)> getImageBackingStore,
231 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
232 return getImageBackingStore(a) == getSurfaceBackingStore(b);
233}
234
235static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
236 bool surfaceIsDirect,
237 std::function<intptr_t(SkImage*)> imageBackingStore,
238 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
239 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
240 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800241 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800242 {
reed9ce9d672016-03-17 10:51:11 -0700243 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
244 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800245 REPORTER_ASSERT(reporter, image->unique());
246 }
247 {
reed9ce9d672016-03-17 10:51:11 -0700248 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
249 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800250 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700251 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
252 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
253 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800254 REPORTER_ASSERT(reporter, image2->unique());
255 }
256 {
reed9ce9d672016-03-17 10:51:11 -0700257 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
258 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
259 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
260 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800261 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700262 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800263 // If the surface is not direct then images 1 and 3 should alias the surface's
264 // store.
reed9ce9d672016-03-17 10:51:11 -0700265 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800266 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700267 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
268 !same_image(image3.get(), image2.get(), ibs) &&
269 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800270 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700271 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800272 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700273 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
274 !same_image(image3.get(), image4.get(), ibs));
275 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800276 REPORTER_ASSERT(reporter, image4->unique());
277 }
278}
279
280DEF_TEST(UniqueImageSnapshot, reporter) {
281 auto getImageBackingStore = [reporter](SkImage* image) {
282 SkPixmap pm;
283 bool success = image->peekPixels(&pm);
284 REPORTER_ASSERT(reporter, success);
285 return reinterpret_cast<intptr_t>(pm.addr());
286 };
287 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800288 SkPixmap pmap;
289 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800290 REPORTER_ASSERT(reporter, pixels);
291 return reinterpret_cast<intptr_t>(pixels);
292 };
293
reede8f30622016-03-23 18:59:25 -0700294 auto surface(create_surface());
295 test_unique_image_snap(reporter, surface.get(), false, getImageBackingStore,
296 getSufaceBackingStore);
297 surface = create_direct_surface();
298 test_unique_image_snap(reporter, surface.get(), true, getImageBackingStore,
299 getSufaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800300}
301
302#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700303DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700304 GrContext* context = ctxInfo.grContext();
bsalomonf47b9a32016-02-22 11:02:58 -0800305 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700306 auto surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
bsalomonf47b9a32016-02-22 11:02:58 -0800307
308 auto imageBackingStore = [reporter](SkImage* image) {
309 GrTexture* texture = as_IB(image)->peekTexture();
310 if (!texture) {
311 ERRORF(reporter, "Not texture backed.");
312 return static_cast<intptr_t>(0);
313 }
Robert Phillips294870f2016-11-11 12:38:40 -0500314 return static_cast<intptr_t>(texture->uniqueID().asUInt());
bsalomonf47b9a32016-02-22 11:02:58 -0800315 };
316
317 auto surfaceBackingStore = [reporter](SkSurface* surface) {
Brian Osman11052242016-10-27 14:47:55 -0400318 GrRenderTargetContext* rtc =
319 surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
320 GrRenderTarget* rt = rtc->accessRenderTarget();
bsalomonf47b9a32016-02-22 11:02:58 -0800321 if (!rt) {
322 ERRORF(reporter, "Not render target backed.");
323 return static_cast<intptr_t>(0);
324 }
Robert Phillips294870f2016-11-11 12:38:40 -0500325 return static_cast<intptr_t>(rt->uniqueID().asUInt());
bsalomonf47b9a32016-02-22 11:02:58 -0800326 };
327
reede8f30622016-03-23 18:59:25 -0700328 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
329 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800330
331 // Test again with a "direct" render target;
332 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
egdaniel0a3a7f72016-06-24 09:22:31 -0700333 10, 10, kRGBA_8888_GrPixelConfig, true);
bsalomonf47b9a32016-02-22 11:02:58 -0800334 GrBackendTextureDesc desc;
335 desc.fConfig = kRGBA_8888_GrPixelConfig;
336 desc.fWidth = 10;
337 desc.fHeight = 10;
338 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
339 desc.fTextureHandle = textureObject;
robertphillips7e922762016-07-26 11:38:17 -0700340
bsalomonf47b9a32016-02-22 11:02:58 -0800341 {
robertphillips7e922762016-07-26 11:38:17 -0700342 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTexture(context, desc, nullptr));
reede8f30622016-03-23 18:59:25 -0700343 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800344 surfaceBackingStore);
345 }
robertphillips7e922762016-07-26 11:38:17 -0700346
bsalomonf47b9a32016-02-22 11:02:58 -0800347 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
348 }
349}
350#endif
351
kkinnunen179a8f52015-11-20 13:32:24 -0800352#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800353
kkinnunen179a8f52015-11-20 13:32:24 -0800354static void test_backend_handle_unique_id(
355 skiatest::Reporter* reporter, SkSurface* surface,
356 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700357 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800358 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
359 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700360 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800361 // just read access should not affect the snapshot
362 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
363
364 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
365 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700366 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800367 // expect a new image, since we claimed we would write
368 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
369
370 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
371 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700372 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800373 // expect a new(er) image, since we claimed we would write
374 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
375 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
376}
377// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700378DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800379 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700380 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800381 for (auto& handle_access_func :
382 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700383 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700384 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800385 }
386 }
387 }
388}
389#endif
390
391// Verify that the right canvas commands trigger a copy on write.
392static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000393 SkCanvas* canvas = surface->getCanvas();
394
395 const SkRect testRect =
396 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
397 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000398 SkPath testPath;
399 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
400 SkIntToScalar(2), SkIntToScalar(1)));
401
402 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
403
404 SkRegion testRegion;
405 testRegion.setRect(testIRect);
406
407
408 const SkColor testColor = 0x01020304;
409 const SkPaint testPaint;
410 const SkPoint testPoints[3] = {
411 {SkIntToScalar(0), SkIntToScalar(0)},
412 {SkIntToScalar(2), SkIntToScalar(1)},
413 {SkIntToScalar(0), SkIntToScalar(2)}
414 };
415 const size_t testPointCount = 3;
416
417 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000418 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000419 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000420
421 SkRRect testRRect;
422 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
423
424 SkString testText("Hello World");
425 const SkPoint testPoints2[] = {
426 { SkIntToScalar(0), SkIntToScalar(1) },
427 { SkIntToScalar(1), SkIntToScalar(1) },
428 { SkIntToScalar(2), SkIntToScalar(1) },
429 { SkIntToScalar(3), SkIntToScalar(1) },
430 { SkIntToScalar(4), SkIntToScalar(1) },
431 { SkIntToScalar(5), SkIntToScalar(1) },
432 { SkIntToScalar(6), SkIntToScalar(1) },
433 { SkIntToScalar(7), SkIntToScalar(1) },
434 { SkIntToScalar(8), SkIntToScalar(1) },
435 { SkIntToScalar(9), SkIntToScalar(1) },
436 { SkIntToScalar(10), SkIntToScalar(1) },
437 };
438
439#define EXPECT_COPY_ON_WRITE(command) \
440 { \
reed9ce9d672016-03-17 10:51:11 -0700441 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
442 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000443 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700444 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
445 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000446 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
447 }
448
449 EXPECT_COPY_ON_WRITE(clear(testColor))
450 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
451 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
452 testPaint))
453 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
454 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
455 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
456 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
457 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700458 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700459 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000460 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
461 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
462 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700463 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000464 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800465}
466DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700467 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800468}
469#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700470DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800471 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700472 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700473 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700474 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000475}
kkinnunen179a8f52015-11-20 13:32:24 -0800476#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000477
kkinnunen179a8f52015-11-20 13:32:24 -0800478static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
479 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000480 // This test succeeds by not triggering an assertion.
481 // The test verifies that the surface remains writable (usable) after
482 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000483 SkCanvas* canvas = surface->getCanvas();
484 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700485 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000486 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000487}
kkinnunen179a8f52015-11-20 13:32:24 -0800488DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700489 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800490}
491#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700492DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800493 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700494 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700495 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800496 }
497}
498#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000499
junov@chromium.orgb516a412013-05-01 22:49:59 +0000500#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800501static void test_crbug263329(skiatest::Reporter* reporter,
502 SkSurface* surface1,
503 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000504 // This is a regression test for crbug.com/263329
505 // Bug was caused by onCopyOnWrite releasing the old surface texture
506 // back to the scratch texture pool even though the texture is used
507 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000508 SkCanvas* canvas1 = surface1->getCanvas();
509 SkCanvas* canvas2 = surface2->getCanvas();
510 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700511 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000512 // Trigger copy on write, new backing is a scratch texture
513 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700514 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000515 // Trigger copy on write, old backing should not be returned to scratch
516 // pool because it is held by image2
517 canvas1->clear(3);
518
519 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700520 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000521 // Trigger copy on write on surface2. The new backing store should not
522 // be recycling a texture that is held by an existing image.
523 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700524 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800525 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000526 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800527 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
528 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
529 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
530 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
531 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000532}
egdanielab527a52016-06-28 08:07:26 -0700533DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800534 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700535 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
536 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700537 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800538 }
539}
540#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000541
kkinnunen179a8f52015-11-20 13:32:24 -0800542DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700543 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700544 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800545 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800546 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800547 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800548}
549#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700550DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800551 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700552 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700553 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800554 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700555 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000556 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800557 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800558 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000559 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000560}
kkinnunen179a8f52015-11-20 13:32:24 -0800561#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800562
kkinnunen179a8f52015-11-20 13:32:24 -0800563#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800564#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800565#include "SkGpuDevice.h"
566#include "SkImage_Gpu.h"
567#include "SkSurface_Gpu.h"
568
reede8f30622016-03-23 18:59:25 -0700569static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
570 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Brian Osman11052242016-10-27 14:47:55 -0400571 return gsurf->getDevice()->accessRenderTargetContext()
572 ->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800573}
574
bsalomon5ec26ae2016-02-25 08:33:02 -0800575static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800576 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800577}
578
reed9ce9d672016-03-17 10:51:11 -0700579static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
580 return is_budgeted(image.get());
581}
582
egdanielab527a52016-06-28 08:07:26 -0700583DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800584 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800585 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
586 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700587 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800588 SkASSERT(surface);
589 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
590
reed9ce9d672016-03-17 10:51:11 -0700591 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800592
593 // Initially the image shares a texture with the surface, and the surface decides
594 // whether it is budgeted or not.
595 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
596 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
597
598 // Now trigger copy-on-write
599 surface->getCanvas()->clear(SK_ColorBLUE);
600
601 // They don't share a texture anymore. They should each have made their own budget
602 // decision.
603 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
604 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
605 }
606 }
607}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000608#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000609
kkinnunen179a8f52015-11-20 13:32:24 -0800610static void test_no_canvas1(skiatest::Reporter* reporter,
611 SkSurface* surface,
612 SkSurface::ContentChangeMode mode) {
613 // Test passes by not asserting
614 surface->notifyContentWillChange(mode);
615 SkDEBUGCODE(surface->validate();)
616}
617static void test_no_canvas2(skiatest::Reporter* reporter,
618 SkSurface* surface,
619 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000620 // Verifies the robustness of SkSurface for handling use cases where calls
621 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700622 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
623 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800624 SkDEBUGCODE(image1->validate();)
625 SkDEBUGCODE(surface->validate();)
626 surface->notifyContentWillChange(mode);
627 SkDEBUGCODE(image1->validate();)
628 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700629 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
630 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800631 SkDEBUGCODE(image2->validate();)
632 SkDEBUGCODE(surface->validate();)
633 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000634}
kkinnunen179a8f52015-11-20 13:32:24 -0800635DEF_TEST(SurfaceNoCanvas, reporter) {
636 SkSurface::ContentChangeMode modes[] =
637 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
638 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
639 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700640 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800641 }
642 }
643}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000644#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700645DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800646 SkSurface::ContentChangeMode modes[] =
647 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
648 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
649 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
650 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700651 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700652 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700653 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000654 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000655 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000656}
kkinnunen179a8f52015-11-20 13:32:24 -0800657#endif
reed9cd016e2016-01-30 10:01:06 -0800658
659static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800660 SkPixmap surfacePM;
661 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800662
reed9ce9d672016-03-17 10:51:11 -0700663 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800664 SkPixmap pm;
665 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800666
reed6ceeebd2016-03-09 14:26:26 -0800667 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800668
669 // trigger a copy-on-write
670 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700671 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800672 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
673
reed6ceeebd2016-03-09 14:26:26 -0800674 SkPixmap pm2;
675 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
676 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800677}
678
679DEF_TEST(surface_rowbytes, reporter) {
680 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
681
reede8f30622016-03-23 18:59:25 -0700682 auto surf0(SkSurface::MakeRaster(info));
683 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800684
685 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700686 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
687 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800688
689 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700690 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800691 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700692 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800693 REPORTER_ASSERT(reporter, nullptr == s);
694}
bsalomone63ffef2016-02-05 07:17:34 -0800695
fmalita03912f12016-07-06 06:22:06 -0700696DEF_TEST(surface_raster_zeroinitialized, reporter) {
697 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
698 SkPixmap pixmap;
699 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
700
701 for (int i = 0; i < pixmap.info().width(); ++i) {
702 for (int j = 0; j < pixmap.info().height(); ++j) {
703 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
704 }
705 }
706}
707
bsalomone63ffef2016-02-05 07:17:34 -0800708#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700709static sk_sp<SkSurface> create_gpu_surface_backend_texture(
710 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
711 const int kWidth = 10;
712 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400713 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700714 sk_memset32(pixels.get(), color, kWidth * kHeight);
715 GrBackendTextureDesc desc;
716 desc.fConfig = kRGBA_8888_GrPixelConfig;
717 desc.fWidth = kWidth;
718 desc.fHeight = kHeight;
719 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
720 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700721 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700722 desc.fSampleCnt = sampleCnt;
723 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
724 if (!surface) {
725 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
726 return nullptr;
727 }
728 *outTexture = desc.fTextureHandle;
729 return surface;
730}
bsalomone63ffef2016-02-05 07:17:34 -0800731
ericrkc4025182016-05-04 12:01:58 -0700732static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
733 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
734 const int kWidth = 10;
735 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400736 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700737 sk_memset32(pixels.get(), color, kWidth * kHeight);
738 GrBackendTextureDesc desc;
739 desc.fConfig = kRGBA_8888_GrPixelConfig;
740 desc.fWidth = kWidth;
741 desc.fHeight = kHeight;
742 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
743 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700744 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700745 desc.fSampleCnt = sampleCnt;
746 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
747 nullptr);
748 if (!surface) {
749 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
750 return nullptr;
751 }
752 *outTexture = desc.fTextureHandle;
753 return surface;
754}
755
756static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
757 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
758 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800759 if (!surface) {
760 ERRORF(reporter, "Could not create GPU SkSurface.");
761 return;
762 }
763 int w = surface->width();
764 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400765 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700766 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800767
Hal Canary342b7ac2016-11-04 11:49:42 -0400768 sk_sp<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800769 if (!grSurface) {
770 ERRORF(reporter, "Could access render target of GPU SkSurface.");
771 return;
772 }
bsalomon2fba8092016-02-05 13:47:06 -0800773 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800774 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
775 for (int y = 0; y < h; ++y) {
776 for (int x = 0; x < w; ++x) {
777 uint32_t pixel = pixels.get()[y * w + x];
778 if (pixel != expectedValue) {
779 SkString msg;
780 if (expectedValue) {
781 msg = "SkSurface should have left render target unmodified";
782 } else {
783 msg = "SkSurface should have cleared the render target";
784 }
785 ERRORF(reporter,
786 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
787 expectedValue, x, y);
788 return;
789 }
790 }
791 }
792}
793
bsalomon758586c2016-04-06 14:02:39 -0700794DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700795 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700796
bsalomone63ffef2016-02-05 07:17:34 -0800797 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700798 [] (SkSurface* s){
Brian Osman11052242016-10-27 14:47:55 -0400799 GrRenderTargetContext* rtc =
800 s->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
801 return rtc->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700802 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
ericrkc4025182016-05-04 12:01:58 -0700803 return as_IB(i)->peekTexture(); }
bsalomone63ffef2016-02-05 07:17:34 -0800804 };
ericrkc4025182016-05-04 12:01:58 -0700805
bsalomone63ffef2016-02-05 07:17:34 -0800806 for (auto grSurfaceGetter : grSurfaceGetters) {
ericrkc4025182016-05-04 12:01:58 -0700807 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800808 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700809 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800810 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
811 }
812 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700813 const uint32_t kOrigColor = 0xABABABAB;
814 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
815 &create_gpu_surface_backend_texture_as_render_target}) {
816 GrBackendObject textureObject;
817 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
818 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
819 surface.reset();
820 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
821 }
822 }
823}
bsalomone63ffef2016-02-05 07:17:34 -0800824
ericrkc4025182016-05-04 12:01:58 -0700825static void test_surface_draw_partially(
826 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
827 const int kW = surface->width();
828 const int kH = surface->height();
829 SkPaint paint;
830 const SkColor kRectColor = ~origColor | 0xFF000000;
831 paint.setColor(kRectColor);
832 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
833 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400834 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700835 sk_memset32(pixels.get(), ~origColor, kW * kH);
836 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
837 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
838 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
839 bool stop = false;
840 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
841 (origColor >> 0 & 0xFF),
842 (origColor >> 8 & 0xFF),
843 (origColor >> 16 & 0xFF));
844 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
845 (kRectColor >> 16 & 0xFF),
846 (kRectColor >> 8 & 0xFF),
847 (kRectColor >> 0 & 0xFF));
848 for (int y = 0; y < kH/2 && !stop; ++y) {
849 for (int x = 0; x < kW && !stop; ++x) {
850 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
851 if (rectColorPM != pixels[x + y * kW]) {
852 stop = true;
853 }
854 }
855 }
856 stop = false;
857 for (int y = kH/2; y < kH && !stop; ++y) {
858 for (int x = 0; x < kW && !stop; ++x) {
859 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
860 if (origColorPM != pixels[x + y * kW]) {
861 stop = true;
862 }
863 }
864 }
865}
bsalomone63ffef2016-02-05 07:17:34 -0800866
egdanielab527a52016-06-28 08:07:26 -0700867DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700868 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700869 if (!gpu) {
870 return;
871 }
872 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800873
ericrkc4025182016-05-04 12:01:58 -0700874 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
875 &create_gpu_surface_backend_texture_as_render_target}) {
876 // Validate that we can draw to the canvas and that the original texture color is
877 // preserved in pixels that aren't rendered to via the surface.
878 // This works only for non-multisampled case.
879 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700880 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700881 if (surface) {
882 test_surface_draw_partially(reporter, surface, kOrigColor);
883 surface.reset();
884 gpu->deleteTestingOnlyBackendTexture(textureObject);
885 }
886 }
887}
888
889
890DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700891 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700892 if (!gpu) {
893 return;
894 }
895 static const uint32_t kOrigColor = 0xFFAABBCC;
896
897 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
898 &create_gpu_surface_backend_texture_as_render_target}) {
899 for (int sampleCnt : {0, 4, 8}) {
900 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700901 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700902
903 if (!surface && sampleCnt > 0) {
904 // Certain platforms don't support MSAA, skip these.
905 continue;
906 }
907
908 // Validate that we can attach a stencil buffer to an SkSurface created by either of
909 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400910 GrRenderTarget* rt = surface->getCanvas()
911 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700912 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700913 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700914 gpu->deleteTestingOnlyBackendTexture(textureObject);
915 }
bsalomone63ffef2016-02-05 07:17:34 -0800916 }
917}
918#endif
brianosman0e22eb82016-08-30 07:07:59 -0700919
920static void test_surface_creation_and_snapshot_with_color_space(
921 skiatest::Reporter* reporter,
922 const char* prefix,
923 bool f16Support,
924 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
925
Brian Osman526972e2016-10-24 09:24:02 -0400926 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
927 auto adobeColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700928 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
929 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400930 SkColorSpaceTransferFn oddGamma;
931 oddGamma.fA = 1.0f;
932 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
933 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400934 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
935 auto linearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosman0e22eb82016-08-30 07:07:59 -0700936
937 const struct {
938 SkColorType fColorType;
939 sk_sp<SkColorSpace> fColorSpace;
940 bool fShouldWork;
941 const char* fDescription;
942 } testConfigs[] = {
943 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
944 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
945 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
946 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
947 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Brian Osmaneb21ef62016-11-01 16:30:21 -0400948 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700949 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
950 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
951 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
952 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
953 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
954 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
955 };
956
957 for (auto& testConfig : testConfigs) {
958 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
959 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
960 testConfig.fColorSpace);
961
962 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
963 // any surface of that type:
964 bool shouldWork = testConfig.fShouldWork &&
965 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
966
967 auto surface(surfaceMaker(info));
968 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
969
970 if (shouldWork && surface) {
971 sk_sp<SkImage> image(surface->makeImageSnapshot());
972 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
973 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
974 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
975 fullTestName.c_str());
976 }
977 }
978}
979
980DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
981 auto surfaceMaker = [](const SkImageInfo& info) {
982 return SkSurface::MakeRaster(info);
983 };
984
985 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
986}
987
988#if SK_SUPPORT_GPU
989DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
990 GrContext* context = ctxInfo.grContext();
991 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
992 auto surfaceMaker = [context](const SkImageInfo& info) {
993 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
994 };
995
996 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
997
998 std::vector<GrBackendObject> textureHandles;
999 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
1000 GrBackendTextureDesc desc;
1001 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
1002 desc.fWidth = 10;
1003 desc.fHeight = 10;
1004 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
1005 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
1006 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
1007
1008 if (!desc.fTextureHandle) {
1009 return sk_sp<SkSurface>(nullptr);
1010 }
1011 textureHandles.push_back(desc.fTextureHandle);
1012
1013 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
1014 nullptr);
1015 };
1016
1017 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
1018 wrappedSurfaceMaker);
1019
1020 for (auto textureHandle : textureHandles) {
1021 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
1022 }
1023}
1024#endif
Matt Sarett22886c42016-11-22 11:31:41 -05001025
1026static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001027 SkOverdrawCanvas canvas(surface->getCanvas());
1028 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001029 sk_sp<SkImage> image = surface->makeImageSnapshot();
1030
1031 SkBitmap bitmap;
1032 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
1033 bitmap.lockPixels();
1034 for (int y = 0; y < 10; y++) {
1035 for (int x = 0; x < 10; x++) {
1036 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1037 }
1038 }
1039}
1040
1041DEF_TEST(OverdrawSurface_Raster, r) {
1042 sk_sp<SkSurface> surface = create_surface();
1043 test_overdraw_surface(r, surface.get());
1044}
1045
1046#if SK_SUPPORT_GPU
1047DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1048 GrContext* context = ctxInfo.grContext();
1049 sk_sp<SkSurface> surface = create_gpu_surface(context);
1050 test_overdraw_surface(r, surface.get());
1051}
1052#endif