blob: 17d2687609d52529dadc15f0014e7b7373e49b63 [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"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrGpu.h"
23#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040024#include "src/gpu/GrImageInfo.h"
Stephen Whitefdba6c82020-05-26 17:00:32 -040025#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050027#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/SkGpuDevice.h"
29#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"
junov@chromium.org995beb62013-03-28 13:49:22 +000036
Hal Canary8a001442018-09-19 11:31:27 -040037#include <functional>
38#include <initializer_list>
39#include <vector>
40
kkinnunen179a8f52015-11-20 13:32:24 -080041static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070042 SkASSERT(pixels == context);
43 sk_free(pixels);
44}
reede8f30622016-03-23 18:59:25 -070045static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
46 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070047 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000048 if (requestedInfo) {
49 *requestedInfo = info;
50 }
reede8f30622016-03-23 18:59:25 -070051 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000052}
reede8f30622016-03-23 18:59:25 -070053static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
54 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080055 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
56 if (requestedInfo) {
57 *requestedInfo = info;
58 }
59 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040060 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070061 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
62 release_direct_surface_storage,
63 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080064}
Robert Phillipseffd13f2020-07-20 15:00:36 -040065static sk_sp<SkSurface> create_gpu_surface(GrRecordingContext* rContext,
66 SkAlphaType at = kPremul_SkAlphaType,
reede8f30622016-03-23 18:59:25 -070067 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080068 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
69 if (requestedInfo) {
70 *requestedInfo = info;
71 }
Robert Phillipseffd13f2020-07-20 15:00:36 -040072 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080073}
Robert Phillipseffd13f2020-07-20 15:00:36 -040074static sk_sp<SkSurface> create_gpu_scratch_surface(GrRecordingContext* rContext,
reede8f30622016-03-23 18:59:25 -070075 SkAlphaType at = kPremul_SkAlphaType,
76 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080077 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
78 if (requestedInfo) {
79 *requestedInfo = info;
80 }
Robert Phillipseffd13f2020-07-20 15:00:36 -040081 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080082}
junov@chromium.org995beb62013-03-28 13:49:22 +000083
kkinnunen179a8f52015-11-20 13:32:24 -080084DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080085 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070086 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
87 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080088
reedb2497c22014-12-31 12:31:43 -080089}
egdanielab527a52016-06-28 08:07:26 -070090DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080091 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
92 REPORTER_ASSERT(reporter, nullptr ==
Robert Phillips6d344c32020-07-06 10:56:46 -040093 SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080094}
reedb2497c22014-12-31 12:31:43 -080095
Brian Salomonbdecacf2018-02-02 20:32:49 -050096DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040097 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -040098
Brian Salomonbdecacf2018-02-02 20:32:49 -050099 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
100 static constexpr int kSize = 10;
101
102 SkColorType colorType = static_cast<SkColorType>(ct);
103 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500104
Robert Phillips429f0d32019-09-11 17:03:28 -0400105 {
106 bool can = context->colorTypeSupportedAsSurface(colorType);
107 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
108 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
109 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500110
Brian Salomon72050802020-10-12 20:45:06 -0400111 surf = sk_gpu_test::MakeBackendTextureSurface(context,
112 {kSize, kSize},
113 kTopLeft_GrSurfaceOrigin,
114 /*sample cnt*/ 1,
115 colorType);
Robert Phillips429f0d32019-09-11 17:03:28 -0400116 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
117 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500118 }
119
Robert Phillips429f0d32019-09-11 17:03:28 -0400120 // The MSAA test only makes sense if the colorType is renderable to begin with.
121 if (context->colorTypeSupportedAsSurface(colorType)) {
122 static constexpr int kSampleCnt = 2;
123
124 bool can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
125 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt,
126 nullptr);
127 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
128 colorType, can, SkToBool(surf));
129
Brian Salomon72050802020-10-12 20:45:06 -0400130 surf = sk_gpu_test::MakeBackendTextureSurface(
131 context, {kSize, kSize}, kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType);
Robert Phillips429f0d32019-09-11 17:03:28 -0400132 REPORTER_ASSERT(reporter, can == SkToBool(surf),
133 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
134 colorType);
135 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
136 if (surf) {
Brian Salomon8f7d9532020-12-23 09:16:59 -0500137 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(surf->getCanvas());
138 int storedCnt = sdc->numSamples();
139 GrBackendFormat format = sdc->asSurfaceProxy()->backendFormat();
Brian Salomon72050802020-10-12 20:45:06 -0400140 int allowedCnt =
141 context->priv().caps()->getRenderTargetSampleCount(storedCnt, format);
Robert Phillips429f0d32019-09-11 17:03:28 -0400142 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
143 "Should store an allowed sample count (%d vs %d)", allowedCnt,
144 storedCnt);
145 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500146 }
147
Brian Salomon72c7b982020-10-06 10:07:38 -0400148 for (int sampleCnt : {1, 2}) {
Brian Salomonf9b00422020-10-08 16:00:14 -0400149 auto surf = sk_gpu_test::MakeBackendRenderTargetSurface(context,
150 {16, 16},
151 kTopLeft_GrSurfaceOrigin,
152 sampleCnt,
153 colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -0400154 bool can = context->colorTypeSupportedAsSurface(colorType) &&
155 context->maxSurfaceSampleCountForColorType(colorType) >= sampleCnt;
156 if (!surf && can && colorType == kBGRA_8888_SkColorType && sampleCnt > 1 &&
157 context->backend() == GrBackendApi::kOpenGL) {
158 // This is an execeptional case. On iOS GLES we support MSAA BGRA for internally-
159 // created render targets by using a MSAA RGBA8 renderbuffer that resolves to a
160 // BGRA8 texture. However, the GL_APPLE_texture_format_BGRA8888 extension does not
161 // allow creation of BGRA8 renderbuffers and we don't support multisampled textures.
162 // So this is expected to fail. As of 10/5/2020 it actually seems to work to create
163 // a MSAA BGRA8 renderbuffer (at least in the simulator) but we don't want to rely
164 // on this undocumented behavior.
165 continue;
166 }
167 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, sc: %d, can: %d, surf: %d",
168 colorType, sampleCnt, can, SkToBool(surf));
169 if (surf) {
Brian Salomon8f7d9532020-12-23 09:16:59 -0500170 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(surf->getCanvas());
171 auto backendFormat = sdc->asSurfaceProxy()->backendFormat();
172 int storedCnt = sdc->numSamples();
Brian Salomon72c7b982020-10-06 10:07:38 -0400173 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(storedCnt,
174 backendFormat);
175 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
176 "Should store an allowed sample count (%d vs %d)", allowedCnt,
177 storedCnt);
Robert Phillips429f0d32019-09-11 17:03:28 -0400178 }
Brian Salomon93348dd2018-08-29 12:56:23 -0400179 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500180 }
181}
182
183DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400184 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400185
186 static constexpr int kSize = 10;
187
Brian Salomonbdecacf2018-02-02 20:32:49 -0500188 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500189
190 SkColorType colorType = static_cast<SkColorType>(ct);
Brian Salomon72050802020-10-12 20:45:06 -0400191 int maxSampleCnt = context->maxSurfaceSampleCountForColorType(colorType);
192 if (!maxSampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500193 continue;
194 }
Robert Phillips429f0d32019-09-11 17:03:28 -0400195 if (!context->colorTypeSupportedAsSurface(colorType)) {
196 continue;
197 }
198
Brian Salomonbdecacf2018-02-02 20:32:49 -0500199 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomon72050802020-10-12 20:45:06 -0400200 auto surf = sk_gpu_test::MakeBackendTextureSurface(
201 context, info, kTopLeft_GrSurfaceOrigin, maxSampleCnt);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500202 if (!surf) {
Brian Salomon72050802020-10-12 20:45:06 -0400203 ERRORF(reporter, "Could not make surface of color type %d.", colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500204 continue;
205 }
206 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
207 ->getDevice()
Brian Salomon8f7d9532020-12-23 09:16:59 -0500208 ->surfaceDrawContext()
Chris Dalton6ce447a2019-06-23 18:07:38 -0600209 ->numSamples();
Brian Salomon72050802020-10-12 20:45:06 -0400210 REPORTER_ASSERT(reporter, sampleCnt == maxSampleCnt, "Exected: %d, actual: %d",
211 maxSampleCnt, sampleCnt);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500212 }
213}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500214
kkinnunen179a8f52015-11-20 13:32:24 -0800215static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700216 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800217 const SkImageInfo& requestInfo,
218 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000219 const SkColor color = SK_ColorRED;
220 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800221 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000222
reed6ceeebd2016-03-09 14:26:26 -0800223 SkPixmap pmap;
224 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800225 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000226
reed6ceeebd2016-03-09 14:26:26 -0800227 SkPixmap pmap2;
228 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000229
kkinnunen179a8f52015-11-20 13:32:24 -0800230 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800231 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
232 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
233 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000234
reed6ceeebd2016-03-09 14:26:26 -0800235 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
236 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
237 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800238 } else {
239 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000240 }
241}
kkinnunen179a8f52015-11-20 13:32:24 -0800242DEF_TEST(SurfaceCanvasPeek, reporter) {
243 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
244 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700245 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800246 test_canvas_peek(reporter, surface, requestInfo, true);
247 }
248}
egdanielab527a52016-06-28 08:07:26 -0700249DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800250 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
251 SkImageInfo requestInfo;
Robert Phillips6d344c32020-07-06 10:56:46 -0400252 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800253 test_canvas_peek(reporter, surface, requestInfo, false);
254 }
255}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000256
reede8f30622016-03-23 18:59:25 -0700257static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700258 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800259 REPORTER_ASSERT(reporter, surface);
260 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700261 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800262 REPORTER_ASSERT(reporter, image);
263 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700264 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700265 }
266 }
267}
kkinnunen179a8f52015-11-20 13:32:24 -0800268DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
269 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700270 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
271 auto surface(surface_func(at, nullptr));
272 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700273 }
274 }
275}
egdanielab527a52016-06-28 08:07:26 -0700276DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800277 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700278 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
279 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400280 auto surface(surface_func(ctxInfo.directContext(), at, nullptr));
brianosman69c166d2016-08-17 14:01:05 -0700281 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800282 }
283 }
284}
bsalomon74f681d2015-06-23 14:38:48 -0700285
Robert Phillips8caf85f2018-04-05 09:30:38 -0400286static void test_backend_texture_access_copy_on_write(
287 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
288 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700289 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700290
Robert Phillips8caf85f2018-04-05 09:30:38 -0400291 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700292 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700293
294 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400295 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700296}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400297
298static void test_backend_rendertarget_access_copy_on_write(
299 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
300 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
301 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
302
303 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
304 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
305
306 // If the access mode triggers CoW, then the backend objects should reflect it.
307 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
308 (snap1 == snap2));
309}
310
311DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800312 const SkSurface::BackendHandleAccess accessModes[] = {
313 SkSurface::kFlushRead_BackendHandleAccess,
314 SkSurface::kFlushWrite_BackendHandleAccess,
315 SkSurface::kDiscardWrite_BackendHandleAccess,
316 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400317
kkinnunen179a8f52015-11-20 13:32:24 -0800318 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400319 for (auto& accessMode : accessModes) {
320 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400321 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400322 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
323 }
324 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400325 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400326 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800327 }
328 }
329 }
330}
kkinnunen179a8f52015-11-20 13:32:24 -0800331
Robert Phillips8caf85f2018-04-05 09:30:38 -0400332template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
333static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700334 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400335
336 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
337 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700338 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800339 // just read access should not affect the snapshot
340 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
341
Robert Phillips8caf85f2018-04-05 09:30:38 -0400342 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
343 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700344 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800345 // expect a new image, since we claimed we would write
346 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
347
Robert Phillips8caf85f2018-04-05 09:30:38 -0400348 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
349 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800351 // expect a new(er) image, since we claimed we would write
352 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
353 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
354}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400355
kkinnunen179a8f52015-11-20 13:32:24 -0800356// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700357DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800358 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400359 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400360 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400361 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
362 surface.get());
363 }
364 {
Robert Phillips6d344c32020-07-06 10:56:46 -0400365 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400366 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
367 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800368 }
369 }
370}
kkinnunen179a8f52015-11-20 13:32:24 -0800371
372// Verify that the right canvas commands trigger a copy on write.
373static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000374 SkCanvas* canvas = surface->getCanvas();
375
376 const SkRect testRect =
377 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
378 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000379 SkPath testPath;
380 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
381 SkIntToScalar(2), SkIntToScalar(1)));
382
383 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
384
385 SkRegion testRegion;
386 testRegion.setRect(testIRect);
387
388
389 const SkColor testColor = 0x01020304;
390 const SkPaint testPaint;
391 const SkPoint testPoints[3] = {
392 {SkIntToScalar(0), SkIntToScalar(0)},
393 {SkIntToScalar(2), SkIntToScalar(1)},
394 {SkIntToScalar(0), SkIntToScalar(2)}
395 };
396 const size_t testPointCount = 3;
397
398 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000399 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000400 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000401
402 SkRRect testRRect;
403 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
404
405 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000406
407#define EXPECT_COPY_ON_WRITE(command) \
408 { \
reed9ce9d672016-03-17 10:51:11 -0700409 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
John Stiles31954bf2020-08-07 17:35:54 -0400410 sk_sp<SkImage> aur_before(imageBefore); /*NOLINT*/ \
junov@chromium.org995beb62013-03-28 13:49:22 +0000411 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700412 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
John Stiles31954bf2020-08-07 17:35:54 -0400413 sk_sp<SkImage> aur_after(imageAfter); /*NOLINT*/ \
junov@chromium.org995beb62013-03-28 13:49:22 +0000414 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
415 }
416
417 EXPECT_COPY_ON_WRITE(clear(testColor))
418 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
419 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
420 testPaint))
421 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
422 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
423 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
424 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
425 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700426 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
Hal Canary89a644b2019-01-07 09:36:09 -0500427 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800428}
429DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700430 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800431}
egdanielab527a52016-06-28 08:07:26 -0700432DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800433 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400434 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700435 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700436 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000437}
438
kkinnunen179a8f52015-11-20 13:32:24 -0800439static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
440 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000441 // This test succeeds by not triggering an assertion.
442 // The test verifies that the surface remains writable (usable) after
443 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000444 SkCanvas* canvas = surface->getCanvas();
445 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700446 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000447 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000448}
kkinnunen179a8f52015-11-20 13:32:24 -0800449DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700450 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800451}
egdanielab527a52016-06-28 08:07:26 -0700452DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800453 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400454 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700455 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800456 }
457}
junov@chromium.orgda904742013-05-01 22:38:16 +0000458
kkinnunen179a8f52015-11-20 13:32:24 -0800459static void test_crbug263329(skiatest::Reporter* reporter,
460 SkSurface* surface1,
461 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000462 // This is a regression test for crbug.com/263329
463 // Bug was caused by onCopyOnWrite releasing the old surface texture
464 // back to the scratch texture pool even though the texture is used
465 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000466 SkCanvas* canvas1 = surface1->getCanvas();
467 SkCanvas* canvas2 = surface2->getCanvas();
468 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700469 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000470 // Trigger copy on write, new backing is a scratch texture
471 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700472 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000473 // Trigger copy on write, old backing should not be returned to scratch
474 // pool because it is held by image2
475 canvas1->clear(3);
476
477 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700478 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000479 // Trigger copy on write on surface2. The new backing store should not
480 // be recycling a texture that is held by an existing image.
481 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700482 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Greg Daniel7c902112020-03-06 13:07:10 -0500483
484 SkImage_GpuBase* gpuImage1 = static_cast<SkImage_GpuBase*>(as_IB(image1));
485 SkImage_GpuBase* gpuImage2 = static_cast<SkImage_GpuBase*>(as_IB(image2));
486 SkImage_GpuBase* gpuImage3 = static_cast<SkImage_GpuBase*>(as_IB(image3));
487 SkImage_GpuBase* gpuImage4 = static_cast<SkImage_GpuBase*>(as_IB(image4));
488
489 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage3->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000490 // The following assertion checks crbug.com/263329
Greg Daniel7c902112020-03-06 13:07:10 -0500491 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage2->getTexture());
492 REPORTER_ASSERT(reporter, gpuImage4->getTexture() != gpuImage1->getTexture());
493 REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage2->getTexture());
494 REPORTER_ASSERT(reporter, gpuImage3->getTexture() != gpuImage1->getTexture());
495 REPORTER_ASSERT(reporter, gpuImage2->getTexture() != gpuImage1->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000496}
egdanielab527a52016-06-28 08:07:26 -0700497DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800498 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400499 auto surface1(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
500 auto surface2(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700501 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800502 }
503}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000504
kkinnunen179a8f52015-11-20 13:32:24 -0800505DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700506 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700507 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500508 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800509 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500510 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800511}
egdanielab527a52016-06-28 08:07:26 -0700512DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800513 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400514 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700515 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500516
517 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400518 GrBackendTexture backendTex = image->getBackendTexture(false);
519 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800520 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500521 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400522 GrBackendTexture backendTex2 = image->getBackendTexture(false);
523 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000524 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000525}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800526
reede8f30622016-03-23 18:59:25 -0700527static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
528 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500529
Brian Salomon8f7d9532020-12-23 09:16:59 -0500530 GrRenderTargetProxy* proxy = gsurf->getDevice()->surfaceDrawContext()->asRenderTargetProxy();
Robert Phillips6de99042017-01-31 11:31:39 -0500531 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800532}
533
bsalomon5ec26ae2016-02-25 08:33:02 -0800534static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400535 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800536}
537
reed9ce9d672016-03-17 10:51:11 -0700538static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
539 return is_budgeted(image.get());
540}
541
egdanielab527a52016-06-28 08:07:26 -0700542DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800543 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400544 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400545 auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), budgeted, info));
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400546 SkASSERT(surface);
547 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800548
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400549 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800550
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400551 // Initially the image shares a texture with the surface, and the
552 // the budgets should always match.
553 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
554 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800555
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400556 // Now trigger copy-on-write
557 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800558
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400559 // They don't share a texture anymore but the budgets should still match.
560 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
561 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800562 }
563}
junov@chromium.orgda904742013-05-01 22:38:16 +0000564
kkinnunen179a8f52015-11-20 13:32:24 -0800565static void test_no_canvas1(skiatest::Reporter* reporter,
566 SkSurface* surface,
567 SkSurface::ContentChangeMode mode) {
568 // Test passes by not asserting
569 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800570}
571static void test_no_canvas2(skiatest::Reporter* reporter,
572 SkSurface* surface,
573 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000574 // Verifies the robustness of SkSurface for handling use cases where calls
575 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700576 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
John Stiles31954bf2020-08-07 17:35:54 -0400577 sk_sp<SkImage> aur_image1(image1); // NOLINT(performance-unnecessary-copy-initialization)
kkinnunen179a8f52015-11-20 13:32:24 -0800578 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700579 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
John Stiles31954bf2020-08-07 17:35:54 -0400580 sk_sp<SkImage> aur_image2(image2); // NOLINT(performance-unnecessary-copy-initialization)
kkinnunen179a8f52015-11-20 13:32:24 -0800581 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000582}
kkinnunen179a8f52015-11-20 13:32:24 -0800583DEF_TEST(SurfaceNoCanvas, reporter) {
584 SkSurface::ContentChangeMode modes[] =
585 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
586 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
587 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700588 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800589 }
590 }
591}
egdanielab527a52016-06-28 08:07:26 -0700592DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800593 SkSurface::ContentChangeMode modes[] =
594 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
595 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
596 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
597 for (auto& mode : modes) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400598 auto surface(surface_func(ctxInfo.directContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700599 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700600 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000601 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000602 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000603}
reed9cd016e2016-01-30 10:01:06 -0800604
605static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800606 SkPixmap surfacePM;
607 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800608
reed9ce9d672016-03-17 10:51:11 -0700609 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800610 SkPixmap pm;
611 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800612
reed6ceeebd2016-03-09 14:26:26 -0800613 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800614
615 // trigger a copy-on-write
616 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700617 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800618 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
619
reed6ceeebd2016-03-09 14:26:26 -0800620 SkPixmap pm2;
621 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
622 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800623}
624
625DEF_TEST(surface_rowbytes, reporter) {
626 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
627
reede8f30622016-03-23 18:59:25 -0700628 auto surf0(SkSurface::MakeRaster(info));
629 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800630
631 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700632 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
633 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800634
635 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700636 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800637 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400638 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800639 REPORTER_ASSERT(reporter, nullptr == s);
640}
bsalomone63ffef2016-02-05 07:17:34 -0800641
fmalita03912f12016-07-06 06:22:06 -0700642DEF_TEST(surface_raster_zeroinitialized, reporter) {
643 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
644 SkPixmap pixmap;
645 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
646
647 for (int i = 0; i < pixmap.info().width(); ++i) {
648 for (int j = 0; j < pixmap.info().height(); ++j) {
649 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
650 }
651 }
652}
653
Robert Phillipseffd13f2020-07-20 15:00:36 -0400654static sk_sp<SkSurface> create_gpu_surface_backend_texture(GrDirectContext* dContext,
655 int sampleCnt,
Brian Salomon72050802020-10-12 20:45:06 -0400656 const SkColor4f& color) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500657 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
658 // triggers what appears to be a driver race condition where the 10x10 surface from the
659 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
660 // test.
661 //
662 // Immediately after creation of this surface, readback shows the correct initial solid color.
663 // However, sometime before content is rendered into the upper half of the surface, the driver
664 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
665 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
666 // untouched bottom half contains random pixel values that trigger asserts in the
667 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
668 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
669 //
670 // Requesting a much larger backend texture size seems to prevent it from reusing the same
671 // memory and avoids the issue.
672#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700673 const int kWidth = 10;
674 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500675#else
676 const int kWidth = 100;
677 const int kHeight = 100;
678#endif
679
Brian Salomon72050802020-10-12 20:45:06 -0400680 auto surf = sk_gpu_test::MakeBackendTextureSurface(dContext,
681 {kWidth, kHeight},
682 kTopLeft_GrSurfaceOrigin,
683 sampleCnt,
684 kRGBA_8888_SkColorType);
685 if (!surf) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500686 return nullptr;
687 }
Brian Salomon72050802020-10-12 20:45:06 -0400688 surf->getCanvas()->clear(color);
689 return surf;
ericrkc4025182016-05-04 12:01:58 -0700690}
bsalomone63ffef2016-02-05 07:17:34 -0800691
Stephen Whitefdba6c82020-05-26 17:00:32 -0400692static bool supports_readpixels(const GrCaps* caps, SkSurface* surface) {
693 auto surfaceGpu = static_cast<SkSurface_Gpu*>(surface);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500694 GrSurfaceDrawContext* context = surfaceGpu->getDevice()->surfaceDrawContext();
Stephen Whitefdba6c82020-05-26 17:00:32 -0400695 GrRenderTarget* rt = context->accessRenderTarget();
696 if (!rt) {
697 return false;
698 }
699 return caps->surfaceSupportsReadPixels(rt) == GrCaps::SurfaceReadPixelsSupport::kSupported;
700}
701
Brian Salomon72c7b982020-10-06 10:07:38 -0400702static sk_sp<SkSurface> create_gpu_surface_backend_render_target(GrDirectContext* dContext,
703 int sampleCnt,
Brian Salomon72050802020-10-12 20:45:06 -0400704 const SkColor4f& color) {
ericrkc4025182016-05-04 12:01:58 -0700705 const int kWidth = 10;
706 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000707
Brian Salomonf9b00422020-10-08 16:00:14 -0400708 auto surf = sk_gpu_test::MakeBackendRenderTargetSurface(dContext,
709 {kWidth, kHeight},
710 kTopLeft_GrSurfaceOrigin,
711 sampleCnt,
Brian Salomon72050802020-10-12 20:45:06 -0400712 kRGBA_8888_SkColorType);
Brian Salomon72c7b982020-10-06 10:07:38 -0400713 if (!surf) {
ericrkc4025182016-05-04 12:01:58 -0700714 return nullptr;
715 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400716 surf->getCanvas()->clear(color);
717 return surf;
ericrkc4025182016-05-04 12:01:58 -0700718}
719
Brian Salomonbf6b9792019-08-21 09:38:10 -0400720static void test_surface_context_clear(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400721 GrDirectContext* dContext,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400722 GrSurfaceContext* surfaceContext, uint32_t expectedValue) {
723 int w = surfaceContext->width();
724 int h = surfaceContext->height();
Robert Phillipsd3442842019-08-02 12:26:22 -0400725
726 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
727
728 SkAutoPixmapStorage readback;
729 readback.alloc(ii);
bsalomone63ffef2016-02-05 07:17:34 -0800730
Robert Phillipsd3442842019-08-02 12:26:22 -0400731 readback.erase(~expectedValue);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400732 surfaceContext->readPixels(dContext, readback.info(), readback.writable_addr(),
733 readback.rowBytes(), {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800734 for (int y = 0; y < h; ++y) {
735 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400736 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -0800737 if (pixel != expectedValue) {
738 SkString msg;
739 if (expectedValue) {
740 msg = "SkSurface should have left render target unmodified";
741 } else {
742 msg = "SkSurface should have cleared the render target";
743 }
744 ERRORF(reporter,
745 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
746 expectedValue, x, y);
747 return;
748 }
749 }
750 }
751}
752
bsalomon758586c2016-04-06 14:02:39 -0700753DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400754 auto dContext = ctxInfo.directContext();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400755 // Snaps an image from a surface and then makes a GrSurfaceContext from the image's texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400756 auto makeImageSurfaceContext = [dContext](SkSurface* surface) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400757 sk_sp<SkImage> i(surface->makeImageSnapshot());
758 SkImage_Gpu* gpuImage = (SkImage_Gpu*)as_IB(i);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500759 return GrSurfaceContext::Make(dContext,
760 *gpuImage->view(dContext),
761 i->imageInfo().colorInfo());
bsalomone63ffef2016-02-05 07:17:34 -0800762 };
ericrkc4025182016-05-04 12:01:58 -0700763
Brian Salomonbf6b9792019-08-21 09:38:10 -0400764 // Test that non-wrapped RTs are created clear.
765 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400766 auto surface = surface_func(dContext, kPremul_SkAlphaType, nullptr);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400767 if (!surface) {
768 ERRORF(reporter, "Could not create GPU SkSurface.");
769 return;
bsalomone63ffef2016-02-05 07:17:34 -0800770 }
Brian Salomon8f7d9532020-12-23 09:16:59 -0500771 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(surface->getCanvas());
772 if (!sdc) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400773 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
774 return;
ericrkc4025182016-05-04 12:01:58 -0700775 }
Brian Salomon8f7d9532020-12-23 09:16:59 -0500776 test_surface_context_clear(reporter, dContext, sdc, 0x0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400777 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400778 test_surface_context_clear(reporter, dContext, imageSurfaceCtx.get(), 0x0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400779 }
780
781 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
782 const SkColor4f kOrigColor{.67f, .67f, .67f, 1};
Brian Salomon72c7b982020-10-06 10:07:38 -0400783 for (auto& surfaceFunc :
784 {&create_gpu_surface_backend_texture, &create_gpu_surface_backend_render_target}) {
Brian Salomon72050802020-10-12 20:45:06 -0400785 auto surface = surfaceFunc(dContext, 1, kOrigColor);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400786 if (!surface) {
787 ERRORF(reporter, "Could not create GPU SkSurface.");
788 return;
789 }
Brian Salomon8f7d9532020-12-23 09:16:59 -0500790 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(surface->getCanvas());
791 if (!sdc) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400792 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
793 return;
794 }
Brian Salomon8f7d9532020-12-23 09:16:59 -0500795 test_surface_context_clear(reporter, dContext, sdc, kOrigColor.toSkColor());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400796 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400797 test_surface_context_clear(reporter, dContext, imageSurfaceCtx.get(),
798 kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700799 }
800}
bsalomone63ffef2016-02-05 07:17:34 -0800801
ericrkc4025182016-05-04 12:01:58 -0700802static void test_surface_draw_partially(
Robert Phillips66944402019-09-30 13:21:25 -0400803 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
ericrkc4025182016-05-04 12:01:58 -0700804 const int kW = surface->width();
805 const int kH = surface->height();
806 SkPaint paint;
807 const SkColor kRectColor = ~origColor | 0xFF000000;
808 paint.setColor(kRectColor);
Robert Phillipsd3442842019-08-02 12:26:22 -0400809 surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint);
810
ericrkc4025182016-05-04 12:01:58 -0700811 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
812 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsd3442842019-08-02 12:26:22 -0400813
814 SkAutoPixmapStorage readback;
815 readback.alloc(readInfo);
816
817 readback.erase(~origColor);
Stephen Whitedbb3e1d2020-05-13 17:55:18 -0400818 REPORTER_ASSERT(reporter, surface->readPixels(readback.info(), readback.writable_addr(),
819 readback.rowBytes(), 0, 0));
ericrkc4025182016-05-04 12:01:58 -0700820 bool stop = false;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400821
Robert Phillips66944402019-09-30 13:21:25 -0400822 SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
823 SkColorGetR(origColor),
824 SkColorGetG(origColor),
825 SkColorGetB(origColor));
826 SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
827 SkColorGetR(kRectColor),
828 SkColorGetG(kRectColor),
829 SkColorGetB(kRectColor));
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400830
ericrkc4025182016-05-04 12:01:58 -0700831 for (int y = 0; y < kH/2 && !stop; ++y) {
832 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400833 REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]);
834 if (rectColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400835 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400836 readback.addr32()[x + y * kW], rectColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700837 stop = true;
838 }
839 }
840 }
841 stop = false;
842 for (int y = kH/2; y < kH && !stop; ++y) {
843 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400844 REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]);
845 if (origColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400846 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400847 readback.addr32()[x + y * kW], origColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700848 stop = true;
849 }
850 }
851 }
852}
bsalomone63ffef2016-02-05 07:17:34 -0800853
egdanielab527a52016-06-28 08:07:26 -0700854DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400855 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400856
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400857 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
bsalomone63ffef2016-02-05 07:17:34 -0800858
Brian Salomon72c7b982020-10-06 10:07:38 -0400859 for (auto& surfaceFunc :
860 {&create_gpu_surface_backend_texture, &create_gpu_surface_backend_render_target}) {
ericrkc4025182016-05-04 12:01:58 -0700861 // Validate that we can draw to the canvas and that the original texture color is
862 // preserved in pixels that aren't rendered to via the surface.
863 // This works only for non-multisampled case.
Brian Salomon72050802020-10-12 20:45:06 -0400864 auto surface = surfaceFunc(context, 1, kOrigColor);
865 if (surface && supports_readpixels(context->priv().caps(), surface.get())) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400866 test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700867 }
868 }
869}
870
Greg Daniel8ce79912019-02-05 10:08:43 -0500871struct ReleaseChecker {
872 ReleaseChecker() : fReleaseCount(0) {}
873 int fReleaseCount;
874 static void Release(void* self) {
875 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
876 }
877};
878
879
880DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
881 const int kWidth = 10;
882 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500883
Robert Phillips6d344c32020-07-06 10:56:46 -0400884 auto ctx = ctxInfo.directContext();
Greg Daniel8ce79912019-02-05 10:08:43 -0500885 GrGpu* gpu = ctx->priv().getGpu();
886
887 for (bool useTexture : {false, true}) {
Brian Salomon72050802020-10-12 20:45:06 -0400888 sk_sp<sk_gpu_test::ManagedBackendTexture> mbet;
Greg Daniel8ce79912019-02-05 10:08:43 -0500889 GrBackendRenderTarget backendRT;
890 sk_sp<SkSurface> surface;
891
892 ReleaseChecker releaseChecker;
893 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
894
895 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400896 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
897 kPremul_SkAlphaType);
Brian Salomon72050802020-10-12 20:45:06 -0400898 mbet = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(ctx, ii, GrMipMapped::kNo,
899 GrRenderable::kYes);
900 if (!mbet) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500901 continue;
902 }
903
Brian Salomon72050802020-10-12 20:45:06 -0400904 surface = SkSurface::MakeFromBackendTexture(
905 ctx,
906 mbet->texture(),
907 texOrigin,
908 /*sample count*/ 1,
909 kRGBA_8888_SkColorType,
910 /*color space*/ nullptr,
911 /*surface props*/ nullptr,
912 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
913 mbet->releaseContext(ReleaseChecker::Release, &releaseChecker));
Greg Daniel8ce79912019-02-05 10:08:43 -0500914 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400915 backendRT = gpu->createTestingOnlyBackendRenderTarget({kWidth, kHeight},
Greg Daniel8ce79912019-02-05 10:08:43 -0500916 GrColorType::kRGBA_8888);
917 if (!backendRT.isValid()) {
918 continue;
919 }
920 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
921 kRGBA_8888_SkColorType,
922 nullptr, nullptr,
923 ReleaseChecker::Release,
924 &releaseChecker);
925 }
926 if (!surface) {
927 ERRORF(reporter, "Failed to create surface");
928 continue;
929 }
930
931 surface->getCanvas()->clear(SK_ColorRED);
Greg Danielce9f0162020-06-30 13:42:46 -0400932 surface->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400933 ctx->submit(true);
Greg Daniel8ce79912019-02-05 10:08:43 -0500934
935 // Now exercise the release proc
936 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
937 surface.reset(nullptr); // force a release of the surface
938 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
939
Brian Salomon72050802020-10-12 20:45:06 -0400940 if (!useTexture) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500941 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
942 }
943 }
944}
ericrkc4025182016-05-04 12:01:58 -0700945
946DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400947 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400948 const GrCaps* caps = context->priv().caps();
949
950 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -0700951 return;
952 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400953
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400954 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
ericrkc4025182016-05-04 12:01:58 -0700955
Robert Phillips9b16f812019-05-17 10:01:21 -0400956 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500957
Brian Salomon72c7b982020-10-06 10:07:38 -0400958 for (auto& surfaceFunc :
959 {&create_gpu_surface_backend_texture, &create_gpu_surface_backend_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500960 for (int sampleCnt : {1, 4, 8}) {
Brian Salomon72050802020-10-12 20:45:06 -0400961 auto surface = surfaceFunc(context, sampleCnt, kOrigColor);
ericrkc4025182016-05-04 12:01:58 -0700962
Brian Salomonbdecacf2018-02-02 20:32:49 -0500963 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500964 // Certain platforms don't support MSAA, skip these.
965 continue;
ericrkc4025182016-05-04 12:01:58 -0700966 }
967
968 // Validate that we can attach a stencil buffer to an SkSurface created by either of
969 // our surface functions.
Brian Salomon8f7d9532020-12-23 09:16:59 -0500970 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(surface->getCanvas());
971 GrRenderTarget* rt = sdc->accessRenderTarget();
Chris Daltoneffee202019-07-01 22:28:03 -0600972 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt));
ericrkc4025182016-05-04 12:01:58 -0700973 }
bsalomone63ffef2016-02-05 07:17:34 -0800974 }
975}
brianosman0e22eb82016-08-30 07:07:59 -0700976
Brian Salomonaad83152019-05-24 10:16:35 -0400977DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400978 auto context = ctxInfo.directContext();
Brian Salomonaad83152019-05-24 10:16:35 -0400979
980 for (int sampleCnt : {1, 2}) {
Brian Salomonaad83152019-05-24 10:16:35 -0400981 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Brian Salomon72050802020-10-12 20:45:06 -0400982 auto mbet1 = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(
983 context, ii, GrMipmapped::kNo, GrRenderable::kYes);
984 if (!mbet1) {
Brian Salomonaad83152019-05-24 10:16:35 -0400985 continue;
986 }
Brian Salomon72050802020-10-12 20:45:06 -0400987 auto mbet2 = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(
988 context, ii, GrMipmapped::kNo, GrRenderable::kYes);
989 if (!mbet2) {
Brian Salomonaad83152019-05-24 10:16:35 -0400990 ERRORF(reporter, "Expected to be able to make second texture");
991 continue;
992 }
Brian Salomonaad83152019-05-24 10:16:35 -0400993 auto ii2 = ii.makeWH(8, 8);
Brian Salomon72050802020-10-12 20:45:06 -0400994 auto mbet3 = sk_gpu_test::ManagedBackendTexture::MakeFromInfo(
995 context, ii2, GrMipmapped::kNo, GrRenderable::kYes);
Brian Salomonaad83152019-05-24 10:16:35 -0400996 GrBackendTexture backendTexture3;
Brian Salomon72050802020-10-12 20:45:06 -0400997 if (!mbet3) {
Brian Salomonaad83152019-05-24 10:16:35 -0400998 ERRORF(reporter, "Couldn't create different sized texture.");
999 continue;
1000 }
Brian Salomonaad83152019-05-24 10:16:35 -04001001
1002 auto surf = SkSurface::MakeFromBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -04001003 context, mbet1->texture(), kTopLeft_GrSurfaceOrigin, sampleCnt,
Brian Salomonaad83152019-05-24 10:16:35 -04001004 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1005 if (!surf) {
1006 continue;
1007 }
1008 surf->getCanvas()->clear(SK_ColorBLUE);
1009 // Change matrix, layer, and clip state before swapping out the backing texture.
1010 surf->getCanvas()->translate(5, 5);
1011 surf->getCanvas()->saveLayer(nullptr, nullptr);
1012 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1013 // switch origin while we're at it.
Brian Salomon72050802020-10-12 20:45:06 -04001014 bool replaced = surf->replaceBackendTexture(mbet2->texture(), kBottomLeft_GrSurfaceOrigin);
Brian Salomonaad83152019-05-24 10:16:35 -04001015 REPORTER_ASSERT(reporter, replaced);
1016 SkPaint paint;
1017 paint.setColor(SK_ColorRED);
1018 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1019 surf->getCanvas()->restore();
1020
1021 // Check that the replacement texture got the right color values.
1022 SkAutoPixmapStorage pm;
1023 pm.alloc(ii);
1024 bool bad = !surf->readPixels(pm, 0, 0);
1025 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1026 for (int y = 0; y < ii.height() && !bad; ++y) {
1027 for (int x = 0; x < ii.width() && !bad; ++x) {
1028 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1029 auto found = *pm.addr32(x, y);
1030 if (found != expected) {
1031 bad = true;
1032 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1033 expected, found, x, y);
1034 }
1035 }
1036 }
1037 // The original texture should still be all blue.
1038 surf = SkSurface::MakeFromBackendTexture(
Brian Salomon72050802020-10-12 20:45:06 -04001039 context, mbet1->texture(), kBottomLeft_GrSurfaceOrigin, sampleCnt,
Brian Salomonaad83152019-05-24 10:16:35 -04001040 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1041 if (!surf) {
1042 ERRORF(reporter, "Could not create second surface.");
1043 continue;
1044 }
1045 bad = !surf->readPixels(pm, 0, 0);
1046 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1047 for (int y = 0; y < ii.height() && !bad; ++y) {
1048 for (int x = 0; x < ii.width() && !bad; ++x) {
1049 auto expected = 0xFFFF0000;
1050 auto found = *pm.addr32(x, y);
1051 if (found != expected) {
1052 bad = true;
1053 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1054 expected, found, x, y);
1055 }
1056 }
1057 }
1058
1059 // Can't replace with the same texture
1060 REPORTER_ASSERT(reporter,
Brian Salomon72050802020-10-12 20:45:06 -04001061 !surf->replaceBackendTexture(mbet1->texture(), kTopLeft_GrSurfaceOrigin));
Brian Salomonaad83152019-05-24 10:16:35 -04001062 // Can't replace with invalid texture
1063 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1064 // Can't replace with different size texture.
1065 REPORTER_ASSERT(reporter,
Brian Salomon72050802020-10-12 20:45:06 -04001066 !surf->replaceBackendTexture(mbet3->texture(), kTopLeft_GrSurfaceOrigin));
Brian Salomonaad83152019-05-24 10:16:35 -04001067 // Can't replace texture of non-wrapped SkSurface.
1068 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1069 REPORTER_ASSERT(reporter, surf);
1070 if (surf) {
Brian Salomon72050802020-10-12 20:45:06 -04001071 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(mbet1->texture(),
Brian Salomonaad83152019-05-24 10:16:35 -04001072 kTopLeft_GrSurfaceOrigin));
1073 }
1074 }
1075}
1076
Matt Sarett22886c42016-11-22 11:31:41 -05001077static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001078 SkOverdrawCanvas canvas(surface->getCanvas());
1079 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001080 sk_sp<SkImage> image = surface->makeImageSnapshot();
1081
1082 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001083 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001084 for (int y = 0; y < 10; y++) {
1085 for (int x = 0; x < 10; x++) {
1086 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1087 }
1088 }
1089}
1090
1091DEF_TEST(OverdrawSurface_Raster, r) {
1092 sk_sp<SkSurface> surface = create_surface();
1093 test_overdraw_surface(r, surface.get());
1094}
1095
Matt Sarett22886c42016-11-22 11:31:41 -05001096DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001097 auto context = ctxInfo.directContext();
Matt Sarett22886c42016-11-22 11:31:41 -05001098 sk_sp<SkSurface> surface = create_gpu_surface(context);
1099 test_overdraw_surface(r, surface.get());
1100}
Mike Reed44d04bd2017-06-28 19:57:21 -04001101
1102DEF_TEST(Surface_null, r) {
1103 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1104
1105 const int w = 37;
1106 const int h = 1000;
1107 auto surf = SkSurface::MakeNull(w, h);
1108 auto canvas = surf->getCanvas();
1109
1110 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1111 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1112}
Mike Reedd4746982018-02-07 16:05:29 -05001113
1114// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1115// (so the snapshot can succeed)
1116DEF_TEST(surface_image_unity, reporter) {
1117 auto do_test = [reporter](const SkImageInfo& info) {
1118 size_t rowBytes = info.minRowBytes();
1119 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1120 if (surf) {
1121 auto img = surf->makeImageSnapshot();
1122 if (!img && false) { // change to true to document the differences
1123 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001124 info.width(),
1125 info.height(),
1126 ToolUtils::colortype_name(info.colorType()),
1127 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001128 return;
1129 }
1130 REPORTER_ASSERT(reporter, img != nullptr);
1131
1132 char dummyPixel = 0; // just need a valid address (not a valid size)
1133 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1134 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1135 REPORTER_ASSERT(reporter, img != nullptr);
1136 }
1137 };
1138
Mike Kleine978ca22018-10-29 11:29:58 -04001139 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001140 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001141 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001142 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001143 SkAlphaType at = static_cast<SkAlphaType>(ati);
1144 for (int32_t size : sizes) {
1145 do_test(SkImageInfo::Make(1, size, ct, at));
1146 do_test(SkImageInfo::Make(size, 1, ct, at));
1147 }
1148 }
1149 }
1150}