blob: 87c3a52fc9fdfcc700051ff310c9ae4d34521fbd [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,
kkinnunen179a8f52015-11-20 13:32:24 -0800131 bool expectOpaque) {
132 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) {
137 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(expectOpaque));
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 }) {
143 for (auto& isOpaque : { true, false }) {
144 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reede8f30622016-03-23 18:59:25 -0700145 auto surface(surface_func(alphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800146 test_snapshot_alphatype(reporter, surface, isOpaque);
bsalomon74f681d2015-06-23 14:38:48 -0700147 }
148 }
149}
kkinnunen179a8f52015-11-20 13:32:24 -0800150#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700151DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800152 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
153 for (auto& isOpaque : { true, false }) {
154 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
bsalomon8b7451a2016-05-11 06:33:06 -0700155 auto surface(surface_func(ctxInfo.grContext(), alphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800156 test_snapshot_alphatype(reporter, surface, isOpaque);
157 }
158 }
159}
160#endif
bsalomon74f681d2015-06-23 14:38:48 -0700161
kkinnunen179a8f52015-11-20 13:32:24 -0800162static GrBackendObject get_surface_backend_texture_handle(
163 SkSurface* s, SkSurface::BackendHandleAccess a) {
164 return s->getTextureHandle(a);
165}
166static GrBackendObject get_surface_backend_render_target_handle(
167 SkSurface* s, SkSurface::BackendHandleAccess a) {
168 GrBackendObject result;
169 if (!s->getRenderTargetHandle(&result, a)) {
170 return 0;
171 }
172 return result;
173}
174
175static void test_backend_handle_access_copy_on_write(
176 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
177 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700178 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700179 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700180
181 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700182 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700183
184 // If the access mode triggers CoW, then the backend objects should reflect it.
185 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
186}
kkinnunen179a8f52015-11-20 13:32:24 -0800187DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
188 const SkSurface::BackendHandleAccess accessModes[] = {
189 SkSurface::kFlushRead_BackendHandleAccess,
190 SkSurface::kFlushWrite_BackendHandleAccess,
191 SkSurface::kDiscardWrite_BackendHandleAccess,
192 };
193 for (auto& handle_access_func :
194 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
195 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700196 auto surface(create_surface());
197 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800198 handle_access_func);
199 }
200 }
201}
202#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700203DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800204 const SkSurface::BackendHandleAccess accessModes[] = {
205 SkSurface::kFlushRead_BackendHandleAccess,
206 SkSurface::kFlushWrite_BackendHandleAccess,
207 SkSurface::kDiscardWrite_BackendHandleAccess,
208 };
209 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
210 for (auto& handle_access_func :
211 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
212 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700213 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700214 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800215 handle_access_func);
216 }
217 }
218 }
219}
220#endif
fmalitae2639082015-08-06 07:04:51 -0700221
bsalomonf47b9a32016-02-22 11:02:58 -0800222static bool same_image(SkImage* a, SkImage* b,
223 std::function<intptr_t(SkImage*)> getImageBackingStore) {
224 return getImageBackingStore(a) == getImageBackingStore(b);
225}
226
227static bool same_image_surf(SkImage* a, SkSurface* b,
228 std::function<intptr_t(SkImage*)> getImageBackingStore,
229 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
230 return getImageBackingStore(a) == getSurfaceBackingStore(b);
231}
232
233static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
234 bool surfaceIsDirect,
235 std::function<intptr_t(SkImage*)> imageBackingStore,
236 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
237 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
238 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800239 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800240 {
reed9ce9d672016-03-17 10:51:11 -0700241 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
242 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800243 REPORTER_ASSERT(reporter, image->unique());
244 }
245 {
reed9ce9d672016-03-17 10:51:11 -0700246 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
247 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800248 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700249 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
250 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
251 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800252 REPORTER_ASSERT(reporter, image2->unique());
253 }
254 {
reed9ce9d672016-03-17 10:51:11 -0700255 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
256 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
257 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
258 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800259 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700260 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800261 // If the surface is not direct then images 1 and 3 should alias the surface's
262 // store.
reed9ce9d672016-03-17 10:51:11 -0700263 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800264 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700265 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
266 !same_image(image3.get(), image2.get(), ibs) &&
267 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800268 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700269 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800270 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700271 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
272 !same_image(image3.get(), image4.get(), ibs));
273 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800274 REPORTER_ASSERT(reporter, image4->unique());
275 }
276}
277
278DEF_TEST(UniqueImageSnapshot, reporter) {
279 auto getImageBackingStore = [reporter](SkImage* image) {
280 SkPixmap pm;
281 bool success = image->peekPixels(&pm);
282 REPORTER_ASSERT(reporter, success);
283 return reinterpret_cast<intptr_t>(pm.addr());
284 };
285 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800286 SkPixmap pmap;
287 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800288 REPORTER_ASSERT(reporter, pixels);
289 return reinterpret_cast<intptr_t>(pixels);
290 };
291
reede8f30622016-03-23 18:59:25 -0700292 auto surface(create_surface());
293 test_unique_image_snap(reporter, surface.get(), false, getImageBackingStore,
294 getSufaceBackingStore);
295 surface = create_direct_surface();
296 test_unique_image_snap(reporter, surface.get(), true, getImageBackingStore,
297 getSufaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800298}
299
300#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700301DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700302 GrContext* context = ctxInfo.grContext();
bsalomonf47b9a32016-02-22 11:02:58 -0800303 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700304 auto surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
bsalomonf47b9a32016-02-22 11:02:58 -0800305
306 auto imageBackingStore = [reporter](SkImage* image) {
307 GrTexture* texture = as_IB(image)->peekTexture();
308 if (!texture) {
309 ERRORF(reporter, "Not texture backed.");
310 return static_cast<intptr_t>(0);
311 }
312 return static_cast<intptr_t>(texture->getUniqueID());
313 };
314
315 auto surfaceBackingStore = [reporter](SkSurface* surface) {
robertphillips175dd9b2016-04-28 14:32:04 -0700316 GrDrawContext* dc = surface->getCanvas()->internal_private_accessTopLayerDrawContext();
317 GrRenderTarget* rt = dc->accessRenderTarget();
bsalomonf47b9a32016-02-22 11:02:58 -0800318 if (!rt) {
319 ERRORF(reporter, "Not render target backed.");
320 return static_cast<intptr_t>(0);
321 }
322 return static_cast<intptr_t>(rt->getUniqueID());
323 };
324
reede8f30622016-03-23 18:59:25 -0700325 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
326 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800327
328 // Test again with a "direct" render target;
329 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
egdaniel0a3a7f72016-06-24 09:22:31 -0700330 10, 10, kRGBA_8888_GrPixelConfig, true);
bsalomonf47b9a32016-02-22 11:02:58 -0800331 GrBackendTextureDesc desc;
332 desc.fConfig = kRGBA_8888_GrPixelConfig;
333 desc.fWidth = 10;
334 desc.fHeight = 10;
335 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
336 desc.fTextureHandle = textureObject;
robertphillips7e922762016-07-26 11:38:17 -0700337
bsalomonf47b9a32016-02-22 11:02:58 -0800338 {
robertphillips7e922762016-07-26 11:38:17 -0700339 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTexture(context, desc, nullptr));
reede8f30622016-03-23 18:59:25 -0700340 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800341 surfaceBackingStore);
342 }
robertphillips7e922762016-07-26 11:38:17 -0700343
bsalomonf47b9a32016-02-22 11:02:58 -0800344 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
345 }
346}
347#endif
348
kkinnunen179a8f52015-11-20 13:32:24 -0800349#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800350
kkinnunen179a8f52015-11-20 13:32:24 -0800351static void test_backend_handle_unique_id(
352 skiatest::Reporter* reporter, SkSurface* surface,
353 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700354 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800355 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
356 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700357 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800358 // just read access should not affect the snapshot
359 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
360
361 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
362 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700363 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800364 // expect a new image, since we claimed we would write
365 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
366
367 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
368 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700369 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800370 // expect a new(er) image, since we claimed we would write
371 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
372 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
373}
374// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700375DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800376 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700377 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800378 for (auto& handle_access_func :
379 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700380 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700381 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800382 }
383 }
384 }
385}
386#endif
387
388// Verify that the right canvas commands trigger a copy on write.
389static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000390 SkCanvas* canvas = surface->getCanvas();
391
392 const SkRect testRect =
393 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
394 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000395 SkPath testPath;
396 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
397 SkIntToScalar(2), SkIntToScalar(1)));
398
399 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
400
401 SkRegion testRegion;
402 testRegion.setRect(testIRect);
403
404
405 const SkColor testColor = 0x01020304;
406 const SkPaint testPaint;
407 const SkPoint testPoints[3] = {
408 {SkIntToScalar(0), SkIntToScalar(0)},
409 {SkIntToScalar(2), SkIntToScalar(1)},
410 {SkIntToScalar(0), SkIntToScalar(2)}
411 };
412 const size_t testPointCount = 3;
413
414 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000415 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000416 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000417
418 SkRRect testRRect;
419 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
420
421 SkString testText("Hello World");
422 const SkPoint testPoints2[] = {
423 { SkIntToScalar(0), SkIntToScalar(1) },
424 { SkIntToScalar(1), SkIntToScalar(1) },
425 { SkIntToScalar(2), SkIntToScalar(1) },
426 { SkIntToScalar(3), SkIntToScalar(1) },
427 { SkIntToScalar(4), SkIntToScalar(1) },
428 { SkIntToScalar(5), SkIntToScalar(1) },
429 { SkIntToScalar(6), SkIntToScalar(1) },
430 { SkIntToScalar(7), SkIntToScalar(1) },
431 { SkIntToScalar(8), SkIntToScalar(1) },
432 { SkIntToScalar(9), SkIntToScalar(1) },
433 { SkIntToScalar(10), SkIntToScalar(1) },
434 };
435
436#define EXPECT_COPY_ON_WRITE(command) \
437 { \
reed9ce9d672016-03-17 10:51:11 -0700438 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
439 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000440 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700441 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
442 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000443 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
444 }
445
446 EXPECT_COPY_ON_WRITE(clear(testColor))
447 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
448 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
449 testPaint))
450 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
451 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
452 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
453 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
454 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700455 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700456 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000457 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
458 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
459 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700460 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000461 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800462}
463DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700464 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800465}
466#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700467DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800468 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700469 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700470 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700471 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000472}
kkinnunen179a8f52015-11-20 13:32:24 -0800473#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000474
kkinnunen179a8f52015-11-20 13:32:24 -0800475static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
476 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000477 // This test succeeds by not triggering an assertion.
478 // The test verifies that the surface remains writable (usable) after
479 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000480 SkCanvas* canvas = surface->getCanvas();
481 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700482 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000483 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000484}
kkinnunen179a8f52015-11-20 13:32:24 -0800485DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700486 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800487}
488#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700489DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800490 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700491 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700492 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800493 }
494}
495#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000496
junov@chromium.orgb516a412013-05-01 22:49:59 +0000497#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800498static void test_crbug263329(skiatest::Reporter* reporter,
499 SkSurface* surface1,
500 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000501 // This is a regression test for crbug.com/263329
502 // Bug was caused by onCopyOnWrite releasing the old surface texture
503 // back to the scratch texture pool even though the texture is used
504 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000505 SkCanvas* canvas1 = surface1->getCanvas();
506 SkCanvas* canvas2 = surface2->getCanvas();
507 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700508 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000509 // Trigger copy on write, new backing is a scratch texture
510 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700511 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000512 // Trigger copy on write, old backing should not be returned to scratch
513 // pool because it is held by image2
514 canvas1->clear(3);
515
516 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700517 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000518 // Trigger copy on write on surface2. The new backing store should not
519 // be recycling a texture that is held by an existing image.
520 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700521 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800522 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000523 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800524 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
525 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
526 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
527 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
528 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000529}
egdanielab527a52016-06-28 08:07:26 -0700530DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800531 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700532 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
533 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700534 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800535 }
536}
537#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000538
kkinnunen179a8f52015-11-20 13:32:24 -0800539DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700540 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700541 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800542 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800543 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800544 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800545}
546#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700547DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800548 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700549 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700550 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800551 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700552 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000553 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800554 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800555 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000556 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000557}
kkinnunen179a8f52015-11-20 13:32:24 -0800558#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800559
kkinnunen179a8f52015-11-20 13:32:24 -0800560#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800561#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800562#include "SkGpuDevice.h"
563#include "SkImage_Gpu.h"
564#include "SkSurface_Gpu.h"
565
reede8f30622016-03-23 18:59:25 -0700566static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
567 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
robertphillipsea70c4b2016-07-20 08:54:31 -0700568 return gsurf->getDevice()->accessDrawContext()->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800569}
570
bsalomon5ec26ae2016-02-25 08:33:02 -0800571static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800572 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800573}
574
reed9ce9d672016-03-17 10:51:11 -0700575static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
576 return is_budgeted(image.get());
577}
578
egdanielab527a52016-06-28 08:07:26 -0700579DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800580 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800581 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
582 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700583 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800584 SkASSERT(surface);
585 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
586
reed9ce9d672016-03-17 10:51:11 -0700587 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800588
589 // Initially the image shares a texture with the surface, and the surface decides
590 // whether it is budgeted or not.
591 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
592 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
593
594 // Now trigger copy-on-write
595 surface->getCanvas()->clear(SK_ColorBLUE);
596
597 // They don't share a texture anymore. They should each have made their own budget
598 // decision.
599 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
600 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
601 }
602 }
603}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000604#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000605
kkinnunen179a8f52015-11-20 13:32:24 -0800606static void test_no_canvas1(skiatest::Reporter* reporter,
607 SkSurface* surface,
608 SkSurface::ContentChangeMode mode) {
609 // Test passes by not asserting
610 surface->notifyContentWillChange(mode);
611 SkDEBUGCODE(surface->validate();)
612}
613static void test_no_canvas2(skiatest::Reporter* reporter,
614 SkSurface* surface,
615 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000616 // Verifies the robustness of SkSurface for handling use cases where calls
617 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700618 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
619 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800620 SkDEBUGCODE(image1->validate();)
621 SkDEBUGCODE(surface->validate();)
622 surface->notifyContentWillChange(mode);
623 SkDEBUGCODE(image1->validate();)
624 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700625 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
626 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800627 SkDEBUGCODE(image2->validate();)
628 SkDEBUGCODE(surface->validate();)
629 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000630}
kkinnunen179a8f52015-11-20 13:32:24 -0800631DEF_TEST(SurfaceNoCanvas, reporter) {
632 SkSurface::ContentChangeMode modes[] =
633 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
634 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
635 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700636 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800637 }
638 }
639}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000640#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700641DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800642 SkSurface::ContentChangeMode modes[] =
643 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
644 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
645 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
646 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700647 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700648 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700649 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000650 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000651 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000652}
kkinnunen179a8f52015-11-20 13:32:24 -0800653#endif
reed9cd016e2016-01-30 10:01:06 -0800654
655static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800656 SkPixmap surfacePM;
657 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800658
reed9ce9d672016-03-17 10:51:11 -0700659 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800660 SkPixmap pm;
661 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800662
reed6ceeebd2016-03-09 14:26:26 -0800663 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800664
665 // trigger a copy-on-write
666 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700667 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800668 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
669
reed6ceeebd2016-03-09 14:26:26 -0800670 SkPixmap pm2;
671 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
672 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800673}
674
675DEF_TEST(surface_rowbytes, reporter) {
676 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
677
reede8f30622016-03-23 18:59:25 -0700678 auto surf0(SkSurface::MakeRaster(info));
679 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800680
681 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700682 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
683 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800684
685 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700686 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800687 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700688 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800689 REPORTER_ASSERT(reporter, nullptr == s);
690}
bsalomone63ffef2016-02-05 07:17:34 -0800691
fmalita03912f12016-07-06 06:22:06 -0700692DEF_TEST(surface_raster_zeroinitialized, reporter) {
693 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
694 SkPixmap pixmap;
695 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
696
697 for (int i = 0; i < pixmap.info().width(); ++i) {
698 for (int j = 0; j < pixmap.info().height(); ++j) {
699 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
700 }
701 }
702}
703
bsalomone63ffef2016-02-05 07:17:34 -0800704#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700705static sk_sp<SkSurface> create_gpu_surface_backend_texture(
706 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
707 const int kWidth = 10;
708 const int kHeight = 10;
709 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
710 sk_memset32(pixels.get(), color, kWidth * kHeight);
711 GrBackendTextureDesc desc;
712 desc.fConfig = kRGBA_8888_GrPixelConfig;
713 desc.fWidth = kWidth;
714 desc.fHeight = kHeight;
715 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
716 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700717 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700718 desc.fSampleCnt = sampleCnt;
719 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
720 if (!surface) {
721 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
722 return nullptr;
723 }
724 *outTexture = desc.fTextureHandle;
725 return surface;
726}
bsalomone63ffef2016-02-05 07:17:34 -0800727
ericrkc4025182016-05-04 12:01:58 -0700728static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
729 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
730 const int kWidth = 10;
731 const int kHeight = 10;
732 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
733 sk_memset32(pixels.get(), color, kWidth * kHeight);
734 GrBackendTextureDesc desc;
735 desc.fConfig = kRGBA_8888_GrPixelConfig;
736 desc.fWidth = kWidth;
737 desc.fHeight = kHeight;
738 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
739 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700740 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700741 desc.fSampleCnt = sampleCnt;
742 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
743 nullptr);
744 if (!surface) {
745 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
746 return nullptr;
747 }
748 *outTexture = desc.fTextureHandle;
749 return surface;
750}
751
752static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
753 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
754 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800755 if (!surface) {
756 ERRORF(reporter, "Could not create GPU SkSurface.");
757 return;
758 }
759 int w = surface->width();
760 int h = surface->height();
761 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700762 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800763
reede8f30622016-03-23 18:59:25 -0700764 SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800765 if (!grSurface) {
766 ERRORF(reporter, "Could access render target of GPU SkSurface.");
767 return;
768 }
bsalomon2fba8092016-02-05 13:47:06 -0800769 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800770 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
771 for (int y = 0; y < h; ++y) {
772 for (int x = 0; x < w; ++x) {
773 uint32_t pixel = pixels.get()[y * w + x];
774 if (pixel != expectedValue) {
775 SkString msg;
776 if (expectedValue) {
777 msg = "SkSurface should have left render target unmodified";
778 } else {
779 msg = "SkSurface should have cleared the render target";
780 }
781 ERRORF(reporter,
782 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
783 expectedValue, x, y);
784 return;
785 }
786 }
787 }
788}
789
bsalomon758586c2016-04-06 14:02:39 -0700790DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700791 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700792
bsalomone63ffef2016-02-05 07:17:34 -0800793 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700794 [] (SkSurface* s){
robertphillips175dd9b2016-04-28 14:32:04 -0700795 GrDrawContext* dc = s->getCanvas()->internal_private_accessTopLayerDrawContext();
796 return dc->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700797 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
ericrkc4025182016-05-04 12:01:58 -0700798 return as_IB(i)->peekTexture(); }
bsalomone63ffef2016-02-05 07:17:34 -0800799 };
ericrkc4025182016-05-04 12:01:58 -0700800
bsalomone63ffef2016-02-05 07:17:34 -0800801 for (auto grSurfaceGetter : grSurfaceGetters) {
ericrkc4025182016-05-04 12:01:58 -0700802 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800803 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700804 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800805 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
806 }
807 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700808 const uint32_t kOrigColor = 0xABABABAB;
809 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
810 &create_gpu_surface_backend_texture_as_render_target}) {
811 GrBackendObject textureObject;
812 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
813 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
814 surface.reset();
815 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
816 }
817 }
818}
bsalomone63ffef2016-02-05 07:17:34 -0800819
ericrkc4025182016-05-04 12:01:58 -0700820static void test_surface_draw_partially(
821 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
822 const int kW = surface->width();
823 const int kH = surface->height();
824 SkPaint paint;
825 const SkColor kRectColor = ~origColor | 0xFF000000;
826 paint.setColor(kRectColor);
827 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
828 paint);
829 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kW * kH]);
830 sk_memset32(pixels.get(), ~origColor, kW * kH);
831 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
832 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
833 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
834 bool stop = false;
835 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
836 (origColor >> 0 & 0xFF),
837 (origColor >> 8 & 0xFF),
838 (origColor >> 16 & 0xFF));
839 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
840 (kRectColor >> 16 & 0xFF),
841 (kRectColor >> 8 & 0xFF),
842 (kRectColor >> 0 & 0xFF));
843 for (int y = 0; y < kH/2 && !stop; ++y) {
844 for (int x = 0; x < kW && !stop; ++x) {
845 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
846 if (rectColorPM != pixels[x + y * kW]) {
847 stop = true;
848 }
849 }
850 }
851 stop = false;
852 for (int y = kH/2; y < kH && !stop; ++y) {
853 for (int x = 0; x < kW && !stop; ++x) {
854 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
855 if (origColorPM != pixels[x + y * kW]) {
856 stop = true;
857 }
858 }
859 }
860}
bsalomone63ffef2016-02-05 07:17:34 -0800861
egdanielab527a52016-06-28 08:07:26 -0700862DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700863 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700864 if (!gpu) {
865 return;
866 }
867 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800868
ericrkc4025182016-05-04 12:01:58 -0700869 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
870 &create_gpu_surface_backend_texture_as_render_target}) {
871 // Validate that we can draw to the canvas and that the original texture color is
872 // preserved in pixels that aren't rendered to via the surface.
873 // This works only for non-multisampled case.
874 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700875 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700876 if (surface) {
877 test_surface_draw_partially(reporter, surface, kOrigColor);
878 surface.reset();
879 gpu->deleteTestingOnlyBackendTexture(textureObject);
880 }
881 }
882}
883
884
885DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700886 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700887 if (!gpu) {
888 return;
889 }
890 static const uint32_t kOrigColor = 0xFFAABBCC;
891
892 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
893 &create_gpu_surface_backend_texture_as_render_target}) {
894 for (int sampleCnt : {0, 4, 8}) {
895 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700896 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700897
898 if (!surface && sampleCnt > 0) {
899 // Certain platforms don't support MSAA, skip these.
900 continue;
901 }
902
903 // Validate that we can attach a stencil buffer to an SkSurface created by either of
904 // our surface functions.
905 GrRenderTarget* rt = surface->getCanvas()->internal_private_accessTopLayerDrawContext()
906 ->accessRenderTarget();
907 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700908 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700909 gpu->deleteTestingOnlyBackendTexture(textureObject);
910 }
bsalomone63ffef2016-02-05 07:17:34 -0800911 }
912}
913#endif