blob: c2a81dca166d789686e1aa77e14c6d3c1e0450f2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkOverdrawCanvas.h"
11#include "include/core/SkPath.h"
12#include "include/core/SkRRect.h"
13#include "include/core/SkRegion.h"
14#include "include/core/SkSurface.h"
15#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040016#include "include/gpu/GrDirectContext.h"
Brian Salomonaad83152019-05-24 10:16:35 -040017#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkDevice.h"
19#include "src/core/SkUtils.h"
20#include "src/gpu/GrContextPriv.h"
21#include "src/gpu/GrGpu.h"
22#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040023#include "src/gpu/GrImageInfo.h"
Stephen Whitefdba6c82020-05-26 17:00:32 -040024#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRenderTargetContext.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/SkGpuDevice.h"
28#include "src/image/SkImage_Base.h"
29#include "src/image/SkImage_Gpu.h"
30#include "src/image/SkSurface_Gpu.h"
31#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040032#include "tests/TestUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000033
Hal Canary8a001442018-09-19 11:31:27 -040034#include <functional>
35#include <initializer_list>
36#include <vector>
37
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "tools/ToolUtils.h"
bsalomon74f681d2015-06-23 14:38:48 -070039
kkinnunen179a8f52015-11-20 13:32:24 -080040static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070041 SkASSERT(pixels == context);
42 sk_free(pixels);
43}
reede8f30622016-03-23 18:59:25 -070044static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
45 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070046 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000047 if (requestedInfo) {
48 *requestedInfo = info;
49 }
reede8f30622016-03-23 18:59:25 -070050 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000051}
reede8f30622016-03-23 18:59:25 -070052static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
53 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080054 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
55 if (requestedInfo) {
56 *requestedInfo = info;
57 }
58 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040059 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070060 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
61 release_direct_surface_storage,
62 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080063}
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}
junov@chromium.org995beb62013-03-28 13:49:22 +000081
kkinnunen179a8f52015-11-20 13:32:24 -080082DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080083 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070084 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
85 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080086
reedb2497c22014-12-31 12:31:43 -080087}
egdanielab527a52016-06-28 08:07:26 -070088DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080089 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
90 REPORTER_ASSERT(reporter, nullptr ==
Robert Phillips6d344c32020-07-06 10:56:46 -040091 SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080092}
reedb2497c22014-12-31 12:31:43 -080093
Brian Salomonbdecacf2018-02-02 20:32:49 -050094DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040095 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -040096
Brian Salomonbdecacf2018-02-02 20:32:49 -050097 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
98 static constexpr int kSize = 10;
99
100 SkColorType colorType = static_cast<SkColorType>(ct);
101 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500102
Robert Phillips429f0d32019-09-11 17:03:28 -0400103 {
104 bool can = context->colorTypeSupportedAsSurface(colorType);
105 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
106 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
107 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500108
Greg Danielc1ad77c2020-05-06 11:40:03 -0400109 GrBackendTexture backendTex;
110 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType,
111 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes,
112 GrProtected::kNo);
Robert Phillips429f0d32019-09-11 17:03:28 -0400113 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
114 kTopLeft_GrSurfaceOrigin, 0, colorType,
115 nullptr, nullptr);
116 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
117 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500118
Robert Phillips429f0d32019-09-11 17:03:28 -0400119 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, 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));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500124
Robert Phillips429f0d32019-09-11 17:03:28 -0400125 surf.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400126 context->flushAndSubmit();
Robert Phillips429f0d32019-09-11 17:03:28 -0400127 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 }
129
Robert Phillips429f0d32019-09-11 17:03:28 -0400130 // The MSAA test only makes sense if the colorType is renderable to begin with.
131 if (context->colorTypeSupportedAsSurface(colorType)) {
132 static constexpr int kSampleCnt = 2;
133
134 bool can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
135 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt,
136 nullptr);
137 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
138 colorType, can, SkToBool(surf));
139
Greg Danielc1ad77c2020-05-06 11:40:03 -0400140 GrBackendTexture backendTex;
141 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType,
142 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes,
143 GrProtected::kNo);
Robert Phillips429f0d32019-09-11 17:03:28 -0400144 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
145 kTopLeft_GrSurfaceOrigin, kSampleCnt,
146 colorType, nullptr, nullptr);
147 REPORTER_ASSERT(reporter, can == SkToBool(surf),
148 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
149 colorType);
150 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
151 if (surf) {
152 auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
153 int storedCnt = rtc->numSamples();
154 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
155 storedCnt, backendTex.getBackendFormat());
156 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
157 "Should store an allowed sample count (%d vs %d)", allowedCnt,
158 storedCnt);
159 }
160
161 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
162 kTopLeft_GrSurfaceOrigin,
163 kSampleCnt, colorType,
164 nullptr, nullptr);
165 REPORTER_ASSERT(reporter, can == SkToBool(surf),
166 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
167 colorType);
168 if (surf) {
169 auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
170 int storedCnt = rtc->numSamples();
171 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
172 storedCnt, backendTex.getBackendFormat());
173 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
174 "Should store an allowed sample count (%d vs %d)", allowedCnt,
175 storedCnt);
176 }
177
178 surf.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400179 context->flushAndSubmit();
Robert Phillips429f0d32019-09-11 17:03:28 -0400180 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500181 }
182
Robert Phillips429f0d32019-09-11 17:03:28 -0400183 {
184 auto* gpu = context->priv().getGpu();
Robert Phillips9b16f812019-05-17 10:01:21 -0400185
Robert Phillips429f0d32019-09-11 17:03:28 -0400186 GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget(
187 16, 16, SkColorTypeToGrColorType(colorType));
188 bool can = context->colorTypeSupportedAsSurface(colorType);
189 auto surf = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget,
190 kTopLeft_GrSurfaceOrigin, colorType,
191 nullptr, nullptr);
192 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType,
193 can, SkToBool(surf));
194 surf.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400195 context->flushAndSubmit();
Robert Phillips429f0d32019-09-11 17:03:28 -0400196 if (backendRenderTarget.isValid()) {
197 gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget);
198 }
Brian Salomon93348dd2018-08-29 12:56:23 -0400199 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500200 }
201}
202
203DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400204 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400205
206 static constexpr int kSize = 10;
207
Brian Salomonbdecacf2018-02-02 20:32:49 -0500208 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500209
210 SkColorType colorType = static_cast<SkColorType>(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -0400211 int max = context->maxSurfaceSampleCountForColorType(colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500212 if (!max) {
213 continue;
214 }
Greg Danielc1ad77c2020-05-06 11:40:03 -0400215
216 GrBackendTexture backendTex;
217 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType,
218 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes,
219 GrProtected::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400220 if (!backendTex.isValid()) {
221 continue;
222 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400223 SkScopeExit freeTex([&backendTex, context] {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400224 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400225 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400226
227 if (!context->colorTypeSupportedAsSurface(colorType)) {
228 continue;
229 }
230
Brian Salomonbdecacf2018-02-02 20:32:49 -0500231 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Robert Phillips9b16f812019-05-17 10:01:21 -0400232 auto surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500233 kTopLeft_GrSurfaceOrigin, max,
234 colorType, nullptr, nullptr);
235 REPORTER_ASSERT(reporter, surf);
236 if (!surf) {
237 continue;
238 }
239 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
240 ->getDevice()
241 ->accessRenderTargetContext()
Chris Dalton6ce447a2019-06-23 18:07:38 -0600242 ->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500243 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
244 }
245}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500246
kkinnunen179a8f52015-11-20 13:32:24 -0800247static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700248 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800249 const SkImageInfo& requestInfo,
250 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000251 const SkColor color = SK_ColorRED;
252 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800253 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000254
reed6ceeebd2016-03-09 14:26:26 -0800255 SkPixmap pmap;
256 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800257 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000258
reed6ceeebd2016-03-09 14:26:26 -0800259 SkPixmap pmap2;
260 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000261
kkinnunen179a8f52015-11-20 13:32:24 -0800262 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800263 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
264 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
265 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000266
reed6ceeebd2016-03-09 14:26:26 -0800267 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
268 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
269 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800270 } else {
271 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000272 }
273}
kkinnunen179a8f52015-11-20 13:32:24 -0800274DEF_TEST(SurfaceCanvasPeek, reporter) {
275 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
276 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700277 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800278 test_canvas_peek(reporter, surface, requestInfo, true);
279 }
280}
egdanielab527a52016-06-28 08:07:26 -0700281DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800282 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
283 SkImageInfo requestInfo;
Robert Phillips6d344c32020-07-06 10:56:46 -0400284 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800285 test_canvas_peek(reporter, surface, requestInfo, false);
286 }
287}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000288
reede8f30622016-03-23 18:59:25 -0700289static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700290 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800291 REPORTER_ASSERT(reporter, surface);
292 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700293 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800294 REPORTER_ASSERT(reporter, image);
295 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700296 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700297 }
298 }
299}
kkinnunen179a8f52015-11-20 13:32:24 -0800300DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
301 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700302 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
303 auto surface(surface_func(at, nullptr));
304 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700305 }
306 }
307}
egdanielab527a52016-06-28 08:07:26 -0700308DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800309 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700310 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
311 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400312 auto surface(surface_func(ctxInfo.directContext(), at, nullptr));
brianosman69c166d2016-08-17 14:01:05 -0700313 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800314 }
315 }
316}
bsalomon74f681d2015-06-23 14:38:48 -0700317
Robert Phillips8caf85f2018-04-05 09:30:38 -0400318static void test_backend_texture_access_copy_on_write(
319 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
320 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700321 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700322
Robert Phillips8caf85f2018-04-05 09:30:38 -0400323 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700324 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700325
326 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400327 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700328}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400329
330static void test_backend_rendertarget_access_copy_on_write(
331 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
332 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
333 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
334
335 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
336 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
337
338 // If the access mode triggers CoW, then the backend objects should reflect it.
339 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
340 (snap1 == snap2));
341}
342
343DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800344 const SkSurface::BackendHandleAccess accessModes[] = {
345 SkSurface::kFlushRead_BackendHandleAccess,
346 SkSurface::kFlushWrite_BackendHandleAccess,
347 SkSurface::kDiscardWrite_BackendHandleAccess,
348 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400349
kkinnunen179a8f52015-11-20 13:32:24 -0800350 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400351 for (auto& accessMode : accessModes) {
352 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400353 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400354 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
355 }
356 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400357 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400358 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800359 }
360 }
361 }
362}
kkinnunen179a8f52015-11-20 13:32:24 -0800363
Robert Phillips8caf85f2018-04-05 09:30:38 -0400364template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
365static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700366 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400367
368 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
369 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700370 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800371 // just read access should not affect the snapshot
372 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
373
Robert Phillips8caf85f2018-04-05 09:30:38 -0400374 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
375 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700376 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800377 // expect a new image, since we claimed we would write
378 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
379
Robert Phillips8caf85f2018-04-05 09:30:38 -0400380 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
381 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700382 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800383 // expect a new(er) image, since we claimed we would write
384 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
385 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
386}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400387
kkinnunen179a8f52015-11-20 13:32:24 -0800388// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700389DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800390 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400391 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400392 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400393 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
394 surface.get());
395 }
396 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400397 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400398 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
399 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800400 }
401 }
402}
kkinnunen179a8f52015-11-20 13:32:24 -0800403
404// Verify that the right canvas commands trigger a copy on write.
405static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000406 SkCanvas* canvas = surface->getCanvas();
407
408 const SkRect testRect =
409 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
410 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000411 SkPath testPath;
412 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
413 SkIntToScalar(2), SkIntToScalar(1)));
414
415 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
416
417 SkRegion testRegion;
418 testRegion.setRect(testIRect);
419
420
421 const SkColor testColor = 0x01020304;
422 const SkPaint testPaint;
423 const SkPoint testPoints[3] = {
424 {SkIntToScalar(0), SkIntToScalar(0)},
425 {SkIntToScalar(2), SkIntToScalar(1)},
426 {SkIntToScalar(0), SkIntToScalar(2)}
427 };
428 const size_t testPointCount = 3;
429
430 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000431 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000432 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000433
434 SkRRect testRRect;
435 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
436
437 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000438
439#define EXPECT_COPY_ON_WRITE(command) \
440 { \
reed9ce9d672016-03-17 10:51:11 -0700441 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
442 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000443 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700444 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
445 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000446 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
447 }
448
449 EXPECT_COPY_ON_WRITE(clear(testColor))
450 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
451 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
452 testPaint))
453 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
454 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
455 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
456 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
457 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700458 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
Hal Canary89a644b2019-01-07 09:36:09 -0500459 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), 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}
egdanielab527a52016-06-28 08:07:26 -0700464DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800465 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400466 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700467 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700468 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000469}
470
kkinnunen179a8f52015-11-20 13:32:24 -0800471static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
472 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000473 // This test succeeds by not triggering an assertion.
474 // The test verifies that the surface remains writable (usable) after
475 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000476 SkCanvas* canvas = surface->getCanvas();
477 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700478 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000479 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000480}
kkinnunen179a8f52015-11-20 13:32:24 -0800481DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700482 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800483}
egdanielab527a52016-06-28 08:07:26 -0700484DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800485 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400486 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700487 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800488 }
489}
junov@chromium.orgda904742013-05-01 22:38:16 +0000490
kkinnunen179a8f52015-11-20 13:32:24 -0800491static void test_crbug263329(skiatest::Reporter* reporter,
492 SkSurface* surface1,
493 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000494 // This is a regression test for crbug.com/263329
495 // Bug was caused by onCopyOnWrite releasing the old surface texture
496 // back to the scratch texture pool even though the texture is used
497 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000498 SkCanvas* canvas1 = surface1->getCanvas();
499 SkCanvas* canvas2 = surface2->getCanvas();
500 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700501 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000502 // Trigger copy on write, new backing is a scratch texture
503 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700504 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000505 // Trigger copy on write, old backing should not be returned to scratch
506 // pool because it is held by image2
507 canvas1->clear(3);
508
509 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700510 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000511 // Trigger copy on write on surface2. The new backing store should not
512 // be recycling a texture that is held by an existing image.
513 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700514 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Greg Daniel7c902112020-03-06 13:07:10 -0500515
516 SkImage_GpuBase* gpuImage1 = static_cast<SkImage_GpuBase*>(as_IB(image1));
517 SkImage_GpuBase* gpuImage2 = static_cast<SkImage_GpuBase*>(as_IB(image2));
518 SkImage_GpuBase* gpuImage3 = static_cast<SkImage_GpuBase*>(as_IB(image3));
519 SkImage_GpuBase* gpuImage4 = static_cast<SkImage_GpuBase*>(as_IB(image4));
520
521 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage3->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000522 // The following assertion checks crbug.com/263329
Greg Daniel7c902112020-03-06 13:07:10 -0500523 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage2->getTexture());
524 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage1->getTexture());
525 REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage2->getTexture());
526 REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage1->getTexture());
527 REPORTER_ASSERT(reporter, gpuImage2->getTexture() != gpuImage1->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000528}
egdanielab527a52016-06-28 08:07:26 -0700529DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800530 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400531 auto surface1(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
532 auto surface2(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700533 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800534 }
535}
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}
egdanielab527a52016-06-28 08:07:26 -0700544DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800545 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400546 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700547 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500548
549 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400550 GrBackendTexture backendTex = image->getBackendTexture(false);
551 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800552 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500553 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400554 GrBackendTexture backendTex2 = image->getBackendTexture(false);
555 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000556 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000557}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800558
reede8f30622016-03-23 18:59:25 -0700559static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
560 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500561
562 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
563 ->asRenderTargetProxy();
564 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800565}
566
bsalomon5ec26ae2016-02-25 08:33:02 -0800567static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400568 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800569}
570
reed9ce9d672016-03-17 10:51:11 -0700571static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
572 return is_budgeted(image.get());
573}
574
egdanielab527a52016-06-28 08:07:26 -0700575DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800576 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400577 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400578 auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), budgeted, info));
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400579 SkASSERT(surface);
580 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800581
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400582 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800583
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400584 // Initially the image shares a texture with the surface, and the
585 // the budgets should always match.
586 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
587 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800588
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400589 // Now trigger copy-on-write
590 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800591
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400592 // They don't share a texture anymore but the budgets should still match.
593 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
594 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800595 }
596}
junov@chromium.orgda904742013-05-01 22:38:16 +0000597
kkinnunen179a8f52015-11-20 13:32:24 -0800598static void test_no_canvas1(skiatest::Reporter* reporter,
599 SkSurface* surface,
600 SkSurface::ContentChangeMode mode) {
601 // Test passes by not asserting
602 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800603}
604static void test_no_canvas2(skiatest::Reporter* reporter,
605 SkSurface* surface,
606 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000607 // Verifies the robustness of SkSurface for handling use cases where calls
608 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700609 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
610 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800611 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700612 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
613 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800614 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000615}
kkinnunen179a8f52015-11-20 13:32:24 -0800616DEF_TEST(SurfaceNoCanvas, reporter) {
617 SkSurface::ContentChangeMode modes[] =
618 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
619 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
620 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700621 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800622 }
623 }
624}
egdanielab527a52016-06-28 08:07:26 -0700625DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800626 SkSurface::ContentChangeMode modes[] =
627 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
628 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
629 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
630 for (auto& mode : modes) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400631 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700632 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700633 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000634 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000635 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000636}
reed9cd016e2016-01-30 10:01:06 -0800637
638static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800639 SkPixmap surfacePM;
640 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800641
reed9ce9d672016-03-17 10:51:11 -0700642 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800643 SkPixmap pm;
644 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800645
reed6ceeebd2016-03-09 14:26:26 -0800646 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800647
648 // trigger a copy-on-write
649 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700650 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800651 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
652
reed6ceeebd2016-03-09 14:26:26 -0800653 SkPixmap pm2;
654 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
655 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800656}
657
658DEF_TEST(surface_rowbytes, reporter) {
659 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
660
reede8f30622016-03-23 18:59:25 -0700661 auto surf0(SkSurface::MakeRaster(info));
662 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800663
664 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700665 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
666 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800667
668 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700669 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800670 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400671 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800672 REPORTER_ASSERT(reporter, nullptr == s);
673}
bsalomone63ffef2016-02-05 07:17:34 -0800674
fmalita03912f12016-07-06 06:22:06 -0700675DEF_TEST(surface_raster_zeroinitialized, reporter) {
676 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
677 SkPixmap pixmap;
678 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
679
680 for (int i = 0; i < pixmap.info().width(); ++i) {
681 for (int j = 0; j < pixmap.info().height(); ++j) {
682 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
683 }
684 }
685}
686
ericrkc4025182016-05-04 12:01:58 -0700687static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400688 GrContext* ctx, int sampleCnt, const SkColor4f& color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500689
Michael Ludwig72ab3462018-12-10 12:43:36 -0500690 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
691 // triggers what appears to be a driver race condition where the 10x10 surface from the
692 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
693 // test.
694 //
695 // Immediately after creation of this surface, readback shows the correct initial solid color.
696 // However, sometime before content is rendered into the upper half of the surface, the driver
697 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
698 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
699 // untouched bottom half contains random pixel values that trigger asserts in the
700 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
701 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
702 //
703 // Requesting a much larger backend texture size seems to prevent it from reusing the same
704 // memory and avoids the issue.
705#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700706 const int kWidth = 10;
707 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500708#else
709 const int kWidth = 100;
710 const int kHeight = 100;
711#endif
712
Robert Phillipsee5fd132019-05-07 13:29:22 -0400713 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
714 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000715
Brian Salomon28a8f282019-10-24 20:07:39 -0400716 if (!CreateBackendTexture(ctx, outTexture, ii, color, GrMipMapped::kNo, GrRenderable::kYes)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500717 return nullptr;
718 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000719
Robert Phillipsee5fd132019-05-07 13:29:22 -0400720 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(ctx, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400721 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500722 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000723 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700724 if (!surface) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400725 DeleteBackendTexture(ctx, *outTexture);
ericrkc4025182016-05-04 12:01:58 -0700726 return nullptr;
727 }
ericrkc4025182016-05-04 12:01:58 -0700728 return surface;
729}
bsalomone63ffef2016-02-05 07:17:34 -0800730
Stephen Whitefdba6c82020-05-26 17:00:32 -0400731static bool supports_readpixels(const GrCaps* caps, SkSurface* surface) {
732 auto surfaceGpu = static_cast<SkSurface_Gpu*>(surface);
733 GrRenderTargetContext* context = surfaceGpu->getDevice()->accessRenderTargetContext();
734 GrRenderTarget* rt = context->accessRenderTarget();
735 if (!rt) {
736 return false;
737 }
738 return caps->surfaceSupportsReadPixels(rt) == GrCaps::SurfaceReadPixelsSupport::kSupported;
739}
740
ericrkc4025182016-05-04 12:01:58 -0700741static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400742 GrContext* ctx, int sampleCnt, const SkColor4f& color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500743
ericrkc4025182016-05-04 12:01:58 -0700744 const int kWidth = 10;
745 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000746
Robert Phillipsee5fd132019-05-07 13:29:22 -0400747 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
748 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000749
Brian Salomon28a8f282019-10-24 20:07:39 -0400750 if (!CreateBackendTexture(ctx, outTexture, ii, color, GrMipMapped::kNo, GrRenderable::kYes)) {
ericrkc4025182016-05-04 12:01:58 -0700751 return nullptr;
752 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500753
754 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400755 ctx, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
Greg Danielfaa095e2017-12-19 13:15:02 -0500756 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500757
758 if (!surface) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400759 DeleteBackendTexture(ctx, *outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500760 return nullptr;
761 }
ericrkc4025182016-05-04 12:01:58 -0700762 return surface;
763}
764
Brian Salomonbf6b9792019-08-21 09:38:10 -0400765static void test_surface_context_clear(skiatest::Reporter* reporter,
766 GrSurfaceContext* surfaceContext, uint32_t expectedValue) {
767 int w = surfaceContext->width();
768 int h = surfaceContext->height();
Robert Phillipsd3442842019-08-02 12:26:22 -0400769
770 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
771
772 SkAutoPixmapStorage readback;
773 readback.alloc(ii);
bsalomone63ffef2016-02-05 07:17:34 -0800774
Robert Phillipsd3442842019-08-02 12:26:22 -0400775 readback.erase(~expectedValue);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400776 surfaceContext->readPixels(readback.info(), readback.writable_addr(), readback.rowBytes(),
777 {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800778 for (int y = 0; y < h; ++y) {
779 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400780 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -0800781 if (pixel != expectedValue) {
782 SkString msg;
783 if (expectedValue) {
784 msg = "SkSurface should have left render target unmodified";
785 } else {
786 msg = "SkSurface should have cleared the render target";
787 }
788 ERRORF(reporter,
789 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
790 expectedValue, x, y);
791 return;
792 }
793 }
794 }
795}
796
bsalomon758586c2016-04-06 14:02:39 -0700797DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400798 auto context = ctxInfo.directContext();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400799 // Snaps an image from a surface and then makes a GrSurfaceContext from the image's texture.
800 auto makeImageSurfaceContext = [context](SkSurface* surface) {
801 sk_sp<SkImage> i(surface->makeImageSnapshot());
802 SkImage_Gpu* gpuImage = (SkImage_Gpu*)as_IB(i);
Greg Daniel37c127f2020-02-05 10:37:27 -0500803 return GrSurfaceContext::Make(context, *gpuImage->view(context),
Greg Danielbfa19c42019-12-19 16:41:40 -0500804 SkColorTypeToGrColorType(i->colorType()), kPremul_SkAlphaType,
805 gpuImage->refColorSpace());
bsalomone63ffef2016-02-05 07:17:34 -0800806 };
ericrkc4025182016-05-04 12:01:58 -0700807
Brian Salomonbf6b9792019-08-21 09:38:10 -0400808 // Test that non-wrapped RTs are created clear.
809 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
810 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
811 if (!surface) {
812 ERRORF(reporter, "Could not create GPU SkSurface.");
813 return;
bsalomone63ffef2016-02-05 07:17:34 -0800814 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400815 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
816 if (!rtc) {
817 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
818 return;
ericrkc4025182016-05-04 12:01:58 -0700819 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400820 test_surface_context_clear(reporter, rtc, 0x0);
821 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
822 test_surface_context_clear(reporter, imageSurfaceCtx.get(), 0x0);
823 }
824
825 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
826 const SkColor4f kOrigColor{.67f, .67f, .67f, 1};
827 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
828 &create_gpu_surface_backend_texture_as_render_target}) {
829 GrBackendTexture backendTex;
830 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
831 if (!surface) {
832 ERRORF(reporter, "Could not create GPU SkSurface.");
833 return;
834 }
835 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
836 if (!rtc) {
837 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
838 return;
839 }
840 test_surface_context_clear(reporter, rtc, kOrigColor.toSkColor());
841 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
842 test_surface_context_clear(reporter, imageSurfaceCtx.get(), kOrigColor.toSkColor());
843 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700844 }
845}
bsalomone63ffef2016-02-05 07:17:34 -0800846
ericrkc4025182016-05-04 12:01:58 -0700847static void test_surface_draw_partially(
Robert Phillips66944402019-09-30 13:21:25 -0400848 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
ericrkc4025182016-05-04 12:01:58 -0700849 const int kW = surface->width();
850 const int kH = surface->height();
851 SkPaint paint;
852 const SkColor kRectColor = ~origColor | 0xFF000000;
853 paint.setColor(kRectColor);
Robert Phillipsd3442842019-08-02 12:26:22 -0400854 surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint);
855
ericrkc4025182016-05-04 12:01:58 -0700856 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
857 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsd3442842019-08-02 12:26:22 -0400858
859 SkAutoPixmapStorage readback;
860 readback.alloc(readInfo);
861
862 readback.erase(~origColor);
Stephen Whitedbb3e1d2020-05-13 17:55:18 -0400863 REPORTER_ASSERT(reporter, surface->readPixels(readback.info(), readback.writable_addr(),
864 readback.rowBytes(), 0, 0));
ericrkc4025182016-05-04 12:01:58 -0700865 bool stop = false;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400866
Robert Phillips66944402019-09-30 13:21:25 -0400867 SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
868 SkColorGetR(origColor),
869 SkColorGetG(origColor),
870 SkColorGetB(origColor));
871 SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
872 SkColorGetR(kRectColor),
873 SkColorGetG(kRectColor),
874 SkColorGetB(kRectColor));
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400875
ericrkc4025182016-05-04 12:01:58 -0700876 for (int y = 0; y < kH/2 && !stop; ++y) {
877 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400878 REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]);
879 if (rectColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400880 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400881 readback.addr32()[x + y * kW], rectColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700882 stop = true;
883 }
884 }
885 }
886 stop = false;
887 for (int y = kH/2; y < kH && !stop; ++y) {
888 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400889 REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]);
890 if (origColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400891 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400892 readback.addr32()[x + y * kW], origColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700893 stop = true;
894 }
895 }
896 }
897}
bsalomone63ffef2016-02-05 07:17:34 -0800898
egdanielab527a52016-06-28 08:07:26 -0700899DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400900 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400901
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400902 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
bsalomone63ffef2016-02-05 07:17:34 -0800903
ericrkc4025182016-05-04 12:01:58 -0700904 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
905 &create_gpu_surface_backend_texture_as_render_target}) {
906 // Validate that we can draw to the canvas and that the original texture color is
907 // preserved in pixels that aren't rendered to via the surface.
908 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500909 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400910 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
Stephen Whitefdba6c82020-05-26 17:00:32 -0400911 const GrCaps* caps = context->priv().caps();
912 if (!supports_readpixels(caps, surface.get())) {
913 continue;
914 }
ericrkc4025182016-05-04 12:01:58 -0700915 if (surface) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400916 test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700917 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400918 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700919 }
920 }
921}
922
Greg Daniel8ce79912019-02-05 10:08:43 -0500923struct ReleaseChecker {
924 ReleaseChecker() : fReleaseCount(0) {}
925 int fReleaseCount;
926 static void Release(void* self) {
927 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
928 }
929};
930
931
932DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
933 const int kWidth = 10;
934 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500935
Robert Phillips6d344c32020-07-06 10:56:46 -0400936 auto ctx = ctxInfo.directContext();
Greg Daniel8ce79912019-02-05 10:08:43 -0500937 GrGpu* gpu = ctx->priv().getGpu();
938
939 for (bool useTexture : {false, true}) {
940 GrBackendTexture backendTex;
941 GrBackendRenderTarget backendRT;
942 sk_sp<SkSurface> surface;
943
944 ReleaseChecker releaseChecker;
945 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
946
947 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400948 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
949 kPremul_SkAlphaType);
Brian Salomon28a8f282019-10-24 20:07:39 -0400950 if (!CreateBackendTexture(ctx, &backendTex, ii, SkColors::kRed, GrMipMapped::kNo,
951 GrRenderable::kYes)) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500952 continue;
953 }
954
955 surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1,
956 kRGBA_8888_SkColorType,
957 nullptr, nullptr,
958 ReleaseChecker::Release,
959 &releaseChecker);
960 } else {
961 backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight,
962 GrColorType::kRGBA_8888);
963 if (!backendRT.isValid()) {
964 continue;
965 }
966 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
967 kRGBA_8888_SkColorType,
968 nullptr, nullptr,
969 ReleaseChecker::Release,
970 &releaseChecker);
971 }
972 if (!surface) {
973 ERRORF(reporter, "Failed to create surface");
974 continue;
975 }
976
977 surface->getCanvas()->clear(SK_ColorRED);
Greg Danielce9f0162020-06-30 13:42:46 -0400978 surface->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400979 ctx->submit(true);
Greg Daniel8ce79912019-02-05 10:08:43 -0500980
981 // Now exercise the release proc
982 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
983 surface.reset(nullptr); // force a release of the surface
984 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
985
986 if (useTexture) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400987 DeleteBackendTexture(ctx, backendTex);
Greg Daniel8ce79912019-02-05 10:08:43 -0500988 } else {
989 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
990 }
991 }
992}
ericrkc4025182016-05-04 12:01:58 -0700993
994DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400995 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400996 const GrCaps* caps = context->priv().caps();
997
998 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -0700999 return;
1000 }
Robert Phillips9b16f812019-05-17 10:01:21 -04001001
Robert Phillips4d87b2b2019-07-23 13:44:16 -04001002 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
ericrkc4025182016-05-04 12:01:58 -07001003
Robert Phillips9b16f812019-05-17 10:01:21 -04001004 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001005
ericrkc4025182016-05-04 12:01:58 -07001006 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
1007 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001008 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001009 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -04001010 auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -07001011
Brian Salomonbdecacf2018-02-02 20:32:49 -05001012 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001013 // Certain platforms don't support MSAA, skip these.
1014 continue;
ericrkc4025182016-05-04 12:01:58 -07001015 }
1016
1017 // Validate that we can attach a stencil buffer to an SkSurface created by either of
1018 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -04001019 GrRenderTarget* rt = surface->getCanvas()
1020 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Chris Daltoneffee202019-07-01 22:28:03 -06001021 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt));
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001022 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -07001023 }
bsalomone63ffef2016-02-05 07:17:34 -08001024 }
1025}
brianosman0e22eb82016-08-30 07:07:59 -07001026
Brian Salomonaad83152019-05-24 10:16:35 -04001027DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001028 auto context = ctxInfo.directContext();
Brian Salomonaad83152019-05-24 10:16:35 -04001029
1030 for (int sampleCnt : {1, 2}) {
1031 GrBackendTexture backendTexture1;
1032 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Brian Salomon28a8f282019-10-24 20:07:39 -04001033 if (!CreateBackendTexture(context, &backendTexture1, ii, SkColors::kTransparent,
1034 GrMipMapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001035 continue;
1036 }
1037 SkScopeExit delete1(
Brian Salomon28a8f282019-10-24 20:07:39 -04001038 [context, &backendTexture1] { DeleteBackendTexture(context, backendTexture1); });
Brian Salomonaad83152019-05-24 10:16:35 -04001039 GrBackendTexture backendTexture2;
Brian Salomon28a8f282019-10-24 20:07:39 -04001040 if (!CreateBackendTexture(context, &backendTexture2, ii, SkColors::kTransparent,
1041 GrMipMapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001042 ERRORF(reporter, "Expected to be able to make second texture");
1043 continue;
1044 }
1045 SkScopeExit delete2(
Brian Salomon28a8f282019-10-24 20:07:39 -04001046 [context, &backendTexture2] { DeleteBackendTexture(context, backendTexture2); });
Brian Salomonaad83152019-05-24 10:16:35 -04001047 auto ii2 = ii.makeWH(8, 8);
1048 GrBackendTexture backendTexture3;
Brian Salomon28a8f282019-10-24 20:07:39 -04001049 if (!CreateBackendTexture(context, &backendTexture3, ii2, SkColors::kTransparent,
1050 GrMipMapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001051 ERRORF(reporter, "Couldn't create different sized texture.");
1052 continue;
1053 }
1054 SkScopeExit delete3(
Brian Salomon28a8f282019-10-24 20:07:39 -04001055 [context, &backendTexture3] { DeleteBackendTexture(context, backendTexture3); });
Brian Salomonaad83152019-05-24 10:16:35 -04001056
1057 auto surf = SkSurface::MakeFromBackendTexture(
1058 context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt,
1059 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1060 if (!surf) {
1061 continue;
1062 }
1063 surf->getCanvas()->clear(SK_ColorBLUE);
1064 // Change matrix, layer, and clip state before swapping out the backing texture.
1065 surf->getCanvas()->translate(5, 5);
1066 surf->getCanvas()->saveLayer(nullptr, nullptr);
1067 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1068 // switch origin while we're at it.
1069 bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin);
1070 REPORTER_ASSERT(reporter, replaced);
1071 SkPaint paint;
1072 paint.setColor(SK_ColorRED);
1073 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1074 surf->getCanvas()->restore();
1075
1076 // Check that the replacement texture got the right color values.
1077 SkAutoPixmapStorage pm;
1078 pm.alloc(ii);
1079 bool bad = !surf->readPixels(pm, 0, 0);
1080 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1081 for (int y = 0; y < ii.height() && !bad; ++y) {
1082 for (int x = 0; x < ii.width() && !bad; ++x) {
1083 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1084 auto found = *pm.addr32(x, y);
1085 if (found != expected) {
1086 bad = true;
1087 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1088 expected, found, x, y);
1089 }
1090 }
1091 }
1092 // The original texture should still be all blue.
1093 surf = SkSurface::MakeFromBackendTexture(
1094 context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt,
1095 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1096 if (!surf) {
1097 ERRORF(reporter, "Could not create second surface.");
1098 continue;
1099 }
1100 bad = !surf->readPixels(pm, 0, 0);
1101 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1102 for (int y = 0; y < ii.height() && !bad; ++y) {
1103 for (int x = 0; x < ii.width() && !bad; ++x) {
1104 auto expected = 0xFFFF0000;
1105 auto found = *pm.addr32(x, y);
1106 if (found != expected) {
1107 bad = true;
1108 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1109 expected, found, x, y);
1110 }
1111 }
1112 }
1113
1114 // Can't replace with the same texture
1115 REPORTER_ASSERT(reporter,
1116 !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin));
1117 // Can't replace with invalid texture
1118 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1119 // Can't replace with different size texture.
1120 REPORTER_ASSERT(reporter,
1121 !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin));
1122 // Can't replace texture of non-wrapped SkSurface.
1123 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1124 REPORTER_ASSERT(reporter, surf);
1125 if (surf) {
1126 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1,
1127 kTopLeft_GrSurfaceOrigin));
1128 }
1129 }
1130}
1131
Matt Sarett22886c42016-11-22 11:31:41 -05001132static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001133 SkOverdrawCanvas canvas(surface->getCanvas());
1134 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001135 sk_sp<SkImage> image = surface->makeImageSnapshot();
1136
1137 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001138 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001139 for (int y = 0; y < 10; y++) {
1140 for (int x = 0; x < 10; x++) {
1141 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1142 }
1143 }
1144}
1145
1146DEF_TEST(OverdrawSurface_Raster, r) {
1147 sk_sp<SkSurface> surface = create_surface();
1148 test_overdraw_surface(r, surface.get());
1149}
1150
Matt Sarett22886c42016-11-22 11:31:41 -05001151DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001152 auto context = ctxInfo.directContext();
Matt Sarett22886c42016-11-22 11:31:41 -05001153 sk_sp<SkSurface> surface = create_gpu_surface(context);
1154 test_overdraw_surface(r, surface.get());
1155}
Mike Reed44d04bd2017-06-28 19:57:21 -04001156
1157DEF_TEST(Surface_null, r) {
1158 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1159
1160 const int w = 37;
1161 const int h = 1000;
1162 auto surf = SkSurface::MakeNull(w, h);
1163 auto canvas = surf->getCanvas();
1164
1165 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1166 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1167}
Mike Reedd4746982018-02-07 16:05:29 -05001168
1169// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1170// (so the snapshot can succeed)
1171DEF_TEST(surface_image_unity, reporter) {
1172 auto do_test = [reporter](const SkImageInfo& info) {
1173 size_t rowBytes = info.minRowBytes();
1174 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1175 if (surf) {
1176 auto img = surf->makeImageSnapshot();
1177 if (!img && false) { // change to true to document the differences
1178 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001179 info.width(),
1180 info.height(),
1181 ToolUtils::colortype_name(info.colorType()),
1182 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001183 return;
1184 }
1185 REPORTER_ASSERT(reporter, img != nullptr);
1186
1187 char dummyPixel = 0; // just need a valid address (not a valid size)
1188 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1189 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1190 REPORTER_ASSERT(reporter, img != nullptr);
1191 }
1192 };
1193
Mike Kleine978ca22018-10-29 11:29:58 -04001194 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001195 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001196 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001197 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001198 SkAlphaType at = static_cast<SkAlphaType>(ati);
1199 for (int32_t size : sizes) {
1200 do_test(SkImageInfo::Make(1, size, ct, at));
1201 do_test(SkImageInfo::Make(size, 1, ct, at));
1202 }
1203 }
1204 }
1205}