blob: 71c437ce8aa0ee4da7f226120934c5366f75c2eb [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
Herb Derbyd3895d82018-09-04 13:27:00 -04008#include "GrBackendSurface.h"
kkinnunen179a8f52015-11-20 13:32:24 -08009#include "GrContext.h"
Robert Phillips2c862492017-01-18 10:08:39 -050010#include "GrContextPriv.h"
Brian Salomon3a2cc2c2018-02-03 00:25:12 +000011#include "GrGpu.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050012#include "GrGpuResourcePriv.h"
13#include "GrRenderTargetContext.h"
ericrkc4025182016-05-04 12:01:58 -070014#include "GrResourceProvider.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040015#include "SkCanvas.h"
16#include "SkData.h"
17#include "SkDevice.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050018#include "SkGpuDevice.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040019#include "SkImage_Base.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050020#include "SkImage_Gpu.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040021#include "SkOverdrawCanvas.h"
22#include "SkPath.h"
23#include "SkRegion.h"
24#include "SkRRect.h"
25#include "SkSurface.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050026#include "SkSurface_Gpu.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040027#include "SkUtils.h"
28#include "Test.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000029
Hal Canary8a001442018-09-19 11:31:27 -040030#include <functional>
31#include <initializer_list>
32#include <vector>
33
Mike Reedd4746982018-02-07 16:05:29 -050034#include "sk_tool_utils.h"
35
bsalomon74f681d2015-06-23 14:38:48 -070036
kkinnunen179a8f52015-11-20 13:32:24 -080037static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070038 SkASSERT(pixels == context);
39 sk_free(pixels);
40}
reede8f30622016-03-23 18:59:25 -070041static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
42 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070043 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000044 if (requestedInfo) {
45 *requestedInfo = info;
46 }
reede8f30622016-03-23 18:59:25 -070047 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000048}
reede8f30622016-03-23 18:59:25 -070049static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
50 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080051 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
52 if (requestedInfo) {
53 *requestedInfo = info;
54 }
55 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040056 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070057 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
58 release_direct_surface_storage,
59 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080060}
reede8f30622016-03-23 18:59:25 -070061static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
62 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080063 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
64 if (requestedInfo) {
65 *requestedInfo = info;
66 }
robertphillips7e922762016-07-26 11:38:17 -070067 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080068}
reede8f30622016-03-23 18:59:25 -070069static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
70 SkAlphaType at = kPremul_SkAlphaType,
71 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080072 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
73 if (requestedInfo) {
74 *requestedInfo = info;
75 }
robertphillips7e922762016-07-26 11:38:17 -070076 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080077}
junov@chromium.org995beb62013-03-28 13:49:22 +000078
kkinnunen179a8f52015-11-20 13:32:24 -080079DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080080 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070081 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
82 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080083
reedb2497c22014-12-31 12:31:43 -080084}
egdanielab527a52016-06-28 08:07:26 -070085DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080086 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
87 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070088 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080089}
reedb2497c22014-12-31 12:31:43 -080090
Brian Salomonbdecacf2018-02-02 20:32:49 -050091DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
92 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
93 static constexpr int kSize = 10;
94
95 SkColorType colorType = static_cast<SkColorType>(ct);
96 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
97 bool can = ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType);
98 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, 1,
99 nullptr);
100 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
101 colorType, can, SkToBool(surf));
102
103 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
104 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400105 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500106 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
107 kTopLeft_GrSurfaceOrigin, 0, colorType, nullptr,
108 nullptr);
109 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
110 colorType, can, SkToBool(surf));
111
112 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
113 kTopLeft_GrSurfaceOrigin, 1,
114 colorType, nullptr, nullptr);
115 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
116 colorType, can, SkToBool(surf));
117
118 surf.reset();
119 ctxInfo.grContext()->flush();
120 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500121 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500122 }
123
124 static constexpr int kSampleCnt = 2;
125
126 can = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
127 surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, kSampleCnt,
128 nullptr);
129 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
130 colorType, can, SkToBool(surf));
131
Brian Osman2b23c4b2018-06-01 12:25:08 -0400132 backendTex = gpu->createTestingOnlyBackendTexture(nullptr, kSize, kSize, colorType, true,
133 GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500134 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
135 kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType,
136 nullptr, nullptr);
137 REPORTER_ASSERT(reporter, can == SkToBool(surf),
138 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
139 colorType);
140 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
141 if (surf) {
142 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
143 int storedCnt = rtc->numStencilSamples();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400144 int allowedCnt = ctxInfo.grContext()->contextPriv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500145 storedCnt, rtc->asSurfaceProxy()->config());
146 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
147 "Should store an allowed sample count (%d vs %d)", allowedCnt,
148 storedCnt);
149 }
150
151 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
152 kTopLeft_GrSurfaceOrigin, kSampleCnt,
153 colorType, nullptr, nullptr);
154 REPORTER_ASSERT(reporter, can == SkToBool(surf),
155 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
156 colorType);
157 if (surf) {
158 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
159 int storedCnt = rtc->numStencilSamples();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400160 int allowedCnt = ctxInfo.grContext()->contextPriv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500161 storedCnt, rtc->asSurfaceProxy()->config());
162 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
163 "Should store an allowed sample count (%d vs %d)", allowedCnt,
164 storedCnt);
165 }
166
167 surf.reset();
168 ctxInfo.grContext()->flush();
169 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500170 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500171 }
Brian Salomon93348dd2018-08-29 12:56:23 -0400172
173 GrBackendRenderTarget backendRenderTarget = gpu->createTestingOnlyBackendRenderTarget(
174 16, 16, SkColorTypeToGrColorType(colorType));
175 can = ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType);
176 surf = SkSurface::MakeFromBackendRenderTarget(ctxInfo.grContext(), backendRenderTarget,
177 kTopLeft_GrSurfaceOrigin, colorType, nullptr,
178 nullptr);
179 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d", colorType,
180 can, SkToBool(surf));
181 surf.reset();
182 ctxInfo.grContext()->flush();
183 if (backendRenderTarget.isValid()) {
184 gpu->deleteTestingOnlyBackendRenderTarget(backendRenderTarget);
185 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500186 }
187}
188
189DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
190 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
191 static constexpr int kSize = 10;
192
193 SkColorType colorType = static_cast<SkColorType>(ct);
194 int max = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType);
195 if (!max) {
196 continue;
197 }
198 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
199 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400200 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400201 if (!backendTex.isValid()) {
202 continue;
203 }
204 SkScopeExit freeTex([&backendTex, gpu] {gpu->deleteTestingOnlyBackendTexture(backendTex);});
Brian Salomonbdecacf2018-02-02 20:32:49 -0500205 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
206 auto surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
207 kTopLeft_GrSurfaceOrigin, max,
208 colorType, nullptr, nullptr);
209 REPORTER_ASSERT(reporter, surf);
210 if (!surf) {
211 continue;
212 }
213 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
214 ->getDevice()
215 ->accessRenderTargetContext()
216 ->numStencilSamples();
217 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
218 }
219}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500220
kkinnunen179a8f52015-11-20 13:32:24 -0800221static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700222 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800223 const SkImageInfo& requestInfo,
224 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000225 const SkColor color = SK_ColorRED;
226 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800227 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000228
reed6ceeebd2016-03-09 14:26:26 -0800229 SkPixmap pmap;
230 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800231 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000232
reed6ceeebd2016-03-09 14:26:26 -0800233 SkPixmap pmap2;
234 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000235
kkinnunen179a8f52015-11-20 13:32:24 -0800236 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800237 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
238 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
239 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000240
reed6ceeebd2016-03-09 14:26:26 -0800241 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
242 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
243 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800244 } else {
245 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000246 }
247}
kkinnunen179a8f52015-11-20 13:32:24 -0800248DEF_TEST(SurfaceCanvasPeek, reporter) {
249 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
250 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700251 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800252 test_canvas_peek(reporter, surface, requestInfo, true);
253 }
254}
egdanielab527a52016-06-28 08:07:26 -0700255DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800256 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
257 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700258 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800259 test_canvas_peek(reporter, surface, requestInfo, false);
260 }
261}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000262
reede8f30622016-03-23 18:59:25 -0700263static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700264 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800265 REPORTER_ASSERT(reporter, surface);
266 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700267 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800268 REPORTER_ASSERT(reporter, image);
269 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700270 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700271 }
272 }
273}
kkinnunen179a8f52015-11-20 13:32:24 -0800274DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
275 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700276 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
277 auto surface(surface_func(at, nullptr));
278 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700279 }
280 }
281}
egdanielab527a52016-06-28 08:07:26 -0700282DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800283 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700284 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
285 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
286 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
287 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800288 }
289 }
290}
bsalomon74f681d2015-06-23 14:38:48 -0700291
Robert Phillips8caf85f2018-04-05 09:30:38 -0400292static void test_backend_texture_access_copy_on_write(
293 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
294 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700295 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700296
Robert Phillips8caf85f2018-04-05 09:30:38 -0400297 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700298 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700299
300 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400301 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700302}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400303
304static void test_backend_rendertarget_access_copy_on_write(
305 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
306 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
307 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
308
309 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
310 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
311
312 // If the access mode triggers CoW, then the backend objects should reflect it.
313 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
314 (snap1 == snap2));
315}
316
317DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800318 const SkSurface::BackendHandleAccess accessModes[] = {
319 SkSurface::kFlushRead_BackendHandleAccess,
320 SkSurface::kFlushWrite_BackendHandleAccess,
321 SkSurface::kDiscardWrite_BackendHandleAccess,
322 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400323
kkinnunen179a8f52015-11-20 13:32:24 -0800324 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400325 for (auto& accessMode : accessModes) {
326 {
bsalomon8b7451a2016-05-11 06:33:06 -0700327 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400328 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
329 }
330 {
331 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
332 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800333 }
334 }
335 }
336}
kkinnunen179a8f52015-11-20 13:32:24 -0800337
Robert Phillips8caf85f2018-04-05 09:30:38 -0400338template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
339static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700340 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400341
342 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
343 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700344 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800345 // just read access should not affect the snapshot
346 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
347
Robert Phillips8caf85f2018-04-05 09:30:38 -0400348 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
349 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800351 // expect a new image, since we claimed we would write
352 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
353
Robert Phillips8caf85f2018-04-05 09:30:38 -0400354 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
355 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800357 // expect a new(er) image, since we claimed we would write
358 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
359 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
360}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400361
kkinnunen179a8f52015-11-20 13:32:24 -0800362// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700363DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800364 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400365 {
366 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
367 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
368 surface.get());
369 }
370 {
371 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
372 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
373 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800374 }
375 }
376}
kkinnunen179a8f52015-11-20 13:32:24 -0800377
378// Verify that the right canvas commands trigger a copy on write.
379static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000380 SkCanvas* canvas = surface->getCanvas();
381
382 const SkRect testRect =
383 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
384 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000385 SkPath testPath;
386 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
387 SkIntToScalar(2), SkIntToScalar(1)));
388
389 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
390
391 SkRegion testRegion;
392 testRegion.setRect(testIRect);
393
394
395 const SkColor testColor = 0x01020304;
396 const SkPaint testPaint;
397 const SkPoint testPoints[3] = {
398 {SkIntToScalar(0), SkIntToScalar(0)},
399 {SkIntToScalar(2), SkIntToScalar(1)},
400 {SkIntToScalar(0), SkIntToScalar(2)}
401 };
402 const size_t testPointCount = 3;
403
404 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000405 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000406 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000407
408 SkRRect testRRect;
409 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
410
411 SkString testText("Hello World");
412 const SkPoint testPoints2[] = {
413 { SkIntToScalar(0), SkIntToScalar(1) },
414 { SkIntToScalar(1), SkIntToScalar(1) },
415 { SkIntToScalar(2), SkIntToScalar(1) },
416 { SkIntToScalar(3), SkIntToScalar(1) },
417 { SkIntToScalar(4), SkIntToScalar(1) },
418 { SkIntToScalar(5), SkIntToScalar(1) },
419 { SkIntToScalar(6), SkIntToScalar(1) },
420 { SkIntToScalar(7), SkIntToScalar(1) },
421 { SkIntToScalar(8), SkIntToScalar(1) },
422 { SkIntToScalar(9), SkIntToScalar(1) },
423 { SkIntToScalar(10), SkIntToScalar(1) },
424 };
425
426#define EXPECT_COPY_ON_WRITE(command) \
427 { \
reed9ce9d672016-03-17 10:51:11 -0700428 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
429 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000430 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700431 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
432 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000433 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
434 }
435
436 EXPECT_COPY_ON_WRITE(clear(testColor))
437 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
438 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
439 testPaint))
440 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
441 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
442 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
443 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
444 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700445 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700446 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Cary Clark2a475ea2017-04-28 15:35:12 -0400447 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, testPaint))
junov@chromium.org995beb62013-03-28 13:49:22 +0000448 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
449 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800450}
451DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700452 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800453}
egdanielab527a52016-06-28 08:07:26 -0700454DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800455 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700456 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700457 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700458 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000459}
460
kkinnunen179a8f52015-11-20 13:32:24 -0800461static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
462 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000463 // This test succeeds by not triggering an assertion.
464 // The test verifies that the surface remains writable (usable) after
465 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000466 SkCanvas* canvas = surface->getCanvas();
467 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700468 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000469 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000470}
kkinnunen179a8f52015-11-20 13:32:24 -0800471DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700472 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800473}
egdanielab527a52016-06-28 08:07:26 -0700474DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800475 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700476 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700477 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800478 }
479}
junov@chromium.orgda904742013-05-01 22:38:16 +0000480
kkinnunen179a8f52015-11-20 13:32:24 -0800481static void test_crbug263329(skiatest::Reporter* reporter,
482 SkSurface* surface1,
483 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000484 // This is a regression test for crbug.com/263329
485 // Bug was caused by onCopyOnWrite releasing the old surface texture
486 // back to the scratch texture pool even though the texture is used
487 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000488 SkCanvas* canvas1 = surface1->getCanvas();
489 SkCanvas* canvas2 = surface2->getCanvas();
490 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700491 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000492 // Trigger copy on write, new backing is a scratch texture
493 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700494 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000495 // Trigger copy on write, old backing should not be returned to scratch
496 // pool because it is held by image2
497 canvas1->clear(3);
498
499 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700500 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000501 // Trigger copy on write on surface2. The new backing store should not
502 // be recycling a texture that is held by an existing image.
503 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700504 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400505 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000506 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400507 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
508 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
509 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
510 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
511 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000512}
egdanielab527a52016-06-28 08:07:26 -0700513DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800514 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700515 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
516 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700517 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800518 }
519}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000520
kkinnunen179a8f52015-11-20 13:32:24 -0800521DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700522 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700523 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500524 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800525 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500526 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800527}
egdanielab527a52016-06-28 08:07:26 -0700528DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800529 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700530 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700531 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500532
533 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400534 GrBackendTexture backendTex = image->getBackendTexture(false);
535 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800536 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500537 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400538 GrBackendTexture backendTex2 = image->getBackendTexture(false);
539 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000540 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000541}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800542
reede8f30622016-03-23 18:59:25 -0700543static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
544 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500545
546 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
547 ->asRenderTargetProxy();
548 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800549}
550
bsalomon5ec26ae2016-02-25 08:33:02 -0800551static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400552 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800553}
554
reed9ce9d672016-03-17 10:51:11 -0700555static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
556 return is_budgeted(image.get());
557}
558
egdanielab527a52016-06-28 08:07:26 -0700559DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800560 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400561 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
562 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
563 SkASSERT(surface);
564 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800565
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400566 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800567
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400568 // Initially the image shares a texture with the surface, and the
569 // the budgets should always match.
570 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
571 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800572
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400573 // Now trigger copy-on-write
574 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800575
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400576 // They don't share a texture anymore but the budgets should still match.
577 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
578 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800579 }
580}
junov@chromium.orgda904742013-05-01 22:38:16 +0000581
kkinnunen179a8f52015-11-20 13:32:24 -0800582static void test_no_canvas1(skiatest::Reporter* reporter,
583 SkSurface* surface,
584 SkSurface::ContentChangeMode mode) {
585 // Test passes by not asserting
586 surface->notifyContentWillChange(mode);
587 SkDEBUGCODE(surface->validate();)
588}
589static void test_no_canvas2(skiatest::Reporter* reporter,
590 SkSurface* surface,
591 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000592 // Verifies the robustness of SkSurface for handling use cases where calls
593 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700594 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
595 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800596 SkDEBUGCODE(image1->validate();)
597 SkDEBUGCODE(surface->validate();)
598 surface->notifyContentWillChange(mode);
599 SkDEBUGCODE(image1->validate();)
600 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700601 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
602 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800603 SkDEBUGCODE(image2->validate();)
604 SkDEBUGCODE(surface->validate();)
605 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000606}
kkinnunen179a8f52015-11-20 13:32:24 -0800607DEF_TEST(SurfaceNoCanvas, reporter) {
608 SkSurface::ContentChangeMode modes[] =
609 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
610 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
611 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700612 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800613 }
614 }
615}
egdanielab527a52016-06-28 08:07:26 -0700616DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800617 SkSurface::ContentChangeMode modes[] =
618 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
619 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
620 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
621 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700622 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700623 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700624 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000625 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000626 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000627}
reed9cd016e2016-01-30 10:01:06 -0800628
629static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800630 SkPixmap surfacePM;
631 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800632
reed9ce9d672016-03-17 10:51:11 -0700633 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800634 SkPixmap pm;
635 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800636
reed6ceeebd2016-03-09 14:26:26 -0800637 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800638
639 // trigger a copy-on-write
640 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700641 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800642 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
643
reed6ceeebd2016-03-09 14:26:26 -0800644 SkPixmap pm2;
645 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
646 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800647}
648
649DEF_TEST(surface_rowbytes, reporter) {
650 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
651
reede8f30622016-03-23 18:59:25 -0700652 auto surf0(SkSurface::MakeRaster(info));
653 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800654
655 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700656 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
657 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800658
659 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700660 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800661 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400662 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800663 REPORTER_ASSERT(reporter, nullptr == s);
664}
bsalomone63ffef2016-02-05 07:17:34 -0800665
fmalita03912f12016-07-06 06:22:06 -0700666DEF_TEST(surface_raster_zeroinitialized, reporter) {
667 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
668 SkPixmap pixmap;
669 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
670
671 for (int i = 0; i < pixmap.info().width(); ++i) {
672 for (int j = 0; j < pixmap.info().height(); ++j) {
673 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
674 }
675 }
676}
677
ericrkc4025182016-05-04 12:01:58 -0700678static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500679 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500680 GrGpu* gpu = context->contextPriv().getGpu();
681
ericrkc4025182016-05-04 12:01:58 -0700682 const int kWidth = 10;
683 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400684 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700685 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000686
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500687 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -0400688 pixels.get(), kWidth, kHeight, GrColorType::kRGBA_8888, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000689
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500690 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500691 return nullptr;
692 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000693
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500694 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400695 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500696 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000697 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700698 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500699 gpu->deleteTestingOnlyBackendTexture(*outTexture);
ericrkc4025182016-05-04 12:01:58 -0700700 return nullptr;
701 }
ericrkc4025182016-05-04 12:01:58 -0700702 return surface;
703}
bsalomone63ffef2016-02-05 07:17:34 -0800704
ericrkc4025182016-05-04 12:01:58 -0700705static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500706 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500707 GrGpu* gpu = context->contextPriv().getGpu();
708
ericrkc4025182016-05-04 12:01:58 -0700709 const int kWidth = 10;
710 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400711 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700712 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000713
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500714 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -0400715 pixels.get(), kWidth, kHeight, GrColorType::kRGBA_8888, true, GrMipMapped::kNo, 0);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000716
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500717 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
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(
Greg Danielfaa095e2017-12-19 13:15:02 -0500722 context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
723 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500724
725 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500726 gpu->deleteTestingOnlyBackendTexture(*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);
752 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();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500774 GrGpu* gpu = context->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700775
Robert Phillips2c862492017-01-18 10:08:39 -0500776 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700777 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500778 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
779 },
780 [] (SkSurface* s){
781 sk_sp<SkImage> i(s->makeImageSnapshot());
782 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500783 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500784 GrContext* context = gpuImage->context();
785 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
786 gpuImage->refColorSpace());
787 }
bsalomone63ffef2016-02-05 07:17:34 -0800788 };
ericrkc4025182016-05-04 12:01:58 -0700789
Robert Phillips2c862492017-01-18 10:08:39 -0500790 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700791 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800792 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700793 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800794 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
795 }
796 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700797 const uint32_t kOrigColor = 0xABABABAB;
798 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
799 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500800 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500801 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700802 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
803 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500804 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700805 }
806 }
807}
bsalomone63ffef2016-02-05 07:17:34 -0800808
ericrkc4025182016-05-04 12:01:58 -0700809static void test_surface_draw_partially(
810 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
811 const int kW = surface->width();
812 const int kH = surface->height();
813 SkPaint paint;
814 const SkColor kRectColor = ~origColor | 0xFF000000;
815 paint.setColor(kRectColor);
816 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
817 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400818 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700819 sk_memset32(pixels.get(), ~origColor, kW * kH);
820 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
821 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
822 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
823 bool stop = false;
824 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
825 (origColor >> 0 & 0xFF),
826 (origColor >> 8 & 0xFF),
827 (origColor >> 16 & 0xFF));
828 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
829 (kRectColor >> 16 & 0xFF),
830 (kRectColor >> 8 & 0xFF),
831 (kRectColor >> 0 & 0xFF));
832 for (int y = 0; y < kH/2 && !stop; ++y) {
833 for (int x = 0; x < kW && !stop; ++x) {
834 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
835 if (rectColorPM != pixels[x + y * kW]) {
836 stop = true;
837 }
838 }
839 }
840 stop = false;
841 for (int y = kH/2; y < kH && !stop; ++y) {
842 for (int x = 0; x < kW && !stop; ++x) {
843 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
844 if (origColorPM != pixels[x + y * kW]) {
845 stop = true;
846 }
847 }
848 }
849}
bsalomone63ffef2016-02-05 07:17:34 -0800850
egdanielab527a52016-06-28 08:07:26 -0700851DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500852 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700853 if (!gpu) {
854 return;
855 }
856 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800857
ericrkc4025182016-05-04 12:01:58 -0700858 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
859 &create_gpu_surface_backend_texture_as_render_target}) {
860 // Validate that we can draw to the canvas and that the original texture color is
861 // preserved in pixels that aren't rendered to via the surface.
862 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500863 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500864 auto surface = surfaceFunc(ctxInfo.grContext(), 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700865 if (surface) {
866 test_surface_draw_partially(reporter, surface, kOrigColor);
867 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500868 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700869 }
870 }
871}
872
873
874DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500875 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700876 if (!gpu) {
877 return;
878 }
Eric Karl5c779752017-05-08 12:02:07 -0700879 if (gpu->caps()->avoidStencilBuffers()) {
880 return;
881 }
ericrkc4025182016-05-04 12:01:58 -0700882 static const uint32_t kOrigColor = 0xFFAABBCC;
883
Robert Phillips6be756b2018-01-16 15:07:54 -0500884 auto resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
885
ericrkc4025182016-05-04 12:01:58 -0700886 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
887 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500888 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500889 GrBackendTexture backendTex;
890 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700891
Brian Salomonbdecacf2018-02-02 20:32:49 -0500892 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500893 // Certain platforms don't support MSAA, skip these.
894 continue;
ericrkc4025182016-05-04 12:01:58 -0700895 }
896
897 // Validate that we can attach a stencil buffer to an SkSurface created by either of
898 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400899 GrRenderTarget* rt = surface->getCanvas()
900 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Robert Phillips6be756b2018-01-16 15:07:54 -0500901 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt));
Brian Salomon26102cb2018-03-09 09:33:19 -0500902 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700903 }
bsalomone63ffef2016-02-05 07:17:34 -0800904 }
905}
brianosman0e22eb82016-08-30 07:07:59 -0700906
907static void test_surface_creation_and_snapshot_with_color_space(
908 skiatest::Reporter* reporter,
909 const char* prefix,
Mike Klein37854712018-06-26 11:43:06 -0400910 bool supportsF16,
911 bool supportsF32,
Brian Osman10fc6fd2018-03-02 11:01:10 -0500912 bool supports1010102,
brianosman0e22eb82016-08-30 07:07:59 -0700913 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
914
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500915 auto srgbColorSpace = SkColorSpace::MakeSRGB();
Mike Klein4429a4f2018-10-04 09:06:00 -0400916 SkMatrix44 srgbMatrix;
Mike Kleinae5e8642018-10-03 17:00:41 -0400917 srgbColorSpace->toXYZD50(&srgbMatrix);
918
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400919 SkColorSpaceTransferFn oddGamma;
920 oddGamma.fA = 1.0f;
921 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
922 oddGamma.fG = 4.0f;
Mike Kleinae5e8642018-10-03 17:00:41 -0400923 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500924 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700925
926 const struct {
927 SkColorType fColorType;
928 sk_sp<SkColorSpace> fColorSpace;
929 bool fShouldWork;
930 const char* fDescription;
931 } testConfigs[] = {
Mike Klein37854712018-06-26 11:43:06 -0400932 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
933 { kN32_SkColorType, linearColorSpace, true, "N32-linear" },
934 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
935 { kN32_SkColorType, oddColorSpace, true, "N32-odd" },
936 { kRGBA_F16_SkColorType, nullptr, supportsF16, "F16-nullptr" },
937 { kRGBA_F16_SkColorType, linearColorSpace, supportsF16, "F16-linear" },
938 { kRGBA_F16_SkColorType, srgbColorSpace, supportsF16, "F16-srgb" },
939 { kRGBA_F16_SkColorType, oddColorSpace, supportsF16, "F16-odd" },
940 { kRGBA_F32_SkColorType, nullptr, supportsF32, "F32-nullptr" },
941 { kRGBA_F32_SkColorType, linearColorSpace, supportsF32, "F32-linear" },
942 { kRGBA_F32_SkColorType, srgbColorSpace, supportsF32, "F32-srgb" },
943 { kRGBA_F32_SkColorType, oddColorSpace, supportsF32, "F32-odd" },
944 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
945 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
946 { kRGBA_1010102_SkColorType, nullptr, supports1010102, "1010102-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700947 };
948
949 for (auto& testConfig : testConfigs) {
950 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
951 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
952 testConfig.fColorSpace);
953
brianosman0e22eb82016-08-30 07:07:59 -0700954 auto surface(surfaceMaker(info));
Mike Klein37854712018-06-26 11:43:06 -0400955 REPORTER_ASSERT(reporter,
956 SkToBool(surface) == testConfig.fShouldWork, fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700957
Mike Klein37854712018-06-26 11:43:06 -0400958 if (testConfig.fShouldWork && surface) {
brianosman0e22eb82016-08-30 07:07:59 -0700959 sk_sp<SkImage> image(surface->makeImageSnapshot());
Brian Salomon1c80e992018-01-29 09:50:47 -0500960 REPORTER_ASSERT(reporter, image, testConfig.fDescription);
brianosman0e22eb82016-08-30 07:07:59 -0700961 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
Brian Salomon1c80e992018-01-29 09:50:47 -0500962 REPORTER_ASSERT(reporter, imageColorSpace == testConfig.fColorSpace.get(),
963 fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700964 }
965 }
966}
967
968DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
969 auto surfaceMaker = [](const SkImageInfo& info) {
970 return SkSurface::MakeRaster(info);
971 };
972
Mike Klein37854712018-06-26 11:43:06 -0400973 test_surface_creation_and_snapshot_with_color_space(reporter, "raster",
974 true, true, true,
Brian Osman10fc6fd2018-03-02 11:01:10 -0500975 surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -0700976}
977
brianosman0e22eb82016-08-30 07:07:59 -0700978DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400979 auto context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500980
Mike Klein37854712018-06-26 11:43:06 -0400981 bool supportsF16 = context->contextPriv().caps()->isConfigRenderable(kRGBA_half_GrPixelConfig),
982 supportsF32 = context->contextPriv().caps()->isConfigRenderable(kRGBA_float_GrPixelConfig),
983 supports1010102 = context->contextPriv().caps()->isConfigRenderable(kRGBA_1010102_GrPixelConfig);
984
brianosman0e22eb82016-08-30 07:07:59 -0700985 auto surfaceMaker = [context](const SkImageInfo& info) {
986 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
987 };
988
Mike Klein37854712018-06-26 11:43:06 -0400989 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu",
990 supportsF16, supportsF32, supports1010102,
991 surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -0700992
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500993 std::vector<GrBackendTexture> backendTextures;
994 auto wrappedSurfaceMaker = [ context, &backendTextures ](const SkImageInfo& info) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500995 GrGpu* gpu = context->contextPriv().getGpu();
996
Greg Daniel7ef28f32017-04-20 16:41:55 +0000997 static const int kSize = 10;
brianosman0e22eb82016-08-30 07:07:59 -0700998
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500999 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -04001000 nullptr, kSize, kSize, info.colorType(), true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001001
Greg Daniel5366e592018-01-10 09:57:53 -05001002 if (!backendTex.isValid() ||
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001003 !gpu->isTestingOnlyBackendTexture(backendTex)) {
brianosman0e22eb82016-08-30 07:07:59 -07001004 return sk_sp<SkSurface>(nullptr);
1005 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001006 backendTextures.push_back(backendTex);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001007
1008 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -04001009 kTopLeft_GrSurfaceOrigin, 0,
Greg Danielfaa095e2017-12-19 13:15:02 -05001010 info.colorType(),
Greg Daniel7ef28f32017-04-20 16:41:55 +00001011 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -07001012 };
1013
Mike Klein37854712018-06-26 11:43:06 -04001014 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped",
1015 supportsF16, supportsF32, supports1010102,
1016 wrappedSurfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -07001017
Robert Phillips6cdc22c2017-05-11 16:29:14 -04001018 context->flush();
1019
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001020 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips2fbbd032018-04-10 10:26:44 -04001021 gpu->testingOnly_flushGpuAndSync();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001022 for (auto backendTex : backendTextures) {
Brian Salomon26102cb2018-03-09 09:33:19 -05001023 gpu->deleteTestingOnlyBackendTexture(backendTex);
brianosman0e22eb82016-08-30 07:07:59 -07001024 }
1025}
Matt Sarett22886c42016-11-22 11:31:41 -05001026
1027static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001028 SkOverdrawCanvas canvas(surface->getCanvas());
1029 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001030 sk_sp<SkImage> image = surface->makeImageSnapshot();
1031
1032 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001033 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001034 for (int y = 0; y < 10; y++) {
1035 for (int x = 0; x < 10; x++) {
1036 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1037 }
1038 }
1039}
1040
1041DEF_TEST(OverdrawSurface_Raster, r) {
1042 sk_sp<SkSurface> surface = create_surface();
1043 test_overdraw_surface(r, surface.get());
1044}
1045
Matt Sarett22886c42016-11-22 11:31:41 -05001046DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1047 GrContext* context = ctxInfo.grContext();
1048 sk_sp<SkSurface> surface = create_gpu_surface(context);
1049 test_overdraw_surface(r, surface.get());
1050}
Mike Reed44d04bd2017-06-28 19:57:21 -04001051
1052DEF_TEST(Surface_null, r) {
1053 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1054
1055 const int w = 37;
1056 const int h = 1000;
1057 auto surf = SkSurface::MakeNull(w, h);
1058 auto canvas = surf->getCanvas();
1059
1060 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1061 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1062}
Mike Reedd4746982018-02-07 16:05:29 -05001063
1064// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1065// (so the snapshot can succeed)
1066DEF_TEST(surface_image_unity, reporter) {
1067 auto do_test = [reporter](const SkImageInfo& info) {
1068 size_t rowBytes = info.minRowBytes();
1069 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1070 if (surf) {
1071 auto img = surf->makeImageSnapshot();
1072 if (!img && false) { // change to true to document the differences
1073 SkDebugf("image failed: [%08X %08X] %14s %s\n",
1074 info.width(), info.height(),
1075 sk_tool_utils::colortype_name(info.colorType()),
1076 sk_tool_utils::alphatype_name(info.alphaType()));
1077 return;
1078 }
1079 REPORTER_ASSERT(reporter, img != nullptr);
1080
1081 char dummyPixel = 0; // just need a valid address (not a valid size)
1082 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1083 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1084 REPORTER_ASSERT(reporter, img != nullptr);
1085 }
1086 };
1087
Mike Klein955b3d52018-02-20 11:55:48 -05001088 const int32_t sizes[] = { 0, 1, 1 << 15, 1 << 16, 1 << 18, 1 << 28, 1 << 29, 1 << 30, -1 };
Mike Klein30dc8f92018-02-16 10:08:10 -05001089 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001090 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001091 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001092 SkAlphaType at = static_cast<SkAlphaType>(ati);
1093 for (int32_t size : sizes) {
Mike Klein955b3d52018-02-20 11:55:48 -05001094 // Large allocations tend to make the 32-bit bots run out of virtual address space.
1095 if (sizeof(size_t) == 4 && size > (1<<20)) {
1096 continue;
1097 }
Mike Reedd4746982018-02-07 16:05:29 -05001098 do_test(SkImageInfo::Make(1, size, ct, at));
1099 do_test(SkImageInfo::Make(size, 1, ct, at));
1100 }
1101 }
1102 }
1103}