blob: 96ed7e2dabebdc2c61beb9e1bb8d3b73acfa1d28 [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"
23#include "src/gpu/GrRenderTargetContext.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/SkGpuDevice.h"
26#include "src/image/SkImage_Base.h"
27#include "src/image/SkImage_Gpu.h"
28#include "src/image/SkSurface_Gpu.h"
29#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040030#include "tests/TestUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000031
Hal Canary8a001442018-09-19 11:31:27 -040032#include <functional>
33#include <initializer_list>
34#include <vector>
35
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "tools/ToolUtils.h"
bsalomon74f681d2015-06-23 14:38:48 -070037
kkinnunen179a8f52015-11-20 13:32:24 -080038static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070039 SkASSERT(pixels == context);
40 sk_free(pixels);
41}
reede8f30622016-03-23 18:59:25 -070042static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
43 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070044 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000045 if (requestedInfo) {
46 *requestedInfo = info;
47 }
reede8f30622016-03-23 18:59:25 -070048 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000049}
reede8f30622016-03-23 18:59:25 -070050static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
51 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080052 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
53 if (requestedInfo) {
54 *requestedInfo = info;
55 }
56 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040057 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070058 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
59 release_direct_surface_storage,
60 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080061}
reede8f30622016-03-23 18:59:25 -070062static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
63 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080064 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
65 if (requestedInfo) {
66 *requestedInfo = info;
67 }
robertphillips7e922762016-07-26 11:38:17 -070068 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080069}
reede8f30622016-03-23 18:59:25 -070070static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
71 SkAlphaType at = kPremul_SkAlphaType,
72 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080073 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
74 if (requestedInfo) {
75 *requestedInfo = info;
76 }
robertphillips7e922762016-07-26 11:38:17 -070077 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080078}
junov@chromium.org995beb62013-03-28 13:49:22 +000079
kkinnunen179a8f52015-11-20 13:32:24 -080080DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080081 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070082 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
83 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080084
reedb2497c22014-12-31 12:31:43 -080085}
egdanielab527a52016-06-28 08:07:26 -070086DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080087 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
88 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070089 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080090}
reedb2497c22014-12-31 12:31:43 -080091
Brian Salomonbdecacf2018-02-02 20:32:49 -050092DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -040093 GrContext* context = ctxInfo.grContext();
94
Brian Salomonbdecacf2018-02-02 20:32:49 -050095 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
96 static constexpr int kSize = 10;
97
98 SkColorType colorType = static_cast<SkColorType>(ct);
99 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Robert Phillips9b16f812019-05-17 10:01:21 -0400100 bool can = context->colorTypeSupportedAsSurface(colorType);
101 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500102 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
103 colorType, can, SkToBool(surf));
104
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400105 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400106 kSize, kSize, colorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400107 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Robert Phillips9b16f812019-05-17 10:01:21 -0400108 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109 kTopLeft_GrSurfaceOrigin, 0, colorType, nullptr,
110 nullptr);
111 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
112 colorType, can, SkToBool(surf));
113
Robert Phillips9b16f812019-05-17 10:01:21 -0400114 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500115 kTopLeft_GrSurfaceOrigin, 1,
116 colorType, nullptr, nullptr);
117 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
118 colorType, can, SkToBool(surf));
119
120 surf.reset();
Robert Phillips9b16f812019-05-17 10:01:21 -0400121 context->flush();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400122 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500123
124 static constexpr int kSampleCnt = 2;
125
Robert Phillips9b16f812019-05-17 10:01:21 -0400126 can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
127 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
129 colorType, can, SkToBool(surf));
130
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400131 backendTex = context->createBackendTexture(kSize, kSize, colorType,
132 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400133 GrMipMapped::kNo, GrRenderable::kYes,
134 GrProtected::kNo);
Robert Phillips9b16f812019-05-17 10:01:21 -0400135 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500136 kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType,
137 nullptr, nullptr);
138 REPORTER_ASSERT(reporter, can == SkToBool(surf),
139 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
140 colorType);
141 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
142 if (surf) {
143 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
Chris Dalton6ce447a2019-06-23 18:07:38 -0600144 int storedCnt = rtc->numSamples();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400145 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500146 storedCnt, rtc->asSurfaceProxy()->config());
147 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
148 "Should store an allowed sample count (%d vs %d)", allowedCnt,
149 storedCnt);
150 }
151
Robert Phillips9b16f812019-05-17 10:01:21 -0400152 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500153 kTopLeft_GrSurfaceOrigin, kSampleCnt,
154 colorType, nullptr, nullptr);
155 REPORTER_ASSERT(reporter, can == SkToBool(surf),
156 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
157 colorType);
158 if (surf) {
159 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
Chris Dalton6ce447a2019-06-23 18:07:38 -0600160 int storedCnt = rtc->numSamples();
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400161 int allowedCnt = context->priv().caps()->getRenderTargetSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500162 storedCnt, rtc->asSurfaceProxy()->config());
163 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
164 "Should store an allowed sample count (%d vs %d)", allowedCnt,
165 storedCnt);
166 }
167
168 surf.reset();
Robert Phillips9b16f812019-05-17 10:01:21 -0400169 context->flush();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400170 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400171
172 auto* gpu = context->priv().getGpu();
Brian Salomon93348dd2018-08-29 12:56:23 -0400173
174 GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget(
175 16, 16, SkColorTypeToGrColorType(colorType));
Robert Phillips9b16f812019-05-17 10:01:21 -0400176 can = context->colorTypeSupportedAsSurface(colorType);
177 surf = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget,
Brian Salomon93348dd2018-08-29 12:56:23 -0400178 kTopLeft_GrSurfaceOrigin, colorType, nullptr,
179 nullptr);
180 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType,
181 can, SkToBool(surf));
182 surf.reset();
Robert Phillips9b16f812019-05-17 10:01:21 -0400183 context->flush();
Brian Salomon93348dd2018-08-29 12:56:23 -0400184 if (backendRenderTarget.isValid()) {
185 gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget);
186 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500187 }
188}
189
190DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400191 GrContext* context = ctxInfo.grContext();
192
193 static constexpr int kSize = 10;
194
Brian Salomonbdecacf2018-02-02 20:32:49 -0500195 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500196
197 SkColorType colorType = static_cast<SkColorType>(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -0400198 int max = context->maxSurfaceSampleCountForColorType(colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500199 if (!max) {
200 continue;
201 }
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400202 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400203 kSize, kSize, colorType, SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400204 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400205 if (!backendTex.isValid()) {
206 continue;
207 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400208 SkScopeExit freeTex([&backendTex, context] {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400209 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400210 });
Brian Salomonbdecacf2018-02-02 20:32:49 -0500211 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Robert Phillips9b16f812019-05-17 10:01:21 -0400212 auto surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500213 kTopLeft_GrSurfaceOrigin, max,
214 colorType, nullptr, nullptr);
215 REPORTER_ASSERT(reporter, surf);
216 if (!surf) {
217 continue;
218 }
219 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
220 ->getDevice()
221 ->accessRenderTargetContext()
Chris Dalton6ce447a2019-06-23 18:07:38 -0600222 ->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500223 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
224 }
225}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500226
kkinnunen179a8f52015-11-20 13:32:24 -0800227static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700228 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800229 const SkImageInfo& requestInfo,
230 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000231 const SkColor color = SK_ColorRED;
232 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800233 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000234
reed6ceeebd2016-03-09 14:26:26 -0800235 SkPixmap pmap;
236 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800237 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000238
reed6ceeebd2016-03-09 14:26:26 -0800239 SkPixmap pmap2;
240 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000241
kkinnunen179a8f52015-11-20 13:32:24 -0800242 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800243 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
244 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
245 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000246
reed6ceeebd2016-03-09 14:26:26 -0800247 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
248 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
249 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800250 } else {
251 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000252 }
253}
kkinnunen179a8f52015-11-20 13:32:24 -0800254DEF_TEST(SurfaceCanvasPeek, reporter) {
255 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
256 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700257 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800258 test_canvas_peek(reporter, surface, requestInfo, true);
259 }
260}
egdanielab527a52016-06-28 08:07:26 -0700261DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800262 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
263 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700264 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800265 test_canvas_peek(reporter, surface, requestInfo, false);
266 }
267}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000268
reede8f30622016-03-23 18:59:25 -0700269static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700270 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800271 REPORTER_ASSERT(reporter, surface);
272 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700273 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800274 REPORTER_ASSERT(reporter, image);
275 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700276 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700277 }
278 }
279}
kkinnunen179a8f52015-11-20 13:32:24 -0800280DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
281 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700282 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
283 auto surface(surface_func(at, nullptr));
284 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700285 }
286 }
287}
egdanielab527a52016-06-28 08:07:26 -0700288DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800289 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700290 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
291 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
292 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
293 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800294 }
295 }
296}
bsalomon74f681d2015-06-23 14:38:48 -0700297
Robert Phillips8caf85f2018-04-05 09:30:38 -0400298static void test_backend_texture_access_copy_on_write(
299 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
300 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700301 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700302
Robert Phillips8caf85f2018-04-05 09:30:38 -0400303 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700304 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700305
306 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400307 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700308}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400309
310static void test_backend_rendertarget_access_copy_on_write(
311 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
312 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
313 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
314
315 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
316 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
317
318 // If the access mode triggers CoW, then the backend objects should reflect it.
319 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
320 (snap1 == snap2));
321}
322
323DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800324 const SkSurface::BackendHandleAccess accessModes[] = {
325 SkSurface::kFlushRead_BackendHandleAccess,
326 SkSurface::kFlushWrite_BackendHandleAccess,
327 SkSurface::kDiscardWrite_BackendHandleAccess,
328 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400329
kkinnunen179a8f52015-11-20 13:32:24 -0800330 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400331 for (auto& accessMode : accessModes) {
332 {
bsalomon8b7451a2016-05-11 06:33:06 -0700333 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400334 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
335 }
336 {
337 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
338 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800339 }
340 }
341 }
342}
kkinnunen179a8f52015-11-20 13:32:24 -0800343
Robert Phillips8caf85f2018-04-05 09:30:38 -0400344template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
345static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700346 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400347
348 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
349 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800351 // just read access should not affect the snapshot
352 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
353
Robert Phillips8caf85f2018-04-05 09:30:38 -0400354 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
355 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800357 // expect a new image, since we claimed we would write
358 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
359
Robert Phillips8caf85f2018-04-05 09:30:38 -0400360 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
361 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700362 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800363 // expect a new(er) image, since we claimed we would write
364 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
365 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
366}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400367
kkinnunen179a8f52015-11-20 13:32:24 -0800368// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700369DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800370 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400371 {
372 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
373 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
374 surface.get());
375 }
376 {
377 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
378 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
379 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800380 }
381 }
382}
kkinnunen179a8f52015-11-20 13:32:24 -0800383
384// Verify that the right canvas commands trigger a copy on write.
385static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000386 SkCanvas* canvas = surface->getCanvas();
387
388 const SkRect testRect =
389 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
390 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000391 SkPath testPath;
392 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
393 SkIntToScalar(2), SkIntToScalar(1)));
394
395 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
396
397 SkRegion testRegion;
398 testRegion.setRect(testIRect);
399
400
401 const SkColor testColor = 0x01020304;
402 const SkPaint testPaint;
403 const SkPoint testPoints[3] = {
404 {SkIntToScalar(0), SkIntToScalar(0)},
405 {SkIntToScalar(2), SkIntToScalar(1)},
406 {SkIntToScalar(0), SkIntToScalar(2)}
407 };
408 const size_t testPointCount = 3;
409
410 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000411 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000412 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000413
414 SkRRect testRRect;
415 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
416
417 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000418
419#define EXPECT_COPY_ON_WRITE(command) \
420 { \
reed9ce9d672016-03-17 10:51:11 -0700421 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
422 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000423 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700424 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
425 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000426 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
427 }
428
429 EXPECT_COPY_ON_WRITE(clear(testColor))
430 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
431 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
432 testPaint))
433 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
434 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
435 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
436 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
437 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700438 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700439 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Hal Canary89a644b2019-01-07 09:36:09 -0500440 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800441}
442DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700443 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800444}
egdanielab527a52016-06-28 08:07:26 -0700445DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800446 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700447 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700448 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700449 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000450}
451
kkinnunen179a8f52015-11-20 13:32:24 -0800452static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
453 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000454 // This test succeeds by not triggering an assertion.
455 // The test verifies that the surface remains writable (usable) after
456 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000457 SkCanvas* canvas = surface->getCanvas();
458 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700459 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000460 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000461}
kkinnunen179a8f52015-11-20 13:32:24 -0800462DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700463 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800464}
egdanielab527a52016-06-28 08:07:26 -0700465DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800466 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700467 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700468 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800469 }
470}
junov@chromium.orgda904742013-05-01 22:38:16 +0000471
kkinnunen179a8f52015-11-20 13:32:24 -0800472static void test_crbug263329(skiatest::Reporter* reporter,
473 SkSurface* surface1,
474 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000475 // This is a regression test for crbug.com/263329
476 // Bug was caused by onCopyOnWrite releasing the old surface texture
477 // back to the scratch texture pool even though the texture is used
478 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000479 SkCanvas* canvas1 = surface1->getCanvas();
480 SkCanvas* canvas2 = surface2->getCanvas();
481 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700482 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000483 // Trigger copy on write, new backing is a scratch texture
484 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700485 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000486 // Trigger copy on write, old backing should not be returned to scratch
487 // pool because it is held by image2
488 canvas1->clear(3);
489
490 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700491 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000492 // Trigger copy on write on surface2. The new backing store should not
493 // be recycling a texture that is held by an existing image.
494 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700495 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400496 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000497 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400498 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
499 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
500 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
501 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
502 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000503}
egdanielab527a52016-06-28 08:07:26 -0700504DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800505 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700506 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
507 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700508 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800509 }
510}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000511
kkinnunen179a8f52015-11-20 13:32:24 -0800512DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700513 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700514 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500515 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800516 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500517 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800518}
egdanielab527a52016-06-28 08:07:26 -0700519DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800520 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700521 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700522 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500523
524 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400525 GrBackendTexture backendTex = image->getBackendTexture(false);
526 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800527 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500528 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400529 GrBackendTexture backendTex2 = image->getBackendTexture(false);
530 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000531 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000532}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800533
reede8f30622016-03-23 18:59:25 -0700534static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
535 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500536
537 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
538 ->asRenderTargetProxy();
539 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800540}
541
bsalomon5ec26ae2016-02-25 08:33:02 -0800542static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400543 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800544}
545
reed9ce9d672016-03-17 10:51:11 -0700546static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
547 return is_budgeted(image.get());
548}
549
egdanielab527a52016-06-28 08:07:26 -0700550DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800551 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400552 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
553 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
554 SkASSERT(surface);
555 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800556
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400557 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800558
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400559 // Initially the image shares a texture with the surface, and the
560 // the budgets should always match.
561 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
562 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800563
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400564 // Now trigger copy-on-write
565 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800566
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400567 // They don't share a texture anymore but the budgets should still match.
568 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
569 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800570 }
571}
junov@chromium.orgda904742013-05-01 22:38:16 +0000572
kkinnunen179a8f52015-11-20 13:32:24 -0800573static void test_no_canvas1(skiatest::Reporter* reporter,
574 SkSurface* surface,
575 SkSurface::ContentChangeMode mode) {
576 // Test passes by not asserting
577 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800578}
579static void test_no_canvas2(skiatest::Reporter* reporter,
580 SkSurface* surface,
581 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000582 // Verifies the robustness of SkSurface for handling use cases where calls
583 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700584 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
585 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800586 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700587 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
588 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800589 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000590}
kkinnunen179a8f52015-11-20 13:32:24 -0800591DEF_TEST(SurfaceNoCanvas, reporter) {
592 SkSurface::ContentChangeMode modes[] =
593 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
594 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
595 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700596 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800597 }
598 }
599}
egdanielab527a52016-06-28 08:07:26 -0700600DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800601 SkSurface::ContentChangeMode modes[] =
602 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
603 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
604 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
605 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700606 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700607 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700608 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000609 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000610 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000611}
reed9cd016e2016-01-30 10:01:06 -0800612
613static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800614 SkPixmap surfacePM;
615 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800616
reed9ce9d672016-03-17 10:51:11 -0700617 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800618 SkPixmap pm;
619 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800620
reed6ceeebd2016-03-09 14:26:26 -0800621 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800622
623 // trigger a copy-on-write
624 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700625 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800626 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
627
reed6ceeebd2016-03-09 14:26:26 -0800628 SkPixmap pm2;
629 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
630 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800631}
632
633DEF_TEST(surface_rowbytes, reporter) {
634 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
635
reede8f30622016-03-23 18:59:25 -0700636 auto surf0(SkSurface::MakeRaster(info));
637 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800638
639 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700640 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
641 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800642
643 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700644 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800645 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400646 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800647 REPORTER_ASSERT(reporter, nullptr == s);
648}
bsalomone63ffef2016-02-05 07:17:34 -0800649
fmalita03912f12016-07-06 06:22:06 -0700650DEF_TEST(surface_raster_zeroinitialized, reporter) {
651 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
652 SkPixmap pixmap;
653 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
654
655 for (int i = 0; i < pixmap.info().width(); ++i) {
656 for (int j = 0; j < pixmap.info().height(); ++j) {
657 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
658 }
659 }
660}
661
ericrkc4025182016-05-04 12:01:58 -0700662static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400663 GrContext* ctx, int sampleCnt, SkColor color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500664
Michael Ludwig72ab3462018-12-10 12:43:36 -0500665 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
666 // triggers what appears to be a driver race condition where the 10x10 surface from the
667 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
668 // test.
669 //
670 // Immediately after creation of this surface, readback shows the correct initial solid color.
671 // However, sometime before content is rendered into the upper half of the surface, the driver
672 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
673 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
674 // untouched bottom half contains random pixel values that trigger asserts in the
675 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
676 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
677 //
678 // Requesting a much larger backend texture size seems to prevent it from reusing the same
679 // memory and avoids the issue.
680#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700681 const int kWidth = 10;
682 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500683#else
684 const int kWidth = 100;
685 const int kHeight = 100;
686#endif
687
Robert Phillipsee5fd132019-05-07 13:29:22 -0400688 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
689 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000690
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400691 if (!create_backend_texture(ctx, outTexture, ii, GrMipMapped::kNo, color,
692 GrRenderable::kYes)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500693 return nullptr;
694 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000695
Robert Phillipsee5fd132019-05-07 13:29:22 -0400696 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(ctx, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400697 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500698 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000699 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700700 if (!surface) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400701 delete_backend_texture(ctx, *outTexture);
ericrkc4025182016-05-04 12:01:58 -0700702 return nullptr;
703 }
ericrkc4025182016-05-04 12:01:58 -0700704 return surface;
705}
bsalomone63ffef2016-02-05 07:17:34 -0800706
ericrkc4025182016-05-04 12:01:58 -0700707static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400708 GrContext* ctx, int sampleCnt, SkColor color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500709
ericrkc4025182016-05-04 12:01:58 -0700710 const int kWidth = 10;
711 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000712
Robert Phillipsee5fd132019-05-07 13:29:22 -0400713 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
714 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000715
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400716 if (!create_backend_texture(ctx, outTexture, ii, GrMipMapped::kNo, color,
717 GrRenderable::kYes)) {
ericrkc4025182016-05-04 12:01:58 -0700718 return nullptr;
719 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500720
721 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400722 ctx, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
Greg Danielfaa095e2017-12-19 13:15:02 -0500723 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500724
725 if (!surface) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400726 delete_backend_texture(ctx, *outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500727 return nullptr;
728 }
ericrkc4025182016-05-04 12:01:58 -0700729 return surface;
730}
731
732static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500733 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700734 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800735 if (!surface) {
736 ERRORF(reporter, "Could not create GPU SkSurface.");
737 return;
738 }
739 int w = surface->width();
740 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400741 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700742 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800743
Robert Phillips2c862492017-01-18 10:08:39 -0500744 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
745 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800746 ERRORF(reporter, "Could access render target of GPU SkSurface.");
747 return;
748 }
bsalomon2fba8092016-02-05 13:47:06 -0800749 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500750
751 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -0400752 grSurfaceContext->readPixels(ii, pixels.get(), 0, {0, 0});
bsalomone63ffef2016-02-05 07:17:34 -0800753 for (int y = 0; y < h; ++y) {
754 for (int x = 0; x < w; ++x) {
755 uint32_t pixel = pixels.get()[y * w + x];
756 if (pixel != expectedValue) {
757 SkString msg;
758 if (expectedValue) {
759 msg = "SkSurface should have left render target unmodified";
760 } else {
761 msg = "SkSurface should have cleared the render target";
762 }
763 ERRORF(reporter,
764 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
765 expectedValue, x, y);
766 return;
767 }
768 }
769 }
770}
771
bsalomon758586c2016-04-06 14:02:39 -0700772DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700773 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700774
Robert Phillips2c862492017-01-18 10:08:39 -0500775 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700776 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500777 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
778 },
Robert Phillips6603a172019-03-05 12:35:44 -0500779 [context] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500780 sk_sp<SkImage> i(s->makeImageSnapshot());
781 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6603a172019-03-05 12:35:44 -0500782 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(context);
Brian Salomond6287472019-06-24 15:50:07 -0400783 return context->priv().makeWrappedSurfaceContext(
784 std::move(proxy), SkColorTypeToGrColorType(i->colorType()), kPremul_SkAlphaType,
785 gpuImage->refColorSpace());
Robert Phillips2c862492017-01-18 10:08:39 -0500786 }
bsalomone63ffef2016-02-05 07:17:34 -0800787 };
ericrkc4025182016-05-04 12:01:58 -0700788
Robert Phillips2c862492017-01-18 10:08:39 -0500789 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700790 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800791 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700792 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800793 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
794 }
795 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
Robert Phillipsee5fd132019-05-07 13:29:22 -0400796 const SkColor kOrigColor = 0xABABABAB;
ericrkc4025182016-05-04 12:01:58 -0700797 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
798 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500799 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500800 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700801 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
802 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400803 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700804 }
805 }
806}
bsalomone63ffef2016-02-05 07:17:34 -0800807
ericrkc4025182016-05-04 12:01:58 -0700808static void test_surface_draw_partially(
809 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
810 const int kW = surface->width();
811 const int kH = surface->height();
812 SkPaint paint;
813 const SkColor kRectColor = ~origColor | 0xFF000000;
814 paint.setColor(kRectColor);
815 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
816 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400817 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700818 sk_memset32(pixels.get(), ~origColor, kW * kH);
819 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
820 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
821 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
822 bool stop = false;
823 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
824 (origColor >> 0 & 0xFF),
825 (origColor >> 8 & 0xFF),
826 (origColor >> 16 & 0xFF));
827 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
828 (kRectColor >> 16 & 0xFF),
829 (kRectColor >> 8 & 0xFF),
830 (kRectColor >> 0 & 0xFF));
831 for (int y = 0; y < kH/2 && !stop; ++y) {
832 for (int x = 0; x < kW && !stop; ++x) {
833 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
834 if (rectColorPM != pixels[x + y * kW]) {
835 stop = true;
836 }
837 }
838 }
839 stop = false;
840 for (int y = kH/2; y < kH && !stop; ++y) {
841 for (int x = 0; x < kW && !stop; ++x) {
842 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
843 if (origColorPM != pixels[x + y * kW]) {
844 stop = true;
845 }
846 }
847 }
848}
bsalomone63ffef2016-02-05 07:17:34 -0800849
egdanielab527a52016-06-28 08:07:26 -0700850DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400851 GrContext* context = ctxInfo.grContext();
852
Robert Phillipsee5fd132019-05-07 13:29:22 -0400853 static const SkColor kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800854
ericrkc4025182016-05-04 12:01:58 -0700855 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
856 &create_gpu_surface_backend_texture_as_render_target}) {
857 // Validate that we can draw to the canvas and that the original texture color is
858 // preserved in pixels that aren't rendered to via the surface.
859 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500860 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400861 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700862 if (surface) {
863 test_surface_draw_partially(reporter, surface, kOrigColor);
864 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400865 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700866 }
867 }
868}
869
Greg Daniel8ce79912019-02-05 10:08:43 -0500870struct ReleaseChecker {
871 ReleaseChecker() : fReleaseCount(0) {}
872 int fReleaseCount;
873 static void Release(void* self) {
874 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
875 }
876};
877
878
879DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
880 const int kWidth = 10;
881 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500882
883 GrContext* ctx = ctxInfo.grContext();
884 GrGpu* gpu = ctx->priv().getGpu();
885
886 for (bool useTexture : {false, true}) {
887 GrBackendTexture backendTex;
888 GrBackendRenderTarget backendRT;
889 sk_sp<SkSurface> surface;
890
891 ReleaseChecker releaseChecker;
892 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
893
894 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400895 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
896 kPremul_SkAlphaType);
897 if (!create_backend_texture(ctx, &backendTex, ii, GrMipMapped::kNo, SK_ColorRED,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400898 GrRenderable::kYes)) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500899 continue;
900 }
901
902 surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1,
903 kRGBA_8888_SkColorType,
904 nullptr, nullptr,
905 ReleaseChecker::Release,
906 &releaseChecker);
907 } else {
908 backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight,
909 GrColorType::kRGBA_8888);
910 if (!backendRT.isValid()) {
911 continue;
912 }
913 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
914 kRGBA_8888_SkColorType,
915 nullptr, nullptr,
916 ReleaseChecker::Release,
917 &releaseChecker);
918 }
919 if (!surface) {
920 ERRORF(reporter, "Failed to create surface");
921 continue;
922 }
923
924 surface->getCanvas()->clear(SK_ColorRED);
925 surface->flush();
926 gpu->testingOnly_flushGpuAndSync();
927
928 // Now exercise the release proc
929 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
930 surface.reset(nullptr); // force a release of the surface
931 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
932
933 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400934 delete_backend_texture(ctx, backendTex);
Greg Daniel8ce79912019-02-05 10:08:43 -0500935 } else {
936 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
937 }
938 }
939}
ericrkc4025182016-05-04 12:01:58 -0700940
941DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400942 GrContext* context = ctxInfo.grContext();
943 const GrCaps* caps = context->priv().caps();
944
945 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -0700946 return;
947 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400948
Robert Phillipsee5fd132019-05-07 13:29:22 -0400949 static const SkColor kOrigColor = 0xFFAABBCC;
ericrkc4025182016-05-04 12:01:58 -0700950
Robert Phillips9b16f812019-05-17 10:01:21 -0400951 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500952
ericrkc4025182016-05-04 12:01:58 -0700953 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
954 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500955 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500956 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400957 auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700958
Brian Salomonbdecacf2018-02-02 20:32:49 -0500959 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500960 // Certain platforms don't support MSAA, skip these.
961 continue;
ericrkc4025182016-05-04 12:01:58 -0700962 }
963
964 // Validate that we can attach a stencil buffer to an SkSurface created by either of
965 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400966 GrRenderTarget* rt = surface->getCanvas()
967 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Chris Daltoneffee202019-07-01 22:28:03 -0600968 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt, sampleCnt));
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400969 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700970 }
bsalomone63ffef2016-02-05 07:17:34 -0800971 }
972}
brianosman0e22eb82016-08-30 07:07:59 -0700973
Brian Salomonaad83152019-05-24 10:16:35 -0400974DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
975 GrContext* context = ctxInfo.grContext();
976
977 for (int sampleCnt : {1, 2}) {
978 GrBackendTexture backendTexture1;
979 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
980 if (!create_backend_texture(context, &backendTexture1, ii, GrMipMapped::kNo,
981 SK_ColorTRANSPARENT, GrRenderable::kYes)) {
982 continue;
983 }
984 SkScopeExit delete1(
985 [context, &backendTexture1] { delete_backend_texture(context, backendTexture1); });
986 GrBackendTexture backendTexture2;
987 if (!create_backend_texture(context, &backendTexture2, ii, GrMipMapped::kNo,
988 SK_ColorTRANSPARENT, GrRenderable::kYes)) {
989 ERRORF(reporter, "Expected to be able to make second texture");
990 continue;
991 }
992 SkScopeExit delete2(
993 [context, &backendTexture2] { delete_backend_texture(context, backendTexture2); });
994 auto ii2 = ii.makeWH(8, 8);
995 GrBackendTexture backendTexture3;
996 if (!create_backend_texture(context, &backendTexture3, ii2, GrMipMapped::kNo,
997 SK_ColorTRANSPARENT, GrRenderable::kYes)) {
998 ERRORF(reporter, "Couldn't create different sized texture.");
999 continue;
1000 }
1001 SkScopeExit delete3(
1002 [context, &backendTexture3] { delete_backend_texture(context, backendTexture3); });
1003
1004 auto surf = SkSurface::MakeFromBackendTexture(
1005 context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt,
1006 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1007 if (!surf) {
1008 continue;
1009 }
1010 surf->getCanvas()->clear(SK_ColorBLUE);
1011 // Change matrix, layer, and clip state before swapping out the backing texture.
1012 surf->getCanvas()->translate(5, 5);
1013 surf->getCanvas()->saveLayer(nullptr, nullptr);
1014 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1015 // switch origin while we're at it.
1016 bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin);
1017 REPORTER_ASSERT(reporter, replaced);
1018 SkPaint paint;
1019 paint.setColor(SK_ColorRED);
1020 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1021 surf->getCanvas()->restore();
1022
1023 // Check that the replacement texture got the right color values.
1024 SkAutoPixmapStorage pm;
1025 pm.alloc(ii);
1026 bool bad = !surf->readPixels(pm, 0, 0);
1027 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1028 for (int y = 0; y < ii.height() && !bad; ++y) {
1029 for (int x = 0; x < ii.width() && !bad; ++x) {
1030 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1031 auto found = *pm.addr32(x, y);
1032 if (found != expected) {
1033 bad = true;
1034 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1035 expected, found, x, y);
1036 }
1037 }
1038 }
1039 // The original texture should still be all blue.
1040 surf = SkSurface::MakeFromBackendTexture(
1041 context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt,
1042 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1043 if (!surf) {
1044 ERRORF(reporter, "Could not create second surface.");
1045 continue;
1046 }
1047 bad = !surf->readPixels(pm, 0, 0);
1048 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1049 for (int y = 0; y < ii.height() && !bad; ++y) {
1050 for (int x = 0; x < ii.width() && !bad; ++x) {
1051 auto expected = 0xFFFF0000;
1052 auto found = *pm.addr32(x, y);
1053 if (found != expected) {
1054 bad = true;
1055 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1056 expected, found, x, y);
1057 }
1058 }
1059 }
1060
1061 // Can't replace with the same texture
1062 REPORTER_ASSERT(reporter,
1063 !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin));
1064 // Can't replace with invalid texture
1065 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1066 // Can't replace with different size texture.
1067 REPORTER_ASSERT(reporter,
1068 !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin));
1069 // Can't replace texture of non-wrapped SkSurface.
1070 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1071 REPORTER_ASSERT(reporter, surf);
1072 if (surf) {
1073 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1,
1074 kTopLeft_GrSurfaceOrigin));
1075 }
1076 }
1077}
1078
Matt Sarett22886c42016-11-22 11:31:41 -05001079static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001080 SkOverdrawCanvas canvas(surface->getCanvas());
1081 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001082 sk_sp<SkImage> image = surface->makeImageSnapshot();
1083
1084 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001085 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001086 for (int y = 0; y < 10; y++) {
1087 for (int x = 0; x < 10; x++) {
1088 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1089 }
1090 }
1091}
1092
1093DEF_TEST(OverdrawSurface_Raster, r) {
1094 sk_sp<SkSurface> surface = create_surface();
1095 test_overdraw_surface(r, surface.get());
1096}
1097
Matt Sarett22886c42016-11-22 11:31:41 -05001098DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1099 GrContext* context = ctxInfo.grContext();
1100 sk_sp<SkSurface> surface = create_gpu_surface(context);
1101 test_overdraw_surface(r, surface.get());
1102}
Mike Reed44d04bd2017-06-28 19:57:21 -04001103
1104DEF_TEST(Surface_null, r) {
1105 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1106
1107 const int w = 37;
1108 const int h = 1000;
1109 auto surf = SkSurface::MakeNull(w, h);
1110 auto canvas = surf->getCanvas();
1111
1112 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1113 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1114}
Mike Reedd4746982018-02-07 16:05:29 -05001115
1116// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1117// (so the snapshot can succeed)
1118DEF_TEST(surface_image_unity, reporter) {
1119 auto do_test = [reporter](const SkImageInfo& info) {
1120 size_t rowBytes = info.minRowBytes();
1121 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1122 if (surf) {
1123 auto img = surf->makeImageSnapshot();
1124 if (!img && false) { // change to true to document the differences
1125 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001126 info.width(),
1127 info.height(),
1128 ToolUtils::colortype_name(info.colorType()),
1129 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001130 return;
1131 }
1132 REPORTER_ASSERT(reporter, img != nullptr);
1133
1134 char dummyPixel = 0; // just need a valid address (not a valid size)
1135 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1136 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1137 REPORTER_ASSERT(reporter, img != nullptr);
1138 }
1139 };
1140
Mike Kleine978ca22018-10-29 11:29:58 -04001141 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001142 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001143 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001144 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001145 SkAlphaType at = static_cast<SkAlphaType>(ati);
1146 for (int32_t size : sizes) {
1147 do_test(SkImageInfo::Make(1, size, ct, at));
1148 do_test(SkImageInfo::Make(size, 1, ct, at));
1149 }
1150 }
1151 }
1152}