blob: 6fb6df4f9018b606b4f833cf8eb61667f61a09eb [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 Phillips5c7a25b2019-05-20 08:38:07 -0400105 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400106 kSize, kSize, colorType, GrMipMapped::kNo, GrRenderable::kYes);
Robert Phillips9b16f812019-05-17 10:01:21 -0400107 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500108 kTopLeft_GrSurfaceOrigin, 0, colorType, nullptr,
109 nullptr);
110 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
111 colorType, can, SkToBool(surf));
112
Robert Phillips9b16f812019-05-17 10:01:21 -0400113 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500114 kTopLeft_GrSurfaceOrigin, 1,
115 colorType, nullptr, nullptr);
116 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
117 colorType, can, SkToBool(surf));
118
119 surf.reset();
Robert Phillips9b16f812019-05-17 10:01:21 -0400120 context->flush();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400121 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500122
123 static constexpr int kSampleCnt = 2;
124
Robert Phillips9b16f812019-05-17 10:01:21 -0400125 can = context->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
126 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, kSampleCnt, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500127 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
128 colorType, can, SkToBool(surf));
129
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400130 backendTex = context->createBackendTexture(kSize, kSize, colorType,
131 GrMipMapped::kNo, GrRenderable::kYes);
Robert Phillips9b16f812019-05-17 10:01:21 -0400132 surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType,
134 nullptr, nullptr);
135 REPORTER_ASSERT(reporter, can == SkToBool(surf),
136 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
137 colorType);
138 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
139 if (surf) {
140 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
141 int storedCnt = rtc->numStencilSamples();
Robert Phillips9b16f812019-05-17 10:01:21 -0400142 int allowedCnt = context->priv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500143 storedCnt, rtc->asSurfaceProxy()->config());
144 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
145 "Should store an allowed sample count (%d vs %d)", allowedCnt,
146 storedCnt);
147 }
148
Robert Phillips9b16f812019-05-17 10:01:21 -0400149 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500150 kTopLeft_GrSurfaceOrigin, kSampleCnt,
151 colorType, nullptr, nullptr);
152 REPORTER_ASSERT(reporter, can == SkToBool(surf),
153 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
154 colorType);
155 if (surf) {
156 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
157 int storedCnt = rtc->numStencilSamples();
Robert Phillips9b16f812019-05-17 10:01:21 -0400158 int allowedCnt = context->priv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500159 storedCnt, rtc->asSurfaceProxy()->config());
160 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
161 "Should store an allowed sample count (%d vs %d)", allowedCnt,
162 storedCnt);
163 }
164
165 surf.reset();
Robert Phillips9b16f812019-05-17 10:01:21 -0400166 context->flush();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400167 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400168
169 auto* gpu = context->priv().getGpu();
Brian Salomon93348dd2018-08-29 12:56:23 -0400170
171 GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget(
172 16, 16, SkColorTypeToGrColorType(colorType));
Robert Phillips9b16f812019-05-17 10:01:21 -0400173 can = context->colorTypeSupportedAsSurface(colorType);
174 surf = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget,
Brian Salomon93348dd2018-08-29 12:56:23 -0400175 kTopLeft_GrSurfaceOrigin, colorType, nullptr,
176 nullptr);
177 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType,
178 can, SkToBool(surf));
179 surf.reset();
Robert Phillips9b16f812019-05-17 10:01:21 -0400180 context->flush();
Brian Salomon93348dd2018-08-29 12:56:23 -0400181 if (backendRenderTarget.isValid()) {
182 gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget);
183 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500184 }
185}
186
187DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400188 GrContext* context = ctxInfo.grContext();
189
190 static constexpr int kSize = 10;
191
Brian Salomonbdecacf2018-02-02 20:32:49 -0500192 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500193
194 SkColorType colorType = static_cast<SkColorType>(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -0400195 int max = context->maxSurfaceSampleCountForColorType(colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500196 if (!max) {
197 continue;
198 }
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400199 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400200 kSize, kSize, colorType, GrMipMapped::kNo, GrRenderable::kYes);
Brian Salomon99501b72018-03-23 11:26:11 -0400201 if (!backendTex.isValid()) {
202 continue;
203 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400204 SkScopeExit freeTex([&backendTex, context] {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400205 context->deleteBackendTexture(backendTex);
Robert Phillips9b16f812019-05-17 10:01:21 -0400206 });
Brian Salomonbdecacf2018-02-02 20:32:49 -0500207 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
Robert Phillips9b16f812019-05-17 10:01:21 -0400208 auto surf = SkSurface::MakeFromBackendTexture(context, backendTex,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500209 kTopLeft_GrSurfaceOrigin, max,
210 colorType, nullptr, nullptr);
211 REPORTER_ASSERT(reporter, surf);
212 if (!surf) {
213 continue;
214 }
215 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
216 ->getDevice()
217 ->accessRenderTargetContext()
218 ->numStencilSamples();
219 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
220 }
221}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500222
kkinnunen179a8f52015-11-20 13:32:24 -0800223static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700224 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800225 const SkImageInfo& requestInfo,
226 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000227 const SkColor color = SK_ColorRED;
228 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800229 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000230
reed6ceeebd2016-03-09 14:26:26 -0800231 SkPixmap pmap;
232 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800233 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000234
reed6ceeebd2016-03-09 14:26:26 -0800235 SkPixmap pmap2;
236 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000237
kkinnunen179a8f52015-11-20 13:32:24 -0800238 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800239 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
240 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
241 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000242
reed6ceeebd2016-03-09 14:26:26 -0800243 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
244 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
245 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800246 } else {
247 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000248 }
249}
kkinnunen179a8f52015-11-20 13:32:24 -0800250DEF_TEST(SurfaceCanvasPeek, reporter) {
251 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
252 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700253 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800254 test_canvas_peek(reporter, surface, requestInfo, true);
255 }
256}
egdanielab527a52016-06-28 08:07:26 -0700257DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800258 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
259 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700260 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800261 test_canvas_peek(reporter, surface, requestInfo, false);
262 }
263}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000264
reede8f30622016-03-23 18:59:25 -0700265static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700266 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800267 REPORTER_ASSERT(reporter, surface);
268 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700269 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800270 REPORTER_ASSERT(reporter, image);
271 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700272 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700273 }
274 }
275}
kkinnunen179a8f52015-11-20 13:32:24 -0800276DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
277 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700278 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
279 auto surface(surface_func(at, nullptr));
280 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700281 }
282 }
283}
egdanielab527a52016-06-28 08:07:26 -0700284DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800285 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700286 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
287 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
288 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
289 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800290 }
291 }
292}
bsalomon74f681d2015-06-23 14:38:48 -0700293
Robert Phillips8caf85f2018-04-05 09:30:38 -0400294static void test_backend_texture_access_copy_on_write(
295 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
296 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700297 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700298
Robert Phillips8caf85f2018-04-05 09:30:38 -0400299 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700300 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700301
302 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400303 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700304}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400305
306static void test_backend_rendertarget_access_copy_on_write(
307 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
308 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
309 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
310
311 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
312 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
313
314 // If the access mode triggers CoW, then the backend objects should reflect it.
315 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
316 (snap1 == snap2));
317}
318
319DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800320 const SkSurface::BackendHandleAccess accessModes[] = {
321 SkSurface::kFlushRead_BackendHandleAccess,
322 SkSurface::kFlushWrite_BackendHandleAccess,
323 SkSurface::kDiscardWrite_BackendHandleAccess,
324 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400325
kkinnunen179a8f52015-11-20 13:32:24 -0800326 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400327 for (auto& accessMode : accessModes) {
328 {
bsalomon8b7451a2016-05-11 06:33:06 -0700329 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400330 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
331 }
332 {
333 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
334 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800335 }
336 }
337 }
338}
kkinnunen179a8f52015-11-20 13:32:24 -0800339
Robert Phillips8caf85f2018-04-05 09:30:38 -0400340template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
341static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700342 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400343
344 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
345 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700346 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800347 // just read access should not affect the snapshot
348 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
349
Robert Phillips8caf85f2018-04-05 09:30:38 -0400350 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
351 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700352 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800353 // expect a new image, since we claimed we would write
354 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
355
Robert Phillips8caf85f2018-04-05 09:30:38 -0400356 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
357 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700358 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800359 // expect a new(er) image, since we claimed we would write
360 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
361 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
362}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400363
kkinnunen179a8f52015-11-20 13:32:24 -0800364// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700365DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800366 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400367 {
368 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
369 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
370 surface.get());
371 }
372 {
373 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
374 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
375 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800376 }
377 }
378}
kkinnunen179a8f52015-11-20 13:32:24 -0800379
380// Verify that the right canvas commands trigger a copy on write.
381static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000382 SkCanvas* canvas = surface->getCanvas();
383
384 const SkRect testRect =
385 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
386 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000387 SkPath testPath;
388 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
389 SkIntToScalar(2), SkIntToScalar(1)));
390
391 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
392
393 SkRegion testRegion;
394 testRegion.setRect(testIRect);
395
396
397 const SkColor testColor = 0x01020304;
398 const SkPaint testPaint;
399 const SkPoint testPoints[3] = {
400 {SkIntToScalar(0), SkIntToScalar(0)},
401 {SkIntToScalar(2), SkIntToScalar(1)},
402 {SkIntToScalar(0), SkIntToScalar(2)}
403 };
404 const size_t testPointCount = 3;
405
406 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000407 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000408 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000409
410 SkRRect testRRect;
411 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
412
413 SkString testText("Hello World");
junov@chromium.org995beb62013-03-28 13:49:22 +0000414
415#define EXPECT_COPY_ON_WRITE(command) \
416 { \
reed9ce9d672016-03-17 10:51:11 -0700417 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
418 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000419 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700420 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
421 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000422 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
423 }
424
425 EXPECT_COPY_ON_WRITE(clear(testColor))
426 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
427 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
428 testPaint))
429 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
430 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
431 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
432 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
433 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700434 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700435 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Hal Canary89a644b2019-01-07 09:36:09 -0500436 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, SkFont(), testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800437}
438DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700439 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800440}
egdanielab527a52016-06-28 08:07:26 -0700441DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800442 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700443 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700444 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700445 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000446}
447
kkinnunen179a8f52015-11-20 13:32:24 -0800448static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
449 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000450 // This test succeeds by not triggering an assertion.
451 // The test verifies that the surface remains writable (usable) after
452 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000453 SkCanvas* canvas = surface->getCanvas();
454 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700455 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000456 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000457}
kkinnunen179a8f52015-11-20 13:32:24 -0800458DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700459 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800460}
egdanielab527a52016-06-28 08:07:26 -0700461DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800462 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700463 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700464 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800465 }
466}
junov@chromium.orgda904742013-05-01 22:38:16 +0000467
kkinnunen179a8f52015-11-20 13:32:24 -0800468static void test_crbug263329(skiatest::Reporter* reporter,
469 SkSurface* surface1,
470 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000471 // This is a regression test for crbug.com/263329
472 // Bug was caused by onCopyOnWrite releasing the old surface texture
473 // back to the scratch texture pool even though the texture is used
474 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000475 SkCanvas* canvas1 = surface1->getCanvas();
476 SkCanvas* canvas2 = surface2->getCanvas();
477 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700478 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000479 // Trigger copy on write, new backing is a scratch texture
480 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700481 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000482 // Trigger copy on write, old backing should not be returned to scratch
483 // pool because it is held by image2
484 canvas1->clear(3);
485
486 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700487 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000488 // Trigger copy on write on surface2. The new backing store should not
489 // be recycling a texture that is held by an existing image.
490 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700491 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400492 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000493 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400494 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
495 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
496 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
497 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
498 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000499}
egdanielab527a52016-06-28 08:07:26 -0700500DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800501 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700502 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
503 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700504 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800505 }
506}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000507
kkinnunen179a8f52015-11-20 13:32:24 -0800508DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700509 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700510 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500511 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800512 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500513 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800514}
egdanielab527a52016-06-28 08:07:26 -0700515DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800516 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700517 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700518 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500519
520 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400521 GrBackendTexture backendTex = image->getBackendTexture(false);
522 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800523 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500524 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400525 GrBackendTexture backendTex2 = image->getBackendTexture(false);
526 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000527 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000528}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800529
reede8f30622016-03-23 18:59:25 -0700530static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
531 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500532
533 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
534 ->asRenderTargetProxy();
535 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800536}
537
bsalomon5ec26ae2016-02-25 08:33:02 -0800538static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400539 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800540}
541
reed9ce9d672016-03-17 10:51:11 -0700542static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
543 return is_budgeted(image.get());
544}
545
egdanielab527a52016-06-28 08:07:26 -0700546DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800547 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400548 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
549 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
550 SkASSERT(surface);
551 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800552
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400553 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800554
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400555 // Initially the image shares a texture with the surface, and the
556 // the budgets should always match.
557 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
558 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800559
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400560 // Now trigger copy-on-write
561 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800562
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400563 // They don't share a texture anymore but the budgets should still match.
564 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
565 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800566 }
567}
junov@chromium.orgda904742013-05-01 22:38:16 +0000568
kkinnunen179a8f52015-11-20 13:32:24 -0800569static void test_no_canvas1(skiatest::Reporter* reporter,
570 SkSurface* surface,
571 SkSurface::ContentChangeMode mode) {
572 // Test passes by not asserting
573 surface->notifyContentWillChange(mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800574}
575static void test_no_canvas2(skiatest::Reporter* reporter,
576 SkSurface* surface,
577 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000578 // Verifies the robustness of SkSurface for handling use cases where calls
579 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700580 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
581 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800582 surface->notifyContentWillChange(mode);
reed9ce9d672016-03-17 10:51:11 -0700583 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
584 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800585 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000586}
kkinnunen179a8f52015-11-20 13:32:24 -0800587DEF_TEST(SurfaceNoCanvas, reporter) {
588 SkSurface::ContentChangeMode modes[] =
589 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
590 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
591 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700592 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800593 }
594 }
595}
egdanielab527a52016-06-28 08:07:26 -0700596DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800597 SkSurface::ContentChangeMode modes[] =
598 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
599 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
600 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
601 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700602 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700603 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700604 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000605 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000606 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000607}
reed9cd016e2016-01-30 10:01:06 -0800608
609static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800610 SkPixmap surfacePM;
611 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800612
reed9ce9d672016-03-17 10:51:11 -0700613 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800614 SkPixmap pm;
615 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800616
reed6ceeebd2016-03-09 14:26:26 -0800617 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800618
619 // trigger a copy-on-write
620 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700621 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800622 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
623
reed6ceeebd2016-03-09 14:26:26 -0800624 SkPixmap pm2;
625 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
626 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800627}
628
629DEF_TEST(surface_rowbytes, reporter) {
630 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
631
reede8f30622016-03-23 18:59:25 -0700632 auto surf0(SkSurface::MakeRaster(info));
633 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800634
635 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700636 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
637 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800638
639 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700640 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800641 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400642 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800643 REPORTER_ASSERT(reporter, nullptr == s);
644}
bsalomone63ffef2016-02-05 07:17:34 -0800645
fmalita03912f12016-07-06 06:22:06 -0700646DEF_TEST(surface_raster_zeroinitialized, reporter) {
647 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
648 SkPixmap pixmap;
649 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
650
651 for (int i = 0; i < pixmap.info().width(); ++i) {
652 for (int j = 0; j < pixmap.info().height(); ++j) {
653 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
654 }
655 }
656}
657
ericrkc4025182016-05-04 12:01:58 -0700658static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400659 GrContext* ctx, int sampleCnt, SkColor color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500660
Michael Ludwig72ab3462018-12-10 12:43:36 -0500661 // On Pixel and Pixel2XL's with Adreno 530 and 540s, setting width and height to 10s reliably
662 // triggers what appears to be a driver race condition where the 10x10 surface from the
663 // OverdrawSurface_gpu test is reused(?) for this surface created by the SurfacePartialDraw_gpu
664 // test.
665 //
666 // Immediately after creation of this surface, readback shows the correct initial solid color.
667 // However, sometime before content is rendered into the upper half of the surface, the driver
668 // presumably cleans up the OverdrawSurface_gpu's memory which corrupts this color buffer. The
669 // top half of the surface is fine after the partially-covering rectangle is drawn, but the
670 // untouched bottom half contains random pixel values that trigger asserts in the
671 // SurfacePartialDraw_gpu test for no longer matching the initial color. Running the
672 // SurfacePartialDraw_gpu test without the OverdrawSurface_gpu test completes successfully.
673 //
674 // Requesting a much larger backend texture size seems to prevent it from reusing the same
675 // memory and avoids the issue.
676#if defined(SK_BUILD_FOR_SKQP)
ericrkc4025182016-05-04 12:01:58 -0700677 const int kWidth = 10;
678 const int kHeight = 10;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500679#else
680 const int kWidth = 100;
681 const int kHeight = 100;
682#endif
683
Robert Phillipsee5fd132019-05-07 13:29:22 -0400684 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
685 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000686
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400687 if (!create_backend_texture(ctx, outTexture, ii, GrMipMapped::kNo, color,
688 GrRenderable::kYes)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500689 return nullptr;
690 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000691
Robert Phillipsee5fd132019-05-07 13:29:22 -0400692 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(ctx, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400693 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500694 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000695 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700696 if (!surface) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400697 delete_backend_texture(ctx, *outTexture);
ericrkc4025182016-05-04 12:01:58 -0700698 return nullptr;
699 }
ericrkc4025182016-05-04 12:01:58 -0700700 return surface;
701}
bsalomone63ffef2016-02-05 07:17:34 -0800702
ericrkc4025182016-05-04 12:01:58 -0700703static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400704 GrContext* ctx, int sampleCnt, SkColor color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500705
ericrkc4025182016-05-04 12:01:58 -0700706 const int kWidth = 10;
707 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000708
Robert Phillipsee5fd132019-05-07 13:29:22 -0400709 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
710 kPremul_SkAlphaType);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000711
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400712 if (!create_backend_texture(ctx, outTexture, ii, GrMipMapped::kNo, color,
713 GrRenderable::kYes)) {
ericrkc4025182016-05-04 12:01:58 -0700714 return nullptr;
715 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500716
717 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipsee5fd132019-05-07 13:29:22 -0400718 ctx, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
Greg Danielfaa095e2017-12-19 13:15:02 -0500719 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500720
721 if (!surface) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400722 delete_backend_texture(ctx, *outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500723 return nullptr;
724 }
ericrkc4025182016-05-04 12:01:58 -0700725 return surface;
726}
727
728static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500729 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700730 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800731 if (!surface) {
732 ERRORF(reporter, "Could not create GPU SkSurface.");
733 return;
734 }
735 int w = surface->width();
736 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400737 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700738 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800739
Robert Phillips2c862492017-01-18 10:08:39 -0500740 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
741 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800742 ERRORF(reporter, "Could access render target of GPU SkSurface.");
743 return;
744 }
bsalomon2fba8092016-02-05 13:47:06 -0800745 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500746
747 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
748 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800749 for (int y = 0; y < h; ++y) {
750 for (int x = 0; x < w; ++x) {
751 uint32_t pixel = pixels.get()[y * w + x];
752 if (pixel != expectedValue) {
753 SkString msg;
754 if (expectedValue) {
755 msg = "SkSurface should have left render target unmodified";
756 } else {
757 msg = "SkSurface should have cleared the render target";
758 }
759 ERRORF(reporter,
760 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
761 expectedValue, x, y);
762 return;
763 }
764 }
765 }
766}
767
bsalomon758586c2016-04-06 14:02:39 -0700768DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700769 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700770
Robert Phillips2c862492017-01-18 10:08:39 -0500771 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700772 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500773 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
774 },
Robert Phillips6603a172019-03-05 12:35:44 -0500775 [context] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500776 sk_sp<SkImage> i(s->makeImageSnapshot());
777 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6603a172019-03-05 12:35:44 -0500778 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(context);
Robert Phillips9da87e02019-02-04 13:26:26 -0500779 return context->priv().makeWrappedSurfaceContext(std::move(proxy),
780 gpuImage->refColorSpace());
Robert Phillips2c862492017-01-18 10:08:39 -0500781 }
bsalomone63ffef2016-02-05 07:17:34 -0800782 };
ericrkc4025182016-05-04 12:01:58 -0700783
Robert Phillips2c862492017-01-18 10:08:39 -0500784 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700785 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800786 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700787 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800788 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
789 }
790 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
Robert Phillipsee5fd132019-05-07 13:29:22 -0400791 const SkColor kOrigColor = 0xABABABAB;
ericrkc4025182016-05-04 12:01:58 -0700792 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
793 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500794 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500795 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700796 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
797 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400798 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700799 }
800 }
801}
bsalomone63ffef2016-02-05 07:17:34 -0800802
ericrkc4025182016-05-04 12:01:58 -0700803static void test_surface_draw_partially(
804 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
805 const int kW = surface->width();
806 const int kH = surface->height();
807 SkPaint paint;
808 const SkColor kRectColor = ~origColor | 0xFF000000;
809 paint.setColor(kRectColor);
810 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
811 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400812 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700813 sk_memset32(pixels.get(), ~origColor, kW * kH);
814 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
815 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
816 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
817 bool stop = false;
818 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
819 (origColor >> 0 & 0xFF),
820 (origColor >> 8 & 0xFF),
821 (origColor >> 16 & 0xFF));
822 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
823 (kRectColor >> 16 & 0xFF),
824 (kRectColor >> 8 & 0xFF),
825 (kRectColor >> 0 & 0xFF));
826 for (int y = 0; y < kH/2 && !stop; ++y) {
827 for (int x = 0; x < kW && !stop; ++x) {
828 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
829 if (rectColorPM != pixels[x + y * kW]) {
830 stop = true;
831 }
832 }
833 }
834 stop = false;
835 for (int y = kH/2; y < kH && !stop; ++y) {
836 for (int x = 0; x < kW && !stop; ++x) {
837 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
838 if (origColorPM != pixels[x + y * kW]) {
839 stop = true;
840 }
841 }
842 }
843}
bsalomone63ffef2016-02-05 07:17:34 -0800844
egdanielab527a52016-06-28 08:07:26 -0700845DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400846 GrContext* context = ctxInfo.grContext();
847
Robert Phillipsee5fd132019-05-07 13:29:22 -0400848 static const SkColor kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800849
ericrkc4025182016-05-04 12:01:58 -0700850 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
851 &create_gpu_surface_backend_texture_as_render_target}) {
852 // Validate that we can draw to the canvas and that the original texture color is
853 // preserved in pixels that aren't rendered to via the surface.
854 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500855 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400856 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700857 if (surface) {
858 test_surface_draw_partially(reporter, surface, kOrigColor);
859 surface.reset();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400860 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700861 }
862 }
863}
864
Greg Daniel8ce79912019-02-05 10:08:43 -0500865struct ReleaseChecker {
866 ReleaseChecker() : fReleaseCount(0) {}
867 int fReleaseCount;
868 static void Release(void* self) {
869 static_cast<ReleaseChecker*>(self)->fReleaseCount++;
870 }
871};
872
873
874DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedWithRelease_Gpu, reporter, ctxInfo) {
875 const int kWidth = 10;
876 const int kHeight = 10;
Greg Daniel8ce79912019-02-05 10:08:43 -0500877
878 GrContext* ctx = ctxInfo.grContext();
879 GrGpu* gpu = ctx->priv().getGpu();
880
881 for (bool useTexture : {false, true}) {
882 GrBackendTexture backendTex;
883 GrBackendRenderTarget backendRT;
884 sk_sp<SkSurface> surface;
885
886 ReleaseChecker releaseChecker;
887 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
888
889 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400890 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
891 kPremul_SkAlphaType);
892 if (!create_backend_texture(ctx, &backendTex, ii, GrMipMapped::kNo, SK_ColorRED,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400893 GrRenderable::kYes)) {
Greg Daniel8ce79912019-02-05 10:08:43 -0500894 continue;
895 }
896
897 surface = SkSurface::MakeFromBackendTexture(ctx, backendTex, texOrigin, 1,
898 kRGBA_8888_SkColorType,
899 nullptr, nullptr,
900 ReleaseChecker::Release,
901 &releaseChecker);
902 } else {
903 backendRT = gpu->createTestingOnlyBackendRenderTarget(kWidth, kHeight,
904 GrColorType::kRGBA_8888);
905 if (!backendRT.isValid()) {
906 continue;
907 }
908 surface = SkSurface::MakeFromBackendRenderTarget(ctx, backendRT, texOrigin,
909 kRGBA_8888_SkColorType,
910 nullptr, nullptr,
911 ReleaseChecker::Release,
912 &releaseChecker);
913 }
914 if (!surface) {
915 ERRORF(reporter, "Failed to create surface");
916 continue;
917 }
918
919 surface->getCanvas()->clear(SK_ColorRED);
920 surface->flush();
921 gpu->testingOnly_flushGpuAndSync();
922
923 // Now exercise the release proc
924 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
925 surface.reset(nullptr); // force a release of the surface
926 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
927
928 if (useTexture) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400929 delete_backend_texture(ctx, backendTex);
Greg Daniel8ce79912019-02-05 10:08:43 -0500930 } else {
931 gpu->deleteTestingOnlyBackendRenderTarget(backendRT);
932 }
933 }
934}
ericrkc4025182016-05-04 12:01:58 -0700935
936DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillips9b16f812019-05-17 10:01:21 -0400937 GrContext* context = ctxInfo.grContext();
938 const GrCaps* caps = context->priv().caps();
939
940 if (caps->avoidStencilBuffers()) {
ericrkc4025182016-05-04 12:01:58 -0700941 return;
942 }
Robert Phillips9b16f812019-05-17 10:01:21 -0400943
Robert Phillipsee5fd132019-05-07 13:29:22 -0400944 static const SkColor kOrigColor = 0xFFAABBCC;
ericrkc4025182016-05-04 12:01:58 -0700945
Robert Phillips9b16f812019-05-17 10:01:21 -0400946 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500947
ericrkc4025182016-05-04 12:01:58 -0700948 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
949 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500950 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500951 GrBackendTexture backendTex;
Robert Phillips9b16f812019-05-17 10:01:21 -0400952 auto surface = surfaceFunc(context, sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700953
Brian Salomonbdecacf2018-02-02 20:32:49 -0500954 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500955 // Certain platforms don't support MSAA, skip these.
956 continue;
ericrkc4025182016-05-04 12:01:58 -0700957 }
958
959 // Validate that we can attach a stencil buffer to an SkSurface created by either of
960 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400961 GrRenderTarget* rt = surface->getCanvas()
962 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Robert Phillips6be756b2018-01-16 15:07:54 -0500963 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt));
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400964 context->deleteBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700965 }
bsalomone63ffef2016-02-05 07:17:34 -0800966 }
967}
brianosman0e22eb82016-08-30 07:07:59 -0700968
Brian Salomonaad83152019-05-24 10:16:35 -0400969DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReplaceSurfaceBackendTexture, reporter, ctxInfo) {
970 GrContext* context = ctxInfo.grContext();
971
972 for (int sampleCnt : {1, 2}) {
973 GrBackendTexture backendTexture1;
974 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
975 if (!create_backend_texture(context, &backendTexture1, ii, GrMipMapped::kNo,
976 SK_ColorTRANSPARENT, GrRenderable::kYes)) {
977 continue;
978 }
979 SkScopeExit delete1(
980 [context, &backendTexture1] { delete_backend_texture(context, backendTexture1); });
981 GrBackendTexture backendTexture2;
982 if (!create_backend_texture(context, &backendTexture2, ii, GrMipMapped::kNo,
983 SK_ColorTRANSPARENT, GrRenderable::kYes)) {
984 ERRORF(reporter, "Expected to be able to make second texture");
985 continue;
986 }
987 SkScopeExit delete2(
988 [context, &backendTexture2] { delete_backend_texture(context, backendTexture2); });
989 auto ii2 = ii.makeWH(8, 8);
990 GrBackendTexture backendTexture3;
991 if (!create_backend_texture(context, &backendTexture3, ii2, GrMipMapped::kNo,
992 SK_ColorTRANSPARENT, GrRenderable::kYes)) {
993 ERRORF(reporter, "Couldn't create different sized texture.");
994 continue;
995 }
996 SkScopeExit delete3(
997 [context, &backendTexture3] { delete_backend_texture(context, backendTexture3); });
998
999 auto surf = SkSurface::MakeFromBackendTexture(
1000 context, backendTexture1, kTopLeft_GrSurfaceOrigin, sampleCnt,
1001 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1002 if (!surf) {
1003 continue;
1004 }
1005 surf->getCanvas()->clear(SK_ColorBLUE);
1006 // Change matrix, layer, and clip state before swapping out the backing texture.
1007 surf->getCanvas()->translate(5, 5);
1008 surf->getCanvas()->saveLayer(nullptr, nullptr);
1009 surf->getCanvas()->clipRect(SkRect::MakeXYWH(0, 0, 1, 1));
1010 // switch origin while we're at it.
1011 bool replaced = surf->replaceBackendTexture(backendTexture2, kBottomLeft_GrSurfaceOrigin);
1012 REPORTER_ASSERT(reporter, replaced);
1013 SkPaint paint;
1014 paint.setColor(SK_ColorRED);
1015 surf->getCanvas()->drawRect(SkRect::MakeWH(5, 5), paint);
1016 surf->getCanvas()->restore();
1017
1018 // Check that the replacement texture got the right color values.
1019 SkAutoPixmapStorage pm;
1020 pm.alloc(ii);
1021 bool bad = !surf->readPixels(pm, 0, 0);
1022 REPORTER_ASSERT(reporter, !bad, "Could not read surface.");
1023 for (int y = 0; y < ii.height() && !bad; ++y) {
1024 for (int x = 0; x < ii.width() && !bad; ++x) {
1025 auto expected = (x == 5 && y == 5) ? 0xFF0000FF : 0xFFFF0000;
1026 auto found = *pm.addr32(x, y);
1027 if (found != expected) {
1028 bad = true;
1029 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1030 expected, found, x, y);
1031 }
1032 }
1033 }
1034 // The original texture should still be all blue.
1035 surf = SkSurface::MakeFromBackendTexture(
1036 context, backendTexture1, kBottomLeft_GrSurfaceOrigin, sampleCnt,
1037 kRGBA_8888_SkColorType, ii.refColorSpace(), nullptr);
1038 if (!surf) {
1039 ERRORF(reporter, "Could not create second surface.");
1040 continue;
1041 }
1042 bad = !surf->readPixels(pm, 0, 0);
1043 REPORTER_ASSERT(reporter, !bad, "Could not read second surface.");
1044 for (int y = 0; y < ii.height() && !bad; ++y) {
1045 for (int x = 0; x < ii.width() && !bad; ++x) {
1046 auto expected = 0xFFFF0000;
1047 auto found = *pm.addr32(x, y);
1048 if (found != expected) {
1049 bad = true;
1050 ERRORF(reporter, "Expected color 0x%08x, found color 0x%08x at %d, %d.",
1051 expected, found, x, y);
1052 }
1053 }
1054 }
1055
1056 // Can't replace with the same texture
1057 REPORTER_ASSERT(reporter,
1058 !surf->replaceBackendTexture(backendTexture1, kTopLeft_GrSurfaceOrigin));
1059 // Can't replace with invalid texture
1060 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture({}, kTopLeft_GrSurfaceOrigin));
1061 // Can't replace with different size texture.
1062 REPORTER_ASSERT(reporter,
1063 !surf->replaceBackendTexture(backendTexture3, kTopLeft_GrSurfaceOrigin));
1064 // Can't replace texture of non-wrapped SkSurface.
1065 surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, sampleCnt, nullptr);
1066 REPORTER_ASSERT(reporter, surf);
1067 if (surf) {
1068 REPORTER_ASSERT(reporter, !surf->replaceBackendTexture(backendTexture1,
1069 kTopLeft_GrSurfaceOrigin));
1070 }
1071 }
1072}
1073
Matt Sarett22886c42016-11-22 11:31:41 -05001074static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001075 SkOverdrawCanvas canvas(surface->getCanvas());
1076 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001077 sk_sp<SkImage> image = surface->makeImageSnapshot();
1078
1079 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001080 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001081 for (int y = 0; y < 10; y++) {
1082 for (int x = 0; x < 10; x++) {
1083 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1084 }
1085 }
1086}
1087
1088DEF_TEST(OverdrawSurface_Raster, r) {
1089 sk_sp<SkSurface> surface = create_surface();
1090 test_overdraw_surface(r, surface.get());
1091}
1092
Matt Sarett22886c42016-11-22 11:31:41 -05001093DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1094 GrContext* context = ctxInfo.grContext();
1095 sk_sp<SkSurface> surface = create_gpu_surface(context);
1096 test_overdraw_surface(r, surface.get());
1097}
Mike Reed44d04bd2017-06-28 19:57:21 -04001098
1099DEF_TEST(Surface_null, r) {
1100 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1101
1102 const int w = 37;
1103 const int h = 1000;
1104 auto surf = SkSurface::MakeNull(w, h);
1105 auto canvas = surf->getCanvas();
1106
1107 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1108 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1109}
Mike Reedd4746982018-02-07 16:05:29 -05001110
1111// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1112// (so the snapshot can succeed)
1113DEF_TEST(surface_image_unity, reporter) {
1114 auto do_test = [reporter](const SkImageInfo& info) {
1115 size_t rowBytes = info.minRowBytes();
1116 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1117 if (surf) {
1118 auto img = surf->makeImageSnapshot();
1119 if (!img && false) { // change to true to document the differences
1120 SkDebugf("image failed: [%08X %08X] %14s %s\n",
Mike Kleinea3f0142019-03-20 11:12:10 -05001121 info.width(),
1122 info.height(),
1123 ToolUtils::colortype_name(info.colorType()),
1124 ToolUtils::alphatype_name(info.alphaType()));
Mike Reedd4746982018-02-07 16:05:29 -05001125 return;
1126 }
1127 REPORTER_ASSERT(reporter, img != nullptr);
1128
1129 char dummyPixel = 0; // just need a valid address (not a valid size)
1130 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1131 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1132 REPORTER_ASSERT(reporter, img != nullptr);
1133 }
1134 };
1135
Mike Kleine978ca22018-10-29 11:29:58 -04001136 const int32_t sizes[] = { -1, 0, 1, 1 << 18 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001137 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001138 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001139 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001140 SkAlphaType at = static_cast<SkAlphaType>(ati);
1141 for (int32_t size : sizes) {
1142 do_test(SkImageInfo::Make(1, size, ct, at));
1143 do_test(SkImageInfo::Make(size, 1, ct, at));
1144 }
1145 }
1146 }
1147}