blob: 965298a81620360a56741e366a2bebeb42b8929d [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(
Greg Daniel57bf4a32018-04-19 10:28:37 -0400112 nullptr, kSize, kSize, colorType, nullptr, true, GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500113 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
Greg Daniel57bf4a32018-04-19 10:28:37 -0400139 backendTex = gpu->createTestingOnlyBackendTexture(nullptr, kSize, kSize, colorType, nullptr,
140 true, GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 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();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400151 int allowedCnt = ctxInfo.grContext()->contextPriv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500152 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();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400167 int allowedCnt = ctxInfo.grContext()->contextPriv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500168 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(
Greg Daniel57bf4a32018-04-19 10:28:37 -0400193 nullptr, kSize, kSize, colorType, nullptr, true, GrMipMapped::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400194 if (!backendTex.isValid()) {
195 continue;
196 }
197 SkScopeExit freeTex([&backendTex, gpu] {gpu->deleteTestingOnlyBackendTexture(backendTex);});
Brian Salomonbdecacf2018-02-02 20:32:49 -0500198 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
199 auto surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
200 kTopLeft_GrSurfaceOrigin, max,
201 colorType, nullptr, nullptr);
202 REPORTER_ASSERT(reporter, surf);
203 if (!surf) {
204 continue;
205 }
206 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
207 ->getDevice()
208 ->accessRenderTargetContext()
209 ->numStencilSamples();
210 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
211 }
212}
213#endif
214
kkinnunen179a8f52015-11-20 13:32:24 -0800215static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700216 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800217 const SkImageInfo& requestInfo,
218 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000219 const SkColor color = SK_ColorRED;
220 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800221 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000222
reed6ceeebd2016-03-09 14:26:26 -0800223 SkPixmap pmap;
224 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800225 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000226
reed6ceeebd2016-03-09 14:26:26 -0800227 SkPixmap pmap2;
228 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000229
kkinnunen179a8f52015-11-20 13:32:24 -0800230 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800231 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
232 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
233 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000234
reed6ceeebd2016-03-09 14:26:26 -0800235 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
236 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
237 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800238 } else {
239 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000240 }
241}
kkinnunen179a8f52015-11-20 13:32:24 -0800242DEF_TEST(SurfaceCanvasPeek, reporter) {
243 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
244 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700245 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800246 test_canvas_peek(reporter, surface, requestInfo, true);
247 }
248}
249#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700250DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800251 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
252 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700253 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800254 test_canvas_peek(reporter, surface, requestInfo, false);
255 }
256}
257#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000258
reede8f30622016-03-23 18:59:25 -0700259static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700260 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800261 REPORTER_ASSERT(reporter, surface);
262 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700263 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800264 REPORTER_ASSERT(reporter, image);
265 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700266 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700267 }
268 }
269}
kkinnunen179a8f52015-11-20 13:32:24 -0800270DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
271 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700272 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
273 auto surface(surface_func(at, nullptr));
274 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700275 }
276 }
277}
kkinnunen179a8f52015-11-20 13:32:24 -0800278#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700279DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800280 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700281 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
282 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
283 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
284 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800285 }
286 }
287}
bsalomon74f681d2015-06-23 14:38:48 -0700288
Robert Phillips8caf85f2018-04-05 09:30:38 -0400289static void test_backend_texture_access_copy_on_write(
290 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
291 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700292 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700293
Robert Phillips8caf85f2018-04-05 09:30:38 -0400294 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700295 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700296
297 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400298 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700299}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400300
301static void test_backend_rendertarget_access_copy_on_write(
302 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
303 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
304 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
305
306 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
307 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
308
309 // If the access mode triggers CoW, then the backend objects should reflect it.
310 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
311 (snap1 == snap2));
312}
313
314DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800315 const SkSurface::BackendHandleAccess accessModes[] = {
316 SkSurface::kFlushRead_BackendHandleAccess,
317 SkSurface::kFlushWrite_BackendHandleAccess,
318 SkSurface::kDiscardWrite_BackendHandleAccess,
319 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400320
kkinnunen179a8f52015-11-20 13:32:24 -0800321 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400322 for (auto& accessMode : accessModes) {
323 {
bsalomon8b7451a2016-05-11 06:33:06 -0700324 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400325 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
326 }
327 {
328 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
329 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800330 }
331 }
332 }
333}
334#endif
fmalitae2639082015-08-06 07:04:51 -0700335
kkinnunen179a8f52015-11-20 13:32:24 -0800336#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800337
Robert Phillips8caf85f2018-04-05 09:30:38 -0400338template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
339static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700340 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400341
342 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
343 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700344 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800345 // just read access should not affect the snapshot
346 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
347
Robert Phillips8caf85f2018-04-05 09:30:38 -0400348 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
349 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800351 // expect a new image, since we claimed we would write
352 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
353
Robert Phillips8caf85f2018-04-05 09:30:38 -0400354 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
355 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800357 // expect a new(er) image, since we claimed we would write
358 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
359 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
360}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400361
kkinnunen179a8f52015-11-20 13:32:24 -0800362// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700363DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800364 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400365 {
366 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
367 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
368 surface.get());
369 }
370 {
371 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
372 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
373 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800374 }
375 }
376}
377#endif
378
379// Verify that the right canvas commands trigger a copy on write.
380static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000381 SkCanvas* canvas = surface->getCanvas();
382
383 const SkRect testRect =
384 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
385 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000386 SkPath testPath;
387 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
388 SkIntToScalar(2), SkIntToScalar(1)));
389
390 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
391
392 SkRegion testRegion;
393 testRegion.setRect(testIRect);
394
395
396 const SkColor testColor = 0x01020304;
397 const SkPaint testPaint;
398 const SkPoint testPoints[3] = {
399 {SkIntToScalar(0), SkIntToScalar(0)},
400 {SkIntToScalar(2), SkIntToScalar(1)},
401 {SkIntToScalar(0), SkIntToScalar(2)}
402 };
403 const size_t testPointCount = 3;
404
405 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000406 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000407 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000408
409 SkRRect testRRect;
410 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
411
412 SkString testText("Hello World");
413 const SkPoint testPoints2[] = {
414 { SkIntToScalar(0), SkIntToScalar(1) },
415 { SkIntToScalar(1), SkIntToScalar(1) },
416 { SkIntToScalar(2), SkIntToScalar(1) },
417 { SkIntToScalar(3), SkIntToScalar(1) },
418 { SkIntToScalar(4), SkIntToScalar(1) },
419 { SkIntToScalar(5), SkIntToScalar(1) },
420 { SkIntToScalar(6), SkIntToScalar(1) },
421 { SkIntToScalar(7), SkIntToScalar(1) },
422 { SkIntToScalar(8), SkIntToScalar(1) },
423 { SkIntToScalar(9), SkIntToScalar(1) },
424 { SkIntToScalar(10), SkIntToScalar(1) },
425 };
426
427#define EXPECT_COPY_ON_WRITE(command) \
428 { \
reed9ce9d672016-03-17 10:51:11 -0700429 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
430 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000431 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700432 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
433 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000434 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
435 }
436
437 EXPECT_COPY_ON_WRITE(clear(testColor))
438 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
439 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
440 testPaint))
441 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
442 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
443 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
444 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
445 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700446 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700447 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Cary Clark2a475ea2017-04-28 15:35:12 -0400448 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, testPaint))
junov@chromium.org995beb62013-03-28 13:49:22 +0000449 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
450 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700451 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000452 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800453}
454DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700455 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800456}
457#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700458DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800459 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700460 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700461 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700462 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000463}
kkinnunen179a8f52015-11-20 13:32:24 -0800464#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000465
kkinnunen179a8f52015-11-20 13:32:24 -0800466static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
467 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000468 // This test succeeds by not triggering an assertion.
469 // The test verifies that the surface remains writable (usable) after
470 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000471 SkCanvas* canvas = surface->getCanvas();
472 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700473 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000474 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000475}
kkinnunen179a8f52015-11-20 13:32:24 -0800476DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700477 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800478}
479#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700480DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800481 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700482 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700483 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800484 }
485}
486#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000487
junov@chromium.orgb516a412013-05-01 22:49:59 +0000488#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800489static void test_crbug263329(skiatest::Reporter* reporter,
490 SkSurface* surface1,
491 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000492 // This is a regression test for crbug.com/263329
493 // Bug was caused by onCopyOnWrite releasing the old surface texture
494 // back to the scratch texture pool even though the texture is used
495 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000496 SkCanvas* canvas1 = surface1->getCanvas();
497 SkCanvas* canvas2 = surface2->getCanvas();
498 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700499 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000500 // Trigger copy on write, new backing is a scratch texture
501 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700502 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000503 // Trigger copy on write, old backing should not be returned to scratch
504 // pool because it is held by image2
505 canvas1->clear(3);
506
507 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700508 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000509 // Trigger copy on write on surface2. The new backing store should not
510 // be recycling a texture that is held by an existing image.
511 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700512 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400513 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000514 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400515 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
516 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
517 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
518 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
519 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000520}
egdanielab527a52016-06-28 08:07:26 -0700521DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800522 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700523 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
524 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700525 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800526 }
527}
528#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000529
kkinnunen179a8f52015-11-20 13:32:24 -0800530DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700531 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700532 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500533 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800534 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500535 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800536}
537#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700538DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800539 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700540 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700541 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500542
543 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400544 GrBackendTexture backendTex = image->getBackendTexture(false);
545 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800546 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500547 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400548 GrBackendTexture backendTex2 = image->getBackendTexture(false);
549 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000550 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000551}
kkinnunen179a8f52015-11-20 13:32:24 -0800552#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800553
kkinnunen179a8f52015-11-20 13:32:24 -0800554#if SK_SUPPORT_GPU
bsalomoneaaaf0b2015-01-23 08:08:04 -0800555
reede8f30622016-03-23 18:59:25 -0700556static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
557 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500558
559 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
560 ->asRenderTargetProxy();
561 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800562}
563
bsalomon5ec26ae2016-02-25 08:33:02 -0800564static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400565 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800566}
567
reed9ce9d672016-03-17 10:51:11 -0700568static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
569 return is_budgeted(image.get());
570}
571
egdanielab527a52016-06-28 08:07:26 -0700572DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800573 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400574 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
575 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
576 SkASSERT(surface);
577 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800578
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400579 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800580
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400581 // Initially the image shares a texture with the surface, and the
582 // the budgets should always match.
583 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
584 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800585
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400586 // Now trigger copy-on-write
587 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800588
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400589 // They don't share a texture anymore but the budgets should still match.
590 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
591 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800592 }
593}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000594#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000595
kkinnunen179a8f52015-11-20 13:32:24 -0800596static void test_no_canvas1(skiatest::Reporter* reporter,
597 SkSurface* surface,
598 SkSurface::ContentChangeMode mode) {
599 // Test passes by not asserting
600 surface->notifyContentWillChange(mode);
601 SkDEBUGCODE(surface->validate();)
602}
603static void test_no_canvas2(skiatest::Reporter* reporter,
604 SkSurface* surface,
605 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000606 // Verifies the robustness of SkSurface for handling use cases where calls
607 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700608 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
609 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800610 SkDEBUGCODE(image1->validate();)
611 SkDEBUGCODE(surface->validate();)
612 surface->notifyContentWillChange(mode);
613 SkDEBUGCODE(image1->validate();)
614 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700615 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
616 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800617 SkDEBUGCODE(image2->validate();)
618 SkDEBUGCODE(surface->validate();)
619 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000620}
kkinnunen179a8f52015-11-20 13:32:24 -0800621DEF_TEST(SurfaceNoCanvas, reporter) {
622 SkSurface::ContentChangeMode modes[] =
623 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
624 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
625 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700626 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800627 }
628 }
629}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000630#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700631DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800632 SkSurface::ContentChangeMode modes[] =
633 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
634 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
635 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
636 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700637 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700638 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700639 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000640 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000641 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000642}
kkinnunen179a8f52015-11-20 13:32:24 -0800643#endif
reed9cd016e2016-01-30 10:01:06 -0800644
645static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800646 SkPixmap surfacePM;
647 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800648
reed9ce9d672016-03-17 10:51:11 -0700649 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800650 SkPixmap pm;
651 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800652
reed6ceeebd2016-03-09 14:26:26 -0800653 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800654
655 // trigger a copy-on-write
656 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700657 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800658 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
659
reed6ceeebd2016-03-09 14:26:26 -0800660 SkPixmap pm2;
661 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
662 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800663}
664
665DEF_TEST(surface_rowbytes, reporter) {
666 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
667
reede8f30622016-03-23 18:59:25 -0700668 auto surf0(SkSurface::MakeRaster(info));
669 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800670
671 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700672 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
673 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800674
675 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700676 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800677 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400678 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800679 REPORTER_ASSERT(reporter, nullptr == s);
680}
bsalomone63ffef2016-02-05 07:17:34 -0800681
fmalita03912f12016-07-06 06:22:06 -0700682DEF_TEST(surface_raster_zeroinitialized, reporter) {
683 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
684 SkPixmap pixmap;
685 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
686
687 for (int i = 0; i < pixmap.info().width(); ++i) {
688 for (int j = 0; j < pixmap.info().height(); ++j) {
689 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
690 }
691 }
692}
693
bsalomone63ffef2016-02-05 07:17:34 -0800694#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700695static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500696 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500697 GrGpu* gpu = context->contextPriv().getGpu();
698
ericrkc4025182016-05-04 12:01:58 -0700699 const int kWidth = 10;
700 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400701 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700702 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000703
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500704 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500705 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000706
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500707 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500708 return nullptr;
709 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000710
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500711 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400712 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500713 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000714 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700715 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500716 gpu->deleteTestingOnlyBackendTexture(*outTexture);
ericrkc4025182016-05-04 12:01:58 -0700717 return nullptr;
718 }
ericrkc4025182016-05-04 12:01:58 -0700719 return surface;
720}
bsalomone63ffef2016-02-05 07:17:34 -0800721
ericrkc4025182016-05-04 12:01:58 -0700722static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500723 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500724 GrGpu* gpu = context->contextPriv().getGpu();
725
ericrkc4025182016-05-04 12:01:58 -0700726 const int kWidth = 10;
727 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400728 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700729 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000730
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500731 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500732 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000733
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500734 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
ericrkc4025182016-05-04 12:01:58 -0700735 return nullptr;
736 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500737
738 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Greg Danielfaa095e2017-12-19 13:15:02 -0500739 context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
740 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500741
742 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500743 gpu->deleteTestingOnlyBackendTexture(*outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500744 return nullptr;
745 }
ericrkc4025182016-05-04 12:01:58 -0700746 return surface;
747}
748
749static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500750 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700751 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800752 if (!surface) {
753 ERRORF(reporter, "Could not create GPU SkSurface.");
754 return;
755 }
756 int w = surface->width();
757 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400758 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700759 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800760
Robert Phillips2c862492017-01-18 10:08:39 -0500761 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
762 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800763 ERRORF(reporter, "Could access render target of GPU SkSurface.");
764 return;
765 }
bsalomon2fba8092016-02-05 13:47:06 -0800766 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500767
768 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
769 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800770 for (int y = 0; y < h; ++y) {
771 for (int x = 0; x < w; ++x) {
772 uint32_t pixel = pixels.get()[y * w + x];
773 if (pixel != expectedValue) {
774 SkString msg;
775 if (expectedValue) {
776 msg = "SkSurface should have left render target unmodified";
777 } else {
778 msg = "SkSurface should have cleared the render target";
779 }
780 ERRORF(reporter,
781 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
782 expectedValue, x, y);
783 return;
784 }
785 }
786 }
787}
788
bsalomon758586c2016-04-06 14:02:39 -0700789DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700790 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500791 GrGpu* gpu = context->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700792
Robert Phillips2c862492017-01-18 10:08:39 -0500793 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700794 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500795 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
796 },
797 [] (SkSurface* s){
798 sk_sp<SkImage> i(s->makeImageSnapshot());
799 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500800 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500801 GrContext* context = gpuImage->context();
802 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
803 gpuImage->refColorSpace());
804 }
bsalomone63ffef2016-02-05 07:17:34 -0800805 };
ericrkc4025182016-05-04 12:01:58 -0700806
Robert Phillips2c862492017-01-18 10:08:39 -0500807 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700808 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800809 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700810 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800811 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
812 }
813 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700814 const uint32_t kOrigColor = 0xABABABAB;
815 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
816 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500817 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500818 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700819 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
820 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500821 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700822 }
823 }
824}
bsalomone63ffef2016-02-05 07:17:34 -0800825
ericrkc4025182016-05-04 12:01:58 -0700826static void test_surface_draw_partially(
827 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
828 const int kW = surface->width();
829 const int kH = surface->height();
830 SkPaint paint;
831 const SkColor kRectColor = ~origColor | 0xFF000000;
832 paint.setColor(kRectColor);
833 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
834 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400835 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700836 sk_memset32(pixels.get(), ~origColor, kW * kH);
837 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
838 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
839 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
840 bool stop = false;
841 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
842 (origColor >> 0 & 0xFF),
843 (origColor >> 8 & 0xFF),
844 (origColor >> 16 & 0xFF));
845 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
846 (kRectColor >> 16 & 0xFF),
847 (kRectColor >> 8 & 0xFF),
848 (kRectColor >> 0 & 0xFF));
849 for (int y = 0; y < kH/2 && !stop; ++y) {
850 for (int x = 0; x < kW && !stop; ++x) {
851 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
852 if (rectColorPM != pixels[x + y * kW]) {
853 stop = true;
854 }
855 }
856 }
857 stop = false;
858 for (int y = kH/2; y < kH && !stop; ++y) {
859 for (int x = 0; x < kW && !stop; ++x) {
860 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
861 if (origColorPM != pixels[x + y * kW]) {
862 stop = true;
863 }
864 }
865 }
866}
bsalomone63ffef2016-02-05 07:17:34 -0800867
egdanielab527a52016-06-28 08:07:26 -0700868DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500869 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700870 if (!gpu) {
871 return;
872 }
873 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800874
ericrkc4025182016-05-04 12:01:58 -0700875 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
876 &create_gpu_surface_backend_texture_as_render_target}) {
877 // Validate that we can draw to the canvas and that the original texture color is
878 // preserved in pixels that aren't rendered to via the surface.
879 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500880 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500881 auto surface = surfaceFunc(ctxInfo.grContext(), 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700882 if (surface) {
883 test_surface_draw_partially(reporter, surface, kOrigColor);
884 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500885 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700886 }
887 }
888}
889
890
891DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500892 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700893 if (!gpu) {
894 return;
895 }
Eric Karl5c779752017-05-08 12:02:07 -0700896 if (gpu->caps()->avoidStencilBuffers()) {
897 return;
898 }
ericrkc4025182016-05-04 12:01:58 -0700899 static const uint32_t kOrigColor = 0xFFAABBCC;
900
Robert Phillips6be756b2018-01-16 15:07:54 -0500901 auto resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
902
ericrkc4025182016-05-04 12:01:58 -0700903 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
904 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500905 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500906 GrBackendTexture backendTex;
907 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700908
Brian Salomonbdecacf2018-02-02 20:32:49 -0500909 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500910 // Certain platforms don't support MSAA, skip these.
911 continue;
ericrkc4025182016-05-04 12:01:58 -0700912 }
913
914 // Validate that we can attach a stencil buffer to an SkSurface created by either of
915 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400916 GrRenderTarget* rt = surface->getCanvas()
917 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Robert Phillips6be756b2018-01-16 15:07:54 -0500918 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt));
Brian Salomon26102cb2018-03-09 09:33:19 -0500919 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700920 }
bsalomone63ffef2016-02-05 07:17:34 -0800921 }
922}
923#endif
brianosman0e22eb82016-08-30 07:07:59 -0700924
925static void test_surface_creation_and_snapshot_with_color_space(
926 skiatest::Reporter* reporter,
927 const char* prefix,
928 bool f16Support,
Brian Osman10fc6fd2018-03-02 11:01:10 -0500929 bool supports1010102,
brianosman0e22eb82016-08-30 07:07:59 -0700930 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
931
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500932 auto srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500933 const SkMatrix44* srgbMatrix = srgbColorSpace->toXYZD50();
raftias94888332016-10-18 10:02:51 -0700934 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400935 SkColorSpaceTransferFn oddGamma;
936 oddGamma.fA = 1.0f;
937 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
938 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400939 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500940 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700941
942 const struct {
943 SkColorType fColorType;
944 sk_sp<SkColorSpace> fColorSpace;
945 bool fShouldWork;
946 const char* fDescription;
947 } testConfigs[] = {
948 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
949 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
950 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700951 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Stan Iliev4ed9ae42017-07-25 11:59:12 -0400952 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700953 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
Mike Kleince4cf722018-05-10 11:29:15 -0400954 { kRGBA_F16_SkColorType, srgbColorSpace, true, "F16-srgb" },
955 { kRGBA_F16_SkColorType, oddColorSpace, true, "F16-odd" },
brianosman0e22eb82016-08-30 07:07:59 -0700956 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
957 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
Brian Osman10fc6fd2018-03-02 11:01:10 -0500958 { kRGBA_1010102_SkColorType, nullptr, true, "1010102-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700959 };
960
961 for (auto& testConfig : testConfigs) {
962 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
963 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
964 testConfig.fColorSpace);
965
966 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
967 // any surface of that type:
968 bool shouldWork = testConfig.fShouldWork &&
Brian Osman10fc6fd2018-03-02 11:01:10 -0500969 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType) &&
970 (supports1010102 || kRGBA_1010102_SkColorType != testConfig.fColorType);
brianosman0e22eb82016-08-30 07:07:59 -0700971
972 auto surface(surfaceMaker(info));
Brian Salomon1c80e992018-01-29 09:50:47 -0500973 REPORTER_ASSERT(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700974
975 if (shouldWork && surface) {
976 sk_sp<SkImage> image(surface->makeImageSnapshot());
Brian Salomon1c80e992018-01-29 09:50:47 -0500977 REPORTER_ASSERT(reporter, image, testConfig.fDescription);
brianosman0e22eb82016-08-30 07:07:59 -0700978 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
Brian Salomon1c80e992018-01-29 09:50:47 -0500979 REPORTER_ASSERT(reporter, imageColorSpace == testConfig.fColorSpace.get(),
980 fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700981 }
982 }
983}
984
985DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
986 auto surfaceMaker = [](const SkImageInfo& info) {
987 return SkSurface::MakeRaster(info);
988 };
989
Brian Osman10fc6fd2018-03-02 11:01:10 -0500990 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, true,
991 surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -0700992}
993
994#if SK_SUPPORT_GPU
995DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400996 auto context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500997
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400998 bool f16Support = context->contextPriv().caps()->isConfigRenderable(kRGBA_half_GrPixelConfig);
999 bool supports1010102 =
1000 context->contextPriv().caps()->isConfigRenderable(kRGBA_1010102_GrPixelConfig);
brianosman0e22eb82016-08-30 07:07:59 -07001001 auto surfaceMaker = [context](const SkImageInfo& info) {
1002 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
1003 };
1004
Brian Osman10fc6fd2018-03-02 11:01:10 -05001005 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support,
1006 supports1010102, surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -07001007
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001008 std::vector<GrBackendTexture> backendTextures;
1009 auto wrappedSurfaceMaker = [ context, &backendTextures ](const SkImageInfo& info) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001010 GrGpu* gpu = context->contextPriv().getGpu();
1011
Greg Daniel7ef28f32017-04-20 16:41:55 +00001012 static const int kSize = 10;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001013 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *context->contextPriv().caps());
Greg Daniel0a7aa142018-02-21 13:02:32 -05001014 SkASSERT(kUnknown_GrPixelConfig != config);
brianosman0e22eb82016-08-30 07:07:59 -07001015
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001016 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001017 nullptr, kSize, kSize, config, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001018
Greg Daniel5366e592018-01-10 09:57:53 -05001019 if (!backendTex.isValid() ||
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001020 !gpu->isTestingOnlyBackendTexture(backendTex)) {
brianosman0e22eb82016-08-30 07:07:59 -07001021 return sk_sp<SkSurface>(nullptr);
1022 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001023 backendTextures.push_back(backendTex);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001024
1025 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -04001026 kTopLeft_GrSurfaceOrigin, 0,
Greg Danielfaa095e2017-12-19 13:15:02 -05001027 info.colorType(),
Greg Daniel7ef28f32017-04-20 16:41:55 +00001028 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -07001029 };
1030
1031 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
Brian Osman10fc6fd2018-03-02 11:01:10 -05001032 supports1010102, wrappedSurfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -07001033
Robert Phillips6cdc22c2017-05-11 16:29:14 -04001034 context->flush();
1035
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001036 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips2fbbd032018-04-10 10:26:44 -04001037 gpu->testingOnly_flushGpuAndSync();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001038 for (auto backendTex : backendTextures) {
Brian Salomon26102cb2018-03-09 09:33:19 -05001039 gpu->deleteTestingOnlyBackendTexture(backendTex);
brianosman0e22eb82016-08-30 07:07:59 -07001040 }
1041}
1042#endif
Matt Sarett22886c42016-11-22 11:31:41 -05001043
1044static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001045 SkOverdrawCanvas canvas(surface->getCanvas());
1046 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001047 sk_sp<SkImage> image = surface->makeImageSnapshot();
1048
1049 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001050 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001051 for (int y = 0; y < 10; y++) {
1052 for (int x = 0; x < 10; x++) {
1053 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1054 }
1055 }
1056}
1057
1058DEF_TEST(OverdrawSurface_Raster, r) {
1059 sk_sp<SkSurface> surface = create_surface();
1060 test_overdraw_surface(r, surface.get());
1061}
1062
1063#if SK_SUPPORT_GPU
1064DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1065 GrContext* context = ctxInfo.grContext();
1066 sk_sp<SkSurface> surface = create_gpu_surface(context);
1067 test_overdraw_surface(r, surface.get());
1068}
1069#endif
Mike Reed44d04bd2017-06-28 19:57:21 -04001070
1071DEF_TEST(Surface_null, r) {
1072 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1073
1074 const int w = 37;
1075 const int h = 1000;
1076 auto surf = SkSurface::MakeNull(w, h);
1077 auto canvas = surf->getCanvas();
1078
1079 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1080 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1081}
Mike Reedd4746982018-02-07 16:05:29 -05001082
1083// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1084// (so the snapshot can succeed)
1085DEF_TEST(surface_image_unity, reporter) {
1086 auto do_test = [reporter](const SkImageInfo& info) {
1087 size_t rowBytes = info.minRowBytes();
1088 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1089 if (surf) {
1090 auto img = surf->makeImageSnapshot();
1091 if (!img && false) { // change to true to document the differences
1092 SkDebugf("image failed: [%08X %08X] %14s %s\n",
1093 info.width(), info.height(),
1094 sk_tool_utils::colortype_name(info.colorType()),
1095 sk_tool_utils::alphatype_name(info.alphaType()));
1096 return;
1097 }
1098 REPORTER_ASSERT(reporter, img != nullptr);
1099
1100 char dummyPixel = 0; // just need a valid address (not a valid size)
1101 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1102 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1103 REPORTER_ASSERT(reporter, img != nullptr);
1104 }
1105 };
1106
Mike Klein955b3d52018-02-20 11:55:48 -05001107 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 -05001108 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001109 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001110 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001111 SkAlphaType at = static_cast<SkAlphaType>(ati);
1112 for (int32_t size : sizes) {
Mike Klein955b3d52018-02-20 11:55:48 -05001113 // Large allocations tend to make the 32-bit bots run out of virtual address space.
1114 if (sizeof(size_t) == 4 && size > (1<<20)) {
1115 continue;
1116 }
Mike Reedd4746982018-02-07 16:05:29 -05001117 do_test(SkImageInfo::Make(1, size, ct, at));
1118 do_test(SkImageInfo::Make(size, 1, ct, at));
1119 }
1120 }
1121 }
1122}