blob: cf8885804e7c2898a9079a7aea459d0c57a8ebdc [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(); \
443 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000444 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700445 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
446 sk_sp<SkImage> aur_after(imageAfter); \
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();
611 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800612 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700613 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
614 sk_sp<SkImage> aur_image2(image2);
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,
776 GrSurfaceContext* surfaceContext, uint32_t expectedValue) {
777 int w = surfaceContext->width();
778 int h = surfaceContext->height();
Robert Phillipsd3442842019-08-02 12:26:22 -0400779
780 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
781
782 SkAutoPixmapStorage readback;
783 readback.alloc(ii);
bsalomone63ffef2016-02-05 07:17:34 -0800784
Robert Phillipsd3442842019-08-02 12:26:22 -0400785 readback.erase(~expectedValue);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400786 surfaceContext->readPixels(readback.info(), readback.writable_addr(), readback.rowBytes(),
787 {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800788 for (int y = 0; y < h; ++y) {
789 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400790 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -0800791 if (pixel != expectedValue) {
792 SkString msg;
793 if (expectedValue) {
794 msg = "SkSurface should have left render target unmodified";
795 } else {
796 msg = "SkSurface should have cleared the render target";
797 }
798 ERRORF(reporter,
799 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
800 expectedValue, x, y);
801 return;
802 }
803 }
804 }
805}
806
bsalomon758586c2016-04-06 14:02:39 -0700807DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400808 auto context = ctxInfo.directContext();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400809 // Snaps an image from a surface and then makes a GrSurfaceContext from the image's texture.
810 auto makeImageSurfaceContext = [context](SkSurface* surface) {
811 sk_sp<SkImage> i(surface->makeImageSnapshot());
812 SkImage_Gpu* gpuImage = (SkImage_Gpu*)as_IB(i);
Greg Daniel37c127f2020-02-05 10:37:27 -0500813 return GrSurfaceContext::Make(context, *gpuImage->view(context),
Greg Danielbfa19c42019-12-19 16:41:40 -0500814 SkColorTypeToGrColorType(i->colorType()), kPremul_SkAlphaType,
815 gpuImage->refColorSpace());
bsalomone63ffef2016-02-05 07:17:34 -0800816 };
ericrkc4025182016-05-04 12:01:58 -0700817
Brian Salomonbf6b9792019-08-21 09:38:10 -0400818 // Test that non-wrapped RTs are created clear.
819 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
820 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
821 if (!surface) {
822 ERRORF(reporter, "Could not create GPU SkSurface.");
823 return;
bsalomone63ffef2016-02-05 07:17:34 -0800824 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400825 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
826 if (!rtc) {
827 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
828 return;
ericrkc4025182016-05-04 12:01:58 -0700829 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400830 test_surface_context_clear(reporter, rtc, 0x0);
831 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
832 test_surface_context_clear(reporter, imageSurfaceCtx.get(), 0x0);
833 }
834
835 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
836 const SkColor4f kOrigColor{.67f, .67f, .67f, 1};
837 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
838 &create_gpu_surface_backend_texture_as_render_target}) {
839 GrBackendTexture backendTex;
840 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
841 if (!surface) {
842 ERRORF(reporter, "Could not create GPU SkSurface.");
843 return;
844 }
845 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
846 if (!rtc) {
847 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
848 return;
849 }
850 test_surface_context_clear(reporter, rtc, kOrigColor.toSkColor());
851 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
852 test_surface_context_clear(reporter, imageSurfaceCtx.get(), kOrigColor.toSkColor());
853 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700854 }
855}
bsalomone63ffef2016-02-05 07:17:34 -0800856
ericrkc4025182016-05-04 12:01:58 -0700857static void test_surface_draw_partially(
Robert Phillips66944402019-09-30 13:21:25 -0400858 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
ericrkc4025182016-05-04 12:01:58 -0700859 const int kW = surface->width();
860 const int kH = surface->height();
861 SkPaint paint;
862 const SkColor kRectColor = ~origColor | 0xFF000000;
863 paint.setColor(kRectColor);
Robert Phillipsd3442842019-08-02 12:26:22 -0400864 surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint);
865
ericrkc4025182016-05-04 12:01:58 -0700866 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
867 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsd3442842019-08-02 12:26:22 -0400868
869 SkAutoPixmapStorage readback;
870 readback.alloc(readInfo);
871
872 readback.erase(~origColor);
Stephen Whitedbb3e1d2020-05-13 17:55:18 -0400873 REPORTER_ASSERT(reporter, surface->readPixels(readback.info(), readback.writable_addr(),
874 readback.rowBytes(), 0, 0));
ericrkc4025182016-05-04 12:01:58 -0700875 bool stop = false;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400876
Robert Phillips66944402019-09-30 13:21:25 -0400877 SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
878 SkColorGetR(origColor),
879 SkColorGetG(origColor),
880 SkColorGetB(origColor));
881 SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
882 SkColorGetR(kRectColor),
883 SkColorGetG(kRectColor),
884 SkColorGetB(kRectColor));
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400885
ericrkc4025182016-05-04 12:01:58 -0700886 for (int y = 0; y < kH/2 && !stop; ++y) {
887 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400888 REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]);
889 if (rectColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400890 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400891 readback.addr32()[x + y * kW], rectColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700892 stop = true;
893 }
894 }
895 }
896 stop = false;
897 for (int y = kH/2; y < kH && !stop; ++y) {
898 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400899 REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]);
900 if (origColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400901 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400902 readback.addr32()[x + y * kW], origColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700903 stop = true;
904 }
905 }
906 }
907}
bsalomone63ffef2016-02-05 07:17:34 -0800908
egdanielab527a52016-06-28 08:07:26 -0700909DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400910 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400911
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400912 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
bsalomone63ffef2016-02-05 07:17:34 -0800913
ericrkc4025182016-05-04 12:01:58 -0700914 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
915 &create_gpu_surface_backend_texture_as_render_target}) {
916 // Validate that we can draw to the canvas and that the original texture color is
917 // preserved in pixels that aren't rendered to via the surface.
918 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500919 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400920 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
Stephen Whitefdba6c82020-05-26 17:00:32 -0400921 const GrCaps* caps = context->priv().caps();
922 if (!supports_readpixels(caps, surface.get())) {
923 continue;
924 }
ericrkc4025182016-05-04 12:01:58 -0700925 if (surface) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400926 test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700927 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400928 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700929 }
930 }
931}
932
Greg Daniel8ce79912019-02-05 10:08:43 -0500933struct ReleaseChecker {
934 ReleaseChecker() : fReleaseCount(0) {}
935 int fReleaseCount;
936 static void Release(void* self) {
937 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
938 }
939};
940
941
942DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
943 const int kWidth = 10;
944 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500945
Robert Phillips6d344c32020-07-06 10:56:46 -0400946 auto ctx = ctxInfo.directContext();
Greg Daniel8ce79912019-02-05 10:08:43 -0500947 GrGpu* gpu = ctx->priv().getGpu();
948
949 for (bool useTexture : {false, true}) {
950 GrBackendTexture backendTex;
951 GrBackendRenderTarget backendRT;
952 sk_sp<SkSurface> surface;
953
954 ReleaseChecker releaseChecker;
955 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
956
957 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400958 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
959 kPremul_SkAlphaType);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400960 if (!CreateBackendTexture(ctx, &backendTex, ii, SkColors::kRed, GrMipmapped::kNo,
Brian Salomon28a8f282019-10-24 20:07:39 -0400961 GrRenderable::kYes)) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500962 continue;
963 }
964
965 surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1,
966 kRGBA_8888_SkColorType,
967 nullptr, nullptr,
968 ReleaseChecker::Release,
969 &releaseChecker);
970 } else {
971 backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight,
972 GrColorType::kRGBA_8888);
973 if (!backendRT.isValid()) {
974 continue;
975 }
976 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
977 kRGBA_8888_SkColorType,
978 nullptr, nullptr,
979 ReleaseChecker::Release,
980 &releaseChecker);
981 }
982 if (!surface) {
983 ERRORF(reporter, "Failed to create surface");
984 continue;
985 }
986
987 surface->getCanvas()->clear(SK_ColorRED);
Greg Danielce9f0162020-06-30 13:42:46 -0400988 surface->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400989 ctx->submit(true);
Greg Daniel8ce79912019-02-05 10:08:43 -0500990
991 // Now exercise the release proc
992 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
993 surface.reset(nullptr); // force a release of the surface
994 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
995
996 if (useTexture) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400997 DeleteBackendTexture(ctx, backendTex);
Greg Daniel8ce79912019-02-05 10:08:43 -0500998 } else {
999 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
1000 }
1001 }
1002}
ericrkc4025182016-05-04 12:01:58 -07001003
1004DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001005 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -04001006 const GrCaps* caps = context->priv().caps();
1007
1008 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -07001009 return;
1010 }
Robert Phillips9b16f812019-05-17 10:01:21 -04001011
Robert Phillips4d87b2b2019-07-23 13:44:16 -04001012 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
ericrkc4025182016-05-04 12:01:58 -07001013
Robert Phillips9b16f812019-05-17 10:01:21 -04001014 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001015
ericrkc4025182016-05-04 12:01:58 -07001016 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
1017 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001018 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001019 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -04001020 auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -07001021
Brian Salomonbdecacf2018-02-02 20:32:49 -05001022 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001023 // Certain platforms don't support MSAA, skip these.
1024 continue;
ericrkc4025182016-05-04 12:01:58 -07001025 }
1026
1027 // Validate that we can attach a stencil buffer to an SkSurface created by either of
1028 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -04001029 GrRenderTarget* rt = surface->getCanvas()
1030 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Chris Daltoneffee202019-07-01 22:28:03 -06001031 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt));
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001032 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -07001033 }
bsalomone63ffef2016-02-05 07:17:34 -08001034 }
1035}
brianosman0e22eb82016-08-30 07:07:59 -07001036
Brian Salomonaad83152019-05-24 10:16:35 -04001037DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001038 auto context = ctxInfo.directContext();
Brian Salomonaad83152019-05-24 10:16:35 -04001039
1040 for (int sampleCnt : {1, 2}) {
1041 GrBackendTexture backendTexture1;
1042 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Brian Salomon28a8f282019-10-24 20:07:39 -04001043 if (!CreateBackendTexture(context, &backendTexture1, ii, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001044 GrMipmapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001045 continue;
1046 }
1047 SkScopeExit delete1(
Brian Salomon28a8f282019-10-24 20:07:39 -04001048 [context, &backendTexture1] { DeleteBackendTexture(context, backendTexture1); });
Brian Salomonaad83152019-05-24 10:16:35 -04001049 GrBackendTexture backendTexture2;
Brian Salomon28a8f282019-10-24 20:07:39 -04001050 if (!CreateBackendTexture(context, &backendTexture2, ii, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001051 GrMipmapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001052 ERRORF(reporter, "Expected to be able to make second texture");
1053 continue;
1054 }
1055 SkScopeExit delete2(
Brian Salomon28a8f282019-10-24 20:07:39 -04001056 [context, &backendTexture2] { DeleteBackendTexture(context, backendTexture2); });
Brian Salomonaad83152019-05-24 10:16:35 -04001057 auto ii2 = ii.makeWH(8, 8);
1058 GrBackendTexture backendTexture3;
Brian Salomon28a8f282019-10-24 20:07:39 -04001059 if (!CreateBackendTexture(context, &backendTexture3, ii2, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001060 GrMipmapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001061 ERRORF(reporter, "Couldn't create different sized texture.");
1062 continue;
1063 }
1064 SkScopeExit delete3(
Brian Salomon28a8f282019-10-24 20:07:39 -04001065 [context, &backendTexture3] { DeleteBackendTexture(context, backendTexture3); });
Brian Salomonaad83152019-05-24 10:16:35 -04001066
1067 auto surf = SkSurface::MakeFromBackendTexture(
1068 context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt,
1069 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1070 if (!surf) {
1071 continue;
1072 }
1073 surf->getCanvas()->clear(SK_ColorBLUE);
1074 // Change matrix, layer, and clip state before swapping out the backing texture.
1075 surf->getCanvas()->translate(5, 5);
1076 surf->getCanvas()->saveLayer(nullptr, nullptr);
1077 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1078 // switch origin while we're at it.
1079 bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin);
1080 REPORTER_ASSERT(reporter, replaced);
1081 SkPaint paint;
1082 paint.setColor(SK_ColorRED);
1083 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1084 surf->getCanvas()->restore();
1085
1086 // Check that the replacement texture got the right color values.
1087 SkAutoPixmapStorage pm;
1088 pm.alloc(ii);
1089 bool bad = !surf->readPixels(pm, 0, 0);
1090 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1091 for (int y = 0; y < ii.height() && !bad; ++y) {
1092 for (int x = 0; x < ii.width() && !bad; ++x) {
1093 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1094 auto found = *pm.addr32(x, y);
1095 if (found != expected) {
1096 bad = true;
1097 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1098 expected, found, x, y);
1099 }
1100 }
1101 }
1102 // The original texture should still be all blue.
1103 surf = SkSurface::MakeFromBackendTexture(
1104 context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt,
1105 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1106 if (!surf) {
1107 ERRORF(reporter, "Could not create second surface.");
1108 continue;
1109 }
1110 bad = !surf->readPixels(pm, 0, 0);
1111 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1112 for (int y = 0; y < ii.height() && !bad; ++y) {
1113 for (int x = 0; x < ii.width() && !bad; ++x) {
1114 auto expected = 0xFFFF0000;
1115 auto found = *pm.addr32(x, y);
1116 if (found != expected) {
1117 bad = true;
1118 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1119 expected, found, x, y);
1120 }
1121 }
1122 }
1123
1124 // Can't replace with the same texture
1125 REPORTER_ASSERT(reporter,
1126 !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin));
1127 // Can't replace with invalid texture
1128 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1129 // Can't replace with different size texture.
1130 REPORTER_ASSERT(reporter,
1131 !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin));
1132 // Can't replace texture of non-wrapped SkSurface.
1133 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1134 REPORTER_ASSERT(reporter, surf);
1135 if (surf) {
1136 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1,
1137 kTopLeft_GrSurfaceOrigin));
1138 }
1139 }
1140}
1141
Matt Sarett22886c42016-11-22 11:31:41 -05001142static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001143 SkOverdrawCanvas canvas(surface->getCanvas());
1144 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001145 sk_sp<SkImage> image = surface->makeImageSnapshot();
1146
1147 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001148 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001149 for (int y = 0; y < 10; y++) {
1150 for (int x = 0; x < 10; x++) {
1151 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1152 }
1153 }
1154}
1155
1156DEF_TEST(OverdrawSurface_Raster, r) {
1157 sk_sp<SkSurface> surface = create_surface();
1158 test_overdraw_surface(r, surface.get());
1159}
1160
Matt Sarett22886c42016-11-22 11:31:41 -05001161DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001162 auto context = ctxInfo.directContext();
Matt Sarett22886c42016-11-22 11:31:41 -05001163 sk_sp<SkSurface> surface = create_gpu_surface(context);
1164 test_overdraw_surface(r, surface.get());
1165}
Mike Reed44d04bd2017-06-28 19:57:21 -04001166
1167DEF_TEST(Surface_null, r) {
1168 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1169
1170 const int w = 37;
1171 const int h = 1000;
1172 auto surf = SkSurface::MakeNull(w, h);
1173 auto canvas = surf->getCanvas();
1174
1175 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1176 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1177}
Mike Reedd4746982018-02-07 16:05:29 -05001178
1179// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1180// (so the snapshot can succeed)
1181DEF_TEST(surface_image_unity, reporter) {
1182 auto do_test = [reporter](const SkImageInfo& info) {
1183 size_t rowBytes = info.minRowBytes();
1184 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1185 if (surf) {
1186 auto img = surf->makeImageSnapshot();
1187 if (!img && false) { // change to true to document the differences
1188 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001189 info.width(),
1190 info.height(),
1191 ToolUtils::colortype_name(info.colorType()),
1192 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001193 return;
1194 }
1195 REPORTER_ASSERT(reporter, img != nullptr);
1196
1197 char dummyPixel = 0; // just need a valid address (not a valid size)
1198 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1199 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1200 REPORTER_ASSERT(reporter, img != nullptr);
1201 }
1202 };
1203
Mike Kleine978ca22018-10-29 11:29:58 -04001204 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001205 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001206 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001207 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001208 SkAlphaType at = static_cast<SkAlphaType>(ati);
1209 for (int32_t size : sizes) {
1210 do_test(SkImageInfo::Make(1, size, ct, at));
1211 do_test(SkImageInfo::Make(size, 1, ct, at));
1212 }
1213 }
1214 }
1215}