blob: bd3090cc8e49f23d1f74d82cafefc8469614981c [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"
Mike Reed986480a2017-01-13 22:43:16 +000011#include "SkDevice.h"
bsalomon55812362015-06-10 08:49:28 -070012#include "SkImage_Base.h"
Matt Sarette11b6142016-11-28 18:28:07 -050013#include "SkOverdrawCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
Mike Reed267be7f2017-02-13 09:32:54 -050015#include "SkRegion.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000016#include "SkRRect.h"
17#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000018#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000019#include "Test.h"
20
21#if SK_SUPPORT_GPU
Brian Salomonbdecacf2018-02-02 20:32:49 -050022#include <vector>
kkinnunen179a8f52015-11-20 13:32:24 -080023#include "GrContext.h"
Robert Phillips2c862492017-01-18 10:08:39 -050024#include "GrContextPriv.h"
Brian Salomon3a2cc2c2018-02-03 00:25:12 +000025#include "GrGpu.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050026#include "GrGpuResourcePriv.h"
27#include "GrRenderTargetContext.h"
ericrkc4025182016-05-04 12:01:58 -070028#include "GrResourceProvider.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000029#include "GrTest.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050030#include "SkGpuDevice.h"
31#include "SkImage_Gpu.h"
32#include "SkSurface_Gpu.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000033#endif
34
Mike Reedd4746982018-02-07 16:05:29 -050035#include "sk_tool_utils.h"
36
kkinnunen179a8f52015-11-20 13:32:24 -080037#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070038
kkinnunen179a8f52015-11-20 13:32:24 -080039static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070040 SkASSERT(pixels == context);
41 sk_free(pixels);
42}
reede8f30622016-03-23 18:59:25 -070043static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
44 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070045 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000046 if (requestedInfo) {
47 *requestedInfo = info;
48 }
reede8f30622016-03-23 18:59:25 -070049 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000050}
reede8f30622016-03-23 18:59:25 -070051static sk_sp<SkSurface> create_direct_surface(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 }
57 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040058 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070059 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
60 release_direct_surface_storage,
61 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080062}
63#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070064static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
65 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080066 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
67 if (requestedInfo) {
68 *requestedInfo = info;
69 }
robertphillips7e922762016-07-26 11:38:17 -070070 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080071}
reede8f30622016-03-23 18:59:25 -070072static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
73 SkAlphaType at = kPremul_SkAlphaType,
74 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080075 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
76 if (requestedInfo) {
77 *requestedInfo = info;
78 }
robertphillips7e922762016-07-26 11:38:17 -070079 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080080}
81#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000082
kkinnunen179a8f52015-11-20 13:32:24 -080083DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080084 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070085 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
86 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080087
reedb2497c22014-12-31 12:31:43 -080088}
kkinnunen179a8f52015-11-20 13:32:24 -080089#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070090DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080091 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
92 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070093 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080094}
95#endif
reedb2497c22014-12-31 12:31:43 -080096
Brian Salomonbdecacf2018-02-02 20:32:49 -050097#if SK_SUPPORT_GPU
98DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
99 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
100 static constexpr int kSize = 10;
101
102 SkColorType colorType = static_cast<SkColorType>(ct);
103 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
104 bool can = ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType);
105 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, 1,
106 nullptr);
107 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
108 colorType, can, SkToBool(surf));
109
110 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
111 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
112 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
113 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
114 kTopLeft_GrSurfaceOrigin, 0, colorType, nullptr,
115 nullptr);
116 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
117 colorType, can, SkToBool(surf));
118
119 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
120 kTopLeft_GrSurfaceOrigin, 1,
121 colorType, nullptr, nullptr);
122 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
123 colorType, can, SkToBool(surf));
124
125 surf.reset();
126 ctxInfo.grContext()->flush();
127 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500128 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 }
130
131 static constexpr int kSampleCnt = 2;
132
133 can = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
134 surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, kSampleCnt,
135 nullptr);
136 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
137 colorType, can, SkToBool(surf));
138
139 backendTex = gpu->createTestingOnlyBackendTexture(nullptr, kSize, kSize, colorType, true,
140 GrMipMapped::kNo);
141 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
142 kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType,
143 nullptr, nullptr);
144 REPORTER_ASSERT(reporter, can == SkToBool(surf),
145 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
146 colorType);
147 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
148 if (surf) {
149 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
150 int storedCnt = rtc->numStencilSamples();
151 int allowedCnt = ctxInfo.grContext()->caps()->getSampleCount(
152 storedCnt, rtc->asSurfaceProxy()->config());
153 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
154 "Should store an allowed sample count (%d vs %d)", allowedCnt,
155 storedCnt);
156 }
157
158 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
159 kTopLeft_GrSurfaceOrigin, kSampleCnt,
160 colorType, nullptr, nullptr);
161 REPORTER_ASSERT(reporter, can == SkToBool(surf),
162 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
163 colorType);
164 if (surf) {
165 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
166 int storedCnt = rtc->numStencilSamples();
167 int allowedCnt = ctxInfo.grContext()->caps()->getSampleCount(
168 storedCnt, rtc->asSurfaceProxy()->config());
169 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
170 "Should store an allowed sample count (%d vs %d)", allowedCnt,
171 storedCnt);
172 }
173
174 surf.reset();
175 ctxInfo.grContext()->flush();
176 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500177 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500178 }
179 }
180}
181
182DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
183 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
184 static constexpr int kSize = 10;
185
186 SkColorType colorType = static_cast<SkColorType>(ct);
187 int max = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType);
188 if (!max) {
189 continue;
190 }
191 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
192 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
193 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
194
195 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
196 auto surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
197 kTopLeft_GrSurfaceOrigin, max,
198 colorType, nullptr, nullptr);
199 REPORTER_ASSERT(reporter, surf);
200 if (!surf) {
201 continue;
202 }
203 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
204 ->getDevice()
205 ->accessRenderTargetContext()
206 ->numStencilSamples();
207 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
208 }
209}
210#endif
211
kkinnunen179a8f52015-11-20 13:32:24 -0800212static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700213 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800214 const SkImageInfo& requestInfo,
215 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000216 const SkColor color = SK_ColorRED;
217 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800218 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000219
reed6ceeebd2016-03-09 14:26:26 -0800220 SkPixmap pmap;
221 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800222 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000223
reed6ceeebd2016-03-09 14:26:26 -0800224 SkPixmap pmap2;
225 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000226
kkinnunen179a8f52015-11-20 13:32:24 -0800227 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800228 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
229 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
230 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000231
reed6ceeebd2016-03-09 14:26:26 -0800232 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
233 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
234 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800235 } else {
236 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000237 }
238}
kkinnunen179a8f52015-11-20 13:32:24 -0800239DEF_TEST(SurfaceCanvasPeek, reporter) {
240 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
241 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700242 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800243 test_canvas_peek(reporter, surface, requestInfo, true);
244 }
245}
246#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700247DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800248 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
249 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700250 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800251 test_canvas_peek(reporter, surface, requestInfo, false);
252 }
253}
254#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000255
reede8f30622016-03-23 18:59:25 -0700256static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700257 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800258 REPORTER_ASSERT(reporter, surface);
259 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700260 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800261 REPORTER_ASSERT(reporter, image);
262 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700263 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700264 }
265 }
266}
kkinnunen179a8f52015-11-20 13:32:24 -0800267DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
268 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700269 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
270 auto surface(surface_func(at, nullptr));
271 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700272 }
273 }
274}
kkinnunen179a8f52015-11-20 13:32:24 -0800275#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700276DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800277 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700278 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
279 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
280 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
281 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800282 }
283 }
284}
285#endif
bsalomon74f681d2015-06-23 14:38:48 -0700286
kkinnunen179a8f52015-11-20 13:32:24 -0800287static GrBackendObject get_surface_backend_texture_handle(
288 SkSurface* s, SkSurface::BackendHandleAccess a) {
289 return s->getTextureHandle(a);
290}
291static GrBackendObject get_surface_backend_render_target_handle(
292 SkSurface* s, SkSurface::BackendHandleAccess a) {
293 GrBackendObject result;
294 if (!s->getRenderTargetHandle(&result, a)) {
295 return 0;
296 }
297 return result;
298}
299
300static void test_backend_handle_access_copy_on_write(
301 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
302 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700303 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700304 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700305
306 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700307 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700308
309 // If the access mode triggers CoW, then the backend objects should reflect it.
310 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
311}
kkinnunen179a8f52015-11-20 13:32:24 -0800312DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
313 const SkSurface::BackendHandleAccess accessModes[] = {
314 SkSurface::kFlushRead_BackendHandleAccess,
315 SkSurface::kFlushWrite_BackendHandleAccess,
316 SkSurface::kDiscardWrite_BackendHandleAccess,
317 };
318 for (auto& handle_access_func :
319 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
320 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700321 auto surface(create_surface());
322 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800323 handle_access_func);
324 }
325 }
326}
327#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700328DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800329 const SkSurface::BackendHandleAccess accessModes[] = {
330 SkSurface::kFlushRead_BackendHandleAccess,
331 SkSurface::kFlushWrite_BackendHandleAccess,
332 SkSurface::kDiscardWrite_BackendHandleAccess,
333 };
334 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
335 for (auto& handle_access_func :
336 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
337 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700338 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700339 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800340 handle_access_func);
341 }
342 }
343 }
344}
345#endif
fmalitae2639082015-08-06 07:04:51 -0700346
kkinnunen179a8f52015-11-20 13:32:24 -0800347#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800348
kkinnunen179a8f52015-11-20 13:32:24 -0800349static void test_backend_handle_unique_id(
350 skiatest::Reporter* reporter, SkSurface* surface,
351 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700352 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800353 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
354 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700355 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800356 // just read access should not affect the snapshot
357 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
358
359 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
360 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700361 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800362 // expect a new image, since we claimed we would write
363 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
364
365 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
366 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700367 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800368 // expect a new(er) image, since we claimed we would write
369 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
370 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
371}
372// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700373DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800374 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700375 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800376 for (auto& handle_access_func :
377 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700378 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700379 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800380 }
381 }
382 }
383}
384#endif
385
386// Verify that the right canvas commands trigger a copy on write.
387static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000388 SkCanvas* canvas = surface->getCanvas();
389
390 const SkRect testRect =
391 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
392 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000393 SkPath testPath;
394 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
395 SkIntToScalar(2), SkIntToScalar(1)));
396
397 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
398
399 SkRegion testRegion;
400 testRegion.setRect(testIRect);
401
402
403 const SkColor testColor = 0x01020304;
404 const SkPaint testPaint;
405 const SkPoint testPoints[3] = {
406 {SkIntToScalar(0), SkIntToScalar(0)},
407 {SkIntToScalar(2), SkIntToScalar(1)},
408 {SkIntToScalar(0), SkIntToScalar(2)}
409 };
410 const size_t testPointCount = 3;
411
412 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000413 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000414 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000415
416 SkRRect testRRect;
417 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
418
419 SkString testText("Hello World");
420 const SkPoint testPoints2[] = {
421 { SkIntToScalar(0), SkIntToScalar(1) },
422 { SkIntToScalar(1), SkIntToScalar(1) },
423 { SkIntToScalar(2), SkIntToScalar(1) },
424 { SkIntToScalar(3), SkIntToScalar(1) },
425 { SkIntToScalar(4), SkIntToScalar(1) },
426 { SkIntToScalar(5), SkIntToScalar(1) },
427 { SkIntToScalar(6), SkIntToScalar(1) },
428 { SkIntToScalar(7), SkIntToScalar(1) },
429 { SkIntToScalar(8), SkIntToScalar(1) },
430 { SkIntToScalar(9), SkIntToScalar(1) },
431 { SkIntToScalar(10), SkIntToScalar(1) },
432 };
433
434#define EXPECT_COPY_ON_WRITE(command) \
435 { \
reed9ce9d672016-03-17 10:51:11 -0700436 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
437 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000438 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700439 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
440 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000441 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
442 }
443
444 EXPECT_COPY_ON_WRITE(clear(testColor))
445 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
446 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
447 testPaint))
448 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
449 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
450 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
451 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
452 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700453 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700454 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Cary Clark2a475ea2017-04-28 15:35:12 -0400455 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, testPaint))
junov@chromium.org995beb62013-03-28 13:49:22 +0000456 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
457 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700458 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000459 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800460}
461DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700462 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800463}
464#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700465DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800466 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700467 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700468 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700469 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000470}
kkinnunen179a8f52015-11-20 13:32:24 -0800471#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000472
kkinnunen179a8f52015-11-20 13:32:24 -0800473static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
474 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000475 // This test succeeds by not triggering an assertion.
476 // The test verifies that the surface remains writable (usable) after
477 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000478 SkCanvas* canvas = surface->getCanvas();
479 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700480 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000481 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000482}
kkinnunen179a8f52015-11-20 13:32:24 -0800483DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700484 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800485}
486#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700487DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800488 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700489 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700490 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800491 }
492}
493#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000494
junov@chromium.orgb516a412013-05-01 22:49:59 +0000495#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800496static void test_crbug263329(skiatest::Reporter* reporter,
497 SkSurface* surface1,
498 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000499 // This is a regression test for crbug.com/263329
500 // Bug was caused by onCopyOnWrite releasing the old surface texture
501 // back to the scratch texture pool even though the texture is used
502 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000503 SkCanvas* canvas1 = surface1->getCanvas();
504 SkCanvas* canvas2 = surface2->getCanvas();
505 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700506 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000507 // Trigger copy on write, new backing is a scratch texture
508 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700509 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000510 // Trigger copy on write, old backing should not be returned to scratch
511 // pool because it is held by image2
512 canvas1->clear(3);
513
514 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700515 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000516 // Trigger copy on write on surface2. The new backing store should not
517 // be recycling a texture that is held by an existing image.
518 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700519 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400520 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000521 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400522 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
523 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
524 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
525 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
526 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000527}
egdanielab527a52016-06-28 08:07:26 -0700528DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800529 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700530 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
531 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700532 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800533 }
534}
535#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000536
kkinnunen179a8f52015-11-20 13:32:24 -0800537DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700538 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700539 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500540 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800541 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500542 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800543}
544#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700545DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800546 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700547 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700548 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500549
550 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
551 GrBackendObject textureHandle = image->getTextureHandle(false);
552 REPORTER_ASSERT(reporter, 0 != textureHandle);
kkinnunen179a8f52015-11-20 13:32:24 -0800553 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500554 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
555 REPORTER_ASSERT(reporter, textureHandle == image->getTextureHandle(false));
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
bsalomoneaaaf0b2015-01-23 08:08:04 -0800561
reede8f30622016-03-23 18:59:25 -0700562static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
563 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500564
565 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
566 ->asRenderTargetProxy();
567 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800568}
569
bsalomon5ec26ae2016-02-25 08:33:02 -0800570static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400571 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800572}
573
reed9ce9d672016-03-17 10:51:11 -0700574static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
575 return is_budgeted(image.get());
576}
577
egdanielab527a52016-06-28 08:07:26 -0700578DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800579 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400580 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
581 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
582 SkASSERT(surface);
583 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800584
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400585 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800586
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400587 // Initially the image shares a texture with the surface, and the
588 // the budgets should always match.
589 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
590 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800591
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400592 // Now trigger copy-on-write
593 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800594
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400595 // They don't share a texture anymore but the budgets should still match.
596 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
597 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800598 }
599}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000600#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000601
kkinnunen179a8f52015-11-20 13:32:24 -0800602static void test_no_canvas1(skiatest::Reporter* reporter,
603 SkSurface* surface,
604 SkSurface::ContentChangeMode mode) {
605 // Test passes by not asserting
606 surface->notifyContentWillChange(mode);
607 SkDEBUGCODE(surface->validate();)
608}
609static void test_no_canvas2(skiatest::Reporter* reporter,
610 SkSurface* surface,
611 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000612 // Verifies the robustness of SkSurface for handling use cases where calls
613 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700614 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
615 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800616 SkDEBUGCODE(image1->validate();)
617 SkDEBUGCODE(surface->validate();)
618 surface->notifyContentWillChange(mode);
619 SkDEBUGCODE(image1->validate();)
620 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700621 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
622 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800623 SkDEBUGCODE(image2->validate();)
624 SkDEBUGCODE(surface->validate();)
625 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000626}
kkinnunen179a8f52015-11-20 13:32:24 -0800627DEF_TEST(SurfaceNoCanvas, reporter) {
628 SkSurface::ContentChangeMode modes[] =
629 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
630 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
631 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700632 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800633 }
634 }
635}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000636#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700637DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800638 SkSurface::ContentChangeMode modes[] =
639 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
640 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
641 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
642 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700643 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700644 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700645 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000646 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000647 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000648}
kkinnunen179a8f52015-11-20 13:32:24 -0800649#endif
reed9cd016e2016-01-30 10:01:06 -0800650
651static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800652 SkPixmap surfacePM;
653 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800654
reed9ce9d672016-03-17 10:51:11 -0700655 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800656 SkPixmap pm;
657 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800658
reed6ceeebd2016-03-09 14:26:26 -0800659 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800660
661 // trigger a copy-on-write
662 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700663 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800664 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
665
reed6ceeebd2016-03-09 14:26:26 -0800666 SkPixmap pm2;
667 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
668 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800669}
670
671DEF_TEST(surface_rowbytes, reporter) {
672 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
673
reede8f30622016-03-23 18:59:25 -0700674 auto surf0(SkSurface::MakeRaster(info));
675 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800676
677 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700678 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
679 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800680
681 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700682 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800683 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400684 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800685 REPORTER_ASSERT(reporter, nullptr == s);
686}
bsalomone63ffef2016-02-05 07:17:34 -0800687
fmalita03912f12016-07-06 06:22:06 -0700688DEF_TEST(surface_raster_zeroinitialized, reporter) {
689 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
690 SkPixmap pixmap;
691 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
692
693 for (int i = 0; i < pixmap.info().width(); ++i) {
694 for (int j = 0; j < pixmap.info().height(); ++j) {
695 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
696 }
697 }
698}
699
bsalomone63ffef2016-02-05 07:17:34 -0800700#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700701static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500702 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500703 GrGpu* gpu = context->contextPriv().getGpu();
704
ericrkc4025182016-05-04 12:01:58 -0700705 const int kWidth = 10;
706 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400707 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700708 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000709
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500710 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500711 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000712
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500713 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500714 return nullptr;
715 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000716
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500717 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400718 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500719 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000720 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700721 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500722 gpu->deleteTestingOnlyBackendTexture(*outTexture);
ericrkc4025182016-05-04 12:01:58 -0700723 return nullptr;
724 }
ericrkc4025182016-05-04 12:01:58 -0700725 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(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500729 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500730 GrGpu* gpu = context->contextPriv().getGpu();
731
ericrkc4025182016-05-04 12:01:58 -0700732 const int kWidth = 10;
733 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400734 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700735 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000736
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500737 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500738 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000739
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500740 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
ericrkc4025182016-05-04 12:01:58 -0700741 return nullptr;
742 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500743
744 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Greg Danielfaa095e2017-12-19 13:15:02 -0500745 context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
746 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500747
748 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500749 gpu->deleteTestingOnlyBackendTexture(*outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500750 return nullptr;
751 }
ericrkc4025182016-05-04 12:01:58 -0700752 return surface;
753}
754
755static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500756 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700757 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800758 if (!surface) {
759 ERRORF(reporter, "Could not create GPU SkSurface.");
760 return;
761 }
762 int w = surface->width();
763 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400764 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700765 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800766
Robert Phillips2c862492017-01-18 10:08:39 -0500767 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
768 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800769 ERRORF(reporter, "Could access render target of GPU SkSurface.");
770 return;
771 }
bsalomon2fba8092016-02-05 13:47:06 -0800772 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500773
774 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
775 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800776 for (int y = 0; y < h; ++y) {
777 for (int x = 0; x < w; ++x) {
778 uint32_t pixel = pixels.get()[y * w + x];
779 if (pixel != expectedValue) {
780 SkString msg;
781 if (expectedValue) {
782 msg = "SkSurface should have left render target unmodified";
783 } else {
784 msg = "SkSurface should have cleared the render target";
785 }
786 ERRORF(reporter,
787 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
788 expectedValue, x, y);
789 return;
790 }
791 }
792 }
793}
794
bsalomon758586c2016-04-06 14:02:39 -0700795DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700796 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500797 GrGpu* gpu = context->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700798
Robert Phillips2c862492017-01-18 10:08:39 -0500799 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700800 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500801 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
802 },
803 [] (SkSurface* s){
804 sk_sp<SkImage> i(s->makeImageSnapshot());
805 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500806 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500807 GrContext* context = gpuImage->context();
808 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
809 gpuImage->refColorSpace());
810 }
bsalomone63ffef2016-02-05 07:17:34 -0800811 };
ericrkc4025182016-05-04 12:01:58 -0700812
Robert Phillips2c862492017-01-18 10:08:39 -0500813 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700814 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800815 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700816 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800817 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
818 }
819 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700820 const uint32_t kOrigColor = 0xABABABAB;
821 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
822 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500823 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500824 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700825 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
826 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500827 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700828 }
829 }
830}
bsalomone63ffef2016-02-05 07:17:34 -0800831
ericrkc4025182016-05-04 12:01:58 -0700832static void test_surface_draw_partially(
833 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
834 const int kW = surface->width();
835 const int kH = surface->height();
836 SkPaint paint;
837 const SkColor kRectColor = ~origColor | 0xFF000000;
838 paint.setColor(kRectColor);
839 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
840 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400841 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700842 sk_memset32(pixels.get(), ~origColor, kW * kH);
843 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
844 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
845 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
846 bool stop = false;
847 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
848 (origColor >> 0 & 0xFF),
849 (origColor >> 8 & 0xFF),
850 (origColor >> 16 & 0xFF));
851 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
852 (kRectColor >> 16 & 0xFF),
853 (kRectColor >> 8 & 0xFF),
854 (kRectColor >> 0 & 0xFF));
855 for (int y = 0; y < kH/2 && !stop; ++y) {
856 for (int x = 0; x < kW && !stop; ++x) {
857 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
858 if (rectColorPM != pixels[x + y * kW]) {
859 stop = true;
860 }
861 }
862 }
863 stop = false;
864 for (int y = kH/2; y < kH && !stop; ++y) {
865 for (int x = 0; x < kW && !stop; ++x) {
866 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
867 if (origColorPM != pixels[x + y * kW]) {
868 stop = true;
869 }
870 }
871 }
872}
bsalomone63ffef2016-02-05 07:17:34 -0800873
egdanielab527a52016-06-28 08:07:26 -0700874DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500875 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700876 if (!gpu) {
877 return;
878 }
879 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800880
ericrkc4025182016-05-04 12:01:58 -0700881 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
882 &create_gpu_surface_backend_texture_as_render_target}) {
883 // Validate that we can draw to the canvas and that the original texture color is
884 // preserved in pixels that aren't rendered to via the surface.
885 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500886 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500887 auto surface = surfaceFunc(ctxInfo.grContext(), 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700888 if (surface) {
889 test_surface_draw_partially(reporter, surface, kOrigColor);
890 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500891 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700892 }
893 }
894}
895
896
897DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500898 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700899 if (!gpu) {
900 return;
901 }
Eric Karl5c779752017-05-08 12:02:07 -0700902 if (gpu->caps()->avoidStencilBuffers()) {
903 return;
904 }
ericrkc4025182016-05-04 12:01:58 -0700905 static const uint32_t kOrigColor = 0xFFAABBCC;
906
Robert Phillips6be756b2018-01-16 15:07:54 -0500907 auto resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
908
ericrkc4025182016-05-04 12:01:58 -0700909 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
910 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500911 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500912 GrBackendTexture backendTex;
913 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700914
Brian Salomonbdecacf2018-02-02 20:32:49 -0500915 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500916 // Certain platforms don't support MSAA, skip these.
917 continue;
ericrkc4025182016-05-04 12:01:58 -0700918 }
919
920 // Validate that we can attach a stencil buffer to an SkSurface created by either of
921 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400922 GrRenderTarget* rt = surface->getCanvas()
923 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Robert Phillips6be756b2018-01-16 15:07:54 -0500924 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt));
Brian Salomon26102cb2018-03-09 09:33:19 -0500925 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700926 }
bsalomone63ffef2016-02-05 07:17:34 -0800927 }
928}
929#endif
brianosman0e22eb82016-08-30 07:07:59 -0700930
931static void test_surface_creation_and_snapshot_with_color_space(
932 skiatest::Reporter* reporter,
933 const char* prefix,
934 bool f16Support,
Brian Osman10fc6fd2018-03-02 11:01:10 -0500935 bool supports1010102,
brianosman0e22eb82016-08-30 07:07:59 -0700936 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
937
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500938 auto srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500939 const SkMatrix44* srgbMatrix = srgbColorSpace->toXYZD50();
raftias94888332016-10-18 10:02:51 -0700940 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400941 SkColorSpaceTransferFn oddGamma;
942 oddGamma.fA = 1.0f;
943 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
944 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400945 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500946 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700947
948 const struct {
949 SkColorType fColorType;
950 sk_sp<SkColorSpace> fColorSpace;
951 bool fShouldWork;
952 const char* fDescription;
953 } testConfigs[] = {
954 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
955 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
956 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700957 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Stan Iliev4ed9ae42017-07-25 11:59:12 -0400958 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700959 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
960 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700961 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
962 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
963 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
Brian Osman10fc6fd2018-03-02 11:01:10 -0500964 { kRGBA_1010102_SkColorType, nullptr, true, "1010102-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700965 };
966
967 for (auto& testConfig : testConfigs) {
968 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
969 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
970 testConfig.fColorSpace);
971
972 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
973 // any surface of that type:
974 bool shouldWork = testConfig.fShouldWork &&
Brian Osman10fc6fd2018-03-02 11:01:10 -0500975 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType) &&
976 (supports1010102 || kRGBA_1010102_SkColorType != testConfig.fColorType);
brianosman0e22eb82016-08-30 07:07:59 -0700977
978 auto surface(surfaceMaker(info));
Brian Salomon1c80e992018-01-29 09:50:47 -0500979 REPORTER_ASSERT(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700980
981 if (shouldWork && surface) {
982 sk_sp<SkImage> image(surface->makeImageSnapshot());
Brian Salomon1c80e992018-01-29 09:50:47 -0500983 REPORTER_ASSERT(reporter, image, testConfig.fDescription);
brianosman0e22eb82016-08-30 07:07:59 -0700984 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
Brian Salomon1c80e992018-01-29 09:50:47 -0500985 REPORTER_ASSERT(reporter, imageColorSpace == testConfig.fColorSpace.get(),
986 fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700987 }
988 }
989}
990
991DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
992 auto surfaceMaker = [](const SkImageInfo& info) {
993 return SkSurface::MakeRaster(info);
994 };
995
Brian Osman10fc6fd2018-03-02 11:01:10 -0500996 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, true,
997 surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -0700998}
999
1000#if SK_SUPPORT_GPU
1001DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
1002 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001003
Brian Salomonbdecacf2018-02-02 20:32:49 -05001004 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig);
Brian Osman10fc6fd2018-03-02 11:01:10 -05001005 bool supports1010102 = context->caps()->isConfigRenderable(kRGBA_1010102_GrPixelConfig);
brianosman0e22eb82016-08-30 07:07:59 -07001006 auto surfaceMaker = [context](const SkImageInfo& info) {
1007 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
1008 };
1009
Brian Osman10fc6fd2018-03-02 11:01:10 -05001010 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support,
1011 supports1010102, surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -07001012
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001013 std::vector<GrBackendTexture> backendTextures;
1014 auto wrappedSurfaceMaker = [ context, &backendTextures ](const SkImageInfo& info) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001015 GrGpu* gpu = context->contextPriv().getGpu();
1016
Greg Daniel7ef28f32017-04-20 16:41:55 +00001017 static const int kSize = 10;
1018 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *context->caps());
Greg Daniel0a7aa142018-02-21 13:02:32 -05001019 SkASSERT(kUnknown_GrPixelConfig != config);
brianosman0e22eb82016-08-30 07:07:59 -07001020
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001021 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001022 nullptr, kSize, kSize, config, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001023
Greg Daniel5366e592018-01-10 09:57:53 -05001024 if (!backendTex.isValid() ||
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001025 !gpu->isTestingOnlyBackendTexture(backendTex)) {
brianosman0e22eb82016-08-30 07:07:59 -07001026 return sk_sp<SkSurface>(nullptr);
1027 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001028 backendTextures.push_back(backendTex);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001029
1030 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -04001031 kTopLeft_GrSurfaceOrigin, 0,
Greg Danielfaa095e2017-12-19 13:15:02 -05001032 info.colorType(),
Greg Daniel7ef28f32017-04-20 16:41:55 +00001033 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -07001034 };
1035
1036 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
Brian Osman10fc6fd2018-03-02 11:01:10 -05001037 supports1010102, wrappedSurfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -07001038
Robert Phillips6cdc22c2017-05-11 16:29:14 -04001039 context->flush();
1040
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001041 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001042 for (auto backendTex : backendTextures) {
Brian Salomon26102cb2018-03-09 09:33:19 -05001043 gpu->deleteTestingOnlyBackendTexture(backendTex);
brianosman0e22eb82016-08-30 07:07:59 -07001044 }
1045}
1046#endif
Matt Sarett22886c42016-11-22 11:31:41 -05001047
1048static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001049 SkOverdrawCanvas canvas(surface->getCanvas());
1050 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001051 sk_sp<SkImage> image = surface->makeImageSnapshot();
1052
1053 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001054 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001055 for (int y = 0; y < 10; y++) {
1056 for (int x = 0; x < 10; x++) {
1057 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1058 }
1059 }
1060}
1061
1062DEF_TEST(OverdrawSurface_Raster, r) {
1063 sk_sp<SkSurface> surface = create_surface();
1064 test_overdraw_surface(r, surface.get());
1065}
1066
1067#if SK_SUPPORT_GPU
1068DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1069 GrContext* context = ctxInfo.grContext();
1070 sk_sp<SkSurface> surface = create_gpu_surface(context);
1071 test_overdraw_surface(r, surface.get());
1072}
1073#endif
Mike Reed44d04bd2017-06-28 19:57:21 -04001074
1075DEF_TEST(Surface_null, r) {
1076 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1077
1078 const int w = 37;
1079 const int h = 1000;
1080 auto surf = SkSurface::MakeNull(w, h);
1081 auto canvas = surf->getCanvas();
1082
1083 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1084 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1085}
Mike Reedd4746982018-02-07 16:05:29 -05001086
1087// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1088// (so the snapshot can succeed)
1089DEF_TEST(surface_image_unity, reporter) {
1090 auto do_test = [reporter](const SkImageInfo& info) {
1091 size_t rowBytes = info.minRowBytes();
1092 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1093 if (surf) {
1094 auto img = surf->makeImageSnapshot();
1095 if (!img && false) { // change to true to document the differences
1096 SkDebugf("image failed: [%08X %08X] %14s %s\n",
1097 info.width(), info.height(),
1098 sk_tool_utils::colortype_name(info.colorType()),
1099 sk_tool_utils::alphatype_name(info.alphaType()));
1100 return;
1101 }
1102 REPORTER_ASSERT(reporter, img != nullptr);
1103
1104 char dummyPixel = 0; // just need a valid address (not a valid size)
1105 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1106 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1107 REPORTER_ASSERT(reporter, img != nullptr);
1108 }
1109 };
1110
Mike Klein955b3d52018-02-20 11:55:48 -05001111 const int32_t sizes[] = { 0, 1, 1 << 15, 1 << 16, 1 << 18, 1 << 28, 1 << 29, 1 << 30, -1 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001112 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001113 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001114 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001115 SkAlphaType at = static_cast<SkAlphaType>(ati);
1116 for (int32_t size : sizes) {
Mike Klein955b3d52018-02-20 11:55:48 -05001117 // Large allocations tend to make the 32-bit bots run out of virtual address space.
1118 if (sizeof(size_t) == 4 && size > (1<<20)) {
1119 continue;
1120 }
Mike Reedd4746982018-02-07 16:05:29 -05001121 do_test(SkImageInfo::Make(1, size, ct, at));
1122 do_test(SkImageInfo::Make(size, 1, ct, at));
1123 }
1124 }
1125 }
1126}