blob: 4aaabd8062257acd3edb25f0750b9accb3d845a2 [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"
21#include "GrGpu.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000022#endif
23
kkinnunen179a8f52015-11-20 13:32:24 -080024#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070025
kkinnunen179a8f52015-11-20 13:32:24 -080026static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070027 SkASSERT(pixels == context);
28 sk_free(pixels);
29}
reede8f30622016-03-23 18:59:25 -070030static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
31 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070032 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000033 if (requestedInfo) {
34 *requestedInfo = info;
35 }
reede8f30622016-03-23 18:59:25 -070036 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000037}
reede8f30622016-03-23 18:59:25 -070038static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
39 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080040 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
41 if (requestedInfo) {
42 *requestedInfo = info;
43 }
44 const size_t rowBytes = info.minRowBytes();
45 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070046 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
47 release_direct_surface_storage,
48 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080049}
50#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070051static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
52 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080053 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
54 if (requestedInfo) {
55 *requestedInfo = info;
56 }
reede8f30622016-03-23 18:59:25 -070057 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -080058}
reede8f30622016-03-23 18:59:25 -070059static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
60 SkAlphaType at = kPremul_SkAlphaType,
61 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080062 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
63 if (requestedInfo) {
64 *requestedInfo = info;
65 }
reede8f30622016-03-23 18:59:25 -070066 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 0, nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -080067}
68#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000069
kkinnunen179a8f52015-11-20 13:32:24 -080070DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080071 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070072 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
73 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080074
reedb2497c22014-12-31 12:31:43 -080075}
kkinnunen179a8f52015-11-20 13:32:24 -080076#if SK_SUPPORT_GPU
77DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, context) {
78 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
79 REPORTER_ASSERT(reporter, nullptr ==
reede8f30622016-03-23 18:59:25 -070080 SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -080081}
82#endif
reedb2497c22014-12-31 12:31:43 -080083
bsalomone4579ad2015-04-08 08:38:40 -070084#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080085DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedTexture, reporter, context) {
bsalomone63ffef2016-02-05 07:17:34 -080086 GrGpu* gpu = context->getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -070087 if (!gpu) {
bsalomone4579ad2015-04-08 08:38:40 -070088 return;
89 }
bsalomon7a617932015-06-16 08:07:16 -070090
jvanverth672bb7f2015-07-13 07:19:57 -070091 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
joshualitt81793412015-07-08 12:54:04 -070092 // a SkSurface.
joshualitt81793412015-07-08 12:54:04 -070093 static const int kW = 100;
94 static const int kH = 100;
95 static const uint32_t kOrigColor = 0xFFAABBCC;
96 SkAutoTArray<uint32_t> pixels(kW * kH);
97 sk_memset32(pixels.get(), kOrigColor, kW * kH);
bsalomon091f60c2015-11-10 11:54:56 -080098 GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
99 kRGBA_8888_GrPixelConfig);
bsalomone4579ad2015-04-08 08:38:40 -0700100
joshualitt81793412015-07-08 12:54:04 -0700101 GrBackendTextureDesc wrappedDesc;
102 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
103 wrappedDesc.fWidth = kW;
104 wrappedDesc.fHeight = kH;
105 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
106 wrappedDesc.fSampleCnt = 0;
107 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
bsalomon091f60c2015-11-10 11:54:56 -0800108 wrappedDesc.fTextureHandle = texHandle;
bsalomone4579ad2015-04-08 08:38:40 -0700109
reede8f30622016-03-23 18:59:25 -0700110 auto surface(SkSurface::MakeFromBackendTexture(context, wrappedDesc, nullptr));
joshualitt81793412015-07-08 12:54:04 -0700111 REPORTER_ASSERT(reporter, surface);
112 if (surface) {
113 // Validate that we can draw to the canvas and that the original texture color is preserved
114 // in pixels that aren't rendered to via the surface.
115 SkPaint paint;
116 static const SkColor kRectColor = ~kOrigColor | 0xFF000000;
117 paint.setColor(kRectColor);
118 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
119 paint);
120 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
121 surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0);
122 bool stop = false;
123 SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF),
124 (kOrigColor >> 0 & 0xFF),
125 (kOrigColor >> 8 & 0xFF),
126 (kOrigColor >> 16 & 0xFF));
127 SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
128 (kRectColor >> 16 & 0xFF),
129 (kRectColor >> 8 & 0xFF),
130 (kRectColor >> 0 & 0xFF));
131 for (int y = 0; y < kH/2 && !stop; ++y) {
132 for (int x = 0; x < kW && !stop; ++x) {
133 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
134 if (rectColorPM != pixels[x + y * kW]) {
135 stop = true;
bsalomon7a617932015-06-16 08:07:16 -0700136 }
137 }
138 }
joshualitt81793412015-07-08 12:54:04 -0700139 stop = false;
140 for (int y = kH/2; y < kH && !stop; ++y) {
141 for (int x = 0; x < kW && !stop; ++x) {
142 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
143 if (origColorPM != pixels[x + y * kW]) {
144 stop = true;
145 }
146 }
bsalomon7a617932015-06-16 08:07:16 -0700147 }
148 }
bsalomon67d76202015-11-11 12:40:42 -0800149 gpu->deleteTestingOnlyBackendTexture(texHandle);
bsalomone4579ad2015-04-08 08:38:40 -0700150}
151#endif
152
kkinnunen179a8f52015-11-20 13:32:24 -0800153static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700154 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800155 const SkImageInfo& requestInfo,
156 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000157 const SkColor color = SK_ColorRED;
158 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800159 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000160
reed6ceeebd2016-03-09 14:26:26 -0800161 SkPixmap pmap;
162 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800163 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000164
reed6ceeebd2016-03-09 14:26:26 -0800165 SkPixmap pmap2;
166 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000167
kkinnunen179a8f52015-11-20 13:32:24 -0800168 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800169 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
170 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
171 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000172
reed6ceeebd2016-03-09 14:26:26 -0800173 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
174 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
175 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800176 } else {
177 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000178 }
179}
kkinnunen179a8f52015-11-20 13:32:24 -0800180DEF_TEST(SurfaceCanvasPeek, reporter) {
181 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
182 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700183 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800184 test_canvas_peek(reporter, surface, requestInfo, true);
185 }
186}
187#if SK_SUPPORT_GPU
188DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, context) {
189 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
190 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700191 auto surface(surface_func(context, kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800192 test_canvas_peek(reporter, surface, requestInfo, false);
193 }
194}
195#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000196
reed41e010c2015-06-09 12:16:53 -0700197// For compatibility with clients that still call accessBitmap(), we need to ensure that we bump
198// the bitmap's genID when we draw to it, else they won't know it has new values. When they are
199// exclusively using surface/image, and we can hide accessBitmap from device, we can remove this
200// test.
reede8f30622016-03-23 18:59:25 -0700201void test_access_pixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface) {
kkinnunen179a8f52015-11-20 13:32:24 -0800202 SkCanvas* canvas = surface->getCanvas();
203 canvas->clear(0);
reed41e010c2015-06-09 12:16:53 -0700204
kkinnunen179a8f52015-11-20 13:32:24 -0800205 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
206 SkBitmap bm = device->accessBitmap(false);
207 uint32_t genID0 = bm.getGenerationID();
208 // Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write)
209 canvas->drawColor(SK_ColorBLUE);
210 // Now check that we get a different genID
211 uint32_t genID1 = bm.getGenerationID();
212 REPORTER_ASSERT(reporter, genID0 != genID1);
213}
214DEF_TEST(SurfaceAccessPixels, reporter) {
215 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
reede8f30622016-03-23 18:59:25 -0700216 auto surface(surface_func(kPremul_SkAlphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800217 test_access_pixels(reporter, surface);
218 }
219}
220#if SK_SUPPORT_GPU
221DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAccessPixels_Gpu, reporter, context) {
222 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700223 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800224 test_access_pixels(reporter, surface);
225 }
226}
227#endif
228
reede8f30622016-03-23 18:59:25 -0700229static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800230 bool expectOpaque) {
231 REPORTER_ASSERT(reporter, surface);
232 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700233 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800234 REPORTER_ASSERT(reporter, image);
235 if (image) {
236 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(expectOpaque));
reed41e010c2015-06-09 12:16:53 -0700237 }
238 }
239}
kkinnunen179a8f52015-11-20 13:32:24 -0800240DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
241 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
242 for (auto& isOpaque : { true, false }) {
243 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reede8f30622016-03-23 18:59:25 -0700244 auto surface(surface_func(alphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800245 test_snapshot_alphatype(reporter, surface, isOpaque);
bsalomon74f681d2015-06-23 14:38:48 -0700246 }
247 }
248}
kkinnunen179a8f52015-11-20 13:32:24 -0800249#if SK_SUPPORT_GPU
250DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, context) {
251 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
252 for (auto& isOpaque : { true, false }) {
253 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reede8f30622016-03-23 18:59:25 -0700254 auto surface(surface_func(context, alphaType, nullptr));
kkinnunen179a8f52015-11-20 13:32:24 -0800255 test_snapshot_alphatype(reporter, surface, isOpaque);
256 }
257 }
258}
259#endif
bsalomon74f681d2015-06-23 14:38:48 -0700260
kkinnunen179a8f52015-11-20 13:32:24 -0800261static GrBackendObject get_surface_backend_texture_handle(
262 SkSurface* s, SkSurface::BackendHandleAccess a) {
263 return s->getTextureHandle(a);
264}
265static GrBackendObject get_surface_backend_render_target_handle(
266 SkSurface* s, SkSurface::BackendHandleAccess a) {
267 GrBackendObject result;
268 if (!s->getRenderTargetHandle(&result, a)) {
269 return 0;
270 }
271 return result;
272}
273
274static void test_backend_handle_access_copy_on_write(
275 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
276 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700277 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700278 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700279
280 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700281 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700282
283 // If the access mode triggers CoW, then the backend objects should reflect it.
284 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
285}
kkinnunen179a8f52015-11-20 13:32:24 -0800286DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
287 const SkSurface::BackendHandleAccess accessModes[] = {
288 SkSurface::kFlushRead_BackendHandleAccess,
289 SkSurface::kFlushWrite_BackendHandleAccess,
290 SkSurface::kDiscardWrite_BackendHandleAccess,
291 };
292 for (auto& handle_access_func :
293 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
294 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700295 auto surface(create_surface());
296 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800297 handle_access_func);
298 }
299 }
300}
301#if SK_SUPPORT_GPU
302DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, context) {
303 const SkSurface::BackendHandleAccess accessModes[] = {
304 SkSurface::kFlushRead_BackendHandleAccess,
305 SkSurface::kFlushWrite_BackendHandleAccess,
306 SkSurface::kDiscardWrite_BackendHandleAccess,
307 };
308 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
309 for (auto& handle_access_func :
310 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
311 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700312 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
313 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800314 handle_access_func);
315 }
316 }
317 }
318}
319#endif
fmalitae2639082015-08-06 07:04:51 -0700320
bsalomonf47b9a32016-02-22 11:02:58 -0800321static bool same_image(SkImage* a, SkImage* b,
322 std::function<intptr_t(SkImage*)> getImageBackingStore) {
323 return getImageBackingStore(a) == getImageBackingStore(b);
324}
325
326static bool same_image_surf(SkImage* a, SkSurface* b,
327 std::function<intptr_t(SkImage*)> getImageBackingStore,
328 std::function<intptr_t(SkSurface*)> getSurfaceBackingStore) {
329 return getImageBackingStore(a) == getSurfaceBackingStore(b);
330}
331
332static void test_unique_image_snap(skiatest::Reporter* reporter, SkSurface* surface,
333 bool surfaceIsDirect,
334 std::function<intptr_t(SkImage*)> imageBackingStore,
335 std::function<intptr_t(SkSurface*)> surfaceBackingStore) {
336 std::function<intptr_t(SkImage*)> ibs = imageBackingStore;
337 std::function<intptr_t(SkSurface*)> sbs = surfaceBackingStore;
bsalomon5ec26ae2016-02-25 08:33:02 -0800338 static const SkBudgeted kB = SkBudgeted::kNo;
bsalomonf47b9a32016-02-22 11:02:58 -0800339 {
reed9ce9d672016-03-17 10:51:11 -0700340 sk_sp<SkImage> image(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
341 REPORTER_ASSERT(reporter, !same_image_surf(image.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800342 REPORTER_ASSERT(reporter, image->unique());
343 }
344 {
reed9ce9d672016-03-17 10:51:11 -0700345 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
346 REPORTER_ASSERT(reporter, !same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800347 REPORTER_ASSERT(reporter, image1->unique());
reed9ce9d672016-03-17 10:51:11 -0700348 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
349 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
350 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800351 REPORTER_ASSERT(reporter, image2->unique());
352 }
353 {
reed9ce9d672016-03-17 10:51:11 -0700354 sk_sp<SkImage> image1(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
355 sk_sp<SkImage> image2(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
356 sk_sp<SkImage> image3(surface->makeImageSnapshot(kB, SkSurface::kNo_ForceUnique));
357 sk_sp<SkImage> image4(surface->makeImageSnapshot(kB, SkSurface::kYes_ForceUnique));
bsalomonf47b9a32016-02-22 11:02:58 -0800358 // Image 1 and 3 ought to be the same (or we're missing an optimization).
reed9ce9d672016-03-17 10:51:11 -0700359 REPORTER_ASSERT(reporter, same_image(image1.get(), image3.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800360 // If the surface is not direct then images 1 and 3 should alias the surface's
361 // store.
reed9ce9d672016-03-17 10:51:11 -0700362 REPORTER_ASSERT(reporter, !surfaceIsDirect == same_image_surf(image1.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800363 // Image 2 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700364 REPORTER_ASSERT(reporter, !same_image(image1.get(), image2.get(), ibs) &&
365 !same_image(image3.get(), image2.get(), ibs) &&
366 !same_image(image4.get(), image2.get(), ibs));
bsalomonf47b9a32016-02-22 11:02:58 -0800367 REPORTER_ASSERT(reporter, image2->unique());
reed9ce9d672016-03-17 10:51:11 -0700368 REPORTER_ASSERT(reporter, !same_image_surf(image2.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800369 // Image 4 should not be shared with any other image.
reed9ce9d672016-03-17 10:51:11 -0700370 REPORTER_ASSERT(reporter, !same_image(image1.get(), image4.get(), ibs) &&
371 !same_image(image3.get(), image4.get(), ibs));
372 REPORTER_ASSERT(reporter, !same_image_surf(image4.get(), surface, ibs, sbs));
bsalomonf47b9a32016-02-22 11:02:58 -0800373 REPORTER_ASSERT(reporter, image4->unique());
374 }
375}
376
377DEF_TEST(UniqueImageSnapshot, reporter) {
378 auto getImageBackingStore = [reporter](SkImage* image) {
379 SkPixmap pm;
380 bool success = image->peekPixels(&pm);
381 REPORTER_ASSERT(reporter, success);
382 return reinterpret_cast<intptr_t>(pm.addr());
383 };
384 auto getSufaceBackingStore = [reporter](SkSurface* surface) {
reed6ceeebd2016-03-09 14:26:26 -0800385 SkPixmap pmap;
386 const void* pixels = surface->getCanvas()->peekPixels(&pmap) ? pmap.addr() : nullptr;
bsalomonf47b9a32016-02-22 11:02:58 -0800387 REPORTER_ASSERT(reporter, pixels);
388 return reinterpret_cast<intptr_t>(pixels);
389 };
390
reede8f30622016-03-23 18:59:25 -0700391 auto surface(create_surface());
392 test_unique_image_snap(reporter, surface.get(), false, getImageBackingStore,
393 getSufaceBackingStore);
394 surface = create_direct_surface();
395 test_unique_image_snap(reporter, surface.get(), true, getImageBackingStore,
396 getSufaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800397}
398
399#if SK_SUPPORT_GPU
400DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UniqueImageSnapshot_Gpu, reporter, context) {
401 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700402 auto surface(surface_func(context, kOpaque_SkAlphaType, nullptr));
bsalomonf47b9a32016-02-22 11:02:58 -0800403
404 auto imageBackingStore = [reporter](SkImage* image) {
405 GrTexture* texture = as_IB(image)->peekTexture();
406 if (!texture) {
407 ERRORF(reporter, "Not texture backed.");
408 return static_cast<intptr_t>(0);
409 }
410 return static_cast<intptr_t>(texture->getUniqueID());
411 };
412
413 auto surfaceBackingStore = [reporter](SkSurface* surface) {
414 GrRenderTarget* rt =
415 surface->getCanvas()->internal_private_accessTopLayerRenderTarget();
416 if (!rt) {
417 ERRORF(reporter, "Not render target backed.");
418 return static_cast<intptr_t>(0);
419 }
420 return static_cast<intptr_t>(rt->getUniqueID());
421 };
422
reede8f30622016-03-23 18:59:25 -0700423 test_unique_image_snap(reporter, surface.get(), false, imageBackingStore,
424 surfaceBackingStore);
bsalomonf47b9a32016-02-22 11:02:58 -0800425
426 // Test again with a "direct" render target;
427 GrBackendObject textureObject = context->getGpu()->createTestingOnlyBackendTexture(nullptr,
428 10, 10, kRGBA_8888_GrPixelConfig);
429 GrBackendTextureDesc desc;
430 desc.fConfig = kRGBA_8888_GrPixelConfig;
431 desc.fWidth = 10;
432 desc.fHeight = 10;
433 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
434 desc.fTextureHandle = textureObject;
435 GrTexture* texture = context->textureProvider()->wrapBackendTexture(desc);
436 {
reede8f30622016-03-23 18:59:25 -0700437 auto surface(SkSurface::MakeRenderTargetDirect(texture->asRenderTarget()));
438 test_unique_image_snap(reporter, surface.get(), true, imageBackingStore,
bsalomonf47b9a32016-02-22 11:02:58 -0800439 surfaceBackingStore);
440 }
441 texture->unref();
442 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
443 }
444}
445#endif
446
kkinnunen179a8f52015-11-20 13:32:24 -0800447#if SK_SUPPORT_GPU
448// May we (soon) eliminate the need to keep testing this, by hiding the bloody device!
449static uint32_t get_legacy_gen_id(SkSurface* surface) {
450 SkBaseDevice* device =
451 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
452 return device->accessBitmap(false).getGenerationID();
453}
454/*
455 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
456 * texture handle for writing.
457 *
reed9ce9d672016-03-17 10:51:11 -0700458 * Note: this needs to be tested separately from checking makeImageSnapshot, as calling that
kkinnunen179a8f52015-11-20 13:32:24 -0800459 * can also incidentally bump the genID (when a new backing surface is created).
460 */
461static void test_backend_handle_gen_id(
462 skiatest::Reporter* reporter, SkSurface* surface,
463 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
464 const uint32_t gen0 = get_legacy_gen_id(surface);
465 func(surface, SkSurface::kFlushRead_BackendHandleAccess);
466 const uint32_t gen1 = get_legacy_gen_id(surface);
467 REPORTER_ASSERT(reporter, gen0 == gen1);
468
469 func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
470 const uint32_t gen2 = get_legacy_gen_id(surface);
471 REPORTER_ASSERT(reporter, gen0 != gen2);
472
473 func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
474 const uint32_t gen3 = get_legacy_gen_id(surface);
475 REPORTER_ASSERT(reporter, gen0 != gen3);
476 REPORTER_ASSERT(reporter, gen2 != gen3);
477}
478static void test_backend_handle_unique_id(
479 skiatest::Reporter* reporter, SkSurface* surface,
480 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700481 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800482 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
483 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700484 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800485 // just read access should not affect the snapshot
486 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
487
488 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
489 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700490 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800491 // expect a new image, since we claimed we would write
492 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
493
494 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
495 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700496 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800497 // expect a new(er) image, since we claimed we would write
498 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
499 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
500}
501// No CPU test.
502DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, context) {
503 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
504 for (auto& test_func : { &test_backend_handle_unique_id, &test_backend_handle_gen_id }) {
505 for (auto& handle_access_func :
506 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
reede8f30622016-03-23 18:59:25 -0700507 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
508 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800509 }
510 }
511 }
512}
513#endif
514
515// Verify that the right canvas commands trigger a copy on write.
516static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000517 SkCanvas* canvas = surface->getCanvas();
518
519 const SkRect testRect =
520 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
521 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000522 SkPath testPath;
523 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
524 SkIntToScalar(2), SkIntToScalar(1)));
525
526 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
527
528 SkRegion testRegion;
529 testRegion.setRect(testIRect);
530
531
532 const SkColor testColor = 0x01020304;
533 const SkPaint testPaint;
534 const SkPoint testPoints[3] = {
535 {SkIntToScalar(0), SkIntToScalar(0)},
536 {SkIntToScalar(2), SkIntToScalar(1)},
537 {SkIntToScalar(0), SkIntToScalar(2)}
538 };
539 const size_t testPointCount = 3;
540
541 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000542 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000543 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000544
545 SkRRect testRRect;
546 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
547
548 SkString testText("Hello World");
549 const SkPoint testPoints2[] = {
550 { SkIntToScalar(0), SkIntToScalar(1) },
551 { SkIntToScalar(1), SkIntToScalar(1) },
552 { SkIntToScalar(2), SkIntToScalar(1) },
553 { SkIntToScalar(3), SkIntToScalar(1) },
554 { SkIntToScalar(4), SkIntToScalar(1) },
555 { SkIntToScalar(5), SkIntToScalar(1) },
556 { SkIntToScalar(6), SkIntToScalar(1) },
557 { SkIntToScalar(7), SkIntToScalar(1) },
558 { SkIntToScalar(8), SkIntToScalar(1) },
559 { SkIntToScalar(9), SkIntToScalar(1) },
560 { SkIntToScalar(10), SkIntToScalar(1) },
561 };
562
563#define EXPECT_COPY_ON_WRITE(command) \
564 { \
reed9ce9d672016-03-17 10:51:11 -0700565 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
566 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000567 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700568 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
569 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000570 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
571 }
572
573 EXPECT_COPY_ON_WRITE(clear(testColor))
574 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
575 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
576 testPaint))
577 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
578 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
579 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
580 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
581 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700582 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700583 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000584 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
585 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
586 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700587 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000588 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800589}
590DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700591 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800592}
593#if SK_SUPPORT_GPU
594DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, context) {
595 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700596 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
597 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700598 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000599}
kkinnunen179a8f52015-11-20 13:32:24 -0800600#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000601
kkinnunen179a8f52015-11-20 13:32:24 -0800602static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
603 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000604 // This test succeeds by not triggering an assertion.
605 // The test verifies that the surface remains writable (usable) after
606 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000607 SkCanvas* canvas = surface->getCanvas();
608 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700609 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000610 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000611}
kkinnunen179a8f52015-11-20 13:32:24 -0800612DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700613 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800614}
615#if SK_SUPPORT_GPU
616DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, context) {
617 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700618 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
619 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800620 }
621}
622#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000623
junov@chromium.orgb516a412013-05-01 22:49:59 +0000624#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800625static void test_crbug263329(skiatest::Reporter* reporter,
626 SkSurface* surface1,
627 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000628 // This is a regression test for crbug.com/263329
629 // Bug was caused by onCopyOnWrite releasing the old surface texture
630 // back to the scratch texture pool even though the texture is used
631 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000632 SkCanvas* canvas1 = surface1->getCanvas();
633 SkCanvas* canvas2 = surface2->getCanvas();
634 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700635 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000636 // Trigger copy on write, new backing is a scratch texture
637 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700638 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000639 // Trigger copy on write, old backing should not be returned to scratch
640 // pool because it is held by image2
641 canvas1->clear(3);
642
643 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700644 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000645 // Trigger copy on write on surface2. The new backing store should not
646 // be recycling a texture that is held by an existing image.
647 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700648 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800649 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000650 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800651 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
652 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
653 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
654 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
655 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000656}
kkinnunen179a8f52015-11-20 13:32:24 -0800657DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, context) {
658 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700659 auto surface1(surface_func(context, kPremul_SkAlphaType, nullptr));
660 auto surface2(surface_func(context, kPremul_SkAlphaType, nullptr));
661 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800662 }
663}
664#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000665
kkinnunen179a8f52015-11-20 13:32:24 -0800666DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700667 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700668 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800669 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800670 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800671 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800672}
673#if SK_SUPPORT_GPU
bsalomon84a4e5a2016-02-29 11:41:52 -0800674DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, context) {
kkinnunen179a8f52015-11-20 13:32:24 -0800675 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
reede8f30622016-03-23 18:59:25 -0700676 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700677 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800678 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700679 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000680 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800681 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800682 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000683 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000684}
kkinnunen179a8f52015-11-20 13:32:24 -0800685#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800686
kkinnunen179a8f52015-11-20 13:32:24 -0800687#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800688#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800689#include "SkGpuDevice.h"
690#include "SkImage_Gpu.h"
691#include "SkSurface_Gpu.h"
692
reede8f30622016-03-23 18:59:25 -0700693static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
694 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
695 return gsurf->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800696}
697
bsalomon5ec26ae2016-02-25 08:33:02 -0800698static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800699 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800700}
701
reed9ce9d672016-03-17 10:51:11 -0700702static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
703 return is_budgeted(image.get());
704}
705
kkinnunen179a8f52015-11-20 13:32:24 -0800706DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, context) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800707 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800708 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
709 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
reede8f30622016-03-23 18:59:25 -0700710 auto surface(SkSurface::MakeRenderTarget(context, sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800711 SkASSERT(surface);
712 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
713
reed9ce9d672016-03-17 10:51:11 -0700714 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800715
716 // Initially the image shares a texture with the surface, and the surface decides
717 // whether it is budgeted or not.
718 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
719 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
720
721 // Now trigger copy-on-write
722 surface->getCanvas()->clear(SK_ColorBLUE);
723
724 // They don't share a texture anymore. They should each have made their own budget
725 // decision.
726 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
727 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
728 }
729 }
730}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000731#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000732
kkinnunen179a8f52015-11-20 13:32:24 -0800733static void test_no_canvas1(skiatest::Reporter* reporter,
734 SkSurface* surface,
735 SkSurface::ContentChangeMode mode) {
736 // Test passes by not asserting
737 surface->notifyContentWillChange(mode);
738 SkDEBUGCODE(surface->validate();)
739}
740static void test_no_canvas2(skiatest::Reporter* reporter,
741 SkSurface* surface,
742 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000743 // Verifies the robustness of SkSurface for handling use cases where calls
744 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700745 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
746 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800747 SkDEBUGCODE(image1->validate();)
748 SkDEBUGCODE(surface->validate();)
749 surface->notifyContentWillChange(mode);
750 SkDEBUGCODE(image1->validate();)
751 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700752 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
753 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800754 SkDEBUGCODE(image2->validate();)
755 SkDEBUGCODE(surface->validate();)
756 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000757}
kkinnunen179a8f52015-11-20 13:32:24 -0800758DEF_TEST(SurfaceNoCanvas, reporter) {
759 SkSurface::ContentChangeMode modes[] =
760 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
761 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
762 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700763 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800764 }
765 }
766}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000767#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800768DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, context) {
769 SkSurface::ContentChangeMode modes[] =
770 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
771 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
772 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
773 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700774 auto surface(surface_func(context, kPremul_SkAlphaType, nullptr));
775 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700776 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000777 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000778 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000779}
kkinnunen179a8f52015-11-20 13:32:24 -0800780#endif
reed9cd016e2016-01-30 10:01:06 -0800781
782static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800783 SkPixmap surfacePM;
784 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800785
reed9ce9d672016-03-17 10:51:11 -0700786 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800787 SkPixmap pm;
788 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800789
reed6ceeebd2016-03-09 14:26:26 -0800790 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800791
792 // trigger a copy-on-write
793 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700794 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800795 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
796
reed6ceeebd2016-03-09 14:26:26 -0800797 SkPixmap pm2;
798 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
799 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800800}
801
802DEF_TEST(surface_rowbytes, reporter) {
803 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
804
reede8f30622016-03-23 18:59:25 -0700805 auto surf0(SkSurface::MakeRaster(info));
806 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800807
808 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700809 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
810 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800811
812 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700813 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800814 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700815 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800816 REPORTER_ASSERT(reporter, nullptr == s);
817}
bsalomone63ffef2016-02-05 07:17:34 -0800818
819#if SK_SUPPORT_GPU
820
reede8f30622016-03-23 18:59:25 -0700821void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
bsalomone63ffef2016-02-05 07:17:34 -0800822 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
823 uint32_t expectedValue) {
824 if (!surface) {
825 ERRORF(reporter, "Could not create GPU SkSurface.");
826 return;
827 }
828 int w = surface->width();
829 int h = surface->height();
830 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
831 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
832
reede8f30622016-03-23 18:59:25 -0700833 SkAutoTUnref<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800834 if (!grSurface) {
835 ERRORF(reporter, "Could access render target of GPU SkSurface.");
836 return;
837 }
bsalomon2fba8092016-02-05 13:47:06 -0800838 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800839 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
840 for (int y = 0; y < h; ++y) {
841 for (int x = 0; x < w; ++x) {
842 uint32_t pixel = pixels.get()[y * w + x];
843 if (pixel != expectedValue) {
844 SkString msg;
845 if (expectedValue) {
846 msg = "SkSurface should have left render target unmodified";
847 } else {
848 msg = "SkSurface should have cleared the render target";
849 }
850 ERRORF(reporter,
851 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
852 expectedValue, x, y);
853 return;
854 }
855 }
856 }
857}
858
859DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, context) {
860 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
bsalomon2fba8092016-02-05 13:47:06 -0800861 [] (SkSurface* s){ return s->getCanvas()->internal_private_accessTopLayerRenderTarget(); },
bsalomone63ffef2016-02-05 07:17:34 -0800862 [] (SkSurface* s){
863 SkBaseDevice* d =
864 s->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
865 return d->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700866 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
bsalomone63ffef2016-02-05 07:17:34 -0800867 return as_IB(i)->peekTexture(); },
868 };
869 for (auto grSurfaceGetter : grSurfaceGetters) {
870 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700871 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800872 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
873 }
874 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
875 static const int kWidth = 10;
876 static const int kHeight = 10;
877 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[kWidth * kHeight]);
878 memset(pixels.get(), 0xAB, sizeof(uint32_t) * kWidth * kHeight);
879
880 GrBackendObject textureObject =
881 context->getGpu()->createTestingOnlyBackendTexture(pixels.get(), kWidth, kHeight,
882 kRGBA_8888_GrPixelConfig);
883
884 GrBackendTextureDesc desc;
885 desc.fConfig = kRGBA_8888_GrPixelConfig;
886 desc.fWidth = kWidth;
887 desc.fHeight = kHeight;
888 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
889 desc.fTextureHandle = textureObject;
890
reede8f30622016-03-23 18:59:25 -0700891 auto surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800892 test_surface_clear(reporter, surface, grSurfaceGetter, 0xABABABAB);
893 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
894 }
895}
896#endif