blob: 202df58f3c2f90f7a74168cb9e962fbcaee7d4c2 [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();
53 void* storage = sk_malloc_throw(info.getSafeSize(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);
reede8f30622016-03-23 18:59:25 -0700568 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
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(
586 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
587 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
592 GrBackendObject backendHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700593 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000594
595 GrBackendTexture backendTex = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
596 kWidth,
597 kHeight,
598 kRGBA_8888_GrPixelConfig,
599 backendHandle);
600
601 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -0400602 kTopLeft_GrSurfaceOrigin, sampleCnt,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000603 nullptr, nullptr);
ericrkc4025182016-05-04 12:01:58 -0700604 if (!surface) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000605 context->getGpu()->deleteTestingOnlyBackendTexture(backendHandle);
ericrkc4025182016-05-04 12:01:58 -0700606 return nullptr;
607 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000608 *outTexture = backendHandle;
ericrkc4025182016-05-04 12:01:58 -0700609 return surface;
610}
bsalomone63ffef2016-02-05 07:17:34 -0800611
ericrkc4025182016-05-04 12:01:58 -0700612static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
613 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
614 const int kWidth = 10;
615 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400616 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700617 sk_memset32(pixels.get(), color, kWidth * kHeight);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000618
619 GrBackendObject backendHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700620 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000621
622 GrBackendTexture backendTex = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
623 kWidth,
624 kHeight,
625 kRGBA_8888_GrPixelConfig,
626 backendHandle);
627 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipse44ef102017-07-21 15:37:19 -0400628 context, backendTex, kTopLeft_GrSurfaceOrigin, sampleCnt, nullptr, nullptr);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000629
ericrkc4025182016-05-04 12:01:58 -0700630 if (!surface) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000631 context->getGpu()->deleteTestingOnlyBackendTexture(backendHandle);
ericrkc4025182016-05-04 12:01:58 -0700632 return nullptr;
633 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000634 *outTexture = backendHandle;
ericrkc4025182016-05-04 12:01:58 -0700635 return surface;
636}
637
638static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500639 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700640 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800641 if (!surface) {
642 ERRORF(reporter, "Could not create GPU SkSurface.");
643 return;
644 }
645 int w = surface->width();
646 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400647 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700648 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800649
Robert Phillips2c862492017-01-18 10:08:39 -0500650 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
651 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800652 ERRORF(reporter, "Could access render target of GPU SkSurface.");
653 return;
654 }
bsalomon2fba8092016-02-05 13:47:06 -0800655 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500656
657 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
658 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800659 for (int y = 0; y < h; ++y) {
660 for (int x = 0; x < w; ++x) {
661 uint32_t pixel = pixels.get()[y * w + x];
662 if (pixel != expectedValue) {
663 SkString msg;
664 if (expectedValue) {
665 msg = "SkSurface should have left render target unmodified";
666 } else {
667 msg = "SkSurface should have cleared the render target";
668 }
669 ERRORF(reporter,
670 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
671 expectedValue, x, y);
672 return;
673 }
674 }
675 }
676}
677
bsalomon758586c2016-04-06 14:02:39 -0700678DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700679 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700680
Robert Phillips2c862492017-01-18 10:08:39 -0500681 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700682 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500683 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
684 },
685 [] (SkSurface* s){
686 sk_sp<SkImage> i(s->makeImageSnapshot());
687 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500688 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500689 GrContext* context = gpuImage->context();
690 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
691 gpuImage->refColorSpace());
692 }
bsalomone63ffef2016-02-05 07:17:34 -0800693 };
ericrkc4025182016-05-04 12:01:58 -0700694
Robert Phillips2c862492017-01-18 10:08:39 -0500695 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700696 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800697 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700698 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800699 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
700 }
701 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700702 const uint32_t kOrigColor = 0xABABABAB;
703 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
704 &create_gpu_surface_backend_texture_as_render_target}) {
705 GrBackendObject textureObject;
706 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
707 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
708 surface.reset();
709 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
710 }
711 }
712}
bsalomone63ffef2016-02-05 07:17:34 -0800713
ericrkc4025182016-05-04 12:01:58 -0700714static void test_surface_draw_partially(
715 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
716 const int kW = surface->width();
717 const int kH = surface->height();
718 SkPaint paint;
719 const SkColor kRectColor = ~origColor | 0xFF000000;
720 paint.setColor(kRectColor);
721 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
722 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400723 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700724 sk_memset32(pixels.get(), ~origColor, kW * kH);
725 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
726 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
727 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
728 bool stop = false;
729 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
730 (origColor >> 0 & 0xFF),
731 (origColor >> 8 & 0xFF),
732 (origColor >> 16 & 0xFF));
733 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
734 (kRectColor >> 16 & 0xFF),
735 (kRectColor >> 8 & 0xFF),
736 (kRectColor >> 0 & 0xFF));
737 for (int y = 0; y < kH/2 && !stop; ++y) {
738 for (int x = 0; x < kW && !stop; ++x) {
739 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
740 if (rectColorPM != pixels[x + y * kW]) {
741 stop = true;
742 }
743 }
744 }
745 stop = false;
746 for (int y = kH/2; y < kH && !stop; ++y) {
747 for (int x = 0; x < kW && !stop; ++x) {
748 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
749 if (origColorPM != pixels[x + y * kW]) {
750 stop = true;
751 }
752 }
753 }
754}
bsalomone63ffef2016-02-05 07:17:34 -0800755
egdanielab527a52016-06-28 08:07:26 -0700756DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700757 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700758 if (!gpu) {
759 return;
760 }
761 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800762
ericrkc4025182016-05-04 12:01:58 -0700763 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
764 &create_gpu_surface_backend_texture_as_render_target}) {
765 // Validate that we can draw to the canvas and that the original texture color is
766 // preserved in pixels that aren't rendered to via the surface.
767 // This works only for non-multisampled case.
768 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700769 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700770 if (surface) {
771 test_surface_draw_partially(reporter, surface, kOrigColor);
772 surface.reset();
773 gpu->deleteTestingOnlyBackendTexture(textureObject);
774 }
775 }
776}
777
778
779DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700780 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700781 if (!gpu) {
782 return;
783 }
Eric Karl5c779752017-05-08 12:02:07 -0700784 if (gpu->caps()->avoidStencilBuffers()) {
785 return;
786 }
ericrkc4025182016-05-04 12:01:58 -0700787 static const uint32_t kOrigColor = 0xFFAABBCC;
788
789 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
790 &create_gpu_surface_backend_texture_as_render_target}) {
791 for (int sampleCnt : {0, 4, 8}) {
792 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700793 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700794
795 if (!surface && sampleCnt > 0) {
796 // Certain platforms don't support MSAA, skip these.
797 continue;
798 }
799
800 // Validate that we can attach a stencil buffer to an SkSurface created by either of
801 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400802 GrRenderTarget* rt = surface->getCanvas()
803 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700804 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700805 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700806 gpu->deleteTestingOnlyBackendTexture(textureObject);
807 }
bsalomone63ffef2016-02-05 07:17:34 -0800808 }
809}
810#endif
brianosman0e22eb82016-08-30 07:07:59 -0700811
812static void test_surface_creation_and_snapshot_with_color_space(
813 skiatest::Reporter* reporter,
814 const char* prefix,
815 bool f16Support,
816 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
817
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500818 auto srgbColorSpace = SkColorSpace::MakeSRGB();
819 auto adobeColorSpace = SkColorSpace_Base::MakeNamed(SkColorSpace_Base::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700820 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
821 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400822 SkColorSpaceTransferFn oddGamma;
823 oddGamma.fA = 1.0f;
824 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
825 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400826 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500827 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700828
829 const struct {
830 SkColorType fColorType;
831 sk_sp<SkColorSpace> fColorSpace;
832 bool fShouldWork;
833 const char* fDescription;
834 } testConfigs[] = {
835 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
836 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
837 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
838 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
839 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Stan Iliev4ed9ae42017-07-25 11:59:12 -0400840 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700841 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
842 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
843 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
844 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
845 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
846 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
847 };
848
849 for (auto& testConfig : testConfigs) {
850 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
851 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
852 testConfig.fColorSpace);
853
854 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
855 // any surface of that type:
856 bool shouldWork = testConfig.fShouldWork &&
857 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
858
859 auto surface(surfaceMaker(info));
860 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
861
862 if (shouldWork && surface) {
863 sk_sp<SkImage> image(surface->makeImageSnapshot());
864 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
865 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
866 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
867 fullTestName.c_str());
868 }
869 }
870}
871
872DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
873 auto surfaceMaker = [](const SkImageInfo& info) {
874 return SkSurface::MakeRaster(info);
875 };
876
877 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
878}
879
880#if SK_SUPPORT_GPU
881DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
882 GrContext* context = ctxInfo.grContext();
883 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
884 auto surfaceMaker = [context](const SkImageInfo& info) {
885 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
886 };
887
888 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
889
890 std::vector<GrBackendObject> textureHandles;
891 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000892 static const int kSize = 10;
893 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *context->caps());
brianosman0e22eb82016-08-30 07:07:59 -0700894
Greg Daniel7ef28f32017-04-20 16:41:55 +0000895 GrBackendObject backendHandle = context->getGpu()->createTestingOnlyBackendTexture(
896 nullptr, kSize, kSize, config, true);
897
898 if (!backendHandle) {
brianosman0e22eb82016-08-30 07:07:59 -0700899 return sk_sp<SkSurface>(nullptr);
900 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000901 textureHandles.push_back(backendHandle);
brianosman0e22eb82016-08-30 07:07:59 -0700902
Greg Daniel7ef28f32017-04-20 16:41:55 +0000903 GrBackendTexture backendTex = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
904 kSize,
905 kSize,
906 config,
907 backendHandle);
908
909 return SkSurface::MakeFromBackendTexture(context, backendTex,
Robert Phillipse44ef102017-07-21 15:37:19 -0400910 kTopLeft_GrSurfaceOrigin, 0,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000911 sk_ref_sp(info.colorSpace()), nullptr);
brianosman0e22eb82016-08-30 07:07:59 -0700912 };
913
914 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
915 wrappedSurfaceMaker);
916
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400917 context->flush();
918
brianosman0e22eb82016-08-30 07:07:59 -0700919 for (auto textureHandle : textureHandles) {
920 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
921 }
922}
923#endif
Matt Sarett22886c42016-11-22 11:31:41 -0500924
925static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -0500926 SkOverdrawCanvas canvas(surface->getCanvas());
927 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -0500928 sk_sp<SkImage> image = surface->makeImageSnapshot();
929
930 SkBitmap bitmap;
931 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Matt Sarett22886c42016-11-22 11:31:41 -0500932 for (int y = 0; y < 10; y++) {
933 for (int x = 0; x < 10; x++) {
934 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
935 }
936 }
937}
938
939DEF_TEST(OverdrawSurface_Raster, r) {
940 sk_sp<SkSurface> surface = create_surface();
941 test_overdraw_surface(r, surface.get());
942}
943
944#if SK_SUPPORT_GPU
945DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
946 GrContext* context = ctxInfo.grContext();
947 sk_sp<SkSurface> surface = create_gpu_surface(context);
948 test_overdraw_surface(r, surface.get());
949}
950#endif
Mike Reed44d04bd2017-06-28 19:57:21 -0400951
952DEF_TEST(Surface_null, r) {
953 REPORTER_ASSERT(r, SkSurface::MakeNull(0, 0) == nullptr);
954
955 const int w = 37;
956 const int h = 1000;
957 auto surf = SkSurface::MakeNull(w, h);
958 auto canvas = surf->getCanvas();
959
960 canvas->drawPaint(SkPaint()); // should not crash, but don't expect anything to draw
961 REPORTER_ASSERT(r, surf->makeImageSnapshot() == nullptr);
962}