blob: de1cd9a98458ed2bedb4308576c48e880404c43a [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>
junov@chromium.org995beb62013-03-28 13:49:22 +00009#include "SkCanvas.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000010#include "SkData.h"
Mike Reed986480a2017-01-13 22:43:16 +000011#include "SkDevice.h"
bsalomon55812362015-06-10 08:49:28 -070012#include "SkImage_Base.h"
Matt Sarette11b6142016-11-28 18:28:07 -050013#include "SkOverdrawCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
Mike Reed267be7f2017-02-13 09:32:54 -050015#include "SkRegion.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000016#include "SkRRect.h"
17#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000018#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000019#include "Test.h"
20
21#if SK_SUPPORT_GPU
Brian Salomonbdecacf2018-02-02 20:32:49 -050022#include <vector>
kkinnunen179a8f52015-11-20 13:32:24 -080023#include "GrContext.h"
Robert Phillips2c862492017-01-18 10:08:39 -050024#include "GrContextPriv.h"
Brian Salomon3a2cc2c2018-02-03 00:25:12 +000025#include "GrGpu.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050026#include "GrGpuResourcePriv.h"
27#include "GrRenderTargetContext.h"
ericrkc4025182016-05-04 12:01:58 -070028#include "GrResourceProvider.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000029#include "GrTest.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050030#include "SkGpuDevice.h"
31#include "SkImage_Gpu.h"
32#include "SkSurface_Gpu.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000033#endif
34
kkinnunen179a8f52015-11-20 13:32:24 -080035#include <initializer_list>
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}
61#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070062static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
63 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080064 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
65 if (requestedInfo) {
66 *requestedInfo = info;
67 }
robertphillips7e922762016-07-26 11:38:17 -070068 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080069}
reede8f30622016-03-23 18:59:25 -070070static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
71 SkAlphaType at = kPremul_SkAlphaType,
72 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080073 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
74 if (requestedInfo) {
75 *requestedInfo = info;
76 }
robertphillips7e922762016-07-26 11:38:17 -070077 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080078}
79#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000080
kkinnunen179a8f52015-11-20 13:32:24 -080081DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080082 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070083 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
84 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080085
reedb2497c22014-12-31 12:31:43 -080086}
kkinnunen179a8f52015-11-20 13:32:24 -080087#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070088DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080089 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
90 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070091 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080092}
93#endif
reedb2497c22014-12-31 12:31:43 -080094
Brian Salomonbdecacf2018-02-02 20:32:49 -050095#if SK_SUPPORT_GPU
96DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsSurface, reporter, ctxInfo) {
97 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
98 static constexpr int kSize = 10;
99
100 SkColorType colorType = static_cast<SkColorType>(ct);
101 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
102 bool can = ctxInfo.grContext()->colorTypeSupportedAsSurface(colorType);
103 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, 1,
104 nullptr);
105 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
106 colorType, can, SkToBool(surf));
107
108 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
109 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
110 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
111 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
112 kTopLeft_GrSurfaceOrigin, 0, colorType, nullptr,
113 nullptr);
114 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
115 colorType, can, SkToBool(surf));
116
117 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
118 kTopLeft_GrSurfaceOrigin, 1,
119 colorType, nullptr, nullptr);
120 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
121 colorType, can, SkToBool(surf));
122
123 surf.reset();
124 ctxInfo.grContext()->flush();
125 if (backendTex.isValid()) {
126 gpu->deleteTestingOnlyBackendTexture(&backendTex);
127 }
128
129 static constexpr int kSampleCnt = 2;
130
131 can = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType) >= kSampleCnt;
132 surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, info, kSampleCnt,
133 nullptr);
134 REPORTER_ASSERT(reporter, can == SkToBool(surf), "ct: %d, can: %d, surf: %d",
135 colorType, can, SkToBool(surf));
136
137 backendTex = gpu->createTestingOnlyBackendTexture(nullptr, kSize, kSize, colorType, true,
138 GrMipMapped::kNo);
139 surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
140 kTopLeft_GrSurfaceOrigin, kSampleCnt, colorType,
141 nullptr, nullptr);
142 REPORTER_ASSERT(reporter, can == SkToBool(surf),
143 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
144 colorType);
145 // Ensure that the sample count stored on the resulting SkSurface is a valid value.
146 if (surf) {
147 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
148 int storedCnt = rtc->numStencilSamples();
149 int allowedCnt = ctxInfo.grContext()->caps()->getSampleCount(
150 storedCnt, rtc->asSurfaceProxy()->config());
151 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
152 "Should store an allowed sample count (%d vs %d)", allowedCnt,
153 storedCnt);
154 }
155
156 surf = SkSurface::MakeFromBackendTextureAsRenderTarget(ctxInfo.grContext(), backendTex,
157 kTopLeft_GrSurfaceOrigin, kSampleCnt,
158 colorType, nullptr, nullptr);
159 REPORTER_ASSERT(reporter, can == SkToBool(surf),
160 "colorTypeSupportedAsSurface:%d, surf:%d, ct:%d", can, SkToBool(surf),
161 colorType);
162 if (surf) {
163 auto* rtc = ((SkSurface_Gpu*)(surf.get()))->getDevice()->accessRenderTargetContext();
164 int storedCnt = rtc->numStencilSamples();
165 int allowedCnt = ctxInfo.grContext()->caps()->getSampleCount(
166 storedCnt, rtc->asSurfaceProxy()->config());
167 REPORTER_ASSERT(reporter, storedCnt == allowedCnt,
168 "Should store an allowed sample count (%d vs %d)", allowedCnt,
169 storedCnt);
170 }
171
172 surf.reset();
173 ctxInfo.grContext()->flush();
174 if (backendTex.isValid()) {
175 gpu->deleteTestingOnlyBackendTexture(&backendTex);
176 }
177 }
178}
179
180DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_maxSurfaceSamplesForColorType, reporter, ctxInfo) {
181 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
182 static constexpr int kSize = 10;
183
184 SkColorType colorType = static_cast<SkColorType>(ct);
185 int max = ctxInfo.grContext()->maxSurfaceSampleCountForColorType(colorType);
186 if (!max) {
187 continue;
188 }
189 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
190 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
191 nullptr, kSize, kSize, colorType, true, GrMipMapped::kNo);
192
193 auto info = SkImageInfo::Make(kSize, kSize, colorType, kOpaque_SkAlphaType, nullptr);
194 auto surf = SkSurface::MakeFromBackendTexture(ctxInfo.grContext(), backendTex,
195 kTopLeft_GrSurfaceOrigin, max,
196 colorType, nullptr, nullptr);
197 REPORTER_ASSERT(reporter, surf);
198 if (!surf) {
199 continue;
200 }
201 int sampleCnt = ((SkSurface_Gpu*)(surf.get()))
202 ->getDevice()
203 ->accessRenderTargetContext()
204 ->numStencilSamples();
205 REPORTER_ASSERT(reporter, sampleCnt == max, "Exected: %d, actual: %d", max, sampleCnt);
206 }
207}
208#endif
209
kkinnunen179a8f52015-11-20 13:32:24 -0800210static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -0700211 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -0800212 const SkImageInfo& requestInfo,
213 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000214 const SkColor color = SK_ColorRED;
215 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -0800216 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000217
reed6ceeebd2016-03-09 14:26:26 -0800218 SkPixmap pmap;
219 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800220 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000221
reed6ceeebd2016-03-09 14:26:26 -0800222 SkPixmap pmap2;
223 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000224
kkinnunen179a8f52015-11-20 13:32:24 -0800225 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800226 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
227 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
228 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000229
reed6ceeebd2016-03-09 14:26:26 -0800230 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
231 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
232 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800233 } else {
234 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000235 }
236}
kkinnunen179a8f52015-11-20 13:32:24 -0800237DEF_TEST(SurfaceCanvasPeek, reporter) {
238 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
239 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700240 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800241 test_canvas_peek(reporter, surface, requestInfo, true);
242 }
243}
244#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700245DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800246 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
247 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700248 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800249 test_canvas_peek(reporter, surface, requestInfo, false);
250 }
251}
252#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000253
reede8f30622016-03-23 18:59:25 -0700254static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700255 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800256 REPORTER_ASSERT(reporter, surface);
257 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700258 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800259 REPORTER_ASSERT(reporter, image);
260 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700261 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700262 }
263 }
264}
kkinnunen179a8f52015-11-20 13:32:24 -0800265DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
266 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700267 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
268 auto surface(surface_func(at, nullptr));
269 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700270 }
271 }
272}
kkinnunen179a8f52015-11-20 13:32:24 -0800273#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700274DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800275 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700276 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
277 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
278 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
279 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800280 }
281 }
282}
283#endif
bsalomon74f681d2015-06-23 14:38:48 -0700284
kkinnunen179a8f52015-11-20 13:32:24 -0800285static GrBackendObject get_surface_backend_texture_handle(
286 SkSurface* s, SkSurface::BackendHandleAccess a) {
287 return s->getTextureHandle(a);
288}
289static GrBackendObject get_surface_backend_render_target_handle(
290 SkSurface* s, SkSurface::BackendHandleAccess a) {
291 GrBackendObject result;
292 if (!s->getRenderTargetHandle(&result, a)) {
293 return 0;
294 }
295 return result;
296}
297
298static void test_backend_handle_access_copy_on_write(
299 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
300 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700301 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700302 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700303
304 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700305 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700306
307 // If the access mode triggers CoW, then the backend objects should reflect it.
308 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
309}
kkinnunen179a8f52015-11-20 13:32:24 -0800310DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
311 const SkSurface::BackendHandleAccess accessModes[] = {
312 SkSurface::kFlushRead_BackendHandleAccess,
313 SkSurface::kFlushWrite_BackendHandleAccess,
314 SkSurface::kDiscardWrite_BackendHandleAccess,
315 };
316 for (auto& handle_access_func :
317 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
318 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700319 auto surface(create_surface());
320 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800321 handle_access_func);
322 }
323 }
324}
325#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700326DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800327 const SkSurface::BackendHandleAccess accessModes[] = {
328 SkSurface::kFlushRead_BackendHandleAccess,
329 SkSurface::kFlushWrite_BackendHandleAccess,
330 SkSurface::kDiscardWrite_BackendHandleAccess,
331 };
332 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
333 for (auto& handle_access_func :
334 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
335 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700336 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700337 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800338 handle_access_func);
339 }
340 }
341 }
342}
343#endif
fmalitae2639082015-08-06 07:04:51 -0700344
kkinnunen179a8f52015-11-20 13:32:24 -0800345#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800346
kkinnunen179a8f52015-11-20 13:32:24 -0800347static void test_backend_handle_unique_id(
348 skiatest::Reporter* reporter, SkSurface* surface,
349 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800351 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
352 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700353 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800354 // just read access should not affect the snapshot
355 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
356
357 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
358 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700359 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800360 // expect a new image, since we claimed we would write
361 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
362
363 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
364 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700365 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800366 // expect a new(er) image, since we claimed we would write
367 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
368 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
369}
370// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700371DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800372 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700373 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800374 for (auto& handle_access_func :
375 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700376 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700377 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800378 }
379 }
380 }
381}
382#endif
383
384// Verify that the right canvas commands trigger a copy on write.
385static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000386 SkCanvas* canvas = surface->getCanvas();
387
388 const SkRect testRect =
389 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
390 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000391 SkPath testPath;
392 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
393 SkIntToScalar(2), SkIntToScalar(1)));
394
395 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
396
397 SkRegion testRegion;
398 testRegion.setRect(testIRect);
399
400
401 const SkColor testColor = 0x01020304;
402 const SkPaint testPaint;
403 const SkPoint testPoints[3] = {
404 {SkIntToScalar(0), SkIntToScalar(0)},
405 {SkIntToScalar(2), SkIntToScalar(1)},
406 {SkIntToScalar(0), SkIntToScalar(2)}
407 };
408 const size_t testPointCount = 3;
409
410 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000411 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000412 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000413
414 SkRRect testRRect;
415 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
416
417 SkString testText("Hello World");
418 const SkPoint testPoints2[] = {
419 { SkIntToScalar(0), SkIntToScalar(1) },
420 { SkIntToScalar(1), SkIntToScalar(1) },
421 { SkIntToScalar(2), SkIntToScalar(1) },
422 { SkIntToScalar(3), SkIntToScalar(1) },
423 { SkIntToScalar(4), SkIntToScalar(1) },
424 { SkIntToScalar(5), SkIntToScalar(1) },
425 { SkIntToScalar(6), SkIntToScalar(1) },
426 { SkIntToScalar(7), SkIntToScalar(1) },
427 { SkIntToScalar(8), SkIntToScalar(1) },
428 { SkIntToScalar(9), SkIntToScalar(1) },
429 { SkIntToScalar(10), SkIntToScalar(1) },
430 };
431
432#define EXPECT_COPY_ON_WRITE(command) \
433 { \
reed9ce9d672016-03-17 10:51:11 -0700434 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
435 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000436 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700437 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
438 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000439 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
440 }
441
442 EXPECT_COPY_ON_WRITE(clear(testColor))
443 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
444 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
445 testPaint))
446 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
447 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
448 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
449 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
450 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700451 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700452 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Cary Clark2a475ea2017-04-28 15:35:12 -0400453 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, testPaint))
junov@chromium.org995beb62013-03-28 13:49:22 +0000454 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
455 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700456 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000457 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800458}
459DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700460 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800461}
462#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700463DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800464 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700465 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700466 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700467 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000468}
kkinnunen179a8f52015-11-20 13:32:24 -0800469#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000470
kkinnunen179a8f52015-11-20 13:32:24 -0800471static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
472 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000473 // This test succeeds by not triggering an assertion.
474 // The test verifies that the surface remains writable (usable) after
475 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000476 SkCanvas* canvas = surface->getCanvas();
477 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700478 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000479 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000480}
kkinnunen179a8f52015-11-20 13:32:24 -0800481DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700482 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800483}
484#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700485DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800486 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700487 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700488 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800489 }
490}
491#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000492
junov@chromium.orgb516a412013-05-01 22:49:59 +0000493#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800494static void test_crbug263329(skiatest::Reporter* reporter,
495 SkSurface* surface1,
496 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000497 // This is a regression test for crbug.com/263329
498 // Bug was caused by onCopyOnWrite releasing the old surface texture
499 // back to the scratch texture pool even though the texture is used
500 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000501 SkCanvas* canvas1 = surface1->getCanvas();
502 SkCanvas* canvas2 = surface2->getCanvas();
503 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700504 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000505 // Trigger copy on write, new backing is a scratch texture
506 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700507 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000508 // Trigger copy on write, old backing should not be returned to scratch
509 // pool because it is held by image2
510 canvas1->clear(3);
511
512 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700513 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000514 // Trigger copy on write on surface2. The new backing store should not
515 // be recycling a texture that is held by an existing image.
516 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700517 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400518 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000519 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400520 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
521 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
522 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
523 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
524 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000525}
egdanielab527a52016-06-28 08:07:26 -0700526DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800527 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700528 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
529 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700530 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800531 }
532}
533#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000534
kkinnunen179a8f52015-11-20 13:32:24 -0800535DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700536 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700537 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500538 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800539 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500540 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800541}
542#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700543DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800544 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700545 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700546 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500547
548 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
549 GrBackendObject textureHandle = image->getTextureHandle(false);
550 REPORTER_ASSERT(reporter, 0 != textureHandle);
kkinnunen179a8f52015-11-20 13:32:24 -0800551 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500552 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
553 REPORTER_ASSERT(reporter, textureHandle == image->getTextureHandle(false));
junov@chromium.orgda904742013-05-01 22:38:16 +0000554 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000555}
kkinnunen179a8f52015-11-20 13:32:24 -0800556#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800557
kkinnunen179a8f52015-11-20 13:32:24 -0800558#if SK_SUPPORT_GPU
bsalomoneaaaf0b2015-01-23 08:08:04 -0800559
reede8f30622016-03-23 18:59:25 -0700560static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
561 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500562
563 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
564 ->asRenderTargetProxy();
565 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800566}
567
bsalomon5ec26ae2016-02-25 08:33:02 -0800568static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400569 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800570}
571
reed9ce9d672016-03-17 10:51:11 -0700572static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
573 return is_budgeted(image.get());
574}
575
egdanielab527a52016-06-28 08:07:26 -0700576DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800577 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400578 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
579 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
580 SkASSERT(surface);
581 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800582
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400583 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800584
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400585 // Initially the image shares a texture with the surface, and the
586 // the budgets should always match.
587 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
588 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800589
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400590 // Now trigger copy-on-write
591 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800592
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400593 // They don't share a texture anymore but the budgets should still match.
594 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
595 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800596 }
597}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000598#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000599
kkinnunen179a8f52015-11-20 13:32:24 -0800600static void test_no_canvas1(skiatest::Reporter* reporter,
601 SkSurface* surface,
602 SkSurface::ContentChangeMode mode) {
603 // Test passes by not asserting
604 surface->notifyContentWillChange(mode);
605 SkDEBUGCODE(surface->validate();)
606}
607static void test_no_canvas2(skiatest::Reporter* reporter,
608 SkSurface* surface,
609 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000610 // Verifies the robustness of SkSurface for handling use cases where calls
611 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700612 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
613 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800614 SkDEBUGCODE(image1->validate();)
615 SkDEBUGCODE(surface->validate();)
616 surface->notifyContentWillChange(mode);
617 SkDEBUGCODE(image1->validate();)
618 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700619 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
620 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800621 SkDEBUGCODE(image2->validate();)
622 SkDEBUGCODE(surface->validate();)
623 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000624}
kkinnunen179a8f52015-11-20 13:32:24 -0800625DEF_TEST(SurfaceNoCanvas, reporter) {
626 SkSurface::ContentChangeMode modes[] =
627 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
628 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
629 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700630 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800631 }
632 }
633}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000634#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700635DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800636 SkSurface::ContentChangeMode modes[] =
637 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
638 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
639 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
640 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700641 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700642 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700643 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000644 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000645 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000646}
kkinnunen179a8f52015-11-20 13:32:24 -0800647#endif
reed9cd016e2016-01-30 10:01:06 -0800648
649static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800650 SkPixmap surfacePM;
651 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800652
reed9ce9d672016-03-17 10:51:11 -0700653 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800654 SkPixmap pm;
655 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800656
reed6ceeebd2016-03-09 14:26:26 -0800657 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800658
659 // trigger a copy-on-write
660 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700661 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800662 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
663
reed6ceeebd2016-03-09 14:26:26 -0800664 SkPixmap pm2;
665 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
666 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800667}
668
669DEF_TEST(surface_rowbytes, reporter) {
670 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
671
reede8f30622016-03-23 18:59:25 -0700672 auto surf0(SkSurface::MakeRaster(info));
673 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800674
675 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700676 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
677 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800678
679 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700680 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800681 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400682 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800683 REPORTER_ASSERT(reporter, nullptr == s);
684}
bsalomone63ffef2016-02-05 07:17:34 -0800685
fmalita03912f12016-07-06 06:22:06 -0700686DEF_TEST(surface_raster_zeroinitialized, reporter) {
687 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
688 SkPixmap pixmap;
689 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
690
691 for (int i = 0; i < pixmap.info().width(); ++i) {
692 for (int j = 0; j < pixmap.info().height(); ++j) {
693 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
694 }
695 }
696}
697
bsalomone63ffef2016-02-05 07:17:34 -0800698#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700699static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500700 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500701 GrGpu* gpu = context->contextPriv().getGpu();
702
ericrkc4025182016-05-04 12:01:58 -0700703 const int kWidth = 10;
704 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400705 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700706 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000707
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500708 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500709 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000710
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500711 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500712 return nullptr;
713 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000714
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500715 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400716 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500717 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000718 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700719 if (!surface) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500720 gpu->deleteTestingOnlyBackendTexture(outTexture);
ericrkc4025182016-05-04 12:01:58 -0700721 return nullptr;
722 }
ericrkc4025182016-05-04 12:01:58 -0700723 return surface;
724}
bsalomone63ffef2016-02-05 07:17:34 -0800725
ericrkc4025182016-05-04 12:01:58 -0700726static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500727 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500728 GrGpu* gpu = context->contextPriv().getGpu();
729
ericrkc4025182016-05-04 12:01:58 -0700730 const int kWidth = 10;
731 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400732 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700733 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000734
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500735 *outTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500736 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000737
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500738 if (!outTexture->isValid() || !gpu->isTestingOnlyBackendTexture(*outTexture)) {
ericrkc4025182016-05-04 12:01:58 -0700739 return nullptr;
740 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500741
742 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Greg Danielfaa095e2017-12-19 13:15:02 -0500743 context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
744 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500745
746 if (!surface) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500747 gpu->deleteTestingOnlyBackendTexture(outTexture);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500748 return nullptr;
749 }
ericrkc4025182016-05-04 12:01:58 -0700750 return surface;
751}
752
753static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500754 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700755 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800756 if (!surface) {
757 ERRORF(reporter, "Could not create GPU SkSurface.");
758 return;
759 }
760 int w = surface->width();
761 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400762 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700763 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800764
Robert Phillips2c862492017-01-18 10:08:39 -0500765 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
766 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800767 ERRORF(reporter, "Could access render target of GPU SkSurface.");
768 return;
769 }
bsalomon2fba8092016-02-05 13:47:06 -0800770 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500771
772 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
773 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800774 for (int y = 0; y < h; ++y) {
775 for (int x = 0; x < w; ++x) {
776 uint32_t pixel = pixels.get()[y * w + x];
777 if (pixel != expectedValue) {
778 SkString msg;
779 if (expectedValue) {
780 msg = "SkSurface should have left render target unmodified";
781 } else {
782 msg = "SkSurface should have cleared the render target";
783 }
784 ERRORF(reporter,
785 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
786 expectedValue, x, y);
787 return;
788 }
789 }
790 }
791}
792
bsalomon758586c2016-04-06 14:02:39 -0700793DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700794 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500795 GrGpu* gpu = context->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700796
Robert Phillips2c862492017-01-18 10:08:39 -0500797 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700798 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500799 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
800 },
801 [] (SkSurface* s){
802 sk_sp<SkImage> i(s->makeImageSnapshot());
803 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500804 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500805 GrContext* context = gpuImage->context();
806 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
807 gpuImage->refColorSpace());
808 }
bsalomone63ffef2016-02-05 07:17:34 -0800809 };
ericrkc4025182016-05-04 12:01:58 -0700810
Robert Phillips2c862492017-01-18 10:08:39 -0500811 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700812 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800813 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700814 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800815 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
816 }
817 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700818 const uint32_t kOrigColor = 0xABABABAB;
819 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
820 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500821 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500822 auto surface = surfaceFunc(context, 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700823 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
824 surface.reset();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500825 gpu->deleteTestingOnlyBackendTexture(&backendTex);
ericrkc4025182016-05-04 12:01:58 -0700826 }
827 }
828}
bsalomone63ffef2016-02-05 07:17:34 -0800829
ericrkc4025182016-05-04 12:01:58 -0700830static void test_surface_draw_partially(
831 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
832 const int kW = surface->width();
833 const int kH = surface->height();
834 SkPaint paint;
835 const SkColor kRectColor = ~origColor | 0xFF000000;
836 paint.setColor(kRectColor);
837 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
838 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400839 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700840 sk_memset32(pixels.get(), ~origColor, kW * kH);
841 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
842 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
843 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
844 bool stop = false;
845 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
846 (origColor >> 0 & 0xFF),
847 (origColor >> 8 & 0xFF),
848 (origColor >> 16 & 0xFF));
849 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
850 (kRectColor >> 16 & 0xFF),
851 (kRectColor >> 8 & 0xFF),
852 (kRectColor >> 0 & 0xFF));
853 for (int y = 0; y < kH/2 && !stop; ++y) {
854 for (int x = 0; x < kW && !stop; ++x) {
855 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
856 if (rectColorPM != pixels[x + y * kW]) {
857 stop = true;
858 }
859 }
860 }
861 stop = false;
862 for (int y = kH/2; y < kH && !stop; ++y) {
863 for (int x = 0; x < kW && !stop; ++x) {
864 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
865 if (origColorPM != pixels[x + y * kW]) {
866 stop = true;
867 }
868 }
869 }
870}
bsalomone63ffef2016-02-05 07:17:34 -0800871
egdanielab527a52016-06-28 08:07:26 -0700872DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500873 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700874 if (!gpu) {
875 return;
876 }
877 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800878
ericrkc4025182016-05-04 12:01:58 -0700879 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
880 &create_gpu_surface_backend_texture_as_render_target}) {
881 // Validate that we can draw to the canvas and that the original texture color is
882 // preserved in pixels that aren't rendered to via the surface.
883 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500884 GrBackendTexture backendTex;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500885 auto surface = surfaceFunc(ctxInfo.grContext(), 1, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700886 if (surface) {
887 test_surface_draw_partially(reporter, surface, kOrigColor);
888 surface.reset();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500889 gpu->deleteTestingOnlyBackendTexture(&backendTex);
ericrkc4025182016-05-04 12:01:58 -0700890 }
891 }
892}
893
894
895DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500896 GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
ericrkc4025182016-05-04 12:01:58 -0700897 if (!gpu) {
898 return;
899 }
Eric Karl5c779752017-05-08 12:02:07 -0700900 if (gpu->caps()->avoidStencilBuffers()) {
901 return;
902 }
ericrkc4025182016-05-04 12:01:58 -0700903 static const uint32_t kOrigColor = 0xFFAABBCC;
904
Robert Phillips6be756b2018-01-16 15:07:54 -0500905 auto resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
906
ericrkc4025182016-05-04 12:01:58 -0700907 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
908 &create_gpu_surface_backend_texture_as_render_target}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500909 for (int sampleCnt : {1, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500910 GrBackendTexture backendTex;
911 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700912
Brian Salomonbdecacf2018-02-02 20:32:49 -0500913 if (!surface && sampleCnt > 1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500914 // Certain platforms don't support MSAA, skip these.
915 continue;
ericrkc4025182016-05-04 12:01:58 -0700916 }
917
918 // Validate that we can attach a stencil buffer to an SkSurface created by either of
919 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400920 GrRenderTarget* rt = surface->getCanvas()
921 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
Robert Phillips6be756b2018-01-16 15:07:54 -0500922 REPORTER_ASSERT(reporter, resourceProvider->attachStencilAttachment(rt));
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500923 gpu->deleteTestingOnlyBackendTexture(&backendTex);
ericrkc4025182016-05-04 12:01:58 -0700924 }
bsalomone63ffef2016-02-05 07:17:34 -0800925 }
926}
927#endif
brianosman0e22eb82016-08-30 07:07:59 -0700928
929static void test_surface_creation_and_snapshot_with_color_space(
930 skiatest::Reporter* reporter,
931 const char* prefix,
932 bool f16Support,
933 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
934
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500935 auto srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500936 const SkMatrix44* srgbMatrix = srgbColorSpace->toXYZD50();
raftias94888332016-10-18 10:02:51 -0700937 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400938 SkColorSpaceTransferFn oddGamma;
939 oddGamma.fA = 1.0f;
940 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
941 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400942 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500943 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700944
945 const struct {
946 SkColorType fColorType;
947 sk_sp<SkColorSpace> fColorSpace;
948 bool fShouldWork;
949 const char* fDescription;
950 } testConfigs[] = {
951 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
952 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
953 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700954 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Stan Iliev4ed9ae42017-07-25 11:59:12 -0400955 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700956 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
957 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700958 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
959 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
960 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
961 };
962
963 for (auto& testConfig : testConfigs) {
964 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
965 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
966 testConfig.fColorSpace);
967
968 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
969 // any surface of that type:
970 bool shouldWork = testConfig.fShouldWork &&
971 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
972
973 auto surface(surfaceMaker(info));
Brian Salomon1c80e992018-01-29 09:50:47 -0500974 REPORTER_ASSERT(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700975
976 if (shouldWork && surface) {
977 sk_sp<SkImage> image(surface->makeImageSnapshot());
Brian Salomon1c80e992018-01-29 09:50:47 -0500978 REPORTER_ASSERT(reporter, image, testConfig.fDescription);
brianosman0e22eb82016-08-30 07:07:59 -0700979 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
Brian Salomon1c80e992018-01-29 09:50:47 -0500980 REPORTER_ASSERT(reporter, imageColorSpace == testConfig.fColorSpace.get(),
981 fullTestName.c_str());
brianosman0e22eb82016-08-30 07:07:59 -0700982 }
983 }
984}
985
986DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
987 auto surfaceMaker = [](const SkImageInfo& info) {
988 return SkSurface::MakeRaster(info);
989 };
990
991 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
992}
993
994#if SK_SUPPORT_GPU
995DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
996 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500997
Brian Salomonbdecacf2018-02-02 20:32:49 -0500998 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig);
brianosman0e22eb82016-08-30 07:07:59 -0700999 auto surfaceMaker = [context](const SkImageInfo& info) {
1000 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
1001 };
1002
1003 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
1004
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001005 std::vector<GrBackendTexture> backendTextures;
1006 auto wrappedSurfaceMaker = [ context, &backendTextures ](const SkImageInfo& info) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001007 GrGpu* gpu = context->contextPriv().getGpu();
1008
Greg Daniel7ef28f32017-04-20 16:41:55 +00001009 static const int kSize = 10;
1010 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *context->caps());
brianosman0e22eb82016-08-30 07:07:59 -07001011
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001012 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001013 nullptr, kSize, kSize, config, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001014
Greg Daniel5366e592018-01-10 09:57:53 -05001015 if (!backendTex.isValid() ||
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001016 !gpu->isTestingOnlyBackendTexture(backendTex)) {
brianosman0e22eb82016-08-30 07:07:59 -07001017 return sk_sp<SkSurface>(nullptr);
1018 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001019 backendTextures.push_back(backendTex);
Greg Daniel7ef28f32017-04-20 16:41:55 +00001020
1021 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -04001022 kTopLeft_GrSurfaceOrigin, 0,
Greg Danielfaa095e2017-12-19 13:15:02 -05001023 info.colorType(),
Greg Daniel7ef28f32017-04-20 16:41:55 +00001024 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -07001025 };
1026
1027 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
1028 wrappedSurfaceMaker);
1029
Robert Phillips6cdc22c2017-05-11 16:29:14 -04001030 context->flush();
1031
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001032 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001033 for (auto backendTex : backendTextures) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -05001034 gpu->deleteTestingOnlyBackendTexture(&backendTex);
brianosman0e22eb82016-08-30 07:07:59 -07001035 }
1036}
1037#endif
Matt Sarett22886c42016-11-22 11:31:41 -05001038
1039static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -05001040 SkOverdrawCanvas canvas(surface->getCanvas());
1041 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -05001042 sk_sp<SkImage> image = surface->makeImageSnapshot();
1043
1044 SkBitmap bitmap;
1045 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Matt Sarett22886c42016-11-22 11:31:41 -05001046 for (int y = 0; y < 10; y++) {
1047 for (int x = 0; x < 10; x++) {
1048 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
1049 }
1050 }
1051}
1052
1053DEF_TEST(OverdrawSurface_Raster, r) {
1054 sk_sp<SkSurface> surface = create_surface();
1055 test_overdraw_surface(r, surface.get());
1056}
1057
1058#if SK_SUPPORT_GPU
1059DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
1060 GrContext* context = ctxInfo.grContext();
1061 sk_sp<SkSurface> surface = create_gpu_surface(context);
1062 test_overdraw_surface(r, surface.get());
1063}
1064#endif
Mike Reed44d04bd2017-06-28 19:57:21 -04001065
1066DEF_TEST(Surface_null, r) {
1067 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
1068
1069 const int w = 37;
1070 const int h = 1000;
1071 auto surf = SkSurface::MakeNull(w, h);
1072 auto canvas = surf->getCanvas();
1073
1074 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
1075 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
1076}