blob: 2703b541fc59854b2ac27989bfcea59adad2db0c [file] [log] [blame]
junov@chromium.org995beb62013-03-28 13:49:22 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00007
bsalomone63ffef2016-02-05 07:17:34 -08008#include <functional>
junov@chromium.org995beb62013-03-28 13:49:22 +00009#include "SkCanvas.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000010#include "SkData.h"
reed41e010c2015-06-09 12:16:53 -070011#include "SkDevice.h"
bsalomon55812362015-06-10 08:49:28 -070012#include "SkImage_Base.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000014#include "SkRRect.h"
15#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000016#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000017#include "Test.h"
18
19#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080020#include "GrContext.h"
robertphillips175dd9b2016-04-28 14:32:04 -070021#include "GrDrawContext.h"
kkinnunen179a8f52015-11-20 13:32:24 -080022#include "GrGpu.h"
ericrkc4025182016-05-04 12:01:58 -070023#include "GrResourceProvider.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000024#endif
25
kkinnunen179a8f52015-11-20 13:32:24 -080026#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070027
kkinnunen179a8f52015-11-20 13:32:24 -080028static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070029 SkASSERT(pixels == context);
30 sk_free(pixels);
31}
reede8f30622016-03-23 18:59:25 -070032static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
33 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070034 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000035 if (requestedInfo) {
36 *requestedInfo = info;
37 }
reede8f30622016-03-23 18:59:25 -070038 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000039}
reede8f30622016-03-23 18:59:25 -070040static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
41 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080042 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
43 if (requestedInfo) {
44 *requestedInfo = info;
45 }
46 const size_t rowBytes = info.minRowBytes();
47 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070048 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
49 release_direct_surface_storage,
50 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080051}
52#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070053static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
54 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080055 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
56 if (requestedInfo) {
57 *requestedInfo = info;
58 }
robertphillips7e922762016-07-26 11:38:17 -070059 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080060}
reede8f30622016-03-23 18:59:25 -070061static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
62 SkAlphaType at = kPremul_SkAlphaType,
63 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080064 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
65 if (requestedInfo) {
66 *requestedInfo = info;
67 }
robertphillips7e922762016-07-26 11:38:17 -070068 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080069}
70#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000071
kkinnunen179a8f52015-11-20 13:32:24 -080072DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080073 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070074 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
75 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080076
reedb2497c22014-12-31 12:31:43 -080077}
kkinnunen179a8f52015-11-20 13:32:24 -080078#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070079DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080080 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
81 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070082 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080083}
84#endif
reedb2497c22014-12-31 12:31:43 -080085
kkinnunen179a8f52015-11-20 13:32:24 -080086static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070087 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080088 const SkImageInfo& requestInfo,
89 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000090 const SkColor color = SK_ColorRED;
91 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080092 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000093
reed6ceeebd2016-03-09 14:26:26 -080094 SkPixmap pmap;
95 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -080096 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000097
reed6ceeebd2016-03-09 14:26:26 -080098 SkPixmap pmap2;
99 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000100
kkinnunen179a8f52015-11-20 13:32:24 -0800101 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800102 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
103 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
104 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000105
reed6ceeebd2016-03-09 14:26:26 -0800106 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
107 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
108 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800109 } else {
110 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000111 }
112}
kkinnunen179a8f52015-11-20 13:32:24 -0800113DEF_TEST(SurfaceCanvasPeek, reporter) {
114 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
115 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700116 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800117 test_canvas_peek(reporter, surface, requestInfo, true);
118 }
119}
120#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700121DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800122 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
123 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700124 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800125 test_canvas_peek(reporter, surface, requestInfo, false);
126 }
127}
128#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000129
reede8f30622016-03-23 18:59:25 -0700130static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700131 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800132 REPORTER_ASSERT(reporter, surface);
133 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700134 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800135 REPORTER_ASSERT(reporter, image);
136 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700137 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700138 }
139 }
140}
kkinnunen179a8f52015-11-20 13:32:24 -0800141DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
142 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700143 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
144 auto surface(surface_func(at, nullptr));
145 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700146 }
147 }
148}
kkinnunen179a8f52015-11-20 13:32:24 -0800149#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700150DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800151 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700152 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
153 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
154 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
155 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800156 }
157 }
158}
159#endif
bsalomon74f681d2015-06-23 14:38:48 -0700160
kkinnunen179a8f52015-11-20 13:32:24 -0800161static GrBackendObject get_surface_backend_texture_handle(
162 SkSurface* s, SkSurface::BackendHandleAccess a) {
163 return s->getTextureHandle(a);
164}
165static GrBackendObject get_surface_backend_render_target_handle(
166 SkSurface* s, SkSurface::BackendHandleAccess a) {
167 GrBackendObject result;
168 if (!s->getRenderTargetHandle(&result, a)) {
169 return 0;
170 }
171 return result;
172}
173
174static void test_backend_handle_access_copy_on_write(
175 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
176 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700177 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700178 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700179
180 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700181 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700182
183 // If the access mode triggers CoW, then the backend objects should reflect it.
184 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
185}
kkinnunen179a8f52015-11-20 13:32:24 -0800186DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
187 const SkSurface::BackendHandleAccess accessModes[] = {
188 SkSurface::kFlushRead_BackendHandleAccess,
189 SkSurface::kFlushWrite_BackendHandleAccess,
190 SkSurface::kDiscardWrite_BackendHandleAccess,
191 };
192 for (auto& handle_access_func :
193 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
194 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700195 auto surface(create_surface());
196 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800197 handle_access_func);
198 }
199 }
200}
201#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700202DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800203 const SkSurface::BackendHandleAccess accessModes[] = {
204 SkSurface::kFlushRead_BackendHandleAccess,
205 SkSurface::kFlushWrite_BackendHandleAccess,
206 SkSurface::kDiscardWrite_BackendHandleAccess,
207 };
208 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
209 for (auto& handle_access_func :
210 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
211 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700212 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700213 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800214 handle_access_func);
215 }
216 }
217 }
218}
219#endif
fmalitae2639082015-08-06 07:04:51 -0700220
bsalomonf47b9a32016-02-22 11:02:58 -0800221static bool same_image(SkImage* a, SkImage* b,
222 std::function<intptr_t(SkImage*)> getImageBackingStore) {
223 return getImageBackingStore(a) == getImageBackingStore(b);
224}
225
226static bool same_image_surf(SkImage* a, SkSurface* b,
227 std::function<intptr_t(SkImage*)> getImageBackingStore,
228 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
229 return getImageBackingStore(a) == getSurfaceBackingStore(b);
230}
231
232static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
233 bool surfaceIsDirect,
234 std::function<intptr_t(SkImage*)> imageBackingStore,
235 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
236 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
237 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800238 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800239 {
reed9ce9d672016-03-17 10:51:11 -0700240 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
241 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800242 REPORTER_ASSERT(reporter, image->unique());
243 }
244 {
reed9ce9d672016-03-17 10:51:11 -0700245 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
246 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800247 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700248 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
249 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
250 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800251 REPORTER_ASSERT(reporter, image2->unique());
252 }
253 {
reed9ce9d672016-03-17 10:51:11 -0700254 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
255 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
256 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
257 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800258 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700259 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800260 // If the surface is not direct then images 1 and 3 should alias the surface's
261 // store.
reed9ce9d672016-03-17 10:51:11 -0700262 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800263 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700264 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
265 !same_image(image3.get(), image2.get(), ibs) &&
266 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800267 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700268 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800269 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700270 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
271 !same_image(image3.get(), image4.get(), ibs));
272 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800273 REPORTER_ASSERT(reporter, image4->unique());
274 }
275}
276
277DEF_TEST(UniqueImageSnapshot, reporter) {
278 auto getImageBackingStore = [reporter](SkImage* image) {
279 SkPixmap pm;
280 bool success = image->peekPixels(&pm);
281 REPORTER_ASSERT(reporter, success);
282 return reinterpret_cast<intptr_t>(pm.addr());
283 };
284 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800285 SkPixmap pmap;
286 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800287 REPORTER_ASSERT(reporter, pixels);
288 return reinterpret_cast<intptr_t>(pixels);
289 };
290
reede8f30622016-03-23 18:59:25 -0700291 auto surface(create_surface());
292 test_unique_image_snap(reporter, surface.get(), false, getImageBackingStore,
293 getSufaceBackingStore);
294 surface = create_direct_surface();
295 test_unique_image_snap(reporter, surface.get(), true, getImageBackingStore,
296 getSufaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800297}
298
299#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700300DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700301 GrContext* context = ctxInfo.grContext();
bsalomonf47b9a32016-02-22 11:02:58 -0800302 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700303 auto surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
bsalomonf47b9a32016-02-22 11:02:58 -0800304
305 auto imageBackingStore = [reporter](SkImage* image) {
306 GrTexture* texture = as_IB(image)->peekTexture();
307 if (!texture) {
308 ERRORF(reporter, "Not texture backed.");
309 return static_cast<intptr_t>(0);
310 }
311 return static_cast<intptr_t>(texture->getUniqueID());
312 };
313
314 auto surfaceBackingStore = [reporter](SkSurface* surface) {
robertphillips175dd9b2016-04-28 14:32:04 -0700315 GrDrawContext* dc = surface->getCanvas()->internal_private_accessTopLayerDrawContext();
316 GrRenderTarget* rt = dc->accessRenderTarget();
bsalomonf47b9a32016-02-22 11:02:58 -0800317 if (!rt) {
318 ERRORF(reporter, "Not render target backed.");
319 return static_cast<intptr_t>(0);
320 }
321 return static_cast<intptr_t>(rt->getUniqueID());
322 };
323
reede8f30622016-03-23 18:59:25 -0700324 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
325 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800326
327 // Test again with a "direct" render target;
328 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
egdaniel0a3a7f72016-06-24 09:22:31 -0700329 10, 10, kRGBA_8888_GrPixelConfig, true);
bsalomonf47b9a32016-02-22 11:02:58 -0800330 GrBackendTextureDesc desc;
331 desc.fConfig = kRGBA_8888_GrPixelConfig;
332 desc.fWidth = 10;
333 desc.fHeight = 10;
334 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
335 desc.fTextureHandle = textureObject;
robertphillips7e922762016-07-26 11:38:17 -0700336
bsalomonf47b9a32016-02-22 11:02:58 -0800337 {
robertphillips7e922762016-07-26 11:38:17 -0700338 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTexture(context, desc, nullptr));
reede8f30622016-03-23 18:59:25 -0700339 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800340 surfaceBackingStore);
341 }
robertphillips7e922762016-07-26 11:38:17 -0700342
bsalomonf47b9a32016-02-22 11:02:58 -0800343 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
344 }
345}
346#endif
347
kkinnunen179a8f52015-11-20 13:32:24 -0800348#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800349
kkinnunen179a8f52015-11-20 13:32:24 -0800350static void test_backend_handle_unique_id(
351 skiatest::Reporter* reporter, SkSurface* surface,
352 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700353 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800354 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
355 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800357 // just read access should not affect the snapshot
358 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
359
360 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
361 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700362 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800363 // expect a new image, since we claimed we would write
364 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
365
366 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
367 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700368 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800369 // expect a new(er) image, since we claimed we would write
370 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
371 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
372}
373// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700374DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800375 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700376 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800377 for (auto& handle_access_func :
378 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700379 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700380 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800381 }
382 }
383 }
384}
385#endif
386
387// Verify that the right canvas commands trigger a copy on write.
388static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000389 SkCanvas* canvas = surface->getCanvas();
390
391 const SkRect testRect =
392 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
393 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000394 SkPath testPath;
395 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
396 SkIntToScalar(2), SkIntToScalar(1)));
397
398 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
399
400 SkRegion testRegion;
401 testRegion.setRect(testIRect);
402
403
404 const SkColor testColor = 0x01020304;
405 const SkPaint testPaint;
406 const SkPoint testPoints[3] = {
407 {SkIntToScalar(0), SkIntToScalar(0)},
408 {SkIntToScalar(2), SkIntToScalar(1)},
409 {SkIntToScalar(0), SkIntToScalar(2)}
410 };
411 const size_t testPointCount = 3;
412
413 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000414 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000415 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000416
417 SkRRect testRRect;
418 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
419
420 SkString testText("Hello World");
421 const SkPoint testPoints2[] = {
422 { SkIntToScalar(0), SkIntToScalar(1) },
423 { SkIntToScalar(1), SkIntToScalar(1) },
424 { SkIntToScalar(2), SkIntToScalar(1) },
425 { SkIntToScalar(3), SkIntToScalar(1) },
426 { SkIntToScalar(4), SkIntToScalar(1) },
427 { SkIntToScalar(5), SkIntToScalar(1) },
428 { SkIntToScalar(6), SkIntToScalar(1) },
429 { SkIntToScalar(7), SkIntToScalar(1) },
430 { SkIntToScalar(8), SkIntToScalar(1) },
431 { SkIntToScalar(9), SkIntToScalar(1) },
432 { SkIntToScalar(10), SkIntToScalar(1) },
433 };
434
435#define EXPECT_COPY_ON_WRITE(command) \
436 { \
reed9ce9d672016-03-17 10:51:11 -0700437 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
438 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000439 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700440 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
441 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000442 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
443 }
444
445 EXPECT_COPY_ON_WRITE(clear(testColor))
446 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
447 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
448 testPaint))
449 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
450 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
451 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
452 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
453 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700454 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700455 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000456 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
457 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
458 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700459 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000460 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800461}
462DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700463 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800464}
465#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700466DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800467 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700468 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700469 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700470 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000471}
kkinnunen179a8f52015-11-20 13:32:24 -0800472#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000473
kkinnunen179a8f52015-11-20 13:32:24 -0800474static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
475 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000476 // This test succeeds by not triggering an assertion.
477 // The test verifies that the surface remains writable (usable) after
478 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000479 SkCanvas* canvas = surface->getCanvas();
480 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700481 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000482 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000483}
kkinnunen179a8f52015-11-20 13:32:24 -0800484DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700485 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800486}
487#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700488DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800489 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700490 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700491 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800492 }
493}
494#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000495
junov@chromium.orgb516a412013-05-01 22:49:59 +0000496#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800497static void test_crbug263329(skiatest::Reporter* reporter,
498 SkSurface* surface1,
499 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000500 // This is a regression test for crbug.com/263329
501 // Bug was caused by onCopyOnWrite releasing the old surface texture
502 // back to the scratch texture pool even though the texture is used
503 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000504 SkCanvas* canvas1 = surface1->getCanvas();
505 SkCanvas* canvas2 = surface2->getCanvas();
506 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700507 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000508 // Trigger copy on write, new backing is a scratch texture
509 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700510 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000511 // Trigger copy on write, old backing should not be returned to scratch
512 // pool because it is held by image2
513 canvas1->clear(3);
514
515 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700516 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000517 // Trigger copy on write on surface2. The new backing store should not
518 // be recycling a texture that is held by an existing image.
519 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700520 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800521 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000522 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800523 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
524 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
525 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
526 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
527 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000528}
egdanielab527a52016-06-28 08:07:26 -0700529DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800530 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700531 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
532 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700533 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800534 }
535}
536#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000537
kkinnunen179a8f52015-11-20 13:32:24 -0800538DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700539 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700540 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800541 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800542 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800543 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800544}
545#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700546DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800547 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700548 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700549 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800550 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700551 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000552 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800553 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800554 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000555 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000556}
kkinnunen179a8f52015-11-20 13:32:24 -0800557#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800558
kkinnunen179a8f52015-11-20 13:32:24 -0800559#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800560#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800561#include "SkGpuDevice.h"
562#include "SkImage_Gpu.h"
563#include "SkSurface_Gpu.h"
564
reede8f30622016-03-23 18:59:25 -0700565static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
566 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
robertphillipsea70c4b2016-07-20 08:54:31 -0700567 return gsurf->getDevice()->accessDrawContext()->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800568}
569
bsalomon5ec26ae2016-02-25 08:33:02 -0800570static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800571 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800572}
573
reed9ce9d672016-03-17 10:51:11 -0700574static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
575 return is_budgeted(image.get());
576}
577
egdanielab527a52016-06-28 08:07:26 -0700578DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800579 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800580 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
581 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700582 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800583 SkASSERT(surface);
584 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
585
reed9ce9d672016-03-17 10:51:11 -0700586 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800587
588 // Initially the image shares a texture with the surface, and the surface decides
589 // whether it is budgeted or not.
590 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
591 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
592
593 // Now trigger copy-on-write
594 surface->getCanvas()->clear(SK_ColorBLUE);
595
596 // They don't share a texture anymore. They should each have made their own budget
597 // decision.
598 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
599 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
600 }
601 }
602}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000603#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000604
kkinnunen179a8f52015-11-20 13:32:24 -0800605static void test_no_canvas1(skiatest::Reporter* reporter,
606 SkSurface* surface,
607 SkSurface::ContentChangeMode mode) {
608 // Test passes by not asserting
609 surface->notifyContentWillChange(mode);
610 SkDEBUGCODE(surface->validate();)
611}
612static void test_no_canvas2(skiatest::Reporter* reporter,
613 SkSurface* surface,
614 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000615 // Verifies the robustness of SkSurface for handling use cases where calls
616 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700617 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
618 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800619 SkDEBUGCODE(image1->validate();)
620 SkDEBUGCODE(surface->validate();)
621 surface->notifyContentWillChange(mode);
622 SkDEBUGCODE(image1->validate();)
623 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700624 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
625 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800626 SkDEBUGCODE(image2->validate();)
627 SkDEBUGCODE(surface->validate();)
628 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000629}
kkinnunen179a8f52015-11-20 13:32:24 -0800630DEF_TEST(SurfaceNoCanvas, reporter) {
631 SkSurface::ContentChangeMode modes[] =
632 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
633 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
634 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700635 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800636 }
637 }
638}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000639#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700640DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800641 SkSurface::ContentChangeMode modes[] =
642 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
643 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
644 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
645 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700646 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700647 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700648 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000649 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000650 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000651}
kkinnunen179a8f52015-11-20 13:32:24 -0800652#endif
reed9cd016e2016-01-30 10:01:06 -0800653
654static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800655 SkPixmap surfacePM;
656 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800657
reed9ce9d672016-03-17 10:51:11 -0700658 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800659 SkPixmap pm;
660 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800661
reed6ceeebd2016-03-09 14:26:26 -0800662 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800663
664 // trigger a copy-on-write
665 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700666 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800667 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
668
reed6ceeebd2016-03-09 14:26:26 -0800669 SkPixmap pm2;
670 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
671 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800672}
673
674DEF_TEST(surface_rowbytes, reporter) {
675 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
676
reede8f30622016-03-23 18:59:25 -0700677 auto surf0(SkSurface::MakeRaster(info));
678 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800679
680 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700681 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
682 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800683
684 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700685 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800686 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700687 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800688 REPORTER_ASSERT(reporter, nullptr == s);
689}
bsalomone63ffef2016-02-05 07:17:34 -0800690
fmalita03912f12016-07-06 06:22:06 -0700691DEF_TEST(surface_raster_zeroinitialized, reporter) {
692 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
693 SkPixmap pixmap;
694 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
695
696 for (int i = 0; i < pixmap.info().width(); ++i) {
697 for (int j = 0; j < pixmap.info().height(); ++j) {
698 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
699 }
700 }
701}
702
bsalomone63ffef2016-02-05 07:17:34 -0800703#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700704static sk_sp<SkSurface> create_gpu_surface_backend_texture(
705 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
706 const int kWidth = 10;
707 const int kHeight = 10;
708 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
709 sk_memset32(pixels.get(), color, kWidth * kHeight);
710 GrBackendTextureDesc desc;
711 desc.fConfig = kRGBA_8888_GrPixelConfig;
712 desc.fWidth = kWidth;
713 desc.fHeight = kHeight;
714 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
715 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700716 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700717 desc.fSampleCnt = sampleCnt;
718 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
719 if (!surface) {
720 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
721 return nullptr;
722 }
723 *outTexture = desc.fTextureHandle;
724 return surface;
725}
bsalomone63ffef2016-02-05 07:17:34 -0800726
ericrkc4025182016-05-04 12:01:58 -0700727static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
728 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
729 const int kWidth = 10;
730 const int kHeight = 10;
731 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
732 sk_memset32(pixels.get(), color, kWidth * kHeight);
733 GrBackendTextureDesc desc;
734 desc.fConfig = kRGBA_8888_GrPixelConfig;
735 desc.fWidth = kWidth;
736 desc.fHeight = kHeight;
737 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
738 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700739 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700740 desc.fSampleCnt = sampleCnt;
741 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
742 nullptr);
743 if (!surface) {
744 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
745 return nullptr;
746 }
747 *outTexture = desc.fTextureHandle;
748 return surface;
749}
750
751static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
752 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
753 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800754 if (!surface) {
755 ERRORF(reporter, "Could not create GPU SkSurface.");
756 return;
757 }
758 int w = surface->width();
759 int h = surface->height();
760 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700761 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800762
reede8f30622016-03-23 18:59:25 -0700763 SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800764 if (!grSurface) {
765 ERRORF(reporter, "Could access render target of GPU SkSurface.");
766 return;
767 }
bsalomon2fba8092016-02-05 13:47:06 -0800768 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800769 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
770 for (int y = 0; y < h; ++y) {
771 for (int x = 0; x < w; ++x) {
772 uint32_t pixel = pixels.get()[y * w + x];
773 if (pixel != expectedValue) {
774 SkString msg;
775 if (expectedValue) {
776 msg = "SkSurface should have left render target unmodified";
777 } else {
778 msg = "SkSurface should have cleared the render target";
779 }
780 ERRORF(reporter,
781 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
782 expectedValue, x, y);
783 return;
784 }
785 }
786 }
787}
788
bsalomon758586c2016-04-06 14:02:39 -0700789DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700790 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700791
bsalomone63ffef2016-02-05 07:17:34 -0800792 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700793 [] (SkSurface* s){
robertphillips175dd9b2016-04-28 14:32:04 -0700794 GrDrawContext* dc = s->getCanvas()->internal_private_accessTopLayerDrawContext();
795 return dc->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700796 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
ericrkc4025182016-05-04 12:01:58 -0700797 return as_IB(i)->peekTexture(); }
bsalomone63ffef2016-02-05 07:17:34 -0800798 };
ericrkc4025182016-05-04 12:01:58 -0700799
bsalomone63ffef2016-02-05 07:17:34 -0800800 for (auto grSurfaceGetter : grSurfaceGetters) {
ericrkc4025182016-05-04 12:01:58 -0700801 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800802 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700803 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800804 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
805 }
806 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700807 const uint32_t kOrigColor = 0xABABABAB;
808 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
809 &create_gpu_surface_backend_texture_as_render_target}) {
810 GrBackendObject textureObject;
811 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
812 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
813 surface.reset();
814 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
815 }
816 }
817}
bsalomone63ffef2016-02-05 07:17:34 -0800818
ericrkc4025182016-05-04 12:01:58 -0700819static void test_surface_draw_partially(
820 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
821 const int kW = surface->width();
822 const int kH = surface->height();
823 SkPaint paint;
824 const SkColor kRectColor = ~origColor | 0xFF000000;
825 paint.setColor(kRectColor);
826 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
827 paint);
828 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kW * kH]);
829 sk_memset32(pixels.get(), ~origColor, kW * kH);
830 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
831 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
832 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
833 bool stop = false;
834 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
835 (origColor >> 0 & 0xFF),
836 (origColor >> 8 & 0xFF),
837 (origColor >> 16 & 0xFF));
838 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
839 (kRectColor >> 16 & 0xFF),
840 (kRectColor >> 8 & 0xFF),
841 (kRectColor >> 0 & 0xFF));
842 for (int y = 0; y < kH/2 && !stop; ++y) {
843 for (int x = 0; x < kW && !stop; ++x) {
844 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
845 if (rectColorPM != pixels[x + y * kW]) {
846 stop = true;
847 }
848 }
849 }
850 stop = false;
851 for (int y = kH/2; y < kH && !stop; ++y) {
852 for (int x = 0; x < kW && !stop; ++x) {
853 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
854 if (origColorPM != pixels[x + y * kW]) {
855 stop = true;
856 }
857 }
858 }
859}
bsalomone63ffef2016-02-05 07:17:34 -0800860
egdanielab527a52016-06-28 08:07:26 -0700861DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700862 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700863 if (!gpu) {
864 return;
865 }
866 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800867
ericrkc4025182016-05-04 12:01:58 -0700868 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
869 &create_gpu_surface_backend_texture_as_render_target}) {
870 // Validate that we can draw to the canvas and that the original texture color is
871 // preserved in pixels that aren't rendered to via the surface.
872 // This works only for non-multisampled case.
873 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700874 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700875 if (surface) {
876 test_surface_draw_partially(reporter, surface, kOrigColor);
877 surface.reset();
878 gpu->deleteTestingOnlyBackendTexture(textureObject);
879 }
880 }
881}
882
883
884DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700885 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700886 if (!gpu) {
887 return;
888 }
889 static const uint32_t kOrigColor = 0xFFAABBCC;
890
891 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
892 &create_gpu_surface_backend_texture_as_render_target}) {
893 for (int sampleCnt : {0, 4, 8}) {
894 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700895 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700896
897 if (!surface && sampleCnt > 0) {
898 // Certain platforms don't support MSAA, skip these.
899 continue;
900 }
901
902 // Validate that we can attach a stencil buffer to an SkSurface created by either of
903 // our surface functions.
904 GrRenderTarget* rt = surface->getCanvas()->internal_private_accessTopLayerDrawContext()
905 ->accessRenderTarget();
906 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700907 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700908 gpu->deleteTestingOnlyBackendTexture(textureObject);
909 }
bsalomone63ffef2016-02-05 07:17:34 -0800910 }
911}
912#endif