blob: 775b8c6c0567c527aa6f76de15fbe7bfb3f40b78 [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"
robertphillips175dd9b2016-04-28 14:32:04 -070022#include "GrDrawContext.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 }
robertphillips8abb3702016-08-31 14:04:06 -0700313 return static_cast<intptr_t>(texture->uniqueID());
bsalomonf47b9a32016-02-22 11:02:58 -0800314 };
315
316 auto surfaceBackingStore = [reporter](SkSurface* surface) {
robertphillips175dd9b2016-04-28 14:32:04 -0700317 GrDrawContext* dc = surface->getCanvas()->internal_private_accessTopLayerDrawContext();
318 GrRenderTarget* rt = dc->accessRenderTarget();
bsalomonf47b9a32016-02-22 11:02:58 -0800319 if (!rt) {
320 ERRORF(reporter, "Not render target backed.");
321 return static_cast<intptr_t>(0);
322 }
robertphillips8abb3702016-08-31 14:04:06 -0700323 return static_cast<intptr_t>(rt->uniqueID());
bsalomonf47b9a32016-02-22 11:02:58 -0800324 };
325
reede8f30622016-03-23 18:59:25 -0700326 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
327 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800328
329 // Test again with a "direct" render target;
330 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
egdaniel0a3a7f72016-06-24 09:22:31 -0700331 10, 10, kRGBA_8888_GrPixelConfig, true);
bsalomonf47b9a32016-02-22 11:02:58 -0800332 GrBackendTextureDesc desc;
333 desc.fConfig = kRGBA_8888_GrPixelConfig;
334 desc.fWidth = 10;
335 desc.fHeight = 10;
336 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
337 desc.fTextureHandle = textureObject;
robertphillips7e922762016-07-26 11:38:17 -0700338
bsalomonf47b9a32016-02-22 11:02:58 -0800339 {
robertphillips7e922762016-07-26 11:38:17 -0700340 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTexture(context, desc, nullptr));
reede8f30622016-03-23 18:59:25 -0700341 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800342 surfaceBackingStore);
343 }
robertphillips7e922762016-07-26 11:38:17 -0700344
bsalomonf47b9a32016-02-22 11:02:58 -0800345 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
346 }
347}
348#endif
349
kkinnunen179a8f52015-11-20 13:32:24 -0800350#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800351
kkinnunen179a8f52015-11-20 13:32:24 -0800352static void test_backend_handle_unique_id(
353 skiatest::Reporter* reporter, SkSurface* surface,
354 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700355 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800356 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
357 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700358 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800359 // just read access should not affect the snapshot
360 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
361
362 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
363 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700364 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800365 // expect a new image, since we claimed we would write
366 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
367
368 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
369 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700370 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800371 // expect a new(er) image, since we claimed we would write
372 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
373 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
374}
375// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700376DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800377 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700378 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800379 for (auto& handle_access_func :
380 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700381 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700382 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800383 }
384 }
385 }
386}
387#endif
388
389// Verify that the right canvas commands trigger a copy on write.
390static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000391 SkCanvas* canvas = surface->getCanvas();
392
393 const SkRect testRect =
394 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
395 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000396 SkPath testPath;
397 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
398 SkIntToScalar(2), SkIntToScalar(1)));
399
400 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
401
402 SkRegion testRegion;
403 testRegion.setRect(testIRect);
404
405
406 const SkColor testColor = 0x01020304;
407 const SkPaint testPaint;
408 const SkPoint testPoints[3] = {
409 {SkIntToScalar(0), SkIntToScalar(0)},
410 {SkIntToScalar(2), SkIntToScalar(1)},
411 {SkIntToScalar(0), SkIntToScalar(2)}
412 };
413 const size_t testPointCount = 3;
414
415 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000416 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000417 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000418
419 SkRRect testRRect;
420 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
421
422 SkString testText("Hello World");
423 const SkPoint testPoints2[] = {
424 { SkIntToScalar(0), SkIntToScalar(1) },
425 { SkIntToScalar(1), SkIntToScalar(1) },
426 { SkIntToScalar(2), SkIntToScalar(1) },
427 { SkIntToScalar(3), SkIntToScalar(1) },
428 { SkIntToScalar(4), SkIntToScalar(1) },
429 { SkIntToScalar(5), SkIntToScalar(1) },
430 { SkIntToScalar(6), SkIntToScalar(1) },
431 { SkIntToScalar(7), SkIntToScalar(1) },
432 { SkIntToScalar(8), SkIntToScalar(1) },
433 { SkIntToScalar(9), SkIntToScalar(1) },
434 { SkIntToScalar(10), SkIntToScalar(1) },
435 };
436
437#define EXPECT_COPY_ON_WRITE(command) \
438 { \
reed9ce9d672016-03-17 10:51:11 -0700439 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
440 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000441 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700442 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
443 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000444 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
445 }
446
447 EXPECT_COPY_ON_WRITE(clear(testColor))
448 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
449 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
450 testPaint))
451 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
452 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
453 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
454 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
455 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700456 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700457 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000458 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
459 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
460 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700461 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000462 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800463}
464DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700465 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800466}
467#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700468DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800469 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700470 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700471 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700472 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000473}
kkinnunen179a8f52015-11-20 13:32:24 -0800474#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000475
kkinnunen179a8f52015-11-20 13:32:24 -0800476static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
477 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000478 // This test succeeds by not triggering an assertion.
479 // The test verifies that the surface remains writable (usable) after
480 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000481 SkCanvas* canvas = surface->getCanvas();
482 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700483 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000484 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000485}
kkinnunen179a8f52015-11-20 13:32:24 -0800486DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700487 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800488}
489#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700490DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800491 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700492 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700493 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800494 }
495}
496#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000497
junov@chromium.orgb516a412013-05-01 22:49:59 +0000498#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800499static void test_crbug263329(skiatest::Reporter* reporter,
500 SkSurface* surface1,
501 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000502 // This is a regression test for crbug.com/263329
503 // Bug was caused by onCopyOnWrite releasing the old surface texture
504 // back to the scratch texture pool even though the texture is used
505 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000506 SkCanvas* canvas1 = surface1->getCanvas();
507 SkCanvas* canvas2 = surface2->getCanvas();
508 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700509 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000510 // Trigger copy on write, new backing is a scratch texture
511 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700512 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000513 // Trigger copy on write, old backing should not be returned to scratch
514 // pool because it is held by image2
515 canvas1->clear(3);
516
517 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700518 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000519 // Trigger copy on write on surface2. The new backing store should not
520 // be recycling a texture that is held by an existing image.
521 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700522 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800523 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000524 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800525 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
526 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
527 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
528 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
529 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000530}
egdanielab527a52016-06-28 08:07:26 -0700531DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800532 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700533 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
534 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700535 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800536 }
537}
538#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000539
kkinnunen179a8f52015-11-20 13:32:24 -0800540DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700541 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700542 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800543 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800544 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800545 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800546}
547#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700548DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800549 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700550 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700551 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800552 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700553 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000554 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800555 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800556 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000557 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000558}
kkinnunen179a8f52015-11-20 13:32:24 -0800559#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800560
kkinnunen179a8f52015-11-20 13:32:24 -0800561#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800562#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800563#include "SkGpuDevice.h"
564#include "SkImage_Gpu.h"
565#include "SkSurface_Gpu.h"
566
reede8f30622016-03-23 18:59:25 -0700567static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
568 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
robertphillipsea70c4b2016-07-20 08:54:31 -0700569 return gsurf->getDevice()->accessDrawContext()->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800570}
571
bsalomon5ec26ae2016-02-25 08:33:02 -0800572static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800573 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800574}
575
reed9ce9d672016-03-17 10:51:11 -0700576static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
577 return is_budgeted(image.get());
578}
579
egdanielab527a52016-06-28 08:07:26 -0700580DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800581 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800582 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
583 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700584 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800585 SkASSERT(surface);
586 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
587
reed9ce9d672016-03-17 10:51:11 -0700588 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800589
590 // Initially the image shares a texture with the surface, and the surface decides
591 // whether it is budgeted or not.
592 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
593 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
594
595 // Now trigger copy-on-write
596 surface->getCanvas()->clear(SK_ColorBLUE);
597
598 // They don't share a texture anymore. They should each have made their own budget
599 // decision.
600 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
601 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
602 }
603 }
604}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000605#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000606
kkinnunen179a8f52015-11-20 13:32:24 -0800607static void test_no_canvas1(skiatest::Reporter* reporter,
608 SkSurface* surface,
609 SkSurface::ContentChangeMode mode) {
610 // Test passes by not asserting
611 surface->notifyContentWillChange(mode);
612 SkDEBUGCODE(surface->validate();)
613}
614static void test_no_canvas2(skiatest::Reporter* reporter,
615 SkSurface* surface,
616 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000617 // Verifies the robustness of SkSurface for handling use cases where calls
618 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700619 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
620 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800621 SkDEBUGCODE(image1->validate();)
622 SkDEBUGCODE(surface->validate();)
623 surface->notifyContentWillChange(mode);
624 SkDEBUGCODE(image1->validate();)
625 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700626 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
627 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800628 SkDEBUGCODE(image2->validate();)
629 SkDEBUGCODE(surface->validate();)
630 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000631}
kkinnunen179a8f52015-11-20 13:32:24 -0800632DEF_TEST(SurfaceNoCanvas, reporter) {
633 SkSurface::ContentChangeMode modes[] =
634 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
635 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
636 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700637 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800638 }
639 }
640}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000641#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700642DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800643 SkSurface::ContentChangeMode modes[] =
644 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
645 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
646 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
647 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700648 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700649 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700650 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000651 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000652 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000653}
kkinnunen179a8f52015-11-20 13:32:24 -0800654#endif
reed9cd016e2016-01-30 10:01:06 -0800655
656static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800657 SkPixmap surfacePM;
658 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800659
reed9ce9d672016-03-17 10:51:11 -0700660 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800661 SkPixmap pm;
662 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800663
reed6ceeebd2016-03-09 14:26:26 -0800664 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800665
666 // trigger a copy-on-write
667 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700668 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800669 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
670
reed6ceeebd2016-03-09 14:26:26 -0800671 SkPixmap pm2;
672 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
673 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800674}
675
676DEF_TEST(surface_rowbytes, reporter) {
677 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
678
reede8f30622016-03-23 18:59:25 -0700679 auto surf0(SkSurface::MakeRaster(info));
680 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800681
682 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700683 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
684 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800685
686 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700687 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800688 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700689 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800690 REPORTER_ASSERT(reporter, nullptr == s);
691}
bsalomone63ffef2016-02-05 07:17:34 -0800692
fmalita03912f12016-07-06 06:22:06 -0700693DEF_TEST(surface_raster_zeroinitialized, reporter) {
694 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
695 SkPixmap pixmap;
696 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
697
698 for (int i = 0; i < pixmap.info().width(); ++i) {
699 for (int j = 0; j < pixmap.info().height(); ++j) {
700 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
701 }
702 }
703}
704
bsalomone63ffef2016-02-05 07:17:34 -0800705#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700706static sk_sp<SkSurface> create_gpu_surface_backend_texture(
707 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
708 const int kWidth = 10;
709 const int kHeight = 10;
710 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
711 sk_memset32(pixels.get(), color, kWidth * kHeight);
712 GrBackendTextureDesc desc;
713 desc.fConfig = kRGBA_8888_GrPixelConfig;
714 desc.fWidth = kWidth;
715 desc.fHeight = kHeight;
716 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
717 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700718 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700719 desc.fSampleCnt = sampleCnt;
720 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
721 if (!surface) {
722 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
723 return nullptr;
724 }
725 *outTexture = desc.fTextureHandle;
726 return surface;
727}
bsalomone63ffef2016-02-05 07:17:34 -0800728
ericrkc4025182016-05-04 12:01:58 -0700729static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
730 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
731 const int kWidth = 10;
732 const int kHeight = 10;
733 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
734 sk_memset32(pixels.get(), color, kWidth * kHeight);
735 GrBackendTextureDesc desc;
736 desc.fConfig = kRGBA_8888_GrPixelConfig;
737 desc.fWidth = kWidth;
738 desc.fHeight = kHeight;
739 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
740 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700741 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700742 desc.fSampleCnt = sampleCnt;
743 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
744 nullptr);
745 if (!surface) {
746 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
747 return nullptr;
748 }
749 *outTexture = desc.fTextureHandle;
750 return surface;
751}
752
753static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
754 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
755 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800756 if (!surface) {
757 ERRORF(reporter, "Could not create GPU SkSurface.");
758 return;
759 }
760 int w = surface->width();
761 int h = surface->height();
762 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700763 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800764
reede8f30622016-03-23 18:59:25 -0700765 SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800766 if (!grSurface) {
767 ERRORF(reporter, "Could access render target of GPU SkSurface.");
768 return;
769 }
bsalomon2fba8092016-02-05 13:47:06 -0800770 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800771 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
772 for (int y = 0; y < h; ++y) {
773 for (int x = 0; x < w; ++x) {
774 uint32_t pixel = pixels.get()[y * w + x];
775 if (pixel != expectedValue) {
776 SkString msg;
777 if (expectedValue) {
778 msg = "SkSurface should have left render target unmodified";
779 } else {
780 msg = "SkSurface should have cleared the render target";
781 }
782 ERRORF(reporter,
783 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
784 expectedValue, x, y);
785 return;
786 }
787 }
788 }
789}
790
bsalomon758586c2016-04-06 14:02:39 -0700791DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700792 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700793
bsalomone63ffef2016-02-05 07:17:34 -0800794 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700795 [] (SkSurface* s){
robertphillips175dd9b2016-04-28 14:32:04 -0700796 GrDrawContext* dc = s->getCanvas()->internal_private_accessTopLayerDrawContext();
797 return dc->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700798 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
ericrkc4025182016-05-04 12:01:58 -0700799 return as_IB(i)->peekTexture(); }
bsalomone63ffef2016-02-05 07:17:34 -0800800 };
ericrkc4025182016-05-04 12:01:58 -0700801
bsalomone63ffef2016-02-05 07:17:34 -0800802 for (auto grSurfaceGetter : grSurfaceGetters) {
ericrkc4025182016-05-04 12:01:58 -0700803 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800804 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700805 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800806 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
807 }
808 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700809 const uint32_t kOrigColor = 0xABABABAB;
810 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
811 &create_gpu_surface_backend_texture_as_render_target}) {
812 GrBackendObject textureObject;
813 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
814 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
815 surface.reset();
816 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
817 }
818 }
819}
bsalomone63ffef2016-02-05 07:17:34 -0800820
ericrkc4025182016-05-04 12:01:58 -0700821static void test_surface_draw_partially(
822 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
823 const int kW = surface->width();
824 const int kH = surface->height();
825 SkPaint paint;
826 const SkColor kRectColor = ~origColor | 0xFF000000;
827 paint.setColor(kRectColor);
828 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
829 paint);
830 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kW * kH]);
831 sk_memset32(pixels.get(), ~origColor, kW * kH);
832 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
833 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
834 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
835 bool stop = false;
836 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
837 (origColor >> 0 & 0xFF),
838 (origColor >> 8 & 0xFF),
839 (origColor >> 16 & 0xFF));
840 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
841 (kRectColor >> 16 & 0xFF),
842 (kRectColor >> 8 & 0xFF),
843 (kRectColor >> 0 & 0xFF));
844 for (int y = 0; y < kH/2 && !stop; ++y) {
845 for (int x = 0; x < kW && !stop; ++x) {
846 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
847 if (rectColorPM != pixels[x + y * kW]) {
848 stop = true;
849 }
850 }
851 }
852 stop = false;
853 for (int y = kH/2; y < kH && !stop; ++y) {
854 for (int x = 0; x < kW && !stop; ++x) {
855 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
856 if (origColorPM != pixels[x + y * kW]) {
857 stop = true;
858 }
859 }
860 }
861}
bsalomone63ffef2016-02-05 07:17:34 -0800862
egdanielab527a52016-06-28 08:07:26 -0700863DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700864 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700865 if (!gpu) {
866 return;
867 }
868 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800869
ericrkc4025182016-05-04 12:01:58 -0700870 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
871 &create_gpu_surface_backend_texture_as_render_target}) {
872 // Validate that we can draw to the canvas and that the original texture color is
873 // preserved in pixels that aren't rendered to via the surface.
874 // This works only for non-multisampled case.
875 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700876 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700877 if (surface) {
878 test_surface_draw_partially(reporter, surface, kOrigColor);
879 surface.reset();
880 gpu->deleteTestingOnlyBackendTexture(textureObject);
881 }
882 }
883}
884
885
886DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700887 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700888 if (!gpu) {
889 return;
890 }
891 static const uint32_t kOrigColor = 0xFFAABBCC;
892
893 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
894 &create_gpu_surface_backend_texture_as_render_target}) {
895 for (int sampleCnt : {0, 4, 8}) {
896 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700897 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700898
899 if (!surface && sampleCnt > 0) {
900 // Certain platforms don't support MSAA, skip these.
901 continue;
902 }
903
904 // Validate that we can attach a stencil buffer to an SkSurface created by either of
905 // our surface functions.
906 GrRenderTarget* rt = surface->getCanvas()->internal_private_accessTopLayerDrawContext()
907 ->accessRenderTarget();
908 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700909 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700910 gpu->deleteTestingOnlyBackendTexture(textureObject);
911 }
bsalomone63ffef2016-02-05 07:17:34 -0800912 }
913}
914#endif
brianosman0e22eb82016-08-30 07:07:59 -0700915
916static void test_surface_creation_and_snapshot_with_color_space(
917 skiatest::Reporter* reporter,
918 const char* prefix,
919 bool f16Support,
920 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
921
922 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
923 auto adobeColorSpace = SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named);
brianosman971cd492016-09-08 10:10:11 -0700924 SkMatrix44 srgbMatrix = srgbColorSpace->toXYZD50();
brianosman0e22eb82016-08-30 07:07:59 -0700925 const float oddGamma[] = { 2.4f, 2.4f, 2.4f };
926 auto oddColorSpace = SkColorSpace_Base::NewRGB(oddGamma, srgbMatrix);
927 auto linearColorSpace = srgbColorSpace->makeLinearGamma();
928
929 const struct {
930 SkColorType fColorType;
931 sk_sp<SkColorSpace> fColorSpace;
932 bool fShouldWork;
933 const char* fDescription;
934 } testConfigs[] = {
935 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
936 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
937 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
938 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
939 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
940 { kRGBA_F16_SkColorType, nullptr, false, "F16-nullptr" },
941 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
942 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
943 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
944 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
945 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
946 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
947 };
948
949 for (auto& testConfig : testConfigs) {
950 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
951 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
952 testConfig.fColorSpace);
953
954 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
955 // any surface of that type:
956 bool shouldWork = testConfig.fShouldWork &&
957 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
958
959 auto surface(surfaceMaker(info));
960 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
961
962 if (shouldWork && surface) {
963 sk_sp<SkImage> image(surface->makeImageSnapshot());
964 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
965 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
966 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
967 fullTestName.c_str());
968 }
969 }
970}
971
972DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
973 auto surfaceMaker = [](const SkImageInfo& info) {
974 return SkSurface::MakeRaster(info);
975 };
976
977 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
978}
979
980#if SK_SUPPORT_GPU
981DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
982 GrContext* context = ctxInfo.grContext();
983 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
984 auto surfaceMaker = [context](const SkImageInfo& info) {
985 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
986 };
987
988 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
989
990 std::vector<GrBackendObject> textureHandles;
991 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
992 GrBackendTextureDesc desc;
993 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
994 desc.fWidth = 10;
995 desc.fHeight = 10;
996 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
997 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
998 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
999
1000 if (!desc.fTextureHandle) {
1001 return sk_sp<SkSurface>(nullptr);
1002 }
1003 textureHandles.push_back(desc.fTextureHandle);
1004
1005 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
1006 nullptr);
1007 };
1008
1009 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
1010 wrappedSurfaceMaker);
1011
1012 for (auto textureHandle : textureHandles) {
1013 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
1014 }
1015}
1016#endif