blob: af17791378da7c99436737338d405b3625814266 [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
bsalomone63ffef2016-02-05 07:17:34 -08008#include <functional>
Brian Osmanc7ad40f2018-05-31 14:27:17 -04009#include <initializer_list>
Brian Salomonbdecacf2018-02-02 20:32:49 -050010#include <vector>
kkinnunen179a8f52015-11-20 13:32:24 -080011#include "GrContext.h"
Robert Phillips2c862492017-01-18 10:08:39 -050012#include "GrContextPriv.h"
Brian Salomon3a2cc2c2018-02-03 00:25:12 +000013#include "GrGpu.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050014#include "GrGpuResourcePriv.h"
15#include "GrRenderTargetContext.h"
ericrkc4025182016-05-04 12:01:58 -070016#include "GrResourceProvider.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000017#include "GrTest.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040018#include "SkCanvas.h"
19#include "SkData.h"
20#include "SkDevice.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050021#include "SkGpuDevice.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040022#include "SkImage_Base.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050023#include "SkImage_Gpu.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040024#include "SkOverdrawCanvas.h"
25#include "SkPath.h"
26#include "SkRegion.h"
27#include "SkRRect.h"
28#include "SkSurface.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050029#include "SkSurface_Gpu.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040030#include "SkUtils.h"
31#include "Test.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000032
Mike Reedd4746982018-02-07 16:05:29 -050033#include "sk_tool_utils.h"
34
bsalomon74f681d2015-06-23 14:38:48 -070035
kkinnunen179a8f52015-11-20 13:32:24 -080036static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070037 SkASSERT(pixels == context);
38 sk_free(pixels);
39}
reede8f30622016-03-23 18:59:25 -070040static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
41 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070042 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000043 if (requestedInfo) {
44 *requestedInfo = info;
45 }
reede8f30622016-03-23 18:59:25 -070046 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000047}
reede8f30622016-03-23 18:59:25 -070048static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
49 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080050 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
51 if (requestedInfo) {
52 *requestedInfo = info;
53 }
54 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040055 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070056 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
57 release_direct_surface_storage,
58 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080059}
reede8f30622016-03-23 18:59:25 -070060static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
61 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080062 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
63 if (requestedInfo) {
64 *requestedInfo = info;
65 }
robertphillips7e922762016-07-26 11:38:17 -070066 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080067}
reede8f30622016-03-23 18:59:25 -070068static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
69 SkAlphaType at = kPremul_SkAlphaType,
70 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080071 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
72 if (requestedInfo) {
73 *requestedInfo = info;
74 }
robertphillips7e922762016-07-26 11:38:17 -070075 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080076}
junov@chromium.org995beb62013-03-28 13:49:22 +000077
kkinnunen179a8f52015-11-20 13:32:24 -080078DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080079 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070080 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
81 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080082
reedb2497c22014-12-31 12:31:43 -080083}
egdanielab527a52016-06-28 08:07:26 -070084DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080085 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
86 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070087 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080088}
reedb2497c22014-12-31 12:31:43 -080089
Brian Salomonbdecacf2018-02-02 20:32:49 -050090DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
91 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
92 static constexpr int kSize = 10;
93
94 SkColorType colorType = static_cast<SkColorType>(ct);
95 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
96 bool can = ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType);
97 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, 1,
98 nullptr);
99 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
100 colorType, can, SkToBool(surf));
101
102 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
103 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400104 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500105 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
106 kTopLeft_GrSurfaceOrigin, 0, colorType, nullptr,
107 nullptr);
108 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
109 colorType, can, SkToBool(surf));
110
111 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
112 kTopLeft_GrSurfaceOrigin, 1,
113 colorType, nullptr, nullptr);
114 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
115 colorType, can, SkToBool(surf));
116
117 surf.reset();
118 ctxInfo.grContext()->flush();
119 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500120 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500121 }
122
123 static constexpr int kSampleCnt = 2;
124
125 can = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
126 surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, kSampleCnt,
127 nullptr);
128 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
129 colorType, can, SkToBool(surf));
130
Brian Osman2b23c4b2018-06-01 12:25:08 -0400131 backendTex = gpu->createTestingOnlyBackendTexture(nullptr, kSize, kSize, colorType, true,
132 GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
134 kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType,
135 nullptr, nullptr);
136 REPORTER_ASSERT(reporter, can == SkToBool(surf),
137 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
138 colorType);
139 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
140 if (surf) {
141 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
142 int storedCnt = rtc->numStencilSamples();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400143 int allowedCnt = ctxInfo.grContext()->contextPriv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500144 storedCnt, rtc->asSurfaceProxy()->config());
145 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
146 "Should store an allowed sample count (%d vs %d)", allowedCnt,
147 storedCnt);
148 }
149
150 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
151 kTopLeft_GrSurfaceOrigin, kSampleCnt,
152 colorType, nullptr, nullptr);
153 REPORTER_ASSERT(reporter, can == SkToBool(surf),
154 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
155 colorType);
156 if (surf) {
157 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
158 int storedCnt = rtc->numStencilSamples();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400159 int allowedCnt = ctxInfo.grContext()->contextPriv().caps()->getSampleCount(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500160 storedCnt, rtc->asSurfaceProxy()->config());
161 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
162 "Should store an allowed sample count (%d vs %d)", allowedCnt,
163 storedCnt);
164 }
165
166 surf.reset();
167 ctxInfo.grContext()->flush();
168 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500169 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500170 }
171 }
172}
173
174DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
175 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
176 static constexpr int kSize = 10;
177
178 SkColorType colorType = static_cast<SkColorType>(ct);
179 int max = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType);
180 if (!max) {
181 continue;
182 }
183 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
184 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400185 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
Brian Salomon99501b72018-03-23 11:26:11 -0400186 if (!backendTex.isValid()) {
187 continue;
188 }
189 SkScopeExit freeTex([&backendTex, gpu] {gpu->deleteTestingOnlyBackendTexture(backendTex);});
Brian Salomonbdecacf2018-02-02 20:32:49 -0500190 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
191 auto surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
192 kTopLeft_GrSurfaceOrigin, max,
193 colorType, nullptr, nullptr);
194 REPORTER_ASSERT(reporter, surf);
195 if (!surf) {
196 continue;
197 }
198 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
199 ->getDevice()
200 ->accessRenderTargetContext()
201 ->numStencilSamples();
202 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
203 }
204}
Brian Salomonbdecacf2018-02-02 20:32:49 -0500205
kkinnunen179a8f52015-11-20 13:32:24 -0800206static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700207 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800208 const SkImageInfo& requestInfo,
209 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000210 const SkColor color = SK_ColorRED;
211 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800212 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000213
reed6ceeebd2016-03-09 14:26:26 -0800214 SkPixmap pmap;
215 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800216 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000217
reed6ceeebd2016-03-09 14:26:26 -0800218 SkPixmap pmap2;
219 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000220
kkinnunen179a8f52015-11-20 13:32:24 -0800221 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800222 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
223 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
224 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000225
reed6ceeebd2016-03-09 14:26:26 -0800226 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
227 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
228 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800229 } else {
230 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000231 }
232}
kkinnunen179a8f52015-11-20 13:32:24 -0800233DEF_TEST(SurfaceCanvasPeek, reporter) {
234 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
235 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700236 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800237 test_canvas_peek(reporter, surface, requestInfo, true);
238 }
239}
egdanielab527a52016-06-28 08:07:26 -0700240DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800241 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
242 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700243 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800244 test_canvas_peek(reporter, surface, requestInfo, false);
245 }
246}
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000247
reede8f30622016-03-23 18:59:25 -0700248static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700249 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800250 REPORTER_ASSERT(reporter, surface);
251 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700252 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800253 REPORTER_ASSERT(reporter, image);
254 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700255 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700256 }
257 }
258}
kkinnunen179a8f52015-11-20 13:32:24 -0800259DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
260 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700261 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
262 auto surface(surface_func(at, nullptr));
263 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700264 }
265 }
266}
egdanielab527a52016-06-28 08:07:26 -0700267DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800268 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700269 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
270 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
271 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
272 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800273 }
274 }
275}
bsalomon74f681d2015-06-23 14:38:48 -0700276
Robert Phillips8caf85f2018-04-05 09:30:38 -0400277static void test_backend_texture_access_copy_on_write(
278 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
279 GrBackendTexture tex1 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700280 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700281
Robert Phillips8caf85f2018-04-05 09:30:38 -0400282 GrBackendTexture tex2 = surface->getBackendTexture(access);
reed9ce9d672016-03-17 10:51:11 -0700283 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700284
285 // If the access mode triggers CoW, then the backend objects should reflect it.
Robert Phillips8caf85f2018-04-05 09:30:38 -0400286 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(tex1, tex2) == (snap1 == snap2));
fmalitae2639082015-08-06 07:04:51 -0700287}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400288
289static void test_backend_rendertarget_access_copy_on_write(
290 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess access) {
291 GrBackendRenderTarget rt1 = surface->getBackendRenderTarget(access);
292 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
293
294 GrBackendRenderTarget rt2 = surface->getBackendRenderTarget(access);
295 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
296
297 // If the access mode triggers CoW, then the backend objects should reflect it.
298 REPORTER_ASSERT(reporter, GrBackendRenderTarget::TestingOnly_Equals(rt1, rt2) ==
299 (snap1 == snap2));
300}
301
302DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendSurfaceAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800303 const SkSurface::BackendHandleAccess accessModes[] = {
304 SkSurface::kFlushRead_BackendHandleAccess,
305 SkSurface::kFlushWrite_BackendHandleAccess,
306 SkSurface::kDiscardWrite_BackendHandleAccess,
307 };
Robert Phillips8caf85f2018-04-05 09:30:38 -0400308
kkinnunen179a8f52015-11-20 13:32:24 -0800309 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400310 for (auto& accessMode : accessModes) {
311 {
bsalomon8b7451a2016-05-11 06:33:06 -0700312 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
Robert Phillips8caf85f2018-04-05 09:30:38 -0400313 test_backend_texture_access_copy_on_write(reporter, surface.get(), accessMode);
314 }
315 {
316 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
317 test_backend_rendertarget_access_copy_on_write(reporter, surface.get(), accessMode);
kkinnunen179a8f52015-11-20 13:32:24 -0800318 }
319 }
320 }
321}
kkinnunen179a8f52015-11-20 13:32:24 -0800322
Robert Phillips8caf85f2018-04-05 09:30:38 -0400323template<typename Type, Type(SkSurface::*func)(SkSurface::BackendHandleAccess)>
324static void test_backend_unique_id(skiatest::Reporter* reporter, SkSurface* surface) {
reed9ce9d672016-03-17 10:51:11 -0700325 sk_sp<SkImage> image0(surface->makeImageSnapshot());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400326
327 Type obj = (surface->*func)(SkSurface::kFlushRead_BackendHandleAccess);
328 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700329 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800330 // just read access should not affect the snapshot
331 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
332
Robert Phillips8caf85f2018-04-05 09:30:38 -0400333 obj = (surface->*func)(SkSurface::kFlushWrite_BackendHandleAccess);
334 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700335 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800336 // expect a new image, since we claimed we would write
337 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
338
Robert Phillips8caf85f2018-04-05 09:30:38 -0400339 obj = (surface->*func)(SkSurface::kDiscardWrite_BackendHandleAccess);
340 REPORTER_ASSERT(reporter, obj.isValid());
reed9ce9d672016-03-17 10:51:11 -0700341 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800342 // expect a new(er) image, since we claimed we would write
343 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
344 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
345}
Robert Phillips8caf85f2018-04-05 09:30:38 -0400346
kkinnunen179a8f52015-11-20 13:32:24 -0800347// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700348DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800349 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
Robert Phillips8caf85f2018-04-05 09:30:38 -0400350 {
351 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
352 test_backend_unique_id<GrBackendTexture, &SkSurface::getBackendTexture>(reporter,
353 surface.get());
354 }
355 {
356 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
357 test_backend_unique_id<GrBackendRenderTarget, &SkSurface::getBackendRenderTarget>(
358 reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800359 }
360 }
361}
kkinnunen179a8f52015-11-20 13:32:24 -0800362
363// Verify that the right canvas commands trigger a copy on write.
364static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000365 SkCanvas* canvas = surface->getCanvas();
366
367 const SkRect testRect =
368 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
369 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000370 SkPath testPath;
371 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
372 SkIntToScalar(2), SkIntToScalar(1)));
373
374 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
375
376 SkRegion testRegion;
377 testRegion.setRect(testIRect);
378
379
380 const SkColor testColor = 0x01020304;
381 const SkPaint testPaint;
382 const SkPoint testPoints[3] = {
383 {SkIntToScalar(0), SkIntToScalar(0)},
384 {SkIntToScalar(2), SkIntToScalar(1)},
385 {SkIntToScalar(0), SkIntToScalar(2)}
386 };
387 const size_t testPointCount = 3;
388
389 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000390 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000391 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000392
393 SkRRect testRRect;
394 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
395
396 SkString testText("Hello World");
397 const SkPoint testPoints2[] = {
398 { SkIntToScalar(0), SkIntToScalar(1) },
399 { SkIntToScalar(1), SkIntToScalar(1) },
400 { SkIntToScalar(2), SkIntToScalar(1) },
401 { SkIntToScalar(3), SkIntToScalar(1) },
402 { SkIntToScalar(4), SkIntToScalar(1) },
403 { SkIntToScalar(5), SkIntToScalar(1) },
404 { SkIntToScalar(6), SkIntToScalar(1) },
405 { SkIntToScalar(7), SkIntToScalar(1) },
406 { SkIntToScalar(8), SkIntToScalar(1) },
407 { SkIntToScalar(9), SkIntToScalar(1) },
408 { SkIntToScalar(10), SkIntToScalar(1) },
409 };
410
411#define EXPECT_COPY_ON_WRITE(command) \
412 { \
reed9ce9d672016-03-17 10:51:11 -0700413 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
414 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000415 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700416 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
417 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000418 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
419 }
420
421 EXPECT_COPY_ON_WRITE(clear(testColor))
422 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
423 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
424 testPaint))
425 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
426 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
427 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
428 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
429 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700430 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700431 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Cary Clark2a475ea2017-04-28 15:35:12 -0400432 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, testPaint))
junov@chromium.org995beb62013-03-28 13:49:22 +0000433 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
434 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800435}
436DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700437 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800438}
egdanielab527a52016-06-28 08:07:26 -0700439DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800440 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700441 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700442 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700443 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000444}
445
kkinnunen179a8f52015-11-20 13:32:24 -0800446static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
447 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000448 // This test succeeds by not triggering an assertion.
449 // The test verifies that the surface remains writable (usable) after
450 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000451 SkCanvas* canvas = surface->getCanvas();
452 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700453 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000454 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000455}
kkinnunen179a8f52015-11-20 13:32:24 -0800456DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700457 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800458}
egdanielab527a52016-06-28 08:07:26 -0700459DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800460 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700461 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700462 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800463 }
464}
junov@chromium.orgda904742013-05-01 22:38:16 +0000465
kkinnunen179a8f52015-11-20 13:32:24 -0800466static void test_crbug263329(skiatest::Reporter* reporter,
467 SkSurface* surface1,
468 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000469 // This is a regression test for crbug.com/263329
470 // Bug was caused by onCopyOnWrite releasing the old surface texture
471 // back to the scratch texture pool even though the texture is used
472 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000473 SkCanvas* canvas1 = surface1->getCanvas();
474 SkCanvas* canvas2 = surface2->getCanvas();
475 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700476 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000477 // Trigger copy on write, new backing is a scratch texture
478 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700479 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000480 // Trigger copy on write, old backing should not be returned to scratch
481 // pool because it is held by image2
482 canvas1->clear(3);
483
484 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700485 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000486 // Trigger copy on write on surface2. The new backing store should not
487 // be recycling a texture that is held by an existing image.
488 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700489 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400490 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000491 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400492 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
493 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
494 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
495 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
496 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000497}
egdanielab527a52016-06-28 08:07:26 -0700498DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800499 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700500 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
501 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700502 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800503 }
504}
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000505
kkinnunen179a8f52015-11-20 13:32:24 -0800506DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700507 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700508 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500509 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800510 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500511 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800512}
egdanielab527a52016-06-28 08:07:26 -0700513DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_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 surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700516 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500517
518 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400519 GrBackendTexture backendTex = image->getBackendTexture(false);
520 REPORTER_ASSERT(reporter, backendTex.isValid());
kkinnunen179a8f52015-11-20 13:32:24 -0800521 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500522 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
Robert Phillipsc5509952018-04-04 15:54:55 -0400523 GrBackendTexture backendTex2 = image->getBackendTexture(false);
524 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTex2));
junov@chromium.orgda904742013-05-01 22:38:16 +0000525 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000526}
bsalomoneaaaf0b2015-01-23 08:08:04 -0800527
reede8f30622016-03-23 18:59:25 -0700528static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
529 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500530
531 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
532 ->asRenderTargetProxy();
533 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800534}
535
bsalomon5ec26ae2016-02-25 08:33:02 -0800536static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400537 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800538}
539
reed9ce9d672016-03-17 10:51:11 -0700540static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
541 return is_budgeted(image.get());
542}
543
egdanielab527a52016-06-28 08:07:26 -0700544DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800545 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400546 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
547 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
548 SkASSERT(surface);
549 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800550
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400551 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800552
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400553 // Initially the image shares a texture with the surface, and the
554 // the budgets should always match.
555 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
556 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800557
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400558 // Now trigger copy-on-write
559 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800560
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400561 // They don't share a texture anymore but the budgets should still match.
562 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
563 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800564 }
565}
junov@chromium.orgda904742013-05-01 22:38:16 +0000566
kkinnunen179a8f52015-11-20 13:32:24 -0800567static void test_no_canvas1(skiatest::Reporter* reporter,
568 SkSurface* surface,
569 SkSurface::ContentChangeMode mode) {
570 // Test passes by not asserting
571 surface->notifyContentWillChange(mode);
572 SkDEBUGCODE(surface->validate();)
573}
574static void test_no_canvas2(skiatest::Reporter* reporter,
575 SkSurface* surface,
576 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000577 // Verifies the robustness of SkSurface for handling use cases where calls
578 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700579 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
580 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800581 SkDEBUGCODE(image1->validate();)
582 SkDEBUGCODE(surface->validate();)
583 surface->notifyContentWillChange(mode);
584 SkDEBUGCODE(image1->validate();)
585 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700586 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
587 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800588 SkDEBUGCODE(image2->validate();)
589 SkDEBUGCODE(surface->validate();)
590 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000591}
kkinnunen179a8f52015-11-20 13:32:24 -0800592DEF_TEST(SurfaceNoCanvas, reporter) {
593 SkSurface::ContentChangeMode modes[] =
594 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
595 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
596 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700597 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800598 }
599 }
600}
egdanielab527a52016-06-28 08:07:26 -0700601DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800602 SkSurface::ContentChangeMode modes[] =
603 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
604 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
605 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
606 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700607 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700608 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700609 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000610 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000611 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000612}
reed9cd016e2016-01-30 10:01:06 -0800613
614static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800615 SkPixmap surfacePM;
616 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800617
reed9ce9d672016-03-17 10:51:11 -0700618 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800619 SkPixmap pm;
620 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800621
reed6ceeebd2016-03-09 14:26:26 -0800622 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800623
624 // trigger a copy-on-write
625 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700626 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800627 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
628
reed6ceeebd2016-03-09 14:26:26 -0800629 SkPixmap pm2;
630 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
631 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800632}
633
634DEF_TEST(surface_rowbytes, reporter) {
635 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
636
reede8f30622016-03-23 18:59:25 -0700637 auto surf0(SkSurface::MakeRaster(info));
638 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800639
640 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700641 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
642 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800643
644 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700645 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800646 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400647 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800648 REPORTER_ASSERT(reporter, nullptr == s);
649}
bsalomone63ffef2016-02-05 07:17:34 -0800650
fmalita03912f12016-07-06 06:22:06 -0700651DEF_TEST(surface_raster_zeroinitialized, reporter) {
652 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
653 SkPixmap pixmap;
654 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
655
656 for (int i = 0; i < pixmap.info().width(); ++i) {
657 for (int j = 0; j < pixmap.info().height(); ++j) {
658 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
659 }
660 }
661}
662
ericrkc4025182016-05-04 12:01:58 -0700663static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500664 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500665 GrGpu* gpu = context->contextPriv().getGpu();
666
ericrkc4025182016-05-04 12:01:58 -0700667 const int kWidth = 10;
668 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400669 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700670 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000671
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500672 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500673 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000674
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500675 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500676 return nullptr;
677 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000678
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500679 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400680 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500681 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000682 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700683 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500684 gpu->deleteTestingOnlyBackendTexture(*outTexture);
ericrkc4025182016-05-04 12:01:58 -0700685 return nullptr;
686 }
ericrkc4025182016-05-04 12:01:58 -0700687 return surface;
688}
bsalomone63ffef2016-02-05 07:17:34 -0800689
ericrkc4025182016-05-04 12:01:58 -0700690static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500691 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500692 GrGpu* gpu = context->contextPriv().getGpu();
693
ericrkc4025182016-05-04 12:01:58 -0700694 const int kWidth = 10;
695 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400696 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700697 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000698
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500699 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500700 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000701
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500702 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
ericrkc4025182016-05-04 12:01:58 -0700703 return nullptr;
704 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500705
706 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Greg Danielfaa095e2017-12-19 13:15:02 -0500707 context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
708 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500709
710 if (!surface) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500711 gpu->deleteTestingOnlyBackendTexture(*outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500712 return nullptr;
713 }
ericrkc4025182016-05-04 12:01:58 -0700714 return surface;
715}
716
717static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500718 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700719 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800720 if (!surface) {
721 ERRORF(reporter, "Could not create GPU SkSurface.");
722 return;
723 }
724 int w = surface->width();
725 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400726 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700727 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800728
Robert Phillips2c862492017-01-18 10:08:39 -0500729 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
730 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800731 ERRORF(reporter, "Could access render target of GPU SkSurface.");
732 return;
733 }
bsalomon2fba8092016-02-05 13:47:06 -0800734 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500735
736 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
737 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800738 for (int y = 0; y < h; ++y) {
739 for (int x = 0; x < w; ++x) {
740 uint32_t pixel = pixels.get()[y * w + x];
741 if (pixel != expectedValue) {
742 SkString msg;
743 if (expectedValue) {
744 msg = "SkSurface should have left render target unmodified";
745 } else {
746 msg = "SkSurface should have cleared the render target";
747 }
748 ERRORF(reporter,
749 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
750 expectedValue, x, y);
751 return;
752 }
753 }
754 }
755}
756
bsalomon758586c2016-04-06 14:02:39 -0700757DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700758 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500759 GrGpu* gpu = context->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700760
Robert Phillips2c862492017-01-18 10:08:39 -0500761 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700762 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500763 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
764 },
765 [] (SkSurface* s){
766 sk_sp<SkImage> i(s->makeImageSnapshot());
767 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500768 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500769 GrContext* context = gpuImage->context();
770 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
771 gpuImage->refColorSpace());
772 }
bsalomone63ffef2016-02-05 07:17:34 -0800773 };
ericrkc4025182016-05-04 12:01:58 -0700774
Robert Phillips2c862492017-01-18 10:08:39 -0500775 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700776 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800777 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700778 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800779 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
780 }
781 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700782 const uint32_t kOrigColor = 0xABABABAB;
783 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
784 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500785 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500786 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700787 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
788 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500789 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700790 }
791 }
792}
bsalomone63ffef2016-02-05 07:17:34 -0800793
ericrkc4025182016-05-04 12:01:58 -0700794static void test_surface_draw_partially(
795 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
796 const int kW = surface->width();
797 const int kH = surface->height();
798 SkPaint paint;
799 const SkColor kRectColor = ~origColor | 0xFF000000;
800 paint.setColor(kRectColor);
801 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
802 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400803 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700804 sk_memset32(pixels.get(), ~origColor, kW * kH);
805 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
806 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
807 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
808 bool stop = false;
809 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
810 (origColor >> 0 & 0xFF),
811 (origColor >> 8 & 0xFF),
812 (origColor >> 16 & 0xFF));
813 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
814 (kRectColor >> 16 & 0xFF),
815 (kRectColor >> 8 & 0xFF),
816 (kRectColor >> 0 & 0xFF));
817 for (int y = 0; y < kH/2 && !stop; ++y) {
818 for (int x = 0; x < kW && !stop; ++x) {
819 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
820 if (rectColorPM != pixels[x + y * kW]) {
821 stop = true;
822 }
823 }
824 }
825 stop = false;
826 for (int y = kH/2; y < kH && !stop; ++y) {
827 for (int x = 0; x < kW && !stop; ++x) {
828 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
829 if (origColorPM != pixels[x + y * kW]) {
830 stop = true;
831 }
832 }
833 }
834}
bsalomone63ffef2016-02-05 07:17:34 -0800835
egdanielab527a52016-06-28 08:07:26 -0700836DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500837 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700838 if (!gpu) {
839 return;
840 }
841 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800842
ericrkc4025182016-05-04 12:01:58 -0700843 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
844 &create_gpu_surface_backend_texture_as_render_target}) {
845 // Validate that we can draw to the canvas and that the original texture color is
846 // preserved in pixels that aren't rendered to via the surface.
847 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500848 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500849 auto surface = surfaceFunc(ctxInfo.grContext(), 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700850 if (surface) {
851 test_surface_draw_partially(reporter, surface, kOrigColor);
852 surface.reset();
Brian Salomon26102cb2018-03-09 09:33:19 -0500853 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700854 }
855 }
856}
857
858
859DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500860 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700861 if (!gpu) {
862 return;
863 }
Eric Karl5c779752017-05-08 12:02:07 -0700864 if (gpu->caps()->avoidStencilBuffers()) {
865 return;
866 }
ericrkc4025182016-05-04 12:01:58 -0700867 static const uint32_t kOrigColor = 0xFFAABBCC;
868
Robert Phillips6be756b2018-01-16 15:07:54 -0500869 auto resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
870
ericrkc4025182016-05-04 12:01:58 -0700871 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
872 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500873 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500874 GrBackendTexture backendTex;
875 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700876
Brian Salomonbdecacf2018-02-02 20:32:49 -0500877 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500878 // Certain platforms don't support MSAA, skip these.
879 continue;
ericrkc4025182016-05-04 12:01:58 -0700880 }
881
882 // Validate that we can attach a stencil buffer to an SkSurface created by either of
883 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400884 GrRenderTarget* rt = surface->getCanvas()
885 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Robert Phillips6be756b2018-01-16 15:07:54 -0500886 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt));
Brian Salomon26102cb2018-03-09 09:33:19 -0500887 gpu->deleteTestingOnlyBackendTexture(backendTex);
ericrkc4025182016-05-04 12:01:58 -0700888 }
bsalomone63ffef2016-02-05 07:17:34 -0800889 }
890}
brianosman0e22eb82016-08-30 07:07:59 -0700891
892static void test_surface_creation_and_snapshot_with_color_space(
893 skiatest::Reporter* reporter,
894 const char* prefix,
Mike Klein37854712018-06-26 11:43:06 -0400895 bool supportsF16,
896 bool supportsF32,
Brian Osman10fc6fd2018-03-02 11:01:10 -0500897 bool supports1010102,
brianosman0e22eb82016-08-30 07:07:59 -0700898 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
899
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500900 auto srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500901 const SkMatrix44* srgbMatrix = srgbColorSpace->toXYZD50();
raftias94888332016-10-18 10:02:51 -0700902 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400903 SkColorSpaceTransferFn oddGamma;
904 oddGamma.fA = 1.0f;
905 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
906 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400907 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500908 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700909
910 const struct {
911 SkColorType fColorType;
912 sk_sp<SkColorSpace> fColorSpace;
913 bool fShouldWork;
914 const char* fDescription;
915 } testConfigs[] = {
Mike Klein37854712018-06-26 11:43:06 -0400916 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
917 { kN32_SkColorType, linearColorSpace, true, "N32-linear" },
918 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
919 { kN32_SkColorType, oddColorSpace, true, "N32-odd" },
920 { kRGBA_F16_SkColorType, nullptr, supportsF16, "F16-nullptr" },
921 { kRGBA_F16_SkColorType, linearColorSpace, supportsF16, "F16-linear" },
922 { kRGBA_F16_SkColorType, srgbColorSpace, supportsF16, "F16-srgb" },
923 { kRGBA_F16_SkColorType, oddColorSpace, supportsF16, "F16-odd" },
924 { kRGBA_F32_SkColorType, nullptr, supportsF32, "F32-nullptr" },
925 { kRGBA_F32_SkColorType, linearColorSpace, supportsF32, "F32-linear" },
926 { kRGBA_F32_SkColorType, srgbColorSpace, supportsF32, "F32-srgb" },
927 { kRGBA_F32_SkColorType, oddColorSpace, supportsF32, "F32-odd" },
928 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
929 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
930 { kRGBA_1010102_SkColorType, nullptr, supports1010102, "1010102-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700931 };
932
933 for (auto& testConfig : testConfigs) {
934 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
935 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
936 testConfig.fColorSpace);
937
brianosman0e22eb82016-08-30 07:07:59 -0700938 auto surface(surfaceMaker(info));
Mike Klein37854712018-06-26 11:43:06 -0400939 REPORTER_ASSERT(reporter,
940 SkToBool(surface) == testConfig.fShouldWork, fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700941
Mike Klein37854712018-06-26 11:43:06 -0400942 if (testConfig.fShouldWork && surface) {
brianosman0e22eb82016-08-30 07:07:59 -0700943 sk_sp<SkImage> image(surface->makeImageSnapshot());
Brian Salomon1c80e992018-01-29 09:50:47 -0500944 REPORTER_ASSERT(reporter, image, testConfig.fDescription);
brianosman0e22eb82016-08-30 07:07:59 -0700945 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
Brian Salomon1c80e992018-01-29 09:50:47 -0500946 REPORTER_ASSERT(reporter, imageColorSpace == testConfig.fColorSpace.get(),
947 fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700948 }
949 }
950}
951
952DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
953 auto surfaceMaker = [](const SkImageInfo& info) {
954 return SkSurface::MakeRaster(info);
955 };
956
Mike Klein37854712018-06-26 11:43:06 -0400957 test_surface_creation_and_snapshot_with_color_space(reporter, "raster",
958 true, true, true,
Brian Osman10fc6fd2018-03-02 11:01:10 -0500959 surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -0700960}
961
brianosman0e22eb82016-08-30 07:07:59 -0700962DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400963 auto context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500964
Mike Klein37854712018-06-26 11:43:06 -0400965 bool supportsF16 = context->contextPriv().caps()->isConfigRenderable(kRGBA_half_GrPixelConfig),
966 supportsF32 = context->contextPriv().caps()->isConfigRenderable(kRGBA_float_GrPixelConfig),
967 supports1010102 = context->contextPriv().caps()->isConfigRenderable(kRGBA_1010102_GrPixelConfig);
968
brianosman0e22eb82016-08-30 07:07:59 -0700969 auto surfaceMaker = [context](const SkImageInfo& info) {
970 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
971 };
972
Mike Klein37854712018-06-26 11:43:06 -0400973 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu",
974 supportsF16, supportsF32, supports1010102,
975 surfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -0700976
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500977 std::vector<GrBackendTexture> backendTextures;
978 auto wrappedSurfaceMaker = [ context, &backendTextures ](const SkImageInfo& info) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500979 GrGpu* gpu = context->contextPriv().getGpu();
980
Greg Daniel7ef28f32017-04-20 16:41:55 +0000981 static const int kSize = 10;
Brian Osman2b23c4b2018-06-01 12:25:08 -0400982 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500983 SkASSERT(kUnknown_GrPixelConfig != config);
brianosman0e22eb82016-08-30 07:07:59 -0700984
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500985 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500986 nullptr, kSize, kSize, config, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000987
Greg Daniel5366e592018-01-10 09:57:53 -0500988 if (!backendTex.isValid() ||
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500989 !gpu->isTestingOnlyBackendTexture(backendTex)) {
brianosman0e22eb82016-08-30 07:07:59 -0700990 return sk_sp<SkSurface>(nullptr);
991 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500992 backendTextures.push_back(backendTex);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000993
994 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -0400995 kTopLeft_GrSurfaceOrigin, 0,
Greg Danielfaa095e2017-12-19 13:15:02 -0500996 info.colorType(),
Greg Daniel7ef28f32017-04-20 16:41:55 +0000997 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -0700998 };
999
Mike Klein37854712018-06-26 11:43:06 -04001000 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped",
1001 supportsF16, supportsF32, supports1010102,
1002 wrappedSurfaceMaker);
brianosman0e22eb82016-08-30 07:07:59 -07001003
Robert Phillips6cdc22c2017-05-11 16:29:14 -04001004 context->flush();
1005
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001006 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips2fbbd032018-04-10 10:26:44 -04001007 gpu->testingOnly_flushGpuAndSync();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001008 for (auto backendTex : backendTextures) {
Brian Salomon26102cb2018-03-09 09:33:19 -05001009 gpu->deleteTestingOnlyBackendTexture(backendTex);
brianosman0e22eb82016-08-30 07:07:59 -07001010 }
1011}
Matt Sarett22886c42016-11-22 11:31:41 -05001012
1013static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001014 SkOverdrawCanvas canvas(surface->getCanvas());
1015 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001016 sk_sp<SkImage> image = surface->makeImageSnapshot();
1017
1018 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001019 image->asLegacyBitmap(&bitmap);
Matt Sarett22886c42016-11-22 11:31:41 -05001020 for (int y = 0; y < 10; y++) {
1021 for (int x = 0; x < 10; x++) {
1022 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1023 }
1024 }
1025}
1026
1027DEF_TEST(OverdrawSurface_Raster, r) {
1028 sk_sp<SkSurface> surface = create_surface();
1029 test_overdraw_surface(r, surface.get());
1030}
1031
Matt Sarett22886c42016-11-22 11:31:41 -05001032DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1033 GrContext* context = ctxInfo.grContext();
1034 sk_sp<SkSurface> surface = create_gpu_surface(context);
1035 test_overdraw_surface(r, surface.get());
1036}
Mike Reed44d04bd2017-06-28 19:57:21 -04001037
1038DEF_TEST(Surface_null, r) {
1039 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1040
1041 const int w = 37;
1042 const int h = 1000;
1043 auto surf = SkSurface::MakeNull(w, h);
1044 auto canvas = surf->getCanvas();
1045
1046 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1047 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1048}
Mike Reedd4746982018-02-07 16:05:29 -05001049
1050// assert: if a given imageinfo is valid for a surface, then it must be valid for an image
1051// (so the snapshot can succeed)
1052DEF_TEST(surface_image_unity, reporter) {
1053 auto do_test = [reporter](const SkImageInfo& info) {
1054 size_t rowBytes = info.minRowBytes();
1055 auto surf = SkSurface::MakeRaster(info, rowBytes, nullptr);
1056 if (surf) {
1057 auto img = surf->makeImageSnapshot();
1058 if (!img && false) { // change to true to document the differences
1059 SkDebugf("image failed: [%08X %08X] %14s %s\n",
1060 info.width(), info.height(),
1061 sk_tool_utils::colortype_name(info.colorType()),
1062 sk_tool_utils::alphatype_name(info.alphaType()));
1063 return;
1064 }
1065 REPORTER_ASSERT(reporter, img != nullptr);
1066
1067 char dummyPixel = 0; // just need a valid address (not a valid size)
1068 SkPixmap pmap = { info, &dummyPixel, rowBytes };
1069 img = SkImage::MakeFromRaster(pmap, nullptr, nullptr);
1070 REPORTER_ASSERT(reporter, img != nullptr);
1071 }
1072 };
1073
Mike Klein955b3d52018-02-20 11:55:48 -05001074 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 -05001075 for (int cti = 0; cti <= kLastEnum_SkColorType; ++cti) {
Mike Reedd4746982018-02-07 16:05:29 -05001076 SkColorType ct = static_cast<SkColorType>(cti);
Mike Klein30dc8f92018-02-16 10:08:10 -05001077 for (int ati = 0; ati <= kLastEnum_SkAlphaType; ++ati) {
Mike Reedd4746982018-02-07 16:05:29 -05001078 SkAlphaType at = static_cast<SkAlphaType>(ati);
1079 for (int32_t size : sizes) {
Mike Klein955b3d52018-02-20 11:55:48 -05001080 // Large allocations tend to make the 32-bit bots run out of virtual address space.
1081 if (sizeof(size_t) == 4 && size > (1<<20)) {
1082 continue;
1083 }
Mike Reedd4746982018-02-07 16:05:29 -05001084 do_test(SkImageInfo::Make(1, size, ct, at));
1085 do_test(SkImageInfo::Make(size, 1, ct, at));
1086 }
1087 }
1088 }
1089}