blob: 5a5d8853ed79579db351b5b6858474257dea1d5c [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}
Robert Phillipseffd13f2020-07-20 15:00:36 -040064static sk_sp<SkSurface> create_gpu_surface(GrRecordingContext* rContext,
65 SkAlphaType at = kPremul_SkAlphaType,
reede8f30622016-03-23 18:59:25 -070066 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080067 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
68 if (requestedInfo) {
69 *requestedInfo = info;
70 }
Robert Phillipseffd13f2020-07-20 15:00:36 -040071 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080072}
Robert Phillipseffd13f2020-07-20 15:00:36 -040073static sk_sp<SkSurface> create_gpu_scratch_surface(GrRecordingContext* rContext,
reede8f30622016-03-23 18:59:25 -070074 SkAlphaType at = kPremul_SkAlphaType,
75 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080076 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
77 if (requestedInfo) {
78 *requestedInfo = info;
79 }
Robert Phillipseffd13f2020-07-20 15:00:36 -040080 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080081}
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}
egdanielab527a52016-06-28 08:07:26 -070089DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080090 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
91 REPORTER_ASSERT(reporter, nullptr ==
Robert Phillips6d344c32020-07-06 10:56:46 -040092 SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080093}
reedb2497c22014-12-31 12:31:43 -080094
Brian Salomonbdecacf2018-02-02 20:32:49 -050095DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040096 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -040097
Brian Salomonbdecacf2018-02-02 20:32:49 -050098 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
99 static constexpr int kSize = 10;
100
101 SkColorType colorType = static_cast<SkColorType>(ct);
102 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500103
Robert Phillips429f0d32019-09-11 17:03:28 -0400104 {
105 bool can = context->colorTypeSupportedAsSurface(colorType);
106 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
107 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
108 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109
Greg Danielc1ad77c2020-05-06 11:40:03 -0400110 GrBackendTexture backendTex;
111 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400112 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kYes,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400113 GrProtected::kNo);
Robert Phillips429f0d32019-09-11 17:03:28 -0400114 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
115 kTopLeft_GrSurfaceOrigin, 0, colorType,
116 nullptr, nullptr);
117 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
118 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500119
Robert Phillips429f0d32019-09-11 17:03:28 -0400120 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
121 kTopLeft_GrSurfaceOrigin, 1,
122 colorType, nullptr, nullptr);
123 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
124 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500125
Robert Phillips429f0d32019-09-11 17:03:28 -0400126 surf.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400127 context->flushAndSubmit();
Robert Phillips429f0d32019-09-11 17:03:28 -0400128 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 }
130
Robert Phillips429f0d32019-09-11 17:03:28 -0400131 // The MSAA test only makes sense if the colorType is renderable to begin with.
132 if (context->colorTypeSupportedAsSurface(colorType)) {
133 static constexpr int kSampleCnt = 2;
134
135 bool can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
136 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt,
137 nullptr);
138 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
139 colorType, can, SkToBool(surf));
140
Greg Danielc1ad77c2020-05-06 11:40:03 -0400141 GrBackendTexture backendTex;
142 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400143 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kYes,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400144 GrProtected::kNo);
Robert Phillips429f0d32019-09-11 17:03:28 -0400145 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
146 kTopLeft_GrSurfaceOrigin, kSampleCnt,
147 colorType, nullptr, nullptr);
148 REPORTER_ASSERT(reporter, can == SkToBool(surf),
149 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
150 colorType);
151 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
152 if (surf) {
153 auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
154 int storedCnt = rtc->numSamples();
155 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
156 storedCnt, backendTex.getBackendFormat());
157 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
158 "Should store an allowed sample count (%d vs %d)", allowedCnt,
159 storedCnt);
160 }
161
162 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
163 kTopLeft_GrSurfaceOrigin,
164 kSampleCnt, colorType,
165 nullptr, nullptr);
166 REPORTER_ASSERT(reporter, can == SkToBool(surf),
167 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
168 colorType);
169 if (surf) {
170 auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
171 int storedCnt = rtc->numSamples();
172 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
173 storedCnt, backendTex.getBackendFormat());
174 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
175 "Should store an allowed sample count (%d vs %d)", allowedCnt,
176 storedCnt);
177 }
178
179 surf.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400180 context->flushAndSubmit();
Robert Phillips429f0d32019-09-11 17:03:28 -0400181 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500182 }
183
Robert Phillips429f0d32019-09-11 17:03:28 -0400184 {
185 auto* gpu = context->priv().getGpu();
Robert Phillips9b16f812019-05-17 10:01:21 -0400186
Robert Phillips429f0d32019-09-11 17:03:28 -0400187 GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget(
188 16, 16, SkColorTypeToGrColorType(colorType));
189 bool can = context->colorTypeSupportedAsSurface(colorType);
190 auto surf = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget,
191 kTopLeft_GrSurfaceOrigin, colorType,
192 nullptr, nullptr);
193 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType,
194 can, SkToBool(surf));
195 surf.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400196 context->flushAndSubmit();
Robert Phillips429f0d32019-09-11 17:03:28 -0400197 if (backendRenderTarget.isValid()) {
198 gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget);
199 }
Brian Salomon93348dd2018-08-29 12:56:23 -0400200 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500201 }
202}
203
204DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400205 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400206
207 static constexpr int kSize = 10;
208
Brian Salomonbdecacf2018-02-02 20:32:49 -0500209 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500210
211 SkColorType colorType = static_cast<SkColorType>(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -0400212 int max = context->maxSurfaceSampleCountForColorType(colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500213 if (!max) {
214 continue;
215 }
Greg Danielc1ad77c2020-05-06 11:40:03 -0400216
217 GrBackendTexture backendTex;
218 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400219 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kYes,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400220 GrProtected::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400221 if (!backendTex.isValid()) {
222 continue;
223 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400224 SkScopeExit freeTex([&backendTex, context] {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400225 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400226 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400227
228 if (!context->colorTypeSupportedAsSurface(colorType)) {
229 continue;
230 }
231
Brian Salomonbdecacf2018-02-02 20:32:49 -0500232 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Robert Phillips9b16f812019-05-17 10:01:21 -0400233 auto surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500234 kTopLeft_GrSurfaceOrigin, max,
235 colorType, nullptr, nullptr);
236 REPORTER_ASSERT(reporter, surf);
237 if (!surf) {
238 continue;
239 }
240 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
241 ->getDevice()
242 ->accessRenderTargetContext()
Chris Dalton6ce447a2019-06-23 18:07:38 -0600243 ->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500244 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
245 }
246}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500247
kkinnunen179a8f52015-11-20 13:32:24 -0800248static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700249 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800250 const SkImageInfo& requestInfo,
251 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000252 const SkColor color = SK_ColorRED;
253 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800254 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000255
reed6ceeebd2016-03-09 14:26:26 -0800256 SkPixmap pmap;
257 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800258 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000259
reed6ceeebd2016-03-09 14:26:26 -0800260 SkPixmap pmap2;
261 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000262
kkinnunen179a8f52015-11-20 13:32:24 -0800263 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800264 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
265 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
266 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000267
reed6ceeebd2016-03-09 14:26:26 -0800268 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
269 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
270 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800271 } else {
272 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000273 }
274}
kkinnunen179a8f52015-11-20 13:32:24 -0800275DEF_TEST(SurfaceCanvasPeek, reporter) {
276 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
277 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700278 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800279 test_canvas_peek(reporter, surface, requestInfo, true);
280 }
281}
egdanielab527a52016-06-28 08:07:26 -0700282DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800283 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
284 SkImageInfo requestInfo;
Robert Phillips6d344c32020-07-06 10:56:46 -0400285 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800286 test_canvas_peek(reporter, surface, requestInfo, false);
287 }
288}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000289
reede8f30622016-03-23 18:59:25 -0700290static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700291 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800292 REPORTER_ASSERT(reporter, surface);
293 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700294 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800295 REPORTER_ASSERT(reporter, image);
296 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700297 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700298 }
299 }
300}
kkinnunen179a8f52015-11-20 13:32:24 -0800301DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
302 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700303 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
304 auto surface(surface_func(at, nullptr));
305 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700306 }
307 }
308}
egdanielab527a52016-06-28 08:07:26 -0700309DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800310 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700311 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
312 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400313 auto surface(surface_func(ctxInfo.directContext(), at, nullptr));
brianosman69c166d2016-08-17 14:01:05 -0700314 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800315 }
316 }
317}
bsalomon74f681d2015-06-23 14:38:48 -0700318
Robert Phillips8caf85f2018-04-05 09:30:38 -0400319static void test_backend_texture_access_copy_on_write(
320 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
321 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700322 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700323
Robert Phillips8caf85f2018-04-05 09:30:38 -0400324 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700325 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700326
327 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400328 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700329}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400330
331static void test_backend_rendertarget_access_copy_on_write(
332 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
333 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
334 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
335
336 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
337 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
338
339 // If the access mode triggers CoW, then the backend objects should reflect it.
340 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
341 (snap1 == snap2));
342}
343
344DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800345 const SkSurface::BackendHandleAccess accessModes[] = {
346 SkSurface::kFlushRead_BackendHandleAccess,
347 SkSurface::kFlushWrite_BackendHandleAccess,
348 SkSurface::kDiscardWrite_BackendHandleAccess,
349 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400350
kkinnunen179a8f52015-11-20 13:32:24 -0800351 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400352 for (auto& accessMode : accessModes) {
353 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400354 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400355 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
356 }
357 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400358 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400359 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800360 }
361 }
362 }
363}
kkinnunen179a8f52015-11-20 13:32:24 -0800364
Robert Phillips8caf85f2018-04-05 09:30:38 -0400365template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
366static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700367 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400368
369 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
370 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700371 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800372 // just read access should not affect the snapshot
373 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
374
Robert Phillips8caf85f2018-04-05 09:30:38 -0400375 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
376 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700377 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800378 // expect a new image, since we claimed we would write
379 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
380
Robert Phillips8caf85f2018-04-05 09:30:38 -0400381 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
382 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700383 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800384 // expect a new(er) image, since we claimed we would write
385 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
386 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
387}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400388
kkinnunen179a8f52015-11-20 13:32:24 -0800389// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700390DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800391 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400392 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400393 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400394 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
395 surface.get());
396 }
397 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400398 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400399 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
400 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800401 }
402 }
403}
kkinnunen179a8f52015-11-20 13:32:24 -0800404
405// Verify that the right canvas commands trigger a copy on write.
406static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000407 SkCanvas* canvas = surface->getCanvas();
408
409 const SkRect testRect =
410 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
411 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000412 SkPath testPath;
413 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
414 SkIntToScalar(2), SkIntToScalar(1)));
415
416 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
417
418 SkRegion testRegion;
419 testRegion.setRect(testIRect);
420
421
422 const SkColor testColor = 0x01020304;
423 const SkPaint testPaint;
424 const SkPoint testPoints[3] = {
425 {SkIntToScalar(0), SkIntToScalar(0)},
426 {SkIntToScalar(2), SkIntToScalar(1)},
427 {SkIntToScalar(0), SkIntToScalar(2)}
428 };
429 const size_t testPointCount = 3;
430
431 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000432 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000433 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000434
435 SkRRect testRRect;
436 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
437
438 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000439
440#define EXPECT_COPY_ON_WRITE(command) \
441 { \
reed9ce9d672016-03-17 10:51:11 -0700442 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
John Stiles31954bf2020-08-07 17:35:54 -0400443 sk_sp<SkImage> aur_before(imageBefore); /*NOLINT*/ \
junov@chromium.org995beb62013-03-28 13:49:22 +0000444 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700445 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
John Stiles31954bf2020-08-07 17:35:54 -0400446 sk_sp<SkImage> aur_after(imageAfter); /*NOLINT*/ \
junov@chromium.org995beb62013-03-28 13:49:22 +0000447 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
448 }
449
450 EXPECT_COPY_ON_WRITE(clear(testColor))
451 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
452 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
453 testPaint))
454 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
455 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
456 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
457 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
458 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700459 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
Hal Canary89a644b2019-01-07 09:36:09 -0500460 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800461}
462DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700463 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800464}
egdanielab527a52016-06-28 08:07:26 -0700465DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800466 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400467 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700468 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700469 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000470}
471
kkinnunen179a8f52015-11-20 13:32:24 -0800472static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
473 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000474 // This test succeeds by not triggering an assertion.
475 // The test verifies that the surface remains writable (usable) after
476 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000477 SkCanvas* canvas = surface->getCanvas();
478 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700479 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000480 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000481}
kkinnunen179a8f52015-11-20 13:32:24 -0800482DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700483 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800484}
egdanielab527a52016-06-28 08:07:26 -0700485DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800486 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400487 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700488 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800489 }
490}
junov@chromium.orgda904742013-05-01 22:38:16 +0000491
kkinnunen179a8f52015-11-20 13:32:24 -0800492static void test_crbug263329(skiatest::Reporter* reporter,
493 SkSurface* surface1,
494 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000495 // This is a regression test for crbug.com/263329
496 // Bug was caused by onCopyOnWrite releasing the old surface texture
497 // back to the scratch texture pool even though the texture is used
498 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000499 SkCanvas* canvas1 = surface1->getCanvas();
500 SkCanvas* canvas2 = surface2->getCanvas();
501 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700502 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000503 // Trigger copy on write, new backing is a scratch texture
504 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700505 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000506 // Trigger copy on write, old backing should not be returned to scratch
507 // pool because it is held by image2
508 canvas1->clear(3);
509
510 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700511 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000512 // Trigger copy on write on surface2. The new backing store should not
513 // be recycling a texture that is held by an existing image.
514 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700515 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Greg Daniel7c902112020-03-06 13:07:10 -0500516
517 SkImage_GpuBase* gpuImage1 = static_cast<SkImage_GpuBase*>(as_IB(image1));
518 SkImage_GpuBase* gpuImage2 = static_cast<SkImage_GpuBase*>(as_IB(image2));
519 SkImage_GpuBase* gpuImage3 = static_cast<SkImage_GpuBase*>(as_IB(image3));
520 SkImage_GpuBase* gpuImage4 = static_cast<SkImage_GpuBase*>(as_IB(image4));
521
522 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage3->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000523 // The following assertion checks crbug.com/263329
Greg Daniel7c902112020-03-06 13:07:10 -0500524 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage2->getTexture());
525 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage1->getTexture());
526 REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage2->getTexture());
527 REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage1->getTexture());
528 REPORTER_ASSERT(reporter, gpuImage2->getTexture() != gpuImage1->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000529}
egdanielab527a52016-06-28 08:07:26 -0700530DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800531 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400532 auto surface1(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
533 auto surface2(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700534 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800535 }
536}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000537
kkinnunen179a8f52015-11-20 13:32:24 -0800538DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700539 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700540 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500541 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800542 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500543 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800544}
egdanielab527a52016-06-28 08:07:26 -0700545DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800546 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400547 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700548 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500549
550 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400551 GrBackendTexture backendTex = image->getBackendTexture(false);
552 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800553 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500554 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400555 GrBackendTexture backendTex2 = image->getBackendTexture(false);
556 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000557 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000558}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800559
reede8f30622016-03-23 18:59:25 -0700560static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
561 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500562
563 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
564 ->asRenderTargetProxy();
565 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800566}
567
bsalomon5ec26ae2016-02-25 08:33:02 -0800568static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400569 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800570}
571
reed9ce9d672016-03-17 10:51:11 -0700572static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
573 return is_budgeted(image.get());
574}
575
egdanielab527a52016-06-28 08:07:26 -0700576DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800577 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400578 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400579 auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), budgeted, info));
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400580 SkASSERT(surface);
581 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800582
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400583 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800584
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400585 // Initially the image shares a texture with the surface, and the
586 // the budgets should always match.
587 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
588 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800589
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400590 // Now trigger copy-on-write
591 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800592
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400593 // They don't share a texture anymore but the budgets should still match.
594 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
595 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800596 }
597}
junov@chromium.orgda904742013-05-01 22:38:16 +0000598
kkinnunen179a8f52015-11-20 13:32:24 -0800599static void test_no_canvas1(skiatest::Reporter* reporter,
600 SkSurface* surface,
601 SkSurface::ContentChangeMode mode) {
602 // Test passes by not asserting
603 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800604}
605static void test_no_canvas2(skiatest::Reporter* reporter,
606 SkSurface* surface,
607 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000608 // Verifies the robustness of SkSurface for handling use cases where calls
609 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700610 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
John Stiles31954bf2020-08-07 17:35:54 -0400611 sk_sp<SkImage> aur_image1(image1); // NOLINT(performance-unnecessary-copy-initialization)
kkinnunen179a8f52015-11-20 13:32:24 -0800612 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700613 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
John Stiles31954bf2020-08-07 17:35:54 -0400614 sk_sp<SkImage> aur_image2(image2); // NOLINT(performance-unnecessary-copy-initialization)
kkinnunen179a8f52015-11-20 13:32:24 -0800615 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000616}
kkinnunen179a8f52015-11-20 13:32:24 -0800617DEF_TEST(SurfaceNoCanvas, reporter) {
618 SkSurface::ContentChangeMode modes[] =
619 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
620 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
621 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700622 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800623 }
624 }
625}
egdanielab527a52016-06-28 08:07:26 -0700626DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800627 SkSurface::ContentChangeMode modes[] =
628 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
629 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
630 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
631 for (auto& mode : modes) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400632 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700633 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700634 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000635 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000636 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000637}
reed9cd016e2016-01-30 10:01:06 -0800638
639static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800640 SkPixmap surfacePM;
641 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800642
reed9ce9d672016-03-17 10:51:11 -0700643 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800644 SkPixmap pm;
645 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800646
reed6ceeebd2016-03-09 14:26:26 -0800647 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800648
649 // trigger a copy-on-write
650 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700651 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800652 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
653
reed6ceeebd2016-03-09 14:26:26 -0800654 SkPixmap pm2;
655 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
656 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800657}
658
659DEF_TEST(surface_rowbytes, reporter) {
660 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
661
reede8f30622016-03-23 18:59:25 -0700662 auto surf0(SkSurface::MakeRaster(info));
663 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800664
665 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700666 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
667 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800668
669 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700670 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800671 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400672 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800673 REPORTER_ASSERT(reporter, nullptr == s);
674}
bsalomone63ffef2016-02-05 07:17:34 -0800675
fmalita03912f12016-07-06 06:22:06 -0700676DEF_TEST(surface_raster_zeroinitialized, reporter) {
677 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
678 SkPixmap pixmap;
679 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
680
681 for (int i = 0; i < pixmap.info().width(); ++i) {
682 for (int j = 0; j < pixmap.info().height(); ++j) {
683 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
684 }
685 }
686}
687
Robert Phillipseffd13f2020-07-20 15:00:36 -0400688static sk_sp<SkSurface> create_gpu_surface_backend_texture(GrDirectContext* dContext,
689 int sampleCnt,
690 const SkColor4f& color,
691 GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500692
Michael Ludwig72ab3462018-12-10 12:43:36 -0500693 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
694 // triggers what appears to be a driver race condition where the 10x10 surface from the
695 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
696 // test.
697 //
698 // Immediately after creation of this surface, readback shows the correct initial solid color.
699 // However, sometime before content is rendered into the upper half of the surface, the driver
700 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
701 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
702 // untouched bottom half contains random pixel values that trigger asserts in the
703 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
704 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
705 //
706 // Requesting a much larger backend texture size seems to prevent it from reusing the same
707 // memory and avoids the issue.
708#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700709 const int kWidth = 10;
710 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500711#else
712 const int kWidth = 100;
713 const int kHeight = 100;
714#endif
715
Robert Phillipsee5fd132019-05-07 13:29:22 -0400716 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
717 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000718
Robert Phillipseffd13f2020-07-20 15:00:36 -0400719 if (!CreateBackendTexture(dContext, outTexture, ii, color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400720 GrMipmapped::kNo, GrRenderable::kYes)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500721 return nullptr;
722 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000723
Robert Phillipseffd13f2020-07-20 15:00:36 -0400724 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(dContext,
725 *outTexture,
726 kTopLeft_GrSurfaceOrigin,
727 sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500728 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000729 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700730 if (!surface) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400731 DeleteBackendTexture(dContext, *outTexture);
ericrkc4025182016-05-04 12:01:58 -0700732 return nullptr;
733 }
ericrkc4025182016-05-04 12:01:58 -0700734 return surface;
735}
bsalomone63ffef2016-02-05 07:17:34 -0800736
Stephen Whitefdba6c82020-05-26 17:00:32 -0400737static bool supports_readpixels(const GrCaps* caps, SkSurface* surface) {
738 auto surfaceGpu = static_cast<SkSurface_Gpu*>(surface);
739 GrRenderTargetContext* context = surfaceGpu->getDevice()->accessRenderTargetContext();
740 GrRenderTarget* rt = context->accessRenderTarget();
741 if (!rt) {
742 return false;
743 }
744 return caps->surfaceSupportsReadPixels(rt) == GrCaps::SurfaceReadPixelsSupport::kSupported;
745}
746
ericrkc4025182016-05-04 12:01:58 -0700747static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipseffd13f2020-07-20 15:00:36 -0400748 GrDirectContext* dContext,
749 int sampleCnt,
750 const SkColor4f& color,
751 GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500752
ericrkc4025182016-05-04 12:01:58 -0700753 const int kWidth = 10;
754 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000755
Robert Phillipsee5fd132019-05-07 13:29:22 -0400756 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
757 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000758
Robert Phillipseffd13f2020-07-20 15:00:36 -0400759 if (!CreateBackendTexture(dContext, outTexture, ii, color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400760 GrMipmapped::kNo, GrRenderable::kYes)) {
ericrkc4025182016-05-04 12:01:58 -0700761 return nullptr;
762 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500763
764 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipseffd13f2020-07-20 15:00:36 -0400765 dContext, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
Greg Danielfaa095e2017-12-19 13:15:02 -0500766 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500767
768 if (!surface) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400769 DeleteBackendTexture(dContext, *outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500770 return nullptr;
771 }
ericrkc4025182016-05-04 12:01:58 -0700772 return surface;
773}
774
Brian Salomonbf6b9792019-08-21 09:38:10 -0400775static void test_surface_context_clear(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400776 GrDirectContext* dContext,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400777 GrSurfaceContext* surfaceContext, uint32_t expectedValue) {
778 int w = surfaceContext->width();
779 int h = surfaceContext->height();
Robert Phillipsd3442842019-08-02 12:26:22 -0400780
781 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
782
783 SkAutoPixmapStorage readback;
784 readback.alloc(ii);
bsalomone63ffef2016-02-05 07:17:34 -0800785
Robert Phillipsd3442842019-08-02 12:26:22 -0400786 readback.erase(~expectedValue);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400787 surfaceContext->readPixels(dContext, readback.info(), readback.writable_addr(),
788 readback.rowBytes(), {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800789 for (int y = 0; y < h; ++y) {
790 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400791 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -0800792 if (pixel != expectedValue) {
793 SkString msg;
794 if (expectedValue) {
795 msg = "SkSurface should have left render target unmodified";
796 } else {
797 msg = "SkSurface should have cleared the render target";
798 }
799 ERRORF(reporter,
800 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
801 expectedValue, x, y);
802 return;
803 }
804 }
805 }
806}
807
bsalomon758586c2016-04-06 14:02:39 -0700808DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400809 auto dContext = ctxInfo.directContext();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400810 // Snaps an image from a surface and then makes a GrSurfaceContext from the image's texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400811 auto makeImageSurfaceContext = [dContext](SkSurface* surface) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400812 sk_sp<SkImage> i(surface->makeImageSnapshot());
813 SkImage_Gpu* gpuImage = (SkImage_Gpu*)as_IB(i);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400814 return GrSurfaceContext::Make(dContext, *gpuImage->view(dContext),
Greg Danielbfa19c42019-12-19 16:41:40 -0500815 SkColorTypeToGrColorType(i->colorType()), kPremul_SkAlphaType,
816 gpuImage->refColorSpace());
bsalomone63ffef2016-02-05 07:17:34 -0800817 };
ericrkc4025182016-05-04 12:01:58 -0700818
Brian Salomonbf6b9792019-08-21 09:38:10 -0400819 // Test that non-wrapped RTs are created clear.
820 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400821 auto surface = surface_func(dContext, kPremul_SkAlphaType, nullptr);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400822 if (!surface) {
823 ERRORF(reporter, "Could not create GPU SkSurface.");
824 return;
bsalomone63ffef2016-02-05 07:17:34 -0800825 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400826 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
827 if (!rtc) {
828 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
829 return;
ericrkc4025182016-05-04 12:01:58 -0700830 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400831 test_surface_context_clear(reporter, dContext, rtc, 0x0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400832 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400833 test_surface_context_clear(reporter, dContext, imageSurfaceCtx.get(), 0x0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400834 }
835
836 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
837 const SkColor4f kOrigColor{.67f, .67f, .67f, 1};
838 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
839 &create_gpu_surface_backend_texture_as_render_target}) {
840 GrBackendTexture backendTex;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400841 auto surface = surfaceFunc(dContext, 1, kOrigColor, &backendTex);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400842 if (!surface) {
843 ERRORF(reporter, "Could not create GPU SkSurface.");
844 return;
845 }
846 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
847 if (!rtc) {
848 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
849 return;
850 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400851 test_surface_context_clear(reporter, dContext, rtc, kOrigColor.toSkColor());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400852 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400853 test_surface_context_clear(reporter, dContext, imageSurfaceCtx.get(),
854 kOrigColor.toSkColor());
855 dContext->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700856 }
857}
bsalomone63ffef2016-02-05 07:17:34 -0800858
ericrkc4025182016-05-04 12:01:58 -0700859static void test_surface_draw_partially(
Robert Phillips66944402019-09-30 13:21:25 -0400860 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
ericrkc4025182016-05-04 12:01:58 -0700861 const int kW = surface->width();
862 const int kH = surface->height();
863 SkPaint paint;
864 const SkColor kRectColor = ~origColor | 0xFF000000;
865 paint.setColor(kRectColor);
Robert Phillipsd3442842019-08-02 12:26:22 -0400866 surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint);
867
ericrkc4025182016-05-04 12:01:58 -0700868 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
869 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsd3442842019-08-02 12:26:22 -0400870
871 SkAutoPixmapStorage readback;
872 readback.alloc(readInfo);
873
874 readback.erase(~origColor);
Stephen Whitedbb3e1d2020-05-13 17:55:18 -0400875 REPORTER_ASSERT(reporter, surface->readPixels(readback.info(), readback.writable_addr(),
876 readback.rowBytes(), 0, 0));
ericrkc4025182016-05-04 12:01:58 -0700877 bool stop = false;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400878
Robert Phillips66944402019-09-30 13:21:25 -0400879 SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
880 SkColorGetR(origColor),
881 SkColorGetG(origColor),
882 SkColorGetB(origColor));
883 SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
884 SkColorGetR(kRectColor),
885 SkColorGetG(kRectColor),
886 SkColorGetB(kRectColor));
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400887
ericrkc4025182016-05-04 12:01:58 -0700888 for (int y = 0; y < kH/2 && !stop; ++y) {
889 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400890 REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]);
891 if (rectColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400892 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400893 readback.addr32()[x + y * kW], rectColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700894 stop = true;
895 }
896 }
897 }
898 stop = false;
899 for (int y = kH/2; y < kH && !stop; ++y) {
900 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400901 REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]);
902 if (origColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400903 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400904 readback.addr32()[x + y * kW], origColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700905 stop = true;
906 }
907 }
908 }
909}
bsalomone63ffef2016-02-05 07:17:34 -0800910
egdanielab527a52016-06-28 08:07:26 -0700911DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400912 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400913
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400914 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
bsalomone63ffef2016-02-05 07:17:34 -0800915
ericrkc4025182016-05-04 12:01:58 -0700916 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
917 &create_gpu_surface_backend_texture_as_render_target}) {
918 // Validate that we can draw to the canvas and that the original texture color is
919 // preserved in pixels that aren't rendered to via the surface.
920 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500921 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400922 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
Stephen Whitefdba6c82020-05-26 17:00:32 -0400923 const GrCaps* caps = context->priv().caps();
924 if (!supports_readpixels(caps, surface.get())) {
925 continue;
926 }
ericrkc4025182016-05-04 12:01:58 -0700927 if (surface) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400928 test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700929 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400930 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700931 }
932 }
933}
934
Greg Daniel8ce79912019-02-05 10:08:43 -0500935struct ReleaseChecker {
936 ReleaseChecker() : fReleaseCount(0) {}
937 int fReleaseCount;
938 static void Release(void* self) {
939 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
940 }
941};
942
943
944DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
945 const int kWidth = 10;
946 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500947
Robert Phillips6d344c32020-07-06 10:56:46 -0400948 auto ctx = ctxInfo.directContext();
Greg Daniel8ce79912019-02-05 10:08:43 -0500949 GrGpu* gpu = ctx->priv().getGpu();
950
951 for (bool useTexture : {false, true}) {
952 GrBackendTexture backendTex;
953 GrBackendRenderTarget backendRT;
954 sk_sp<SkSurface> surface;
955
956 ReleaseChecker releaseChecker;
957 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
958
959 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400960 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
961 kPremul_SkAlphaType);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400962 if (!CreateBackendTexture(ctx, &backendTex, ii, SkColors::kRed, GrMipmapped::kNo,
Brian Salomon28a8f282019-10-24 20:07:39 -0400963 GrRenderable::kYes)) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500964 continue;
965 }
966
967 surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1,
968 kRGBA_8888_SkColorType,
969 nullptr, nullptr,
970 ReleaseChecker::Release,
971 &releaseChecker);
972 } else {
973 backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight,
974 GrColorType::kRGBA_8888);
975 if (!backendRT.isValid()) {
976 continue;
977 }
978 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
979 kRGBA_8888_SkColorType,
980 nullptr, nullptr,
981 ReleaseChecker::Release,
982 &releaseChecker);
983 }
984 if (!surface) {
985 ERRORF(reporter, "Failed to create surface");
986 continue;
987 }
988
989 surface->getCanvas()->clear(SK_ColorRED);
Greg Danielce9f0162020-06-30 13:42:46 -0400990 surface->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400991 ctx->submit(true);
Greg Daniel8ce79912019-02-05 10:08:43 -0500992
993 // Now exercise the release proc
994 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
995 surface.reset(nullptr); // force a release of the surface
996 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
997
998 if (useTexture) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400999 DeleteBackendTexture(ctx, backendTex);
Greg Daniel8ce79912019-02-05 10:08:43 -05001000 } else {
1001 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
1002 }
1003 }
1004}
ericrkc4025182016-05-04 12:01:58 -07001005
1006DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001007 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -04001008 const GrCaps* caps = context->priv().caps();
1009
1010 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -07001011 return;
1012 }
Robert Phillips9b16f812019-05-17 10:01:21 -04001013
Robert Phillips4d87b2b2019-07-23 13:44:16 -04001014 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
ericrkc4025182016-05-04 12:01:58 -07001015
Robert Phillips9b16f812019-05-17 10:01:21 -04001016 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001017
ericrkc4025182016-05-04 12:01:58 -07001018 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
1019 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001020 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001021 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -04001022 auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -07001023
Brian Salomonbdecacf2018-02-02 20:32:49 -05001024 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001025 // Certain platforms don't support MSAA, skip these.
1026 continue;
ericrkc4025182016-05-04 12:01:58 -07001027 }
1028
1029 // Validate that we can attach a stencil buffer to an SkSurface created by either of
1030 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -04001031 GrRenderTarget* rt = surface->getCanvas()
1032 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Chris Daltoneffee202019-07-01 22:28:03 -06001033 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt));
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001034 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -07001035 }
bsalomone63ffef2016-02-05 07:17:34 -08001036 }
1037}
brianosman0e22eb82016-08-30 07:07:59 -07001038
Brian Salomonaad83152019-05-24 10:16:35 -04001039DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001040 auto context = ctxInfo.directContext();
Brian Salomonaad83152019-05-24 10:16:35 -04001041
1042 for (int sampleCnt : {1, 2}) {
1043 GrBackendTexture backendTexture1;
1044 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Brian Salomon28a8f282019-10-24 20:07:39 -04001045 if (!CreateBackendTexture(context, &backendTexture1, ii, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001046 GrMipmapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001047 continue;
1048 }
1049 SkScopeExit delete1(
Brian Salomon28a8f282019-10-24 20:07:39 -04001050 [context, &backendTexture1] { DeleteBackendTexture(context, backendTexture1); });
Brian Salomonaad83152019-05-24 10:16:35 -04001051 GrBackendTexture backendTexture2;
Brian Salomon28a8f282019-10-24 20:07:39 -04001052 if (!CreateBackendTexture(context, &backendTexture2, ii, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001053 GrMipmapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001054 ERRORF(reporter, "Expected to be able to make second texture");
1055 continue;
1056 }
1057 SkScopeExit delete2(
Brian Salomon28a8f282019-10-24 20:07:39 -04001058 [context, &backendTexture2] { DeleteBackendTexture(context, backendTexture2); });
Brian Salomonaad83152019-05-24 10:16:35 -04001059 auto ii2 = ii.makeWH(8, 8);
1060 GrBackendTexture backendTexture3;
Brian Salomon28a8f282019-10-24 20:07:39 -04001061 if (!CreateBackendTexture(context, &backendTexture3, ii2, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001062 GrMipmapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001063 ERRORF(reporter, "Couldn't create different sized texture.");
1064 continue;
1065 }
1066 SkScopeExit delete3(
Brian Salomon28a8f282019-10-24 20:07:39 -04001067 [context, &backendTexture3] { DeleteBackendTexture(context, backendTexture3); });
Brian Salomonaad83152019-05-24 10:16:35 -04001068
1069 auto surf = SkSurface::MakeFromBackendTexture(
1070 context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt,
1071 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1072 if (!surf) {
1073 continue;
1074 }
1075 surf->getCanvas()->clear(SK_ColorBLUE);
1076 // Change matrix, layer, and clip state before swapping out the backing texture.
1077 surf->getCanvas()->translate(5, 5);
1078 surf->getCanvas()->saveLayer(nullptr, nullptr);
1079 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1080 // switch origin while we're at it.
1081 bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin);
1082 REPORTER_ASSERT(reporter, replaced);
1083 SkPaint paint;
1084 paint.setColor(SK_ColorRED);
1085 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1086 surf->getCanvas()->restore();
1087
1088 // Check that the replacement texture got the right color values.
1089 SkAutoPixmapStorage pm;
1090 pm.alloc(ii);
1091 bool bad = !surf->readPixels(pm, 0, 0);
1092 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1093 for (int y = 0; y < ii.height() && !bad; ++y) {
1094 for (int x = 0; x < ii.width() && !bad; ++x) {
1095 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1096 auto found = *pm.addr32(x, y);
1097 if (found != expected) {
1098 bad = true;
1099 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1100 expected, found, x, y);
1101 }
1102 }
1103 }
1104 // The original texture should still be all blue.
1105 surf = SkSurface::MakeFromBackendTexture(
1106 context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt,
1107 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1108 if (!surf) {
1109 ERRORF(reporter, "Could not create second surface.");
1110 continue;
1111 }
1112 bad = !surf->readPixels(pm, 0, 0);
1113 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1114 for (int y = 0; y < ii.height() && !bad; ++y) {
1115 for (int x = 0; x < ii.width() && !bad; ++x) {
1116 auto expected = 0xFFFF0000;
1117 auto found = *pm.addr32(x, y);
1118 if (found != expected) {
1119 bad = true;
1120 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1121 expected, found, x, y);
1122 }
1123 }
1124 }
1125
1126 // Can't replace with the same texture
1127 REPORTER_ASSERT(reporter,
1128 !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin));
1129 // Can't replace with invalid texture
1130 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1131 // Can't replace with different size texture.
1132 REPORTER_ASSERT(reporter,
1133 !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin));
1134 // Can't replace texture of non-wrapped SkSurface.
1135 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1136 REPORTER_ASSERT(reporter, surf);
1137 if (surf) {
1138 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1,
1139 kTopLeft_GrSurfaceOrigin));
1140 }
1141 }
1142}
1143
Matt Sarett22886c42016-11-22 11:31:41 -05001144static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001145 SkOverdrawCanvas canvas(surface->getCanvas());
1146 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001147 sk_sp<SkImage> image = surface->makeImageSnapshot();
1148
1149 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001150 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001151 for (int y = 0; y < 10; y++) {
1152 for (int x = 0; x < 10; x++) {
1153 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1154 }
1155 }
1156}
1157
1158DEF_TEST(OverdrawSurface_Raster, r) {
1159 sk_sp<SkSurface> surface = create_surface();
1160 test_overdraw_surface(r, surface.get());
1161}
1162
Matt Sarett22886c42016-11-22 11:31:41 -05001163DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001164 auto context = ctxInfo.directContext();
Matt Sarett22886c42016-11-22 11:31:41 -05001165 sk_sp<SkSurface> surface = create_gpu_surface(context);
1166 test_overdraw_surface(r, surface.get());
1167}
Mike Reed44d04bd2017-06-28 19:57:21 -04001168
1169DEF_TEST(Surface_null, r) {
1170 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1171
1172 const int w = 37;
1173 const int h = 1000;
1174 auto surf = SkSurface::MakeNull(w, h);
1175 auto canvas = surf->getCanvas();
1176
1177 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1178 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1179}
Mike Reedd4746982018-02-07 16:05:29 -05001180
1181// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1182// (so the snapshot can succeed)
1183DEF_TEST(surface_image_unity, reporter) {
1184 auto do_test = [reporter](const SkImageInfo& info) {
1185 size_t rowBytes = info.minRowBytes();
1186 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1187 if (surf) {
1188 auto img = surf->makeImageSnapshot();
1189 if (!img && false) { // change to true to document the differences
1190 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001191 info.width(),
1192 info.height(),
1193 ToolUtils::colortype_name(info.colorType()),
1194 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001195 return;
1196 }
1197 REPORTER_ASSERT(reporter, img != nullptr);
1198
1199 char dummyPixel = 0; // just need a valid address (not a valid size)
1200 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1201 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1202 REPORTER_ASSERT(reporter, img != nullptr);
1203 }
1204 };
1205
Mike Kleine978ca22018-10-29 11:29:58 -04001206 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001207 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001208 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001209 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001210 SkAlphaType at = static_cast<SkAlphaType>(ati);
1211 for (int32_t size : sizes) {
1212 do_test(SkImageInfo::Make(1, size, ct, at));
1213 do_test(SkImageInfo::Make(size, 1, ct, at));
1214 }
1215 }
1216 }
1217}