blob: 1f04de59032aa7f0587d75bb2c3e52baef38634c [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"
junov@chromium.org995beb62013-03-28 13:49:22 +000016#include "SkRRect.h"
17#include "SkSurface.h"
reed@google.com4f7c6152014-02-06 14:11:56 +000018#include "SkUtils.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000019#include "Test.h"
20
21#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -080022#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040023#include "GrRenderTargetContext.h"
kkinnunen179a8f52015-11-20 13:32:24 -080024#include "GrGpu.h"
ericrkc4025182016-05-04 12:01:58 -070025#include "GrResourceProvider.h"
brianosman0e22eb82016-08-30 07:07:59 -070026#include <vector>
junov@chromium.org995beb62013-03-28 13:49:22 +000027#endif
28
kkinnunen179a8f52015-11-20 13:32:24 -080029#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070030
kkinnunen179a8f52015-11-20 13:32:24 -080031static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070032 SkASSERT(pixels == context);
33 sk_free(pixels);
34}
reede8f30622016-03-23 18:59:25 -070035static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
36 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070037 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000038 if (requestedInfo) {
39 *requestedInfo = info;
40 }
reede8f30622016-03-23 18:59:25 -070041 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000042}
reede8f30622016-03-23 18:59:25 -070043static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
44 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080045 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
46 if (requestedInfo) {
47 *requestedInfo = info;
48 }
49 const size_t rowBytes = info.minRowBytes();
50 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070051 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
52 release_direct_surface_storage,
53 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080054}
55#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070056static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
57 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080058 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
59 if (requestedInfo) {
60 *requestedInfo = info;
61 }
robertphillips7e922762016-07-26 11:38:17 -070062 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080063}
reede8f30622016-03-23 18:59:25 -070064static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
65 SkAlphaType at = kPremul_SkAlphaType,
66 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080067 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
68 if (requestedInfo) {
69 *requestedInfo = info;
70 }
robertphillips7e922762016-07-26 11:38:17 -070071 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080072}
73#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000074
kkinnunen179a8f52015-11-20 13:32:24 -080075DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080076 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070077 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
78 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080079
reedb2497c22014-12-31 12:31:43 -080080}
kkinnunen179a8f52015-11-20 13:32:24 -080081#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070082DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080083 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
84 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070085 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080086}
87#endif
reedb2497c22014-12-31 12:31:43 -080088
kkinnunen179a8f52015-11-20 13:32:24 -080089static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070090 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080091 const SkImageInfo& requestInfo,
92 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000093 const SkColor color = SK_ColorRED;
94 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080095 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000096
reed6ceeebd2016-03-09 14:26:26 -080097 SkPixmap pmap;
98 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -080099 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000100
reed6ceeebd2016-03-09 14:26:26 -0800101 SkPixmap pmap2;
102 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000103
kkinnunen179a8f52015-11-20 13:32:24 -0800104 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800105 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
106 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
107 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000108
reed6ceeebd2016-03-09 14:26:26 -0800109 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
110 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
111 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800112 } else {
113 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000114 }
115}
kkinnunen179a8f52015-11-20 13:32:24 -0800116DEF_TEST(SurfaceCanvasPeek, reporter) {
117 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
118 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700119 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800120 test_canvas_peek(reporter, surface, requestInfo, true);
121 }
122}
123#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700124DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800125 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
126 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700127 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800128 test_canvas_peek(reporter, surface, requestInfo, false);
129 }
130}
131#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000132
reede8f30622016-03-23 18:59:25 -0700133static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700134 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800135 REPORTER_ASSERT(reporter, surface);
136 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700137 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800138 REPORTER_ASSERT(reporter, image);
139 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700140 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700141 }
142 }
143}
kkinnunen179a8f52015-11-20 13:32:24 -0800144DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
145 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700146 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
147 auto surface(surface_func(at, nullptr));
148 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700149 }
150 }
151}
kkinnunen179a8f52015-11-20 13:32:24 -0800152#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700153DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800154 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700155 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
156 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
157 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
158 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800159 }
160 }
161}
162#endif
bsalomon74f681d2015-06-23 14:38:48 -0700163
kkinnunen179a8f52015-11-20 13:32:24 -0800164static GrBackendObject get_surface_backend_texture_handle(
165 SkSurface* s, SkSurface::BackendHandleAccess a) {
166 return s->getTextureHandle(a);
167}
168static GrBackendObject get_surface_backend_render_target_handle(
169 SkSurface* s, SkSurface::BackendHandleAccess a) {
170 GrBackendObject result;
171 if (!s->getRenderTargetHandle(&result, a)) {
172 return 0;
173 }
174 return result;
175}
176
177static void test_backend_handle_access_copy_on_write(
178 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
179 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700180 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700181 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700182
183 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700184 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700185
186 // If the access mode triggers CoW, then the backend objects should reflect it.
187 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
188}
kkinnunen179a8f52015-11-20 13:32:24 -0800189DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
190 const SkSurface::BackendHandleAccess accessModes[] = {
191 SkSurface::kFlushRead_BackendHandleAccess,
192 SkSurface::kFlushWrite_BackendHandleAccess,
193 SkSurface::kDiscardWrite_BackendHandleAccess,
194 };
195 for (auto& handle_access_func :
196 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
197 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700198 auto surface(create_surface());
199 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800200 handle_access_func);
201 }
202 }
203}
204#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700205DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800206 const SkSurface::BackendHandleAccess accessModes[] = {
207 SkSurface::kFlushRead_BackendHandleAccess,
208 SkSurface::kFlushWrite_BackendHandleAccess,
209 SkSurface::kDiscardWrite_BackendHandleAccess,
210 };
211 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
212 for (auto& handle_access_func :
213 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
214 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700215 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700216 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800217 handle_access_func);
218 }
219 }
220 }
221}
222#endif
fmalitae2639082015-08-06 07:04:51 -0700223
kkinnunen179a8f52015-11-20 13:32:24 -0800224#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800225
kkinnunen179a8f52015-11-20 13:32:24 -0800226static void test_backend_handle_unique_id(
227 skiatest::Reporter* reporter, SkSurface* surface,
228 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700229 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800230 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
231 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700232 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800233 // just read access should not affect the snapshot
234 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
235
236 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
237 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700238 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800239 // expect a new image, since we claimed we would write
240 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
241
242 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
243 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700244 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800245 // expect a new(er) image, since we claimed we would write
246 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
247 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
248}
249// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700250DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800251 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700252 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800253 for (auto& handle_access_func :
254 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700255 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700256 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800257 }
258 }
259 }
260}
261#endif
262
263// Verify that the right canvas commands trigger a copy on write.
264static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000265 SkCanvas* canvas = surface->getCanvas();
266
267 const SkRect testRect =
268 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
269 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000270 SkPath testPath;
271 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
272 SkIntToScalar(2), SkIntToScalar(1)));
273
274 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
275
276 SkRegion testRegion;
277 testRegion.setRect(testIRect);
278
279
280 const SkColor testColor = 0x01020304;
281 const SkPaint testPaint;
282 const SkPoint testPoints[3] = {
283 {SkIntToScalar(0), SkIntToScalar(0)},
284 {SkIntToScalar(2), SkIntToScalar(1)},
285 {SkIntToScalar(0), SkIntToScalar(2)}
286 };
287 const size_t testPointCount = 3;
288
289 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000290 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000291 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000292
293 SkRRect testRRect;
294 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
295
296 SkString testText("Hello World");
297 const SkPoint testPoints2[] = {
298 { SkIntToScalar(0), SkIntToScalar(1) },
299 { SkIntToScalar(1), SkIntToScalar(1) },
300 { SkIntToScalar(2), SkIntToScalar(1) },
301 { SkIntToScalar(3), SkIntToScalar(1) },
302 { SkIntToScalar(4), SkIntToScalar(1) },
303 { SkIntToScalar(5), SkIntToScalar(1) },
304 { SkIntToScalar(6), SkIntToScalar(1) },
305 { SkIntToScalar(7), SkIntToScalar(1) },
306 { SkIntToScalar(8), SkIntToScalar(1) },
307 { SkIntToScalar(9), SkIntToScalar(1) },
308 { SkIntToScalar(10), SkIntToScalar(1) },
309 };
310
311#define EXPECT_COPY_ON_WRITE(command) \
312 { \
reed9ce9d672016-03-17 10:51:11 -0700313 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
314 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000315 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700316 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
317 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000318 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
319 }
320
321 EXPECT_COPY_ON_WRITE(clear(testColor))
322 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
323 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
324 testPaint))
325 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
326 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
327 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
328 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
329 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700330 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700331 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000332 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
333 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
334 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700335 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000336 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800337}
338DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700339 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800340}
341#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700342DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800343 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700344 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700345 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700346 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000347}
kkinnunen179a8f52015-11-20 13:32:24 -0800348#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000349
kkinnunen179a8f52015-11-20 13:32:24 -0800350static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
351 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000352 // This test succeeds by not triggering an assertion.
353 // The test verifies that the surface remains writable (usable) after
354 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000355 SkCanvas* canvas = surface->getCanvas();
356 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700357 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000358 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000359}
kkinnunen179a8f52015-11-20 13:32:24 -0800360DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700361 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800362}
363#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700364DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800365 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700366 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700367 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800368 }
369}
370#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000371
junov@chromium.orgb516a412013-05-01 22:49:59 +0000372#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800373static void test_crbug263329(skiatest::Reporter* reporter,
374 SkSurface* surface1,
375 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000376 // This is a regression test for crbug.com/263329
377 // Bug was caused by onCopyOnWrite releasing the old surface texture
378 // back to the scratch texture pool even though the texture is used
379 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000380 SkCanvas* canvas1 = surface1->getCanvas();
381 SkCanvas* canvas2 = surface2->getCanvas();
382 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700383 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000384 // Trigger copy on write, new backing is a scratch texture
385 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700386 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000387 // Trigger copy on write, old backing should not be returned to scratch
388 // pool because it is held by image2
389 canvas1->clear(3);
390
391 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700392 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000393 // Trigger copy on write on surface2. The new backing store should not
394 // be recycling a texture that is held by an existing image.
395 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700396 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800397 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000398 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800399 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
400 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
401 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
402 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
403 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000404}
egdanielab527a52016-06-28 08:07:26 -0700405DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800406 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700407 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
408 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700409 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800410 }
411}
412#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000413
kkinnunen179a8f52015-11-20 13:32:24 -0800414DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700415 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700416 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800417 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800418 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800419 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800420}
421#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700422DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800423 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700424 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700425 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800426 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700427 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000428 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800429 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800430 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000431 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000432}
kkinnunen179a8f52015-11-20 13:32:24 -0800433#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800434
kkinnunen179a8f52015-11-20 13:32:24 -0800435#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800436#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800437#include "SkGpuDevice.h"
438#include "SkImage_Gpu.h"
439#include "SkSurface_Gpu.h"
440
reede8f30622016-03-23 18:59:25 -0700441static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
442 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Brian Osman11052242016-10-27 14:47:55 -0400443 return gsurf->getDevice()->accessRenderTargetContext()
444 ->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800445}
446
bsalomon5ec26ae2016-02-25 08:33:02 -0800447static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800448 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800449}
450
reed9ce9d672016-03-17 10:51:11 -0700451static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
452 return is_budgeted(image.get());
453}
454
egdanielab527a52016-06-28 08:07:26 -0700455DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800456 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800457 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
458 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700459 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800460 SkASSERT(surface);
461 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
462
reed9ce9d672016-03-17 10:51:11 -0700463 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800464
465 // Initially the image shares a texture with the surface, and the surface decides
466 // whether it is budgeted or not.
467 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
468 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
469
470 // Now trigger copy-on-write
471 surface->getCanvas()->clear(SK_ColorBLUE);
472
473 // They don't share a texture anymore. They should each have made their own budget
474 // decision.
475 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
476 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
477 }
478 }
479}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000480#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000481
kkinnunen179a8f52015-11-20 13:32:24 -0800482static void test_no_canvas1(skiatest::Reporter* reporter,
483 SkSurface* surface,
484 SkSurface::ContentChangeMode mode) {
485 // Test passes by not asserting
486 surface->notifyContentWillChange(mode);
487 SkDEBUGCODE(surface->validate();)
488}
489static void test_no_canvas2(skiatest::Reporter* reporter,
490 SkSurface* surface,
491 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000492 // Verifies the robustness of SkSurface for handling use cases where calls
493 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700494 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
495 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800496 SkDEBUGCODE(image1->validate();)
497 SkDEBUGCODE(surface->validate();)
498 surface->notifyContentWillChange(mode);
499 SkDEBUGCODE(image1->validate();)
500 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700501 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
502 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800503 SkDEBUGCODE(image2->validate();)
504 SkDEBUGCODE(surface->validate();)
505 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000506}
kkinnunen179a8f52015-11-20 13:32:24 -0800507DEF_TEST(SurfaceNoCanvas, reporter) {
508 SkSurface::ContentChangeMode modes[] =
509 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
510 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
511 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700512 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800513 }
514 }
515}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000516#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700517DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800518 SkSurface::ContentChangeMode modes[] =
519 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
520 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
521 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
522 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700523 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700524 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700525 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000526 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000527 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000528}
kkinnunen179a8f52015-11-20 13:32:24 -0800529#endif
reed9cd016e2016-01-30 10:01:06 -0800530
531static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800532 SkPixmap surfacePM;
533 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800534
reed9ce9d672016-03-17 10:51:11 -0700535 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800536 SkPixmap pm;
537 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800538
reed6ceeebd2016-03-09 14:26:26 -0800539 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800540
541 // trigger a copy-on-write
542 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700543 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800544 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
545
reed6ceeebd2016-03-09 14:26:26 -0800546 SkPixmap pm2;
547 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
548 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800549}
550
551DEF_TEST(surface_rowbytes, reporter) {
552 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
553
reede8f30622016-03-23 18:59:25 -0700554 auto surf0(SkSurface::MakeRaster(info));
555 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800556
557 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700558 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
559 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800560
561 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700562 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800563 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700564 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800565 REPORTER_ASSERT(reporter, nullptr == s);
566}
bsalomone63ffef2016-02-05 07:17:34 -0800567
fmalita03912f12016-07-06 06:22:06 -0700568DEF_TEST(surface_raster_zeroinitialized, reporter) {
569 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
570 SkPixmap pixmap;
571 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
572
573 for (int i = 0; i < pixmap.info().width(); ++i) {
574 for (int j = 0; j < pixmap.info().height(); ++j) {
575 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
576 }
577 }
578}
579
bsalomone63ffef2016-02-05 07:17:34 -0800580#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700581static sk_sp<SkSurface> create_gpu_surface_backend_texture(
582 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
583 const int kWidth = 10;
584 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400585 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700586 sk_memset32(pixels.get(), color, kWidth * kHeight);
587 GrBackendTextureDesc desc;
588 desc.fConfig = kRGBA_8888_GrPixelConfig;
589 desc.fWidth = kWidth;
590 desc.fHeight = kHeight;
591 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
592 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700593 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700594 desc.fSampleCnt = sampleCnt;
595 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
596 if (!surface) {
597 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
598 return nullptr;
599 }
600 *outTexture = desc.fTextureHandle;
601 return surface;
602}
bsalomone63ffef2016-02-05 07:17:34 -0800603
ericrkc4025182016-05-04 12:01:58 -0700604static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
605 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
606 const int kWidth = 10;
607 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400608 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700609 sk_memset32(pixels.get(), color, kWidth * kHeight);
610 GrBackendTextureDesc desc;
611 desc.fConfig = kRGBA_8888_GrPixelConfig;
612 desc.fWidth = kWidth;
613 desc.fHeight = kHeight;
614 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
615 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700616 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700617 desc.fSampleCnt = sampleCnt;
618 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
619 nullptr);
620 if (!surface) {
621 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
622 return nullptr;
623 }
624 *outTexture = desc.fTextureHandle;
625 return surface;
626}
627
628static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
629 std::function<GrSurface*(SkSurface*)> grSurfaceGetter,
630 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800631 if (!surface) {
632 ERRORF(reporter, "Could not create GPU SkSurface.");
633 return;
634 }
635 int w = surface->width();
636 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400637 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700638 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800639
Hal Canary342b7ac2016-11-04 11:49:42 -0400640 sk_sp<GrSurface> grSurface(SkSafeRef(grSurfaceGetter(surface.get())));
bsalomone63ffef2016-02-05 07:17:34 -0800641 if (!grSurface) {
642 ERRORF(reporter, "Could access render target of GPU SkSurface.");
643 return;
644 }
bsalomon2fba8092016-02-05 13:47:06 -0800645 surface.reset();
bsalomone63ffef2016-02-05 07:17:34 -0800646 grSurface->readPixels(0, 0, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
647 for (int y = 0; y < h; ++y) {
648 for (int x = 0; x < w; ++x) {
649 uint32_t pixel = pixels.get()[y * w + x];
650 if (pixel != expectedValue) {
651 SkString msg;
652 if (expectedValue) {
653 msg = "SkSurface should have left render target unmodified";
654 } else {
655 msg = "SkSurface should have cleared the render target";
656 }
657 ERRORF(reporter,
658 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
659 expectedValue, x, y);
660 return;
661 }
662 }
663 }
664}
665
bsalomon758586c2016-04-06 14:02:39 -0700666DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700667 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700668
bsalomone63ffef2016-02-05 07:17:34 -0800669 std::function<GrSurface*(SkSurface*)> grSurfaceGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700670 [] (SkSurface* s){
Brian Osman11052242016-10-27 14:47:55 -0400671 GrRenderTargetContext* rtc =
672 s->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
673 return rtc->accessRenderTarget(); },
reed9ce9d672016-03-17 10:51:11 -0700674 [] (SkSurface* s){ sk_sp<SkImage> i(s->makeImageSnapshot());
ericrkc4025182016-05-04 12:01:58 -0700675 return as_IB(i)->peekTexture(); }
bsalomone63ffef2016-02-05 07:17:34 -0800676 };
ericrkc4025182016-05-04 12:01:58 -0700677
bsalomone63ffef2016-02-05 07:17:34 -0800678 for (auto grSurfaceGetter : grSurfaceGetters) {
ericrkc4025182016-05-04 12:01:58 -0700679 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800680 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700681 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800682 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
683 }
684 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700685 const uint32_t kOrigColor = 0xABABABAB;
686 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
687 &create_gpu_surface_backend_texture_as_render_target}) {
688 GrBackendObject textureObject;
689 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
690 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
691 surface.reset();
692 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
693 }
694 }
695}
bsalomone63ffef2016-02-05 07:17:34 -0800696
ericrkc4025182016-05-04 12:01:58 -0700697static void test_surface_draw_partially(
698 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
699 const int kW = surface->width();
700 const int kH = surface->height();
701 SkPaint paint;
702 const SkColor kRectColor = ~origColor | 0xFF000000;
703 paint.setColor(kRectColor);
704 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
705 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400706 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700707 sk_memset32(pixels.get(), ~origColor, kW * kH);
708 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
709 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
710 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
711 bool stop = false;
712 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
713 (origColor >> 0 & 0xFF),
714 (origColor >> 8 & 0xFF),
715 (origColor >> 16 & 0xFF));
716 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
717 (kRectColor >> 16 & 0xFF),
718 (kRectColor >> 8 & 0xFF),
719 (kRectColor >> 0 & 0xFF));
720 for (int y = 0; y < kH/2 && !stop; ++y) {
721 for (int x = 0; x < kW && !stop; ++x) {
722 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
723 if (rectColorPM != pixels[x + y * kW]) {
724 stop = true;
725 }
726 }
727 }
728 stop = false;
729 for (int y = kH/2; y < kH && !stop; ++y) {
730 for (int x = 0; x < kW && !stop; ++x) {
731 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
732 if (origColorPM != pixels[x + y * kW]) {
733 stop = true;
734 }
735 }
736 }
737}
bsalomone63ffef2016-02-05 07:17:34 -0800738
egdanielab527a52016-06-28 08:07:26 -0700739DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700740 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700741 if (!gpu) {
742 return;
743 }
744 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800745
ericrkc4025182016-05-04 12:01:58 -0700746 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
747 &create_gpu_surface_backend_texture_as_render_target}) {
748 // Validate that we can draw to the canvas and that the original texture color is
749 // preserved in pixels that aren't rendered to via the surface.
750 // This works only for non-multisampled case.
751 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700752 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700753 if (surface) {
754 test_surface_draw_partially(reporter, surface, kOrigColor);
755 surface.reset();
756 gpu->deleteTestingOnlyBackendTexture(textureObject);
757 }
758 }
759}
760
761
762DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700763 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700764 if (!gpu) {
765 return;
766 }
767 static const uint32_t kOrigColor = 0xFFAABBCC;
768
769 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
770 &create_gpu_surface_backend_texture_as_render_target}) {
771 for (int sampleCnt : {0, 4, 8}) {
772 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700773 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700774
775 if (!surface && sampleCnt > 0) {
776 // Certain platforms don't support MSAA, skip these.
777 continue;
778 }
779
780 // Validate that we can attach a stencil buffer to an SkSurface created by either of
781 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400782 GrRenderTarget* rt = surface->getCanvas()
783 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700784 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700785 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700786 gpu->deleteTestingOnlyBackendTexture(textureObject);
787 }
bsalomone63ffef2016-02-05 07:17:34 -0800788 }
789}
790#endif
brianosman0e22eb82016-08-30 07:07:59 -0700791
792static void test_surface_creation_and_snapshot_with_color_space(
793 skiatest::Reporter* reporter,
794 const char* prefix,
795 bool f16Support,
796 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
797
Brian Osman526972e2016-10-24 09:24:02 -0400798 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
799 auto adobeColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700800 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
801 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400802 SkColorSpaceTransferFn oddGamma;
803 oddGamma.fA = 1.0f;
804 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
805 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400806 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
807 auto linearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosman0e22eb82016-08-30 07:07:59 -0700808
809 const struct {
810 SkColorType fColorType;
811 sk_sp<SkColorSpace> fColorSpace;
812 bool fShouldWork;
813 const char* fDescription;
814 } testConfigs[] = {
815 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
816 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
817 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
818 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
819 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Brian Osmaneb21ef62016-11-01 16:30:21 -0400820 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700821 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
822 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
823 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
824 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
825 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
826 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
827 };
828
829 for (auto& testConfig : testConfigs) {
830 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
831 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
832 testConfig.fColorSpace);
833
834 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
835 // any surface of that type:
836 bool shouldWork = testConfig.fShouldWork &&
837 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
838
839 auto surface(surfaceMaker(info));
840 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
841
842 if (shouldWork && surface) {
843 sk_sp<SkImage> image(surface->makeImageSnapshot());
844 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
845 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
846 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
847 fullTestName.c_str());
848 }
849 }
850}
851
852DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
853 auto surfaceMaker = [](const SkImageInfo& info) {
854 return SkSurface::MakeRaster(info);
855 };
856
857 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
858}
859
860#if SK_SUPPORT_GPU
861DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
862 GrContext* context = ctxInfo.grContext();
863 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
864 auto surfaceMaker = [context](const SkImageInfo& info) {
865 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
866 };
867
868 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
869
870 std::vector<GrBackendObject> textureHandles;
871 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
872 GrBackendTextureDesc desc;
873 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
874 desc.fWidth = 10;
875 desc.fHeight = 10;
876 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
877 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
878 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
879
880 if (!desc.fTextureHandle) {
881 return sk_sp<SkSurface>(nullptr);
882 }
883 textureHandles.push_back(desc.fTextureHandle);
884
885 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
886 nullptr);
887 };
888
889 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
890 wrappedSurfaceMaker);
891
892 for (auto textureHandle : textureHandles) {
893 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
894 }
895}
896#endif
Matt Sarett22886c42016-11-22 11:31:41 -0500897
898static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -0500899 SkOverdrawCanvas canvas(surface->getCanvas());
900 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -0500901 sk_sp<SkImage> image = surface->makeImageSnapshot();
902
903 SkBitmap bitmap;
904 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
905 bitmap.lockPixels();
906 for (int y = 0; y < 10; y++) {
907 for (int x = 0; x < 10; x++) {
908 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
909 }
910 }
911}
912
913DEF_TEST(OverdrawSurface_Raster, r) {
914 sk_sp<SkSurface> surface = create_surface();
915 test_overdraw_surface(r, surface.get());
916}
917
918#if SK_SUPPORT_GPU
919DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
920 GrContext* context = ctxInfo.grContext();
921 sk_sp<SkSurface> surface = create_gpu_surface(context);
922 test_overdraw_surface(r, surface.get());
923}
924#endif