blob: 7c0758d849625f19ce9cd81b6333b438a1551231 [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"
16#include "include/gpu/GrContext.h"
Brian Salomonaad83152019-05-24 10:16:35 -040017#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkDevice.h"
19#include "src/core/SkUtils.h"
20#include "src/gpu/GrContextPriv.h"
21#include "src/gpu/GrGpu.h"
22#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040023#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrRenderTargetContext.h"
25#include "src/gpu/GrResourceProvider.h"
26#include "src/gpu/SkGpuDevice.h"
27#include "src/image/SkImage_Base.h"
28#include "src/image/SkImage_Gpu.h"
29#include "src/image/SkSurface_Gpu.h"
30#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040031#include "tests/TestUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000032
Hal Canary8a001442018-09-19 11:31:27 -040033#include <functional>
34#include <initializer_list>
35#include <vector>
36
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "tools/ToolUtils.h"
bsalomon74f681d2015-06-23 14:38:48 -070038
kkinnunen179a8f52015-11-20 13:32:24 -080039static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070040 SkASSERT(pixels == context);
41 sk_free(pixels);
42}
reede8f30622016-03-23 18:59:25 -070043static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
44 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070045 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000046 if (requestedInfo) {
47 *requestedInfo = info;
48 }
reede8f30622016-03-23 18:59:25 -070049 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000050}
reede8f30622016-03-23 18:59:25 -070051static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
52 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080053 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
54 if (requestedInfo) {
55 *requestedInfo = info;
56 }
57 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040058 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070059 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
60 release_direct_surface_storage,
61 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080062}
reede8f30622016-03-23 18:59:25 -070063static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
64 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080065 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
66 if (requestedInfo) {
67 *requestedInfo = info;
68 }
robertphillips7e922762016-07-26 11:38:17 -070069 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080070}
reede8f30622016-03-23 18:59:25 -070071static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
72 SkAlphaType at = kPremul_SkAlphaType,
73 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080074 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
75 if (requestedInfo) {
76 *requestedInfo = info;
77 }
robertphillips7e922762016-07-26 11:38:17 -070078 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080079}
junov@chromium.org995beb62013-03-28 13:49:22 +000080
kkinnunen179a8f52015-11-20 13:32:24 -080081DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080082 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070083 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
84 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080085
reedb2497c22014-12-31 12:31:43 -080086}
egdanielab527a52016-06-28 08:07:26 -070087DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080088 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
89 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070090 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080091}
reedb2497c22014-12-31 12:31:43 -080092
Brian Salomonbdecacf2018-02-02 20:32:49 -050093DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -040094 GrContext* context = ctxInfo.grContext();
95
Brian Salomonbdecacf2018-02-02 20:32:49 -050096 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
97 static constexpr int kSize = 10;
98
99 SkColorType colorType = static_cast<SkColorType>(ct);
100 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500101
Robert Phillips429f0d32019-09-11 17:03:28 -0400102 {
103 bool can = context->colorTypeSupportedAsSurface(colorType);
104 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
105 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
106 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500107
Robert Phillips429f0d32019-09-11 17:03:28 -0400108 GrBackendTexture backendTex = context->createBackendTexture(
109 kSize, kSize, colorType,
110 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
111 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
112 kTopLeft_GrSurfaceOrigin, 0, colorType,
113 nullptr, nullptr);
114 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
115 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500116
Robert Phillips429f0d32019-09-11 17:03:28 -0400117 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
118 kTopLeft_GrSurfaceOrigin, 1,
119 colorType, nullptr, nullptr);
120 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
121 colorType, can, SkToBool(surf));
Brian Salomonbdecacf2018-02-02 20:32:49 -0500122
Robert Phillips429f0d32019-09-11 17:03:28 -0400123 surf.reset();
124 context->flush();
125 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500126 }
127
Robert Phillips429f0d32019-09-11 17:03:28 -0400128 // The MSAA test only makes sense if the colorType is renderable to begin with.
129 if (context->colorTypeSupportedAsSurface(colorType)) {
130 static constexpr int kSampleCnt = 2;
131
132 bool can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
133 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt,
134 nullptr);
135 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
136 colorType, can, SkToBool(surf));
137
138 GrBackendTexture backendTex = context->createBackendTexture(
139 kSize, kSize, colorType,
140 SkColors::kTransparent,
141 GrMipMapped::kNo, GrRenderable::kYes,
142 GrProtected::kNo);
143 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
144 kTopLeft_GrSurfaceOrigin, kSampleCnt,
145 colorType, nullptr, nullptr);
146 REPORTER_ASSERT(reporter, can == SkToBool(surf),
147 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
148 colorType);
149 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
150 if (surf) {
151 auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
152 int storedCnt = rtc->numSamples();
153 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
154 storedCnt, backendTex.getBackendFormat());
155 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
156 "Should store an allowed sample count (%d vs %d)", allowedCnt,
157 storedCnt);
158 }
159
160 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
161 kTopLeft_GrSurfaceOrigin,
162 kSampleCnt, colorType,
163 nullptr, nullptr);
164 REPORTER_ASSERT(reporter, can == SkToBool(surf),
165 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
166 colorType);
167 if (surf) {
168 auto rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
169 int storedCnt = rtc->numSamples();
170 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
171 storedCnt, backendTex.getBackendFormat());
172 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
173 "Should store an allowed sample count (%d vs %d)", allowedCnt,
174 storedCnt);
175 }
176
177 surf.reset();
178 context->flush();
179 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500180 }
181
Robert Phillips429f0d32019-09-11 17:03:28 -0400182 {
183 auto* gpu = context->priv().getGpu();
Robert Phillips9b16f812019-05-17 10:01:21 -0400184
Robert Phillips429f0d32019-09-11 17:03:28 -0400185 GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget(
186 16, 16, SkColorTypeToGrColorType(colorType));
187 bool can = context->colorTypeSupportedAsSurface(colorType);
188 auto surf = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget,
189 kTopLeft_GrSurfaceOrigin, colorType,
190 nullptr, nullptr);
191 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType,
192 can, SkToBool(surf));
193 surf.reset();
194 context->flush();
195 if (backendRenderTarget.isValid()) {
196 gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget);
197 }
Brian Salomon93348dd2018-08-29 12:56:23 -0400198 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500199 }
200}
201
202DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400203 GrContext* context = ctxInfo.grContext();
204
205 static constexpr int kSize = 10;
206
Brian Salomonbdecacf2018-02-02 20:32:49 -0500207 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500208
209 SkColorType colorType = static_cast<SkColorType>(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -0400210 int max = context->maxSurfaceSampleCountForColorType(colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500211 if (!max) {
212 continue;
213 }
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400214 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400215 kSize, kSize, colorType, SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400216 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400217 if (!backendTex.isValid()) {
218 continue;
219 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400220 SkScopeExit freeTex([&backendTex, context] {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400221 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400222 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400223
224 if (!context->colorTypeSupportedAsSurface(colorType)) {
225 continue;
226 }
227
Brian Salomonbdecacf2018-02-02 20:32:49 -0500228 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Robert Phillips9b16f812019-05-17 10:01:21 -0400229 auto surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500230 kTopLeft_GrSurfaceOrigin, max,
231 colorType, nullptr, nullptr);
232 REPORTER_ASSERT(reporter, surf);
233 if (!surf) {
234 continue;
235 }
236 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
237 ->getDevice()
238 ->accessRenderTargetContext()
Chris Dalton6ce447a2019-06-23 18:07:38 -0600239 ->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500240 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
241 }
242}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500243
kkinnunen179a8f52015-11-20 13:32:24 -0800244static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700245 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800246 const SkImageInfo& requestInfo,
247 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000248 const SkColor color = SK_ColorRED;
249 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800250 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000251
reed6ceeebd2016-03-09 14:26:26 -0800252 SkPixmap pmap;
253 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800254 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000255
reed6ceeebd2016-03-09 14:26:26 -0800256 SkPixmap pmap2;
257 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000258
kkinnunen179a8f52015-11-20 13:32:24 -0800259 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800260 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
261 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
262 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000263
reed6ceeebd2016-03-09 14:26:26 -0800264 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
265 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
266 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800267 } else {
268 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000269 }
270}
kkinnunen179a8f52015-11-20 13:32:24 -0800271DEF_TEST(SurfaceCanvasPeek, reporter) {
272 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
273 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700274 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800275 test_canvas_peek(reporter, surface, requestInfo, true);
276 }
277}
egdanielab527a52016-06-28 08:07:26 -0700278DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800279 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
280 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700281 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800282 test_canvas_peek(reporter, surface, requestInfo, false);
283 }
284}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000285
reede8f30622016-03-23 18:59:25 -0700286static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700287 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800288 REPORTER_ASSERT(reporter, surface);
289 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700290 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800291 REPORTER_ASSERT(reporter, image);
292 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700293 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700294 }
295 }
296}
kkinnunen179a8f52015-11-20 13:32:24 -0800297DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
298 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700299 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
300 auto surface(surface_func(at, nullptr));
301 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700302 }
303 }
304}
egdanielab527a52016-06-28 08:07:26 -0700305DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800306 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700307 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
308 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
309 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
310 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800311 }
312 }
313}
bsalomon74f681d2015-06-23 14:38:48 -0700314
Robert Phillips8caf85f2018-04-05 09:30:38 -0400315static void test_backend_texture_access_copy_on_write(
316 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
317 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700318 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700319
Robert Phillips8caf85f2018-04-05 09:30:38 -0400320 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700321 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700322
323 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400324 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700325}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400326
327static void test_backend_rendertarget_access_copy_on_write(
328 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
329 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
330 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
331
332 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
333 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
334
335 // If the access mode triggers CoW, then the backend objects should reflect it.
336 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
337 (snap1 == snap2));
338}
339
340DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800341 const SkSurface::BackendHandleAccess accessModes[] = {
342 SkSurface::kFlushRead_BackendHandleAccess,
343 SkSurface::kFlushWrite_BackendHandleAccess,
344 SkSurface::kDiscardWrite_BackendHandleAccess,
345 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400346
kkinnunen179a8f52015-11-20 13:32:24 -0800347 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400348 for (auto& accessMode : accessModes) {
349 {
bsalomon8b7451a2016-05-11 06:33:06 -0700350 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400351 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
352 }
353 {
354 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
355 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800356 }
357 }
358 }
359}
kkinnunen179a8f52015-11-20 13:32:24 -0800360
Robert Phillips8caf85f2018-04-05 09:30:38 -0400361template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
362static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700363 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400364
365 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
366 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700367 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800368 // just read access should not affect the snapshot
369 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
370
Robert Phillips8caf85f2018-04-05 09:30:38 -0400371 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
372 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700373 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800374 // expect a new image, since we claimed we would write
375 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
376
Robert Phillips8caf85f2018-04-05 09:30:38 -0400377 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
378 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700379 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800380 // expect a new(er) image, since we claimed we would write
381 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
382 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
383}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400384
kkinnunen179a8f52015-11-20 13:32:24 -0800385// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700386DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800387 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400388 {
389 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
390 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
391 surface.get());
392 }
393 {
394 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
395 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
396 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800397 }
398 }
399}
kkinnunen179a8f52015-11-20 13:32:24 -0800400
401// Verify that the right canvas commands trigger a copy on write.
402static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000403 SkCanvas* canvas = surface->getCanvas();
404
405 const SkRect testRect =
406 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
407 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000408 SkPath testPath;
409 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
410 SkIntToScalar(2), SkIntToScalar(1)));
411
412 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
413
414 SkRegion testRegion;
415 testRegion.setRect(testIRect);
416
417
418 const SkColor testColor = 0x01020304;
419 const SkPaint testPaint;
420 const SkPoint testPoints[3] = {
421 {SkIntToScalar(0), SkIntToScalar(0)},
422 {SkIntToScalar(2), SkIntToScalar(1)},
423 {SkIntToScalar(0), SkIntToScalar(2)}
424 };
425 const size_t testPointCount = 3;
426
427 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000428 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000429 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000430
431 SkRRect testRRect;
432 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
433
434 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000435
436#define EXPECT_COPY_ON_WRITE(command) \
437 { \
reed9ce9d672016-03-17 10:51:11 -0700438 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
439 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000440 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700441 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
442 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000443 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
444 }
445
446 EXPECT_COPY_ON_WRITE(clear(testColor))
447 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
448 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
449 testPaint))
450 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
451 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
452 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
453 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
454 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700455 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700456 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Hal Canary89a644b2019-01-07 09:36:09 -0500457 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800458}
459DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700460 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800461}
egdanielab527a52016-06-28 08:07:26 -0700462DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800463 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700464 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700465 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700466 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000467}
468
kkinnunen179a8f52015-11-20 13:32:24 -0800469static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
470 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000471 // This test succeeds by not triggering an assertion.
472 // The test verifies that the surface remains writable (usable) after
473 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000474 SkCanvas* canvas = surface->getCanvas();
475 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700476 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000477 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000478}
kkinnunen179a8f52015-11-20 13:32:24 -0800479DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700480 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800481}
egdanielab527a52016-06-28 08:07:26 -0700482DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800483 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700484 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700485 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800486 }
487}
junov@chromium.orgda904742013-05-01 22:38:16 +0000488
kkinnunen179a8f52015-11-20 13:32:24 -0800489static void test_crbug263329(skiatest::Reporter* reporter,
490 SkSurface* surface1,
491 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000492 // This is a regression test for crbug.com/263329
493 // Bug was caused by onCopyOnWrite releasing the old surface texture
494 // back to the scratch texture pool even though the texture is used
495 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000496 SkCanvas* canvas1 = surface1->getCanvas();
497 SkCanvas* canvas2 = surface2->getCanvas();
498 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700499 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000500 // Trigger copy on write, new backing is a scratch texture
501 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700502 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000503 // Trigger copy on write, old backing should not be returned to scratch
504 // pool because it is held by image2
505 canvas1->clear(3);
506
507 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700508 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000509 // Trigger copy on write on surface2. The new backing store should not
510 // be recycling a texture that is held by an existing image.
511 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700512 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400513 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000514 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400515 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
516 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
517 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
518 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
519 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000520}
egdanielab527a52016-06-28 08:07:26 -0700521DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800522 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700523 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
524 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700525 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800526 }
527}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000528
kkinnunen179a8f52015-11-20 13:32:24 -0800529DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700530 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700531 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500532 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800533 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500534 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800535}
egdanielab527a52016-06-28 08:07:26 -0700536DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800537 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700538 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700539 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500540
541 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400542 GrBackendTexture backendTex = image->getBackendTexture(false);
543 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800544 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500545 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400546 GrBackendTexture backendTex2 = image->getBackendTexture(false);
547 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000548 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000549}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800550
reede8f30622016-03-23 18:59:25 -0700551static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
552 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500553
554 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
555 ->asRenderTargetProxy();
556 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800557}
558
bsalomon5ec26ae2016-02-25 08:33:02 -0800559static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400560 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800561}
562
reed9ce9d672016-03-17 10:51:11 -0700563static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
564 return is_budgeted(image.get());
565}
566
egdanielab527a52016-06-28 08:07:26 -0700567DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800568 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400569 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
570 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
571 SkASSERT(surface);
572 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800573
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400574 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800575
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400576 // Initially the image shares a texture with the surface, and the
577 // the budgets should always match.
578 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
579 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800580
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400581 // Now trigger copy-on-write
582 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800583
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400584 // They don't share a texture anymore but the budgets should still match.
585 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
586 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800587 }
588}
junov@chromium.orgda904742013-05-01 22:38:16 +0000589
kkinnunen179a8f52015-11-20 13:32:24 -0800590static void test_no_canvas1(skiatest::Reporter* reporter,
591 SkSurface* surface,
592 SkSurface::ContentChangeMode mode) {
593 // Test passes by not asserting
594 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800595}
596static void test_no_canvas2(skiatest::Reporter* reporter,
597 SkSurface* surface,
598 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000599 // Verifies the robustness of SkSurface for handling use cases where calls
600 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700601 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
602 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800603 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700604 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
605 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800606 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000607}
kkinnunen179a8f52015-11-20 13:32:24 -0800608DEF_TEST(SurfaceNoCanvas, reporter) {
609 SkSurface::ContentChangeMode modes[] =
610 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
611 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
612 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700613 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800614 }
615 }
616}
egdanielab527a52016-06-28 08:07:26 -0700617DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800618 SkSurface::ContentChangeMode modes[] =
619 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
620 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
621 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
622 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700623 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700624 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700625 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000626 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000627 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000628}
reed9cd016e2016-01-30 10:01:06 -0800629
630static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800631 SkPixmap surfacePM;
632 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800633
reed9ce9d672016-03-17 10:51:11 -0700634 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800635 SkPixmap pm;
636 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800637
reed6ceeebd2016-03-09 14:26:26 -0800638 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800639
640 // trigger a copy-on-write
641 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700642 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800643 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
644
reed6ceeebd2016-03-09 14:26:26 -0800645 SkPixmap pm2;
646 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
647 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800648}
649
650DEF_TEST(surface_rowbytes, reporter) {
651 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
652
reede8f30622016-03-23 18:59:25 -0700653 auto surf0(SkSurface::MakeRaster(info));
654 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800655
656 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700657 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
658 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800659
660 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700661 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800662 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400663 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800664 REPORTER_ASSERT(reporter, nullptr == s);
665}
bsalomone63ffef2016-02-05 07:17:34 -0800666
fmalita03912f12016-07-06 06:22:06 -0700667DEF_TEST(surface_raster_zeroinitialized, reporter) {
668 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
669 SkPixmap pixmap;
670 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
671
672 for (int i = 0; i < pixmap.info().width(); ++i) {
673 for (int j = 0; j < pixmap.info().height(); ++j) {
674 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
675 }
676 }
677}
678
ericrkc4025182016-05-04 12:01:58 -0700679static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400680 GrContext* ctx, int sampleCnt, const SkColor4f& color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500681
Michael Ludwig72ab3462018-12-10 12:43:36 -0500682 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
683 // triggers what appears to be a driver race condition where the 10x10 surface from the
684 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
685 // test.
686 //
687 // Immediately after creation of this surface, readback shows the correct initial solid color.
688 // However, sometime before content is rendered into the upper half of the surface, the driver
689 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
690 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
691 // untouched bottom half contains random pixel values that trigger asserts in the
692 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
693 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
694 //
695 // Requesting a much larger backend texture size seems to prevent it from reusing the same
696 // memory and avoids the issue.
697#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700698 const int kWidth = 10;
699 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500700#else
701 const int kWidth = 100;
702 const int kHeight = 100;
703#endif
704
Robert Phillipsee5fd132019-05-07 13:29:22 -0400705 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
706 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000707
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400708 if (!create_backend_texture(ctx, outTexture, ii, color, GrMipMapped::kNo,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400709 GrRenderable::kYes)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500710 return nullptr;
711 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000712
Robert Phillipsee5fd132019-05-07 13:29:22 -0400713 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(ctx, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400714 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500715 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000716 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700717 if (!surface) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400718 delete_backend_texture(ctx, *outTexture);
ericrkc4025182016-05-04 12:01:58 -0700719 return nullptr;
720 }
ericrkc4025182016-05-04 12:01:58 -0700721 return surface;
722}
bsalomone63ffef2016-02-05 07:17:34 -0800723
ericrkc4025182016-05-04 12:01:58 -0700724static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400725 GrContext* ctx, int sampleCnt, const SkColor4f& color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500726
ericrkc4025182016-05-04 12:01:58 -0700727 const int kWidth = 10;
728 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000729
Robert Phillipsee5fd132019-05-07 13:29:22 -0400730 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
731 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000732
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400733 if (!create_backend_texture(ctx, outTexture, ii, color, GrMipMapped::kNo,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400734 GrRenderable::kYes)) {
ericrkc4025182016-05-04 12:01:58 -0700735 return nullptr;
736 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500737
738 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400739 ctx, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
Greg Danielfaa095e2017-12-19 13:15:02 -0500740 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500741
742 if (!surface) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400743 delete_backend_texture(ctx, *outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500744 return nullptr;
745 }
ericrkc4025182016-05-04 12:01:58 -0700746 return surface;
747}
748
Brian Salomonbf6b9792019-08-21 09:38:10 -0400749static void test_surface_context_clear(skiatest::Reporter* reporter,
750 GrSurfaceContext* surfaceContext, uint32_t expectedValue) {
751 int w = surfaceContext->width();
752 int h = surfaceContext->height();
Robert Phillipsd3442842019-08-02 12:26:22 -0400753
754 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
755
756 SkAutoPixmapStorage readback;
757 readback.alloc(ii);
bsalomone63ffef2016-02-05 07:17:34 -0800758
Robert Phillipsd3442842019-08-02 12:26:22 -0400759 readback.erase(~expectedValue);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400760 surfaceContext->readPixels(readback.info(), readback.writable_addr(), readback.rowBytes(),
761 {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800762 for (int y = 0; y < h; ++y) {
763 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400764 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -0800765 if (pixel != expectedValue) {
766 SkString msg;
767 if (expectedValue) {
768 msg = "SkSurface should have left render target unmodified";
769 } else {
770 msg = "SkSurface should have cleared the render target";
771 }
772 ERRORF(reporter,
773 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
774 expectedValue, x, y);
775 return;
776 }
777 }
778 }
779}
780
bsalomon758586c2016-04-06 14:02:39 -0700781DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700782 GrContext* context = ctxInfo.grContext();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400783 // Snaps an image from a surface and then makes a GrSurfaceContext from the image's texture.
784 auto makeImageSurfaceContext = [context](SkSurface* surface) {
785 sk_sp<SkImage> i(surface->makeImageSnapshot());
786 SkImage_Gpu* gpuImage = (SkImage_Gpu*)as_IB(i);
787 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(context);
788 return context->priv().makeWrappedSurfaceContext(
789 std::move(proxy), SkColorTypeToGrColorType(i->colorType()), kPremul_SkAlphaType,
790 gpuImage->refColorSpace());
bsalomone63ffef2016-02-05 07:17:34 -0800791 };
ericrkc4025182016-05-04 12:01:58 -0700792
Brian Salomonbf6b9792019-08-21 09:38:10 -0400793 // Test that non-wrapped RTs are created clear.
794 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
795 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
796 if (!surface) {
797 ERRORF(reporter, "Could not create GPU SkSurface.");
798 return;
bsalomone63ffef2016-02-05 07:17:34 -0800799 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400800 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
801 if (!rtc) {
802 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
803 return;
ericrkc4025182016-05-04 12:01:58 -0700804 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400805 test_surface_context_clear(reporter, rtc, 0x0);
806 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
807 test_surface_context_clear(reporter, imageSurfaceCtx.get(), 0x0);
808 }
809
810 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
811 const SkColor4f kOrigColor{.67f, .67f, .67f, 1};
812 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
813 &create_gpu_surface_backend_texture_as_render_target}) {
814 GrBackendTexture backendTex;
815 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
816 if (!surface) {
817 ERRORF(reporter, "Could not create GPU SkSurface.");
818 return;
819 }
820 auto rtc = surface->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
821 if (!rtc) {
822 ERRORF(reporter, "Could access surface context of GPU SkSurface.");
823 return;
824 }
825 test_surface_context_clear(reporter, rtc, kOrigColor.toSkColor());
826 auto imageSurfaceCtx = makeImageSurfaceContext(surface.get());
827 test_surface_context_clear(reporter, imageSurfaceCtx.get(), kOrigColor.toSkColor());
828 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700829 }
830}
bsalomone63ffef2016-02-05 07:17:34 -0800831
ericrkc4025182016-05-04 12:01:58 -0700832static void test_surface_draw_partially(
Robert Phillips66944402019-09-30 13:21:25 -0400833 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
ericrkc4025182016-05-04 12:01:58 -0700834 const int kW = surface->width();
835 const int kH = surface->height();
836 SkPaint paint;
837 const SkColor kRectColor = ~origColor | 0xFF000000;
838 paint.setColor(kRectColor);
Robert Phillipsd3442842019-08-02 12:26:22 -0400839 surface->getCanvas()->drawRect(SkRect::MakeIWH(kW, kH/2), paint);
840
ericrkc4025182016-05-04 12:01:58 -0700841 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
842 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsd3442842019-08-02 12:26:22 -0400843
844 SkAutoPixmapStorage readback;
845 readback.alloc(readInfo);
846
847 readback.erase(~origColor);
848 SkAssertResult(surface->readPixels(readback.info(), readback.writable_addr(),
849 readback.rowBytes(), 0, 0));
ericrkc4025182016-05-04 12:01:58 -0700850 bool stop = false;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400851
Robert Phillips66944402019-09-30 13:21:25 -0400852 SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
853 SkColorGetR(origColor),
854 SkColorGetG(origColor),
855 SkColorGetB(origColor));
856 SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
857 SkColorGetR(kRectColor),
858 SkColorGetG(kRectColor),
859 SkColorGetB(kRectColor));
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400860
ericrkc4025182016-05-04 12:01:58 -0700861 for (int y = 0; y < kH/2 && !stop; ++y) {
862 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400863 REPORTER_ASSERT(reporter, rectColorPM == readback.addr32()[x + y * kW]);
864 if (rectColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400865 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400866 readback.addr32()[x + y * kW], rectColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700867 stop = true;
868 }
869 }
870 }
871 stop = false;
872 for (int y = kH/2; y < kH && !stop; ++y) {
873 for (int x = 0; x < kW && !stop; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -0400874 REPORTER_ASSERT(reporter, origColorPM == readback.addr32()[x + y * kW]);
875 if (origColorPM != readback.addr32()[x + y * kW]) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400876 SkDebugf("--- got [%x] expected [%x], x = %d, y = %d\n",
Robert Phillipsd3442842019-08-02 12:26:22 -0400877 readback.addr32()[x + y * kW], origColorPM, x, y);
ericrkc4025182016-05-04 12:01:58 -0700878 stop = true;
879 }
880 }
881 }
882}
bsalomone63ffef2016-02-05 07:17:34 -0800883
egdanielab527a52016-06-28 08:07:26 -0700884DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400885 GrContext* context = ctxInfo.grContext();
886
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400887 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
bsalomone63ffef2016-02-05 07:17:34 -0800888
ericrkc4025182016-05-04 12:01:58 -0700889 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
890 &create_gpu_surface_backend_texture_as_render_target}) {
891 // Validate that we can draw to the canvas and that the original texture color is
892 // preserved in pixels that aren't rendered to via the surface.
893 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500894 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400895 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700896 if (surface) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400897 test_surface_draw_partially(reporter, surface, kOrigColor.toSkColor());
ericrkc4025182016-05-04 12:01:58 -0700898 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400899 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700900 }
901 }
902}
903
Greg Daniel8ce79912019-02-05 10:08:43 -0500904struct ReleaseChecker {
905 ReleaseChecker() : fReleaseCount(0) {}
906 int fReleaseCount;
907 static void Release(void* self) {
908 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
909 }
910};
911
912
913DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
914 const int kWidth = 10;
915 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500916
917 GrContext* ctx = ctxInfo.grContext();
918 GrGpu* gpu = ctx->priv().getGpu();
919
920 for (bool useTexture : {false, true}) {
921 GrBackendTexture backendTex;
922 GrBackendRenderTarget backendRT;
923 sk_sp<SkSurface> surface;
924
925 ReleaseChecker releaseChecker;
926 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
927
928 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400929 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
930 kPremul_SkAlphaType);
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400931 if (!create_backend_texture(ctx, &backendTex, ii, SkColors::kRed, GrMipMapped::kNo,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400932 GrRenderable::kYes)) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500933 continue;
934 }
935
936 surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1,
937 kRGBA_8888_SkColorType,
938 nullptr, nullptr,
939 ReleaseChecker::Release,
940 &releaseChecker);
941 } else {
942 backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight,
943 GrColorType::kRGBA_8888);
944 if (!backendRT.isValid()) {
945 continue;
946 }
947 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
948 kRGBA_8888_SkColorType,
949 nullptr, nullptr,
950 ReleaseChecker::Release,
951 &releaseChecker);
952 }
953 if (!surface) {
954 ERRORF(reporter, "Failed to create surface");
955 continue;
956 }
957
958 surface->getCanvas()->clear(SK_ColorRED);
959 surface->flush();
960 gpu->testingOnly_flushGpuAndSync();
961
962 // Now exercise the release proc
963 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
964 surface.reset(nullptr); // force a release of the surface
965 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
966
967 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400968 delete_backend_texture(ctx, backendTex);
Greg Daniel8ce79912019-02-05 10:08:43 -0500969 } else {
970 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
971 }
972 }
973}
ericrkc4025182016-05-04 12:01:58 -0700974
975DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400976 GrContext* context = ctxInfo.grContext();
977 const GrCaps* caps = context->priv().caps();
978
979 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -0700980 return;
981 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400982
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400983 static const SkColor4f kOrigColor { 0.667f, 0.733f, 0.8f, 1 };
ericrkc4025182016-05-04 12:01:58 -0700984
Robert Phillips9b16f812019-05-17 10:01:21 -0400985 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500986
ericrkc4025182016-05-04 12:01:58 -0700987 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
988 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500989 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500990 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400991 auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700992
Brian Salomonbdecacf2018-02-02 20:32:49 -0500993 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500994 // Certain platforms don't support MSAA, skip these.
995 continue;
ericrkc4025182016-05-04 12:01:58 -0700996 }
997
998 // Validate that we can attach a stencil buffer to an SkSurface created by either of
999 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -04001000 GrRenderTarget* rt = surface->getCanvas()
1001 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Chris Daltoneffee202019-07-01 22:28:03 -06001002 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt));
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001003 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -07001004 }
bsalomone63ffef2016-02-05 07:17:34 -08001005 }
1006}
brianosman0e22eb82016-08-30 07:07:59 -07001007
Brian Salomonaad83152019-05-24 10:16:35 -04001008DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
1009 GrContext* context = ctxInfo.grContext();
1010
1011 for (int sampleCnt : {1, 2}) {
1012 GrBackendTexture backendTexture1;
1013 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Robert Phillips4d87b2b2019-07-23 13:44:16 -04001014 if (!create_backend_texture(context, &backendTexture1, ii, SkColors::kTransparent,
1015 GrMipMapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001016 continue;
1017 }
1018 SkScopeExit delete1(
1019 [context, &backendTexture1] { delete_backend_texture(context, backendTexture1); });
1020 GrBackendTexture backendTexture2;
Robert Phillips4d87b2b2019-07-23 13:44:16 -04001021 if (!create_backend_texture(context, &backendTexture2, ii, SkColors::kTransparent,
1022 GrMipMapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001023 ERRORF(reporter, "Expected to be able to make second texture");
1024 continue;
1025 }
1026 SkScopeExit delete2(
1027 [context, &backendTexture2] { delete_backend_texture(context, backendTexture2); });
1028 auto ii2 = ii.makeWH(8, 8);
1029 GrBackendTexture backendTexture3;
Robert Phillips4d87b2b2019-07-23 13:44:16 -04001030 if (!create_backend_texture(context, &backendTexture3, ii2, SkColors::kTransparent,
1031 GrMipMapped::kNo, GrRenderable::kYes)) {
Brian Salomonaad83152019-05-24 10:16:35 -04001032 ERRORF(reporter, "Couldn't create different sized texture.");
1033 continue;
1034 }
1035 SkScopeExit delete3(
1036 [context, &backendTexture3] { delete_backend_texture(context, backendTexture3); });
1037
1038 auto surf = SkSurface::MakeFromBackendTexture(
1039 context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt,
1040 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1041 if (!surf) {
1042 continue;
1043 }
1044 surf->getCanvas()->clear(SK_ColorBLUE);
1045 // Change matrix, layer, and clip state before swapping out the backing texture.
1046 surf->getCanvas()->translate(5, 5);
1047 surf->getCanvas()->saveLayer(nullptr, nullptr);
1048 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1049 // switch origin while we're at it.
1050 bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin);
1051 REPORTER_ASSERT(reporter, replaced);
1052 SkPaint paint;
1053 paint.setColor(SK_ColorRED);
1054 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1055 surf->getCanvas()->restore();
1056
1057 // Check that the replacement texture got the right color values.
1058 SkAutoPixmapStorage pm;
1059 pm.alloc(ii);
1060 bool bad = !surf->readPixels(pm, 0, 0);
1061 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1062 for (int y = 0; y < ii.height() && !bad; ++y) {
1063 for (int x = 0; x < ii.width() && !bad; ++x) {
1064 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1065 auto found = *pm.addr32(x, y);
1066 if (found != expected) {
1067 bad = true;
1068 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1069 expected, found, x, y);
1070 }
1071 }
1072 }
1073 // The original texture should still be all blue.
1074 surf = SkSurface::MakeFromBackendTexture(
1075 context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt,
1076 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1077 if (!surf) {
1078 ERRORF(reporter, "Could not create second surface.");
1079 continue;
1080 }
1081 bad = !surf->readPixels(pm, 0, 0);
1082 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1083 for (int y = 0; y < ii.height() && !bad; ++y) {
1084 for (int x = 0; x < ii.width() && !bad; ++x) {
1085 auto expected = 0xFFFF0000;
1086 auto found = *pm.addr32(x, y);
1087 if (found != expected) {
1088 bad = true;
1089 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1090 expected, found, x, y);
1091 }
1092 }
1093 }
1094
1095 // Can't replace with the same texture
1096 REPORTER_ASSERT(reporter,
1097 !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin));
1098 // Can't replace with invalid texture
1099 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1100 // Can't replace with different size texture.
1101 REPORTER_ASSERT(reporter,
1102 !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin));
1103 // Can't replace texture of non-wrapped SkSurface.
1104 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1105 REPORTER_ASSERT(reporter, surf);
1106 if (surf) {
1107 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1,
1108 kTopLeft_GrSurfaceOrigin));
1109 }
1110 }
1111}
1112
Matt Sarett22886c42016-11-22 11:31:41 -05001113static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001114 SkOverdrawCanvas canvas(surface->getCanvas());
1115 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001116 sk_sp<SkImage> image = surface->makeImageSnapshot();
1117
1118 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001119 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001120 for (int y = 0; y < 10; y++) {
1121 for (int x = 0; x < 10; x++) {
1122 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1123 }
1124 }
1125}
1126
1127DEF_TEST(OverdrawSurface_Raster, r) {
1128 sk_sp<SkSurface> surface = create_surface();
1129 test_overdraw_surface(r, surface.get());
1130}
1131
Matt Sarett22886c42016-11-22 11:31:41 -05001132DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1133 GrContext* context = ctxInfo.grContext();
1134 sk_sp<SkSurface> surface = create_gpu_surface(context);
1135 test_overdraw_surface(r, surface.get());
1136}
Mike Reed44d04bd2017-06-28 19:57:21 -04001137
1138DEF_TEST(Surface_null, r) {
1139 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1140
1141 const int w = 37;
1142 const int h = 1000;
1143 auto surf = SkSurface::MakeNull(w, h);
1144 auto canvas = surf->getCanvas();
1145
1146 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1147 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1148}
Mike Reedd4746982018-02-07 16:05:29 -05001149
1150// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1151// (so the snapshot can succeed)
1152DEF_TEST(surface_image_unity, reporter) {
1153 auto do_test = [reporter](const SkImageInfo& info) {
1154 size_t rowBytes = info.minRowBytes();
1155 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1156 if (surf) {
1157 auto img = surf->makeImageSnapshot();
1158 if (!img && false) { // change to true to document the differences
1159 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001160 info.width(),
1161 info.height(),
1162 ToolUtils::colortype_name(info.colorType()),
1163 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001164 return;
1165 }
1166 REPORTER_ASSERT(reporter, img != nullptr);
1167
1168 char dummyPixel = 0; // just need a valid address (not a valid size)
1169 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1170 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1171 REPORTER_ASSERT(reporter, img != nullptr);
1172 }
1173 };
1174
Mike Kleine978ca22018-10-29 11:29:58 -04001175 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001176 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001177 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001178 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001179 SkAlphaType at = static_cast<SkAlphaType>(ati);
1180 for (int32_t size : sizes) {
1181 do_test(SkImageInfo::Make(1, size, ct, at));
1182 do_test(SkImageInfo::Make(size, 1, ct, at));
1183 }
1184 }
1185 }
1186}