blob: 0864e323801d8d9f15f12374ef3f0ad6f22d5c5a [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"
brianosman0e22eb82016-08-30 07:07:59 -070010#include "SkColorSpace_Base.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000011#include "SkData.h"
Mike Reed986480a2017-01-13 22:43:16 +000012#include "SkDevice.h"
bsalomon55812362015-06-10 08:49:28 -070013#include "SkImage_Base.h"
Matt Sarette11b6142016-11-28 18:28:07 -050014#include "SkOverdrawCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070015#include "SkPath.h"
Mike Reed267be7f2017-02-13 09:32:54 -050016#include "SkRegion.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000017#include "SkRRect.h"
18#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000019#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000020#include "Test.h"
21
22#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080023#include "GrContext.h"
Robert Phillips2c862492017-01-18 10:08:39 -050024#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040025#include "GrRenderTargetContext.h"
kkinnunen179a8f52015-11-20 13:32:24 -080026#include "GrGpu.h"
ericrkc4025182016-05-04 12:01:58 -070027#include "GrResourceProvider.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000028#include "GrTest.h"
brianosman0e22eb82016-08-30 07:07:59 -070029#include <vector>
junov@chromium.org995beb62013-03-28 13:49:22 +000030#endif
31
kkinnunen179a8f52015-11-20 13:32:24 -080032#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070033
kkinnunen179a8f52015-11-20 13:32:24 -080034static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070035 SkASSERT(pixels == context);
36 sk_free(pixels);
37}
reede8f30622016-03-23 18:59:25 -070038static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
39 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070040 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000041 if (requestedInfo) {
42 *requestedInfo = info;
43 }
reede8f30622016-03-23 18:59:25 -070044 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000045}
reede8f30622016-03-23 18:59:25 -070046static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
47 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080048 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
49 if (requestedInfo) {
50 *requestedInfo = info;
51 }
52 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -040053 void* storage = sk_malloc_throw(info.computeByteSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070054 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
55 release_direct_surface_storage,
56 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080057}
58#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070059static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
60 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080061 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
62 if (requestedInfo) {
63 *requestedInfo = info;
64 }
robertphillips7e922762016-07-26 11:38:17 -070065 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080066}
reede8f30622016-03-23 18:59:25 -070067static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
68 SkAlphaType at = kPremul_SkAlphaType,
69 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080070 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
71 if (requestedInfo) {
72 *requestedInfo = info;
73 }
robertphillips7e922762016-07-26 11:38:17 -070074 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080075}
76#endif
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}
kkinnunen179a8f52015-11-20 13:32:24 -080084#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070085DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080086 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
87 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070088 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080089}
90#endif
reedb2497c22014-12-31 12:31:43 -080091
kkinnunen179a8f52015-11-20 13:32:24 -080092static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070093 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080094 const SkImageInfo& requestInfo,
95 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000096 const SkColor color = SK_ColorRED;
97 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080098 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000099
reed6ceeebd2016-03-09 14:26:26 -0800100 SkPixmap pmap;
101 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800102 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000103
reed6ceeebd2016-03-09 14:26:26 -0800104 SkPixmap pmap2;
105 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000106
kkinnunen179a8f52015-11-20 13:32:24 -0800107 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800108 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
109 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
110 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000111
reed6ceeebd2016-03-09 14:26:26 -0800112 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
113 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
114 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800115 } else {
116 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000117 }
118}
kkinnunen179a8f52015-11-20 13:32:24 -0800119DEF_TEST(SurfaceCanvasPeek, reporter) {
120 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
121 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700122 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800123 test_canvas_peek(reporter, surface, requestInfo, true);
124 }
125}
126#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700127DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800128 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
129 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700130 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800131 test_canvas_peek(reporter, surface, requestInfo, false);
132 }
133}
134#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000135
reede8f30622016-03-23 18:59:25 -0700136static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700137 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800138 REPORTER_ASSERT(reporter, surface);
139 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700140 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800141 REPORTER_ASSERT(reporter, image);
142 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700143 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700144 }
145 }
146}
kkinnunen179a8f52015-11-20 13:32:24 -0800147DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
148 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700149 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
150 auto surface(surface_func(at, nullptr));
151 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700152 }
153 }
154}
kkinnunen179a8f52015-11-20 13:32:24 -0800155#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700156DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800157 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700158 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
159 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
160 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
161 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800162 }
163 }
164}
165#endif
bsalomon74f681d2015-06-23 14:38:48 -0700166
kkinnunen179a8f52015-11-20 13:32:24 -0800167static GrBackendObject get_surface_backend_texture_handle(
168 SkSurface* s, SkSurface::BackendHandleAccess a) {
169 return s->getTextureHandle(a);
170}
171static GrBackendObject get_surface_backend_render_target_handle(
172 SkSurface* s, SkSurface::BackendHandleAccess a) {
173 GrBackendObject result;
174 if (!s->getRenderTargetHandle(&result, a)) {
175 return 0;
176 }
177 return result;
178}
179
180static void test_backend_handle_access_copy_on_write(
181 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
182 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700183 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700184 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700185
186 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700187 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700188
189 // If the access mode triggers CoW, then the backend objects should reflect it.
190 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
191}
kkinnunen179a8f52015-11-20 13:32:24 -0800192DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
193 const SkSurface::BackendHandleAccess accessModes[] = {
194 SkSurface::kFlushRead_BackendHandleAccess,
195 SkSurface::kFlushWrite_BackendHandleAccess,
196 SkSurface::kDiscardWrite_BackendHandleAccess,
197 };
198 for (auto& handle_access_func :
199 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
200 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700201 auto surface(create_surface());
202 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800203 handle_access_func);
204 }
205 }
206}
207#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700208DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800209 const SkSurface::BackendHandleAccess accessModes[] = {
210 SkSurface::kFlushRead_BackendHandleAccess,
211 SkSurface::kFlushWrite_BackendHandleAccess,
212 SkSurface::kDiscardWrite_BackendHandleAccess,
213 };
214 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
215 for (auto& handle_access_func :
216 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
217 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700218 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700219 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800220 handle_access_func);
221 }
222 }
223 }
224}
225#endif
fmalitae2639082015-08-06 07:04:51 -0700226
kkinnunen179a8f52015-11-20 13:32:24 -0800227#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800228
kkinnunen179a8f52015-11-20 13:32:24 -0800229static void test_backend_handle_unique_id(
230 skiatest::Reporter* reporter, SkSurface* surface,
231 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700232 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800233 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
234 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700235 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800236 // just read access should not affect the snapshot
237 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
238
239 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
240 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700241 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800242 // expect a new image, since we claimed we would write
243 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
244
245 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
246 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700247 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800248 // expect a new(er) image, since we claimed we would write
249 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
250 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
251}
252// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700253DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800254 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700255 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800256 for (auto& handle_access_func :
257 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700258 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700259 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800260 }
261 }
262 }
263}
264#endif
265
266// Verify that the right canvas commands trigger a copy on write.
267static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000268 SkCanvas* canvas = surface->getCanvas();
269
270 const SkRect testRect =
271 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
272 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000273 SkPath testPath;
274 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
275 SkIntToScalar(2), SkIntToScalar(1)));
276
277 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
278
279 SkRegion testRegion;
280 testRegion.setRect(testIRect);
281
282
283 const SkColor testColor = 0x01020304;
284 const SkPaint testPaint;
285 const SkPoint testPoints[3] = {
286 {SkIntToScalar(0), SkIntToScalar(0)},
287 {SkIntToScalar(2), SkIntToScalar(1)},
288 {SkIntToScalar(0), SkIntToScalar(2)}
289 };
290 const size_t testPointCount = 3;
291
292 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000293 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000294 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000295
296 SkRRect testRRect;
297 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
298
299 SkString testText("Hello World");
300 const SkPoint testPoints2[] = {
301 { SkIntToScalar(0), SkIntToScalar(1) },
302 { SkIntToScalar(1), SkIntToScalar(1) },
303 { SkIntToScalar(2), SkIntToScalar(1) },
304 { SkIntToScalar(3), SkIntToScalar(1) },
305 { SkIntToScalar(4), SkIntToScalar(1) },
306 { SkIntToScalar(5), SkIntToScalar(1) },
307 { SkIntToScalar(6), SkIntToScalar(1) },
308 { SkIntToScalar(7), SkIntToScalar(1) },
309 { SkIntToScalar(8), SkIntToScalar(1) },
310 { SkIntToScalar(9), SkIntToScalar(1) },
311 { SkIntToScalar(10), SkIntToScalar(1) },
312 };
313
314#define EXPECT_COPY_ON_WRITE(command) \
315 { \
reed9ce9d672016-03-17 10:51:11 -0700316 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
317 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000318 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700319 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
320 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000321 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
322 }
323
324 EXPECT_COPY_ON_WRITE(clear(testColor))
325 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
326 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
327 testPaint))
328 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
329 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
330 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
331 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
332 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700333 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700334 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
Cary Clark2a475ea2017-04-28 15:35:12 -0400335 EXPECT_COPY_ON_WRITE(drawString(testText, 0, 1, testPaint))
junov@chromium.org995beb62013-03-28 13:49:22 +0000336 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
337 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700338 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000339 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800340}
341DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700342 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800343}
344#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700345DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800346 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700347 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700348 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700349 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000350}
kkinnunen179a8f52015-11-20 13:32:24 -0800351#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000352
kkinnunen179a8f52015-11-20 13:32:24 -0800353static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
354 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000355 // This test succeeds by not triggering an assertion.
356 // The test verifies that the surface remains writable (usable) after
357 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000358 SkCanvas* canvas = surface->getCanvas();
359 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700360 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000361 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000362}
kkinnunen179a8f52015-11-20 13:32:24 -0800363DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700364 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800365}
366#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700367DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800368 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700369 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700370 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800371 }
372}
373#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000374
junov@chromium.orgb516a412013-05-01 22:49:59 +0000375#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800376static void test_crbug263329(skiatest::Reporter* reporter,
377 SkSurface* surface1,
378 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000379 // This is a regression test for crbug.com/263329
380 // Bug was caused by onCopyOnWrite releasing the old surface texture
381 // back to the scratch texture pool even though the texture is used
382 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000383 SkCanvas* canvas1 = surface1->getCanvas();
384 SkCanvas* canvas2 = surface2->getCanvas();
385 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700386 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000387 // Trigger copy on write, new backing is a scratch texture
388 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700389 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000390 // Trigger copy on write, old backing should not be returned to scratch
391 // pool because it is held by image2
392 canvas1->clear(3);
393
394 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700395 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000396 // Trigger copy on write on surface2. The new backing store should not
397 // be recycling a texture that is held by an existing image.
398 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700399 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
Robert Phillips87444052017-06-23 14:09:30 -0400400 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000401 // The following assertion checks crbug.com/263329
Robert Phillips87444052017-06-23 14:09:30 -0400402 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
403 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
404 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
405 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
406 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000407}
egdanielab527a52016-06-28 08:07:26 -0700408DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800409 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700410 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
411 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700412 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800413 }
414}
415#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000416
kkinnunen179a8f52015-11-20 13:32:24 -0800417DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700418 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700419 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500420 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800421 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500422 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800423}
424#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700425DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800426 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700427 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700428 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500429
430 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
431 GrBackendObject textureHandle = image->getTextureHandle(false);
432 REPORTER_ASSERT(reporter, 0 != textureHandle);
kkinnunen179a8f52015-11-20 13:32:24 -0800433 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500434 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
435 REPORTER_ASSERT(reporter, textureHandle == image->getTextureHandle(false));
junov@chromium.orgda904742013-05-01 22:38:16 +0000436 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000437}
kkinnunen179a8f52015-11-20 13:32:24 -0800438#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800439
kkinnunen179a8f52015-11-20 13:32:24 -0800440#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800441#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800442#include "SkGpuDevice.h"
443#include "SkImage_Gpu.h"
444#include "SkSurface_Gpu.h"
445
reede8f30622016-03-23 18:59:25 -0700446static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
447 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500448
449 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
450 ->asRenderTargetProxy();
451 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800452}
453
bsalomon5ec26ae2016-02-25 08:33:02 -0800454static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400455 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800456}
457
reed9ce9d672016-03-17 10:51:11 -0700458static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
459 return is_budgeted(image.get());
460}
461
egdanielab527a52016-06-28 08:07:26 -0700462DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800463 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400464 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
465 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
466 SkASSERT(surface);
467 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800468
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400469 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800470
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400471 // Initially the image shares a texture with the surface, and the
472 // the budgets should always match.
473 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
474 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800475
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400476 // Now trigger copy-on-write
477 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800478
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400479 // They don't share a texture anymore but the budgets should still match.
480 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
481 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800482 }
483}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000484#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000485
kkinnunen179a8f52015-11-20 13:32:24 -0800486static void test_no_canvas1(skiatest::Reporter* reporter,
487 SkSurface* surface,
488 SkSurface::ContentChangeMode mode) {
489 // Test passes by not asserting
490 surface->notifyContentWillChange(mode);
491 SkDEBUGCODE(surface->validate();)
492}
493static void test_no_canvas2(skiatest::Reporter* reporter,
494 SkSurface* surface,
495 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000496 // Verifies the robustness of SkSurface for handling use cases where calls
497 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700498 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
499 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800500 SkDEBUGCODE(image1->validate();)
501 SkDEBUGCODE(surface->validate();)
502 surface->notifyContentWillChange(mode);
503 SkDEBUGCODE(image1->validate();)
504 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700505 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
506 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800507 SkDEBUGCODE(image2->validate();)
508 SkDEBUGCODE(surface->validate();)
509 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000510}
kkinnunen179a8f52015-11-20 13:32:24 -0800511DEF_TEST(SurfaceNoCanvas, reporter) {
512 SkSurface::ContentChangeMode modes[] =
513 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
514 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
515 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700516 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800517 }
518 }
519}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000520#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700521DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800522 SkSurface::ContentChangeMode modes[] =
523 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
524 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
525 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
526 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700527 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700528 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700529 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000530 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000531 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000532}
kkinnunen179a8f52015-11-20 13:32:24 -0800533#endif
reed9cd016e2016-01-30 10:01:06 -0800534
535static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800536 SkPixmap surfacePM;
537 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800538
reed9ce9d672016-03-17 10:51:11 -0700539 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800540 SkPixmap pm;
541 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800542
reed6ceeebd2016-03-09 14:26:26 -0800543 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800544
545 // trigger a copy-on-write
546 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700547 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800548 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
549
reed6ceeebd2016-03-09 14:26:26 -0800550 SkPixmap pm2;
551 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
552 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800553}
554
555DEF_TEST(surface_rowbytes, reporter) {
556 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
557
reede8f30622016-03-23 18:59:25 -0700558 auto surf0(SkSurface::MakeRaster(info));
559 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800560
561 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700562 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
563 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800564
565 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700566 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800567 REPORTER_ASSERT(reporter, nullptr == s);
Mike Reedf0ffb892017-10-03 14:47:21 -0400568 s = SkSurface::MakeRaster(info, std::numeric_limits<size_t>::max(), nullptr);
reed9cd016e2016-01-30 10:01:06 -0800569 REPORTER_ASSERT(reporter, nullptr == s);
570}
bsalomone63ffef2016-02-05 07:17:34 -0800571
fmalita03912f12016-07-06 06:22:06 -0700572DEF_TEST(surface_raster_zeroinitialized, reporter) {
573 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
574 SkPixmap pixmap;
575 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
576
577 for (int i = 0; i < pixmap.info().width(); ++i) {
578 for (int j = 0; j < pixmap.info().height(); ++j) {
579 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
580 }
581 }
582}
583
bsalomone63ffef2016-02-05 07:17:34 -0800584#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700585static sk_sp<SkSurface> create_gpu_surface_backend_texture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500586 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
ericrkc4025182016-05-04 12:01:58 -0700587 const int kWidth = 10;
588 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400589 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700590 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000591
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500592 *outTexture = context->getGpu()->createTestingOnlyBackendTexture(
593 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000594
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500595 if (!context->getGpu()->isTestingOnlyBackendTexture(*outTexture)) {
596 return nullptr;
597 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000598
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500599 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
Robert Phillipse44ef102017-07-21 15:37:19 -0400600 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Danielfaa095e2017-12-19 13:15:02 -0500601 kRGBA_8888_SkColorType,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000602 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700603 if (!surface) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500604 context->getGpu()->deleteTestingOnlyBackendTexture(outTexture);
ericrkc4025182016-05-04 12:01:58 -0700605 return nullptr;
606 }
ericrkc4025182016-05-04 12:01:58 -0700607 return surface;
608}
bsalomone63ffef2016-02-05 07:17:34 -0800609
ericrkc4025182016-05-04 12:01:58 -0700610static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500611 GrContext* context, int sampleCnt, uint32_t color, GrBackendTexture* outTexture) {
ericrkc4025182016-05-04 12:01:58 -0700612 const int kWidth = 10;
613 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400614 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700615 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000616
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500617 *outTexture = context->getGpu()->createTestingOnlyBackendTexture(
618 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000619
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500620 if (!context->getGpu()->isTestingOnlyBackendTexture(*outTexture)) {
ericrkc4025182016-05-04 12:01:58 -0700621 return nullptr;
622 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500623
624 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Greg Danielfaa095e2017-12-19 13:15:02 -0500625 context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
626 nullptr, nullptr);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500627
628 if (!surface) {
629 context->getGpu()->deleteTestingOnlyBackendTexture(outTexture);
630 return nullptr;
631 }
ericrkc4025182016-05-04 12:01:58 -0700632 return surface;
633}
634
635static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500636 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700637 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800638 if (!surface) {
639 ERRORF(reporter, "Could not create GPU SkSurface.");
640 return;
641 }
642 int w = surface->width();
643 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400644 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700645 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800646
Robert Phillips2c862492017-01-18 10:08:39 -0500647 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
648 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800649 ERRORF(reporter, "Could access render target of GPU SkSurface.");
650 return;
651 }
bsalomon2fba8092016-02-05 13:47:06 -0800652 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500653
654 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
655 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800656 for (int y = 0; y < h; ++y) {
657 for (int x = 0; x < w; ++x) {
658 uint32_t pixel = pixels.get()[y * w + x];
659 if (pixel != expectedValue) {
660 SkString msg;
661 if (expectedValue) {
662 msg = "SkSurface should have left render target unmodified";
663 } else {
664 msg = "SkSurface should have cleared the render target";
665 }
666 ERRORF(reporter,
667 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
668 expectedValue, x, y);
669 return;
670 }
671 }
672 }
673}
674
bsalomon758586c2016-04-06 14:02:39 -0700675DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700676 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700677
Robert Phillips2c862492017-01-18 10:08:39 -0500678 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700679 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500680 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
681 },
682 [] (SkSurface* s){
683 sk_sp<SkImage> i(s->makeImageSnapshot());
684 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500685 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500686 GrContext* context = gpuImage->context();
687 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
688 gpuImage->refColorSpace());
689 }
bsalomone63ffef2016-02-05 07:17:34 -0800690 };
ericrkc4025182016-05-04 12:01:58 -0700691
Robert Phillips2c862492017-01-18 10:08:39 -0500692 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700693 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800694 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700695 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800696 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
697 }
698 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700699 const uint32_t kOrigColor = 0xABABABAB;
700 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
701 &create_gpu_surface_backend_texture_as_render_target}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500702 GrBackendTexture backendTex;
703 auto surface = surfaceFunc(context, 0, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700704 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
705 surface.reset();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500706 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
ericrkc4025182016-05-04 12:01:58 -0700707 }
708 }
709}
bsalomone63ffef2016-02-05 07:17:34 -0800710
ericrkc4025182016-05-04 12:01:58 -0700711static void test_surface_draw_partially(
712 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
713 const int kW = surface->width();
714 const int kH = surface->height();
715 SkPaint paint;
716 const SkColor kRectColor = ~origColor | 0xFF000000;
717 paint.setColor(kRectColor);
718 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
719 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400720 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700721 sk_memset32(pixels.get(), ~origColor, kW * kH);
722 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
723 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
724 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
725 bool stop = false;
726 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
727 (origColor >> 0 & 0xFF),
728 (origColor >> 8 & 0xFF),
729 (origColor >> 16 & 0xFF));
730 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
731 (kRectColor >> 16 & 0xFF),
732 (kRectColor >> 8 & 0xFF),
733 (kRectColor >> 0 & 0xFF));
734 for (int y = 0; y < kH/2 && !stop; ++y) {
735 for (int x = 0; x < kW && !stop; ++x) {
736 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
737 if (rectColorPM != pixels[x + y * kW]) {
738 stop = true;
739 }
740 }
741 }
742 stop = false;
743 for (int y = kH/2; y < kH && !stop; ++y) {
744 for (int x = 0; x < kW && !stop; ++x) {
745 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
746 if (origColorPM != pixels[x + y * kW]) {
747 stop = true;
748 }
749 }
750 }
751}
bsalomone63ffef2016-02-05 07:17:34 -0800752
egdanielab527a52016-06-28 08:07:26 -0700753DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700754 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700755 if (!gpu) {
756 return;
757 }
758 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800759
ericrkc4025182016-05-04 12:01:58 -0700760 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
761 &create_gpu_surface_backend_texture_as_render_target}) {
762 // Validate that we can draw to the canvas and that the original texture color is
763 // preserved in pixels that aren't rendered to via the surface.
764 // This works only for non-multisampled case.
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500765 GrBackendTexture backendTex;
766 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700767 if (surface) {
768 test_surface_draw_partially(reporter, surface, kOrigColor);
769 surface.reset();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500770 gpu->deleteTestingOnlyBackendTexture(&backendTex);
ericrkc4025182016-05-04 12:01:58 -0700771 }
772 }
773}
774
775
776DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700777 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700778 if (!gpu) {
779 return;
780 }
Eric Karl5c779752017-05-08 12:02:07 -0700781 if (gpu->caps()->avoidStencilBuffers()) {
782 return;
783 }
ericrkc4025182016-05-04 12:01:58 -0700784 static const uint32_t kOrigColor = 0xFFAABBCC;
785
786 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
787 &create_gpu_surface_backend_texture_as_render_target}) {
788 for (int sampleCnt : {0, 4, 8}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500789 GrBackendTexture backendTex;
790 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &backendTex);
ericrkc4025182016-05-04 12:01:58 -0700791
792 if (!surface && sampleCnt > 0) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500793 // Certain platforms don't support MSAA, skip these.
794 continue;
ericrkc4025182016-05-04 12:01:58 -0700795 }
796
797 // Validate that we can attach a stencil buffer to an SkSurface created by either of
798 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400799 GrRenderTarget* rt = surface->getCanvas()
800 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700801 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700802 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500803 gpu->deleteTestingOnlyBackendTexture(&backendTex);
ericrkc4025182016-05-04 12:01:58 -0700804 }
bsalomone63ffef2016-02-05 07:17:34 -0800805 }
806}
807#endif
brianosman0e22eb82016-08-30 07:07:59 -0700808
809static void test_surface_creation_and_snapshot_with_color_space(
810 skiatest::Reporter* reporter,
811 const char* prefix,
812 bool f16Support,
813 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
814
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500815 auto srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500816 const SkMatrix44* srgbMatrix = srgbColorSpace->toXYZD50();
raftias94888332016-10-18 10:02:51 -0700817 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400818 SkColorSpaceTransferFn oddGamma;
819 oddGamma.fA = 1.0f;
820 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
821 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400822 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500823 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700824
825 const struct {
826 SkColorType fColorType;
827 sk_sp<SkColorSpace> fColorSpace;
828 bool fShouldWork;
829 const char* fDescription;
830 } testConfigs[] = {
831 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
832 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
833 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700834 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Stan Iliev4ed9ae42017-07-25 11:59:12 -0400835 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700836 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
837 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
brianosman0e22eb82016-08-30 07:07:59 -0700838 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
839 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
840 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
841 };
842
843 for (auto& testConfig : testConfigs) {
844 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
845 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
846 testConfig.fColorSpace);
847
848 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
849 // any surface of that type:
850 bool shouldWork = testConfig.fShouldWork &&
851 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
852
853 auto surface(surfaceMaker(info));
854 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
855
856 if (shouldWork && surface) {
857 sk_sp<SkImage> image(surface->makeImageSnapshot());
858 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
859 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
860 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
861 fullTestName.c_str());
862 }
863 }
864}
865
866DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
867 auto surfaceMaker = [](const SkImageInfo& info) {
868 return SkSurface::MakeRaster(info);
869 };
870
871 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
872}
873
874#if SK_SUPPORT_GPU
875DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
876 GrContext* context = ctxInfo.grContext();
877 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
878 auto surfaceMaker = [context](const SkImageInfo& info) {
879 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
880 };
881
882 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
883
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500884 std::vector<GrBackendTexture> backendTextures;
885 auto wrappedSurfaceMaker = [ context, &backendTextures ](const SkImageInfo& info) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000886 static const int kSize = 10;
887 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *context->caps());
brianosman0e22eb82016-08-30 07:07:59 -0700888
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500889 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
890 nullptr, kSize, kSize, config, true, GrMipMapped::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000891
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500892 if (!context->getGpu()->isTestingOnlyBackendTexture(backendTex)) {
brianosman0e22eb82016-08-30 07:07:59 -0700893 return sk_sp<SkSurface>(nullptr);
894 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500895 backendTextures.push_back(backendTex);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000896
897 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -0400898 kTopLeft_GrSurfaceOrigin, 0,
Greg Danielfaa095e2017-12-19 13:15:02 -0500899 info.colorType(),
Greg Daniel7ef28f32017-04-20 16:41:55 +0000900 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -0700901 };
902
903 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
904 wrappedSurfaceMaker);
905
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400906 context->flush();
907
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500908 for (auto backendTex : backendTextures) {
909 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
brianosman0e22eb82016-08-30 07:07:59 -0700910 }
911}
912#endif
Matt Sarett22886c42016-11-22 11:31:41 -0500913
914static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -0500915 SkOverdrawCanvas canvas(surface->getCanvas());
916 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -0500917 sk_sp<SkImage> image = surface->makeImageSnapshot();
918
919 SkBitmap bitmap;
920 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Matt Sarett22886c42016-11-22 11:31:41 -0500921 for (int y = 0; y < 10; y++) {
922 for (int x = 0; x < 10; x++) {
923 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
924 }
925 }
926}
927
928DEF_TEST(OverdrawSurface_Raster, r) {
929 sk_sp<SkSurface> surface = create_surface();
930 test_overdraw_surface(r, surface.get());
931}
932
933#if SK_SUPPORT_GPU
934DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
935 GrContext* context = ctxInfo.grContext();
936 sk_sp<SkSurface> surface = create_gpu_surface(context);
937 test_overdraw_surface(r, surface.get());
938}
939#endif
Mike Reed44d04bd2017-06-28 19:57:21 -0400940
941DEF_TEST(Surface_null, r) {
942 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
943
944 const int w = 37;
945 const int h = 1000;
946 auto surf = SkSurface::MakeNull(w, h);
947 auto canvas = surf->getCanvas();
948
949 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
950 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
951}