blob: ba4b29f68e122298c8454498d561169cd3638696 [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"
Brian Salomon8f7d9532020-12-23 09:16:59 -050018#include "src/core/SkCanvasPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkDevice.h"
20#include "src/core/SkUtils.h"
Robert Phillipscc44feb2021-07-06 12:21:37 -040021#include "src/gpu/BaseDevice.h"
Adlai Hollera0693042020-10-14 11:23:11 -040022#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrGpu.h"
24#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040025#include "src/gpu/GrImageInfo.h"
Stephen Whitefdba6c82020-05-26 17:00:32 -040026#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceProvider.h"
Robert Phillipsf3868622021-08-04 13:27:43 -040028#include "src/gpu/SurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/image/SkImage_Base.h"
30#include "src/image/SkImage_Gpu.h"
31#include "src/image/SkSurface_Gpu.h"
32#include "tests/Test.h"
Brian Salomon72050802020-10-12 20:45:06 -040033#include "tools/ToolUtils.h"
Brian Salomon72c7b982020-10-06 10:07:38 -040034#include "tools/gpu/BackendSurfaceFactory.h"
Brian Salomon72050802020-10-12 20:45:06 -040035#include "tools/gpu/ManagedBackendTexture.h"
Brian Salomone6662542021-02-23 10:45:39 -050036#include "tools/gpu/ProxyUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000037
Hal Canary8a001442018-09-19 11:31:27 -040038#include <functional>
39#include <initializer_list>
40#include <vector>
41
kkinnunen179a8f52015-11-20 13:32:24 -080042static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070043 SkASSERT(pixels == context);
44 sk_free(pixels);
45}
reede8f30622016-03-23 18:59:25 -070046static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
47 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070048 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000049 if (requestedInfo) {
50 *requestedInfo = info;
51 }
reede8f30622016-03-23 18:59:25 -070052 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000053}
reede8f30622016-03-23 18:59:25 -070054static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
55 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080056 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
57 if (requestedInfo) {
58 *requestedInfo = info;
59 }
60 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040061 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070062 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
63 release_direct_surface_storage,
64 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080065}
Robert Phillipseffd13f2020-07-20 15:00:36 -040066static sk_sp<SkSurface> create_gpu_surface(GrRecordingContext* rContext,
67 SkAlphaType at = kPremul_SkAlphaType,
reede8f30622016-03-23 18:59:25 -070068 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080069 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
70 if (requestedInfo) {
71 *requestedInfo = info;
72 }
Robert Phillipseffd13f2020-07-20 15:00:36 -040073 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080074}
Robert Phillipseffd13f2020-07-20 15:00:36 -040075static sk_sp<SkSurface> create_gpu_scratch_surface(GrRecordingContext* rContext,
reede8f30622016-03-23 18:59:25 -070076 SkAlphaType at = kPremul_SkAlphaType,
77 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080078 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
79 if (requestedInfo) {
80 *requestedInfo = info;
81 }
Robert Phillipseffd13f2020-07-20 15:00:36 -040082 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080083}
junov@chromium.org995beb62013-03-28 13:49:22 +000084
kkinnunen179a8f52015-11-20 13:32:24 -080085DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080086 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070087 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
88 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080089
reedb2497c22014-12-31 12:31:43 -080090}
egdanielab527a52016-06-28 08:07:26 -070091DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080092 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
93 REPORTER_ASSERT(reporter, nullptr ==
Robert Phillips6d344c32020-07-06 10:56:46 -040094 SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080095}
reedb2497c22014-12-31 12:31:43 -080096
Brian Salomonbdecacf2018-02-02 20:32:49 -050097DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040098 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -040099
Brian Salomonbdecacf2018-02-02 20:32:49 -0500100 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
101 static constexpr int kSize = 10;
102
103 SkColorType colorType = static_cast<SkColorType>(ct);
104 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500105
Robert Phillips429f0d32019-09-11 17:03:28 -0400106 {
107 bool can = context->colorTypeSupportedAsSurface(colorType);
108 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
109 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
110 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500111
Brian Salomon72050802020-10-12 20:45:06 -0400112 surf = sk_gpu_test::MakeBackendTextureSurface(context,
113 {kSize, kSize},
114 kTopLeft_GrSurfaceOrigin,
115 /*sample cnt*/ 1,
116 colorType);
Robert Phillips429f0d32019-09-11 17:03:28 -0400117 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 }
120
Robert Phillips429f0d32019-09-11 17:03:28 -0400121 // The MSAA test only makes sense if the colorType is renderable to begin with.
122 if (context->colorTypeSupportedAsSurface(colorType)) {
123 static constexpr int kSampleCnt = 2;
124
125 bool can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
126 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt,
127 nullptr);
128 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
129 colorType, can, SkToBool(surf));
130
Brian Salomon72050802020-10-12 20:45:06 -0400131 surf = sk_gpu_test::MakeBackendTextureSurface(
132 context, {kSize, kSize}, kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType);
Robert Phillips429f0d32019-09-11 17:03:28 -0400133 REPORTER_ASSERT(reporter, can == SkToBool(surf),
134 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
135 colorType);
136 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
137 if (surf) {
Robert Phillipsfa8af0a2021-06-03 11:58:43 -0400138 auto rtp = SkCanvasPriv::TopDeviceTargetProxy(surf->getCanvas());
139 int storedCnt = rtp->numSamples();
140 const GrBackendFormat& format = rtp->backendFormat();
Brian Salomon72050802020-10-12 20:45:06 -0400141 int allowedCnt =
142 context->priv().caps()->getRenderTargetSampleCount(storedCnt, format);
Robert Phillips429f0d32019-09-11 17:03:28 -0400143 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
144 "Should store an allowed sample count (%d vs %d)", allowedCnt,
145 storedCnt);
146 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500147 }
148
Brian Salomon72c7b982020-10-06 10:07:38 -0400149 for (int sampleCnt : {1, 2}) {
Brian Salomonf9b00422020-10-08 16:00:14 -0400150 auto surf = sk_gpu_test::MakeBackendRenderTargetSurface(context,
151 {16, 16},
152 kTopLeft_GrSurfaceOrigin,
153 sampleCnt,
154 colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -0400155 bool can = context->colorTypeSupportedAsSurface(colorType) &&
156 context->maxSurfaceSampleCountForColorType(colorType) >= sampleCnt;
157 if (!surf && can && colorType == kBGRA_8888_SkColorType && sampleCnt > 1 &&
158 context->backend() == GrBackendApi::kOpenGL) {
159 // This is an execeptional case. On iOS GLES we support MSAA BGRA for internally-
160 // created render targets by using a MSAA RGBA8 renderbuffer that resolves to a
161 // BGRA8 texture. However, the GL_APPLE_texture_format_BGRA8888 extension does not
162 // allow creation of BGRA8 renderbuffers and we don't support multisampled textures.
163 // So this is expected to fail. As of 10/5/2020 it actually seems to work to create
164 // a MSAA BGRA8 renderbuffer (at least in the simulator) but we don't want to rely
165 // on this undocumented behavior.
166 continue;
167 }
168 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, sc: %d, can: %d, surf: %d",
169 colorType, sampleCnt, can, SkToBool(surf));
170 if (surf) {
Robert Phillipsfa8af0a2021-06-03 11:58:43 -0400171 auto rtp = SkCanvasPriv::TopDeviceTargetProxy(surf->getCanvas());
172 int storedCnt = rtp->numSamples();
173 const GrBackendFormat& backendFormat = rtp->backendFormat();
Brian Salomon72c7b982020-10-06 10:07:38 -0400174 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(storedCnt,
175 backendFormat);
176 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
177 "Should store an allowed sample count (%d vs %d)", allowedCnt,
178 storedCnt);
Robert Phillips429f0d32019-09-11 17:03:28 -0400179 }
Brian Salomon93348dd2018-08-29 12:56:23 -0400180 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500181 }
182}
183
184DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400185 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400186
187 static constexpr int kSize = 10;
188
Brian Salomonbdecacf2018-02-02 20:32:49 -0500189 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500190
191 SkColorType colorType = static_cast<SkColorType>(ct);
Brian Salomon72050802020-10-12 20:45:06 -0400192 int maxSampleCnt = context->maxSurfaceSampleCountForColorType(colorType);
193 if (!maxSampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500194 continue;
195 }
Robert Phillips429f0d32019-09-11 17:03:28 -0400196 if (!context->colorTypeSupportedAsSurface(colorType)) {
197 continue;
198 }
199
Brian Salomonbdecacf2018-02-02 20:32:49 -0500200 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomon72050802020-10-12 20:45:06 -0400201 auto surf = sk_gpu_test::MakeBackendTextureSurface(
202 context, info, kTopLeft_GrSurfaceOrigin, maxSampleCnt);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500203 if (!surf) {
Brian Salomon72050802020-10-12 20:45:06 -0400204 ERRORF(reporter, "Could not make surface of color type %d.", colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500205 continue;
206 }
Robert Phillips5ccb4c12021-05-24 15:35:08 -0400207 int sampleCnt =
208 ((SkSurface_Gpu*)(surf.get()))->getDevice()->targetProxy()->numSamples();
Brian Salomon72050802020-10-12 20:45:06 -0400209 REPORTER_ASSERT(reporter, sampleCnt == maxSampleCnt, "Exected: %d, actual: %d",
210 maxSampleCnt, sampleCnt);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500211 }
212}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500213
kkinnunen179a8f52015-11-20 13:32:24 -0800214static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700215 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800216 const SkImageInfo& requestInfo,
217 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000218 const SkColor color = SK_ColorRED;
219 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800220 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000221
reed6ceeebd2016-03-09 14:26:26 -0800222 SkPixmap pmap;
223 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800224 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000225
reed6ceeebd2016-03-09 14:26:26 -0800226 SkPixmap pmap2;
227 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000228
kkinnunen179a8f52015-11-20 13:32:24 -0800229 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800230 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
231 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
232 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000233
reed6ceeebd2016-03-09 14:26:26 -0800234 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
235 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
236 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800237 } else {
238 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000239 }
240}
kkinnunen179a8f52015-11-20 13:32:24 -0800241DEF_TEST(SurfaceCanvasPeek, reporter) {
242 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
243 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700244 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800245 test_canvas_peek(reporter, surface, requestInfo, true);
246 }
247}
egdanielab527a52016-06-28 08:07:26 -0700248DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800249 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
250 SkImageInfo requestInfo;
Robert Phillips6d344c32020-07-06 10:56:46 -0400251 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800252 test_canvas_peek(reporter, surface, requestInfo, false);
253 }
254}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000255
reede8f30622016-03-23 18:59:25 -0700256static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700257 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800258 REPORTER_ASSERT(reporter, surface);
259 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700260 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800261 REPORTER_ASSERT(reporter, image);
262 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700263 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700264 }
265 }
266}
kkinnunen179a8f52015-11-20 13:32:24 -0800267DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
268 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700269 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
270 auto surface(surface_func(at, nullptr));
271 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700272 }
273 }
274}
egdanielab527a52016-06-28 08:07:26 -0700275DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800276 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700277 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
278 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400279 auto surface(surface_func(ctxInfo.directContext(), at, nullptr));
brianosman69c166d2016-08-17 14:01:05 -0700280 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800281 }
282 }
283}
bsalomon74f681d2015-06-23 14:38:48 -0700284
Robert Phillips8caf85f2018-04-05 09:30:38 -0400285static void test_backend_texture_access_copy_on_write(
286 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
287 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700288 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700289
Robert Phillips8caf85f2018-04-05 09:30:38 -0400290 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700291 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700292
293 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400294 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700295}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400296
297static void test_backend_rendertarget_access_copy_on_write(
298 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
299 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
300 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
301
302 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
303 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
304
305 // If the access mode triggers CoW, then the backend objects should reflect it.
306 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
307 (snap1 == snap2));
308}
309
310DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800311 const SkSurface::BackendHandleAccess accessModes[] = {
312 SkSurface::kFlushRead_BackendHandleAccess,
313 SkSurface::kFlushWrite_BackendHandleAccess,
314 SkSurface::kDiscardWrite_BackendHandleAccess,
315 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400316
kkinnunen179a8f52015-11-20 13:32:24 -0800317 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400318 for (auto& accessMode : accessModes) {
319 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400320 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400321 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
322 }
323 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400324 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400325 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800326 }
327 }
328 }
329}
kkinnunen179a8f52015-11-20 13:32:24 -0800330
Robert Phillips8caf85f2018-04-05 09:30:38 -0400331template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
332static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700333 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400334
335 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
336 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700337 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800338 // just read access should not affect the snapshot
339 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
340
Robert Phillips8caf85f2018-04-05 09:30:38 -0400341 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
342 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700343 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800344 // expect a new image, since we claimed we would write
345 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
346
Robert Phillips8caf85f2018-04-05 09:30:38 -0400347 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
348 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700349 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800350 // expect a new(er) image, since we claimed we would write
351 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
352 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
353}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400354
kkinnunen179a8f52015-11-20 13:32:24 -0800355// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700356DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800357 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400358 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400359 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400360 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
361 surface.get());
362 }
363 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400364 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400365 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
366 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800367 }
368 }
369}
kkinnunen179a8f52015-11-20 13:32:24 -0800370
Greg Daniel9b6e30b2021-03-10 12:45:36 -0500371// No CPU test.
372DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAbandonPostFlush_Gpu, reporter, ctxInfo) {
373 auto direct = ctxInfo.directContext();
374 sk_sp<SkSurface> surface = create_gpu_surface(direct, kPremul_SkAlphaType, nullptr);
375 if (!surface) {
376 return;
377 }
378 // This flush can put command buffer refs on the GrGpuResource for the surface.
379 surface->flush();
380 direct->abandonContext();
381 // We pass the test if we don't hit any asserts or crashes when the ref on the surface goes away
382 // after we abanonded the context. One thing specifically this checks is to make sure we're
383 // correctly handling the mix of normal refs and command buffer refs, and correctly deleting
384 // the object at the right time.
385}
386
Greg Daniel80ef70e2021-03-10 16:17:21 -0500387DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendAccessAbandoned_Gpu, reporter, ctxInfo) {
388 auto dContext = ctxInfo.directContext();
389 sk_sp<SkSurface> surface = create_gpu_surface(dContext, kPremul_SkAlphaType, nullptr);
390 if (!surface) {
391 return;
392 }
393
394 GrBackendRenderTarget beRT =
395 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
396 REPORTER_ASSERT(reporter, beRT.isValid());
397 GrBackendTexture beTex =
398 surface->getBackendTexture(SkSurface::kFlushRead_BackendHandleAccess);
399 REPORTER_ASSERT(reporter, beTex.isValid());
400
401 surface->flush();
402 dContext->abandonContext();
403
404 // After abandoning the context none of the backend surfaces should be valid.
405 beRT = surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
406 REPORTER_ASSERT(reporter, !beRT.isValid());
407 beTex = surface->getBackendTexture(SkSurface::kFlushRead_BackendHandleAccess);
408 REPORTER_ASSERT(reporter, !beTex.isValid());
409}
410
kkinnunen179a8f52015-11-20 13:32:24 -0800411// Verify that the right canvas commands trigger a copy on write.
412static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000413 SkCanvas* canvas = surface->getCanvas();
414
415 const SkRect testRect =
416 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
417 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000418 SkPath testPath;
419 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
420 SkIntToScalar(2), SkIntToScalar(1)));
421
422 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
423
424 SkRegion testRegion;
425 testRegion.setRect(testIRect);
426
427
428 const SkColor testColor = 0x01020304;
429 const SkPaint testPaint;
430 const SkPoint testPoints[3] = {
431 {SkIntToScalar(0), SkIntToScalar(0)},
432 {SkIntToScalar(2), SkIntToScalar(1)},
433 {SkIntToScalar(0), SkIntToScalar(2)}
434 };
435 const size_t testPointCount = 3;
436
437 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000438 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000439 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000440
441 SkRRect testRRect;
442 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
443
444 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000445
446#define EXPECT_COPY_ON_WRITE(command) \
447 { \
reed9ce9d672016-03-17 10:51:11 -0700448 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
John Stiles31954bf2020-08-07 17:35:54 -0400449 sk_sp<SkImage> aur_before(imageBefore); /*NOLINT*/ \
junov@chromium.org995beb62013-03-28 13:49:22 +0000450 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700451 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
John Stiles31954bf2020-08-07 17:35:54 -0400452 sk_sp<SkImage> aur_after(imageAfter); /*NOLINT*/ \
junov@chromium.org995beb62013-03-28 13:49:22 +0000453 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
454 }
455
456 EXPECT_COPY_ON_WRITE(clear(testColor))
457 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
458 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
459 testPaint))
460 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
461 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
462 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
463 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
Mike Reed34a0c972021-01-25 17:49:32 -0500464 EXPECT_COPY_ON_WRITE(drawImage(testBitmap.asImage(), 0, 0))
465 EXPECT_COPY_ON_WRITE(drawImageRect(testBitmap.asImage(), testRect, SkSamplingOptions()))
Hal Canary89a644b2019-01-07 09:36:09 -0500466 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800467}
468DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700469 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800470}
egdanielab527a52016-06-28 08:07:26 -0700471DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800472 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400473 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700474 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700475 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000476}
477
kkinnunen179a8f52015-11-20 13:32:24 -0800478static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
479 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000480 // This test succeeds by not triggering an assertion.
481 // The test verifies that the surface remains writable (usable) after
482 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000483 SkCanvas* canvas = surface->getCanvas();
484 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700485 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000486 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000487}
kkinnunen179a8f52015-11-20 13:32:24 -0800488DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700489 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800490}
egdanielab527a52016-06-28 08:07:26 -0700491DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800492 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400493 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700494 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800495 }
496}
junov@chromium.orgda904742013-05-01 22:38:16 +0000497
kkinnunen179a8f52015-11-20 13:32:24 -0800498static void test_crbug263329(skiatest::Reporter* reporter,
499 SkSurface* surface1,
500 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000501 // This is a regression test for crbug.com/263329
502 // Bug was caused by onCopyOnWrite releasing the old surface texture
503 // back to the scratch texture pool even though the texture is used
504 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000505 SkCanvas* canvas1 = surface1->getCanvas();
506 SkCanvas* canvas2 = surface2->getCanvas();
507 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700508 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000509 // Trigger copy on write, new backing is a scratch texture
510 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700511 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000512 // Trigger copy on write, old backing should not be returned to scratch
513 // pool because it is held by image2
514 canvas1->clear(3);
515
516 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700517 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000518 // Trigger copy on write on surface2. The new backing store should not
519 // be recycling a texture that is held by an existing image.
520 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700521 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Greg Daniel7c902112020-03-06 13:07:10 -0500522
Brian Salomone6662542021-02-23 10:45:39 -0500523 auto imageProxy = [ctx = surface1->recordingContext()](SkImage* img) {
524 GrTextureProxy* proxy = sk_gpu_test::GetTextureImageProxy(img, ctx);
525 SkASSERT(proxy);
526 return proxy;
527 };
Greg Daniel7c902112020-03-06 13:07:10 -0500528
Brian Salomone6662542021-02-23 10:45:39 -0500529 REPORTER_ASSERT(reporter, imageProxy(image4.get()) != imageProxy(image3.get()));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000530 // The following assertion checks crbug.com/263329
Brian Salomone6662542021-02-23 10:45:39 -0500531 REPORTER_ASSERT(reporter, imageProxy(image4.get()) != imageProxy(image2.get()));
532 REPORTER_ASSERT(reporter, imageProxy(image4.get()) != imageProxy(image1.get()));
533 REPORTER_ASSERT(reporter, imageProxy(image3.get()) != imageProxy(image2.get()));
534 REPORTER_ASSERT(reporter, imageProxy(image3.get()) != imageProxy(image1.get()));
535 REPORTER_ASSERT(reporter, imageProxy(image2.get()) != imageProxy(image1.get()));
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000536}
egdanielab527a52016-06-28 08:07:26 -0700537DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800538 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400539 auto surface1(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
540 auto surface2(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700541 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800542 }
543}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000544
kkinnunen179a8f52015-11-20 13:32:24 -0800545DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700546 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700547 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500548 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800549 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500550 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800551}
egdanielab527a52016-06-28 08:07:26 -0700552DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800553 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400554 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700555 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500556
557 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400558 GrBackendTexture backendTex = image->getBackendTexture(false);
559 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800560 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500561 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400562 GrBackendTexture backendTex2 = image->getBackendTexture(false);
563 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000564 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000565}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800566
reede8f30622016-03-23 18:59:25 -0700567static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
568 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500569
Robert Phillips5ccb4c12021-05-24 15:35:08 -0400570 GrRenderTargetProxy* proxy = gsurf->getDevice()->targetProxy();
Robert Phillips6de99042017-01-31 11:31:39 -0500571 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800572}
573
Brian Salomone6662542021-02-23 10:45:39 -0500574static SkBudgeted is_budgeted(SkImage* image, GrRecordingContext* rc) {
575 return sk_gpu_test::GetTextureImageProxy(image, rc)->isBudgeted();
reed9ce9d672016-03-17 10:51:11 -0700576}
577
egdanielab527a52016-06-28 08:07:26 -0700578DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800579 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Brian Salomone6662542021-02-23 10:45:39 -0500580 GrDirectContext* dContext = ctxInfo.directContext();
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400581 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
Brian Salomone6662542021-02-23 10:45:39 -0500582 auto surface(SkSurface::MakeRenderTarget(dContext, budgeted, info));
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400583 SkASSERT(surface);
584 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800585
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400586 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800587
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400588 // Initially the image shares a texture with the surface, and the
589 // the budgets should always match.
590 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
Brian Salomone6662542021-02-23 10:45:39 -0500591 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image.get(), dContext));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800592
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400593 // Now trigger copy-on-write
594 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800595
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400596 // They don't share a texture anymore but the budgets should still match.
597 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
Brian Salomone6662542021-02-23 10:45:39 -0500598 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image.get(), dContext));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800599 }
600}
junov@chromium.orgda904742013-05-01 22:38:16 +0000601
kkinnunen179a8f52015-11-20 13:32:24 -0800602static void test_no_canvas1(skiatest::Reporter* reporter,
603 SkSurface* surface,
604 SkSurface::ContentChangeMode mode) {
605 // Test passes by not asserting
606 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800607}
608static void test_no_canvas2(skiatest::Reporter* reporter,
609 SkSurface* surface,
610 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000611 // Verifies the robustness of SkSurface for handling use cases where calls
612 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700613 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
John Stiles31954bf2020-08-07 17:35:54 -0400614 sk_sp<SkImage> aur_image1(image1); // NOLINT(performance-unnecessary-copy-initialization)
kkinnunen179a8f52015-11-20 13:32:24 -0800615 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700616 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
John Stiles31954bf2020-08-07 17:35:54 -0400617 sk_sp<SkImage> aur_image2(image2); // NOLINT(performance-unnecessary-copy-initialization)
kkinnunen179a8f52015-11-20 13:32:24 -0800618 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000619}
kkinnunen179a8f52015-11-20 13:32:24 -0800620DEF_TEST(SurfaceNoCanvas, reporter) {
621 SkSurface::ContentChangeMode modes[] =
622 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
623 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
624 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700625 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800626 }
627 }
628}
egdanielab527a52016-06-28 08:07:26 -0700629DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800630 SkSurface::ContentChangeMode modes[] =
631 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
632 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
633 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
634 for (auto& mode : modes) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400635 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700636 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700637 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000638 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000639 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000640}
reed9cd016e2016-01-30 10:01:06 -0800641
642static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800643 SkPixmap surfacePM;
644 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800645
reed9ce9d672016-03-17 10:51:11 -0700646 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800647 SkPixmap pm;
648 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800649
reed6ceeebd2016-03-09 14:26:26 -0800650 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800651
652 // trigger a copy-on-write
653 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700654 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800655 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
656
reed6ceeebd2016-03-09 14:26:26 -0800657 SkPixmap pm2;
658 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
659 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800660}
661
662DEF_TEST(surface_rowbytes, reporter) {
663 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
664
reede8f30622016-03-23 18:59:25 -0700665 auto surf0(SkSurface::MakeRaster(info));
666 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800667
668 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700669 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
670 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800671
672 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700673 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800674 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400675 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800676 REPORTER_ASSERT(reporter, nullptr == s);
677}
bsalomone63ffef2016-02-05 07:17:34 -0800678
fmalita03912f12016-07-06 06:22:06 -0700679DEF_TEST(surface_raster_zeroinitialized, reporter) {
680 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
681 SkPixmap pixmap;
682 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
683
684 for (int i = 0; i < pixmap.info().width(); ++i) {
685 for (int j = 0; j < pixmap.info().height(); ++j) {
686 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
687 }
688 }
689}
690
Robert Phillipseffd13f2020-07-20 15:00:36 -0400691static sk_sp<SkSurface> create_gpu_surface_backend_texture(GrDirectContext* dContext,
692 int sampleCnt,
Brian Salomon72050802020-10-12 20:45:06 -0400693 const SkColor4f& color) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500694 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
695 // triggers what appears to be a driver race condition where the 10x10 surface from the
696 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
697 // test.
698 //
699 // Immediately after creation of this surface, readback shows the correct initial solid color.
700 // However, sometime before content is rendered into the upper half of the surface, the driver
701 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
702 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
703 // untouched bottom half contains random pixel values that trigger asserts in the
704 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
705 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
706 //
707 // Requesting a much larger backend texture size seems to prevent it from reusing the same
708 // memory and avoids the issue.
709#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700710 const int kWidth = 10;
711 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500712#else
713 const int kWidth = 100;
714 const int kHeight = 100;
715#endif
716
Brian Salomon72050802020-10-12 20:45:06 -0400717 auto surf = sk_gpu_test::MakeBackendTextureSurface(dContext,
718 {kWidth, kHeight},
719 kTopLeft_GrSurfaceOrigin,
720 sampleCnt,
721 kRGBA_8888_SkColorType);
722 if (!surf) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500723 return nullptr;
724 }
Brian Salomon72050802020-10-12 20:45:06 -0400725 surf->getCanvas()->clear(color);
726 return surf;
ericrkc4025182016-05-04 12:01:58 -0700727}
bsalomone63ffef2016-02-05 07:17:34 -0800728
Stephen Whitefdba6c82020-05-26 17:00:32 -0400729static bool supports_readpixels(const GrCaps* caps, SkSurface* surface) {
730 auto surfaceGpu = static_cast<SkSurface_Gpu*>(surface);
Robert Phillips5ccb4c12021-05-24 15:35:08 -0400731 GrRenderTarget* rt = surfaceGpu->getDevice()->targetProxy()->peekRenderTarget();
Stephen Whitefdba6c82020-05-26 17:00:32 -0400732 if (!rt) {
733 return false;
734 }
735 return caps->surfaceSupportsReadPixels(rt) == GrCaps::SurfaceReadPixelsSupport::kSupported;
736}
737
Brian Salomon72c7b982020-10-06 10:07:38 -0400738static sk_sp<SkSurface> create_gpu_surface_backend_render_target(GrDirectContext* dContext,
739 int sampleCnt,
Brian Salomon72050802020-10-12 20:45:06 -0400740 const SkColor4f& color) {
ericrkc4025182016-05-04 12:01:58 -0700741 const int kWidth = 10;
742 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000743
Brian Salomonf9b00422020-10-08 16:00:14 -0400744 auto surf = sk_gpu_test::MakeBackendRenderTargetSurface(dContext,
745 {kWidth, kHeight},
746 kTopLeft_GrSurfaceOrigin,
747 sampleCnt,
Brian Salomon72050802020-10-12 20:45:06 -0400748 kRGBA_8888_SkColorType);
Brian Salomon72c7b982020-10-06 10:07:38 -0400749 if (!surf) {
ericrkc4025182016-05-04 12:01:58 -0700750 return nullptr;
751 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400752 surf->getCanvas()->clear(color);
753 return surf;
ericrkc4025182016-05-04 12:01:58 -0700754}
755
Brian Salomonbf6b9792019-08-21 09:38:10 -0400756static void test_surface_context_clear(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400757 GrDirectContext* dContext,
Robert Phillips53eaa642021-08-10 13:49:51 -0400758 skgpu::SurfaceContext* surfaceContext,
Robert Phillips04f22ea2021-07-22 15:11:03 -0400759 uint32_t expectedValue) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400760 int w = surfaceContext->width();
761 int h = surfaceContext->height();
Robert Phillipsd3442842019-08-02 12:26:22 -0400762
763 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
764
765 SkAutoPixmapStorage readback;
766 readback.alloc(ii);
bsalomone63ffef2016-02-05 07:17:34 -0800767
Robert Phillipsd3442842019-08-02 12:26:22 -0400768 readback.erase(~expectedValue);
Brian Salomondd4087d2020-12-23 20:36:44 -0500769 surfaceContext->readPixels(dContext, readback, {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800770 for (int y = 0; y < h; ++y) {
771 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400772 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -0800773 if (pixel != expectedValue) {
774 SkString msg;
775 if (expectedValue) {
776 msg = "SkSurface should have left render target unmodified";
777 } else {
778 msg = "SkSurface should have cleared the render target";
779 }
780 ERRORF(reporter,
781 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
782 expectedValue, x, y);
783 return;
784 }
785 }
786 }
787}
788
bsalomon758586c2016-04-06 14:02:39 -0700789DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400790 auto dContext = ctxInfo.directContext();
Robert Phillips53eaa642021-08-10 13:49:51 -0400791 // Snaps an image from a surface and then makes a SurfaceContext from the image's texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400792 auto makeImageSurfaceContext = [dContext](SkSurface* surface) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400793 sk_sp<SkImage> i(surface->makeImageSnapshot());
Brian Salomond0924f32021-02-03 10:15:31 -0500794 auto gpuImage = static_cast<SkImage_Gpu*>(as_IB(i));
795 auto [view, ct] = gpuImage->asView(dContext, GrMipmapped::kNo);
796 GrColorInfo colorInfo(ct, i->alphaType(), i->refColorSpace());
Robert Phillips33bf2b52021-08-02 11:14:38 -0400797 return dContext->priv().makeSC(view, std::move(colorInfo));
bsalomone63ffef2016-02-05 07:17:34 -0800798 };
ericrkc4025182016-05-04 12:01:58 -0700799
Brian Salomonbf6b9792019-08-21 09:38:10 -0400800 // Test that non-wrapped RTs are created clear.
801 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400802 auto surface = surface_func(dContext, kPremul_SkAlphaType, nullptr);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400803 if (!surface) {
804 ERRORF(reporter, "Could not create GPU SkSurface.");
805 return;
bsalomone63ffef2016-02-05 07:17:34 -0800806 }
Robert Phillips04f22ea2021-07-22 15:11:03 -0400807 auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(surface->getCanvas());
808 if (!sfc) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400809 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
810 return;
ericrkc4025182016-05-04 12:01:58 -0700811 }
Robert Phillips04f22ea2021-07-22 15:11:03 -0400812 test_surface_context_clear(reporter, dContext, sfc, 0x0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400813 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400814 test_surface_context_clear(reporter, dContext, imageSurfaceCtx.get(), 0x0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400815 }
816
817 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
818 const SkColor4f kOrigColor{.67f, .67f, .67f, 1};
Brian Salomon72c7b982020-10-06 10:07:38 -0400819 for (auto& surfaceFunc :
820 {&create_gpu_surface_backend_texture, &create_gpu_surface_backend_render_target}) {
Brian Salomon72050802020-10-12 20:45:06 -0400821 auto surface = surfaceFunc(dContext, 1, kOrigColor);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400822 if (!surface) {
823 ERRORF(reporter, "Could not create GPU SkSurface.");
824 return;
825 }
Robert Phillips04f22ea2021-07-22 15:11:03 -0400826 auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(surface->getCanvas());
827 if (!sfc) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400828 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
829 return;
830 }
Robert Phillips04f22ea2021-07-22 15:11:03 -0400831 test_surface_context_clear(reporter, dContext, sfc, kOrigColor.toSkColor());
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(),
834 kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700835 }
836}
bsalomone63ffef2016-02-05 07:17:34 -0800837
ericrkc4025182016-05-04 12:01:58 -0700838static void test_surface_draw_partially(
Robert Phillips66944402019-09-30 13:21:25 -0400839 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
ericrkc4025182016-05-04 12:01:58 -0700840 const int kW = surface->width();
841 const int kH = surface->height();
842 SkPaint paint;
843 const SkColor kRectColor = ~origColor | 0xFF000000;
844 paint.setColor(kRectColor);
Robert Phillipsd3442842019-08-02 12:26:22 -0400845 surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint);
846
ericrkc4025182016-05-04 12:01:58 -0700847 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
848 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsd3442842019-08-02 12:26:22 -0400849
850 SkAutoPixmapStorage readback;
851 readback.alloc(readInfo);
852
853 readback.erase(~origColor);
Stephen Whitedbb3e1d2020-05-13 17:55:18 -0400854 REPORTER_ASSERT(reporter, surface->readPixels(readback.info(), readback.writable_addr(),
855 readback.rowBytes(), 0, 0));
ericrkc4025182016-05-04 12:01:58 -0700856 bool stop = false;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400857
Robert Phillips66944402019-09-30 13:21:25 -0400858 SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
859 SkColorGetR(origColor),
860 SkColorGetG(origColor),
861 SkColorGetB(origColor));
862 SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
863 SkColorGetR(kRectColor),
864 SkColorGetG(kRectColor),
865 SkColorGetB(kRectColor));
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400866
ericrkc4025182016-05-04 12:01:58 -0700867 for (int y = 0; y < kH/2 && !stop; ++y) {
868 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400869 REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]);
870 if (rectColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400871 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400872 readback.addr32()[x + y * kW], rectColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700873 stop = true;
874 }
875 }
876 }
877 stop = false;
878 for (int y = kH/2; y < kH && !stop; ++y) {
879 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400880 REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]);
881 if (origColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400882 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400883 readback.addr32()[x + y * kW], origColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700884 stop = true;
885 }
886 }
887 }
888}
bsalomone63ffef2016-02-05 07:17:34 -0800889
egdanielab527a52016-06-28 08:07:26 -0700890DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400891 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400892
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400893 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
bsalomone63ffef2016-02-05 07:17:34 -0800894
Brian Salomon72c7b982020-10-06 10:07:38 -0400895 for (auto& surfaceFunc :
896 {&create_gpu_surface_backend_texture, &create_gpu_surface_backend_render_target}) {
ericrkc4025182016-05-04 12:01:58 -0700897 // Validate that we can draw to the canvas and that the original texture color is
898 // preserved in pixels that aren't rendered to via the surface.
899 // This works only for non-multisampled case.
Brian Salomon72050802020-10-12 20:45:06 -0400900 auto surface = surfaceFunc(context, 1, kOrigColor);
901 if (surface && supports_readpixels(context->priv().caps(), surface.get())) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400902 test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700903 }
904 }
905}
906
Greg Daniel8ce79912019-02-05 10:08:43 -0500907struct ReleaseChecker {
908 ReleaseChecker() : fReleaseCount(0) {}
909 int fReleaseCount;
910 static void Release(void* self) {
911 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
912 }
913};
914
915
916DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
917 const int kWidth = 10;
918 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500919
Robert Phillips6d344c32020-07-06 10:56:46 -0400920 auto ctx = ctxInfo.directContext();
Greg Daniel8ce79912019-02-05 10:08:43 -0500921 GrGpu* gpu = ctx->priv().getGpu();
922
923 for (bool useTexture : {false, true}) {
Brian Salomon72050802020-10-12 20:45:06 -0400924 sk_sp<sk_gpu_test::ManagedBackendTexture> mbet;
Greg Daniel8ce79912019-02-05 10:08:43 -0500925 GrBackendRenderTarget backendRT;
926 sk_sp<SkSurface> surface;
927
928 ReleaseChecker releaseChecker;
929 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
930
931 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400932 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
933 kPremul_SkAlphaType);
Brian Salomon7d2757f2021-01-26 17:46:50 -0500934 mbet = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(ctx, ii, GrMipmapped::kNo,
Brian Salomon72050802020-10-12 20:45:06 -0400935 GrRenderable::kYes);
936 if (!mbet) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500937 continue;
938 }
939
Brian Salomon72050802020-10-12 20:45:06 -0400940 surface = SkSurface::MakeFromBackendTexture(
941 ctx,
942 mbet->texture(),
943 texOrigin,
944 /*sample count*/ 1,
945 kRGBA_8888_SkColorType,
946 /*color space*/ nullptr,
947 /*surface props*/ nullptr,
948 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
949 mbet->releaseContext(ReleaseChecker::Release, &releaseChecker));
Greg Daniel8ce79912019-02-05 10:08:43 -0500950 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400951 backendRT = gpu->createTestingOnlyBackendRenderTarget({kWidth, kHeight},
Greg Daniel8ce79912019-02-05 10:08:43 -0500952 GrColorType::kRGBA_8888);
953 if (!backendRT.isValid()) {
954 continue;
955 }
956 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
957 kRGBA_8888_SkColorType,
958 nullptr, nullptr,
959 ReleaseChecker::Release,
960 &releaseChecker);
961 }
962 if (!surface) {
963 ERRORF(reporter, "Failed to create surface");
964 continue;
965 }
966
967 surface->getCanvas()->clear(SK_ColorRED);
Greg Danielce9f0162020-06-30 13:42:46 -0400968 surface->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400969 ctx->submit(true);
Greg Daniel8ce79912019-02-05 10:08:43 -0500970
971 // Now exercise the release proc
972 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
973 surface.reset(nullptr); // force a release of the surface
974 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
975
Brian Salomon72050802020-10-12 20:45:06 -0400976 if (!useTexture) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500977 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
978 }
979 }
980}
ericrkc4025182016-05-04 12:01:58 -0700981
982DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400983 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400984 const GrCaps* caps = context->priv().caps();
985
986 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -0700987 return;
988 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400989
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400990 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
ericrkc4025182016-05-04 12:01:58 -0700991
Robert Phillips9b16f812019-05-17 10:01:21 -0400992 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500993
Brian Salomon72c7b982020-10-06 10:07:38 -0400994 for (auto& surfaceFunc :
995 {&create_gpu_surface_backend_texture, &create_gpu_surface_backend_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500996 for (int sampleCnt : {1, 4, 8}) {
Brian Salomon72050802020-10-12 20:45:06 -0400997 auto surface = surfaceFunc(context, sampleCnt, kOrigColor);
ericrkc4025182016-05-04 12:01:58 -0700998
Brian Salomonbdecacf2018-02-02 20:32:49 -0500999 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001000 // Certain platforms don't support MSAA, skip these.
1001 continue;
ericrkc4025182016-05-04 12:01:58 -07001002 }
1003
1004 // Validate that we can attach a stencil buffer to an SkSurface created by either of
1005 // our surface functions.
Robert Phillipsfa8af0a2021-06-03 11:58:43 -04001006 auto rtp = SkCanvasPriv::TopDeviceTargetProxy(surface->getCanvas());
1007 GrRenderTarget* rt = rtp->peekRenderTarget();
Chris Daltone0fe23a2021-04-23 13:11:44 -06001008 REPORTER_ASSERT(reporter,
1009 resourceProvider->attachStencilAttachment(rt, rt->numSamples() > 1));
ericrkc4025182016-05-04 12:01:58 -07001010 }
bsalomone63ffef2016-02-05 07:17:34 -08001011 }
1012}
brianosman0e22eb82016-08-30 07:07:59 -07001013
Brian Salomonaad83152019-05-24 10:16:35 -04001014DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001015 auto context = ctxInfo.directContext();
Brian Salomonaad83152019-05-24 10:16:35 -04001016
1017 for (int sampleCnt : {1, 2}) {
Brian Salomonaad83152019-05-24 10:16:35 -04001018 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Brian Salomon72050802020-10-12 20:45:06 -04001019 auto mbet1 = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(
1020 context, ii, GrMipmapped::kNo, GrRenderable::kYes);
1021 if (!mbet1) {
Brian Salomonaad83152019-05-24 10:16:35 -04001022 continue;
1023 }
Brian Salomon72050802020-10-12 20:45:06 -04001024 auto mbet2 = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(
1025 context, ii, GrMipmapped::kNo, GrRenderable::kYes);
1026 if (!mbet2) {
Brian Salomonaad83152019-05-24 10:16:35 -04001027 ERRORF(reporter, "Expected to be able to make second texture");
1028 continue;
1029 }
Brian Salomonaad83152019-05-24 10:16:35 -04001030 auto ii2 = ii.makeWH(8, 8);
Brian Salomon72050802020-10-12 20:45:06 -04001031 auto mbet3 = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(
1032 context, ii2, GrMipmapped::kNo, GrRenderable::kYes);
Brian Salomonaad83152019-05-24 10:16:35 -04001033 GrBackendTexture backendTexture3;
Brian Salomon72050802020-10-12 20:45:06 -04001034 if (!mbet3) {
Brian Salomonaad83152019-05-24 10:16:35 -04001035 ERRORF(reporter, "Couldn't create different sized texture.");
1036 continue;
1037 }
Brian Salomonaad83152019-05-24 10:16:35 -04001038
1039 auto surf = SkSurface::MakeFromBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -04001040 context, mbet1->texture(), kTopLeft_GrSurfaceOrigin, sampleCnt,
Brian Salomonaad83152019-05-24 10:16:35 -04001041 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1042 if (!surf) {
1043 continue;
1044 }
1045 surf->getCanvas()->clear(SK_ColorBLUE);
1046 // Change matrix, layer, and clip state before swapping out the backing texture.
1047 surf->getCanvas()->translate(5, 5);
1048 surf->getCanvas()->saveLayer(nullptr, nullptr);
1049 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1050 // switch origin while we're at it.
Brian Salomon72050802020-10-12 20:45:06 -04001051 bool replaced = surf->replaceBackendTexture(mbet2->texture(), kBottomLeft_GrSurfaceOrigin);
Brian Salomonaad83152019-05-24 10:16:35 -04001052 REPORTER_ASSERT(reporter, replaced);
1053 SkPaint paint;
1054 paint.setColor(SK_ColorRED);
1055 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1056 surf->getCanvas()->restore();
1057
1058 // Check that the replacement texture got the right color values.
1059 SkAutoPixmapStorage pm;
1060 pm.alloc(ii);
1061 bool bad = !surf->readPixels(pm, 0, 0);
1062 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1063 for (int y = 0; y < ii.height() && !bad; ++y) {
1064 for (int x = 0; x < ii.width() && !bad; ++x) {
1065 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1066 auto found = *pm.addr32(x, y);
1067 if (found != expected) {
1068 bad = true;
1069 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1070 expected, found, x, y);
1071 }
1072 }
1073 }
1074 // The original texture should still be all blue.
1075 surf = SkSurface::MakeFromBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -04001076 context, mbet1->texture(), kBottomLeft_GrSurfaceOrigin, sampleCnt,
Brian Salomonaad83152019-05-24 10:16:35 -04001077 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1078 if (!surf) {
1079 ERRORF(reporter, "Could not create second surface.");
1080 continue;
1081 }
1082 bad = !surf->readPixels(pm, 0, 0);
1083 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1084 for (int y = 0; y < ii.height() && !bad; ++y) {
1085 for (int x = 0; x < ii.width() && !bad; ++x) {
1086 auto expected = 0xFFFF0000;
1087 auto found = *pm.addr32(x, y);
1088 if (found != expected) {
1089 bad = true;
1090 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1091 expected, found, x, y);
1092 }
1093 }
1094 }
1095
1096 // Can't replace with the same texture
1097 REPORTER_ASSERT(reporter,
Brian Salomon72050802020-10-12 20:45:06 -04001098 !surf->replaceBackendTexture(mbet1->texture(), kTopLeft_GrSurfaceOrigin));
Brian Salomonaad83152019-05-24 10:16:35 -04001099 // Can't replace with invalid texture
1100 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1101 // Can't replace with different size texture.
1102 REPORTER_ASSERT(reporter,
Brian Salomon72050802020-10-12 20:45:06 -04001103 !surf->replaceBackendTexture(mbet3->texture(), kTopLeft_GrSurfaceOrigin));
Brian Salomonaad83152019-05-24 10:16:35 -04001104 // Can't replace texture of non-wrapped SkSurface.
1105 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1106 REPORTER_ASSERT(reporter, surf);
1107 if (surf) {
Brian Salomon72050802020-10-12 20:45:06 -04001108 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(mbet1->texture(),
Brian Salomonaad83152019-05-24 10:16:35 -04001109 kTopLeft_GrSurfaceOrigin));
1110 }
1111 }
1112}
1113
Matt Sarett22886c42016-11-22 11:31:41 -05001114static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001115 SkOverdrawCanvas canvas(surface->getCanvas());
1116 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001117 sk_sp<SkImage> image = surface->makeImageSnapshot();
1118
1119 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001120 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001121 for (int y = 0; y < 10; y++) {
1122 for (int x = 0; x < 10; x++) {
1123 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1124 }
1125 }
1126}
1127
1128DEF_TEST(OverdrawSurface_Raster, r) {
1129 sk_sp<SkSurface> surface = create_surface();
1130 test_overdraw_surface(r, surface.get());
1131}
1132
Matt Sarett22886c42016-11-22 11:31:41 -05001133DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001134 auto context = ctxInfo.directContext();
Matt Sarett22886c42016-11-22 11:31:41 -05001135 sk_sp<SkSurface> surface = create_gpu_surface(context);
1136 test_overdraw_surface(r, surface.get());
1137}
Mike Reed44d04bd2017-06-28 19:57:21 -04001138
1139DEF_TEST(Surface_null, r) {
1140 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1141
1142 const int w = 37;
1143 const int h = 1000;
1144 auto surf = SkSurface::MakeNull(w, h);
1145 auto canvas = surf->getCanvas();
1146
1147 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1148 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1149}
Mike Reedd4746982018-02-07 16:05:29 -05001150
1151// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1152// (so the snapshot can succeed)
1153DEF_TEST(surface_image_unity, reporter) {
1154 auto do_test = [reporter](const SkImageInfo& info) {
1155 size_t rowBytes = info.minRowBytes();
1156 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1157 if (surf) {
1158 auto img = surf->makeImageSnapshot();
1159 if (!img && false) { // change to true to document the differences
1160 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001161 info.width(),
1162 info.height(),
1163 ToolUtils::colortype_name(info.colorType()),
1164 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001165 return;
1166 }
1167 REPORTER_ASSERT(reporter, img != nullptr);
1168
Kevin Lubickbe03ef12021-06-16 15:28:00 -04001169 char tempPixel = 0; // just need a valid address (not a valid size)
1170 SkPixmap pmap = { info, &tempPixel, rowBytes };
Mike Reedd4746982018-02-07 16:05:29 -05001171 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1172 REPORTER_ASSERT(reporter, img != nullptr);
1173 }
1174 };
1175
Mike Kleine978ca22018-10-29 11:29:58 -04001176 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001177 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001178 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001179 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001180 SkAlphaType at = static_cast<SkAlphaType>(ati);
1181 for (int32_t size : sizes) {
1182 do_test(SkImageInfo::Make(1, size, ct, at));
1183 do_test(SkImageInfo::Make(size, 1, ct, at));
1184 }
1185 }
1186 }
1187}