blob: f620cff9965c3feb6f088b74c04f42c4f4fe24a3 [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"
brianosman0e22eb82016-08-30 07:07:59 -070028#include <vector>
junov@chromium.org995beb62013-03-28 13:49:22 +000029#endif
30
kkinnunen179a8f52015-11-20 13:32:24 -080031#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070032
kkinnunen179a8f52015-11-20 13:32:24 -080033static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070034 SkASSERT(pixels == context);
35 sk_free(pixels);
36}
reede8f30622016-03-23 18:59:25 -070037static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
38 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070039 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000040 if (requestedInfo) {
41 *requestedInfo = info;
42 }
reede8f30622016-03-23 18:59:25 -070043 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000044}
reede8f30622016-03-23 18:59:25 -070045static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
46 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080047 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
48 if (requestedInfo) {
49 *requestedInfo = info;
50 }
51 const size_t rowBytes = info.minRowBytes();
52 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070053 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
54 release_direct_surface_storage,
55 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080056}
57#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070058static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
59 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080060 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
61 if (requestedInfo) {
62 *requestedInfo = info;
63 }
robertphillips7e922762016-07-26 11:38:17 -070064 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080065}
reede8f30622016-03-23 18:59:25 -070066static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
67 SkAlphaType at = kPremul_SkAlphaType,
68 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080069 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
70 if (requestedInfo) {
71 *requestedInfo = info;
72 }
robertphillips7e922762016-07-26 11:38:17 -070073 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080074}
75#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000076
kkinnunen179a8f52015-11-20 13:32:24 -080077DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080078 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070079 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
80 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080081
reedb2497c22014-12-31 12:31:43 -080082}
kkinnunen179a8f52015-11-20 13:32:24 -080083#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070084DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080085 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
86 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070087 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080088}
89#endif
reedb2497c22014-12-31 12:31:43 -080090
kkinnunen179a8f52015-11-20 13:32:24 -080091static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070092 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080093 const SkImageInfo& requestInfo,
94 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000095 const SkColor color = SK_ColorRED;
96 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080097 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000098
reed6ceeebd2016-03-09 14:26:26 -080099 SkPixmap pmap;
100 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800101 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000102
reed6ceeebd2016-03-09 14:26:26 -0800103 SkPixmap pmap2;
104 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000105
kkinnunen179a8f52015-11-20 13:32:24 -0800106 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800107 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
108 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
109 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000110
reed6ceeebd2016-03-09 14:26:26 -0800111 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
112 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
113 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800114 } else {
115 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000116 }
117}
kkinnunen179a8f52015-11-20 13:32:24 -0800118DEF_TEST(SurfaceCanvasPeek, reporter) {
119 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
120 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700121 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800122 test_canvas_peek(reporter, surface, requestInfo, true);
123 }
124}
125#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700126DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800127 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
128 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700129 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800130 test_canvas_peek(reporter, surface, requestInfo, false);
131 }
132}
133#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000134
reede8f30622016-03-23 18:59:25 -0700135static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700136 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800137 REPORTER_ASSERT(reporter, surface);
138 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700139 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800140 REPORTER_ASSERT(reporter, image);
141 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700142 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700143 }
144 }
145}
kkinnunen179a8f52015-11-20 13:32:24 -0800146DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
147 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700148 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
149 auto surface(surface_func(at, nullptr));
150 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700151 }
152 }
153}
kkinnunen179a8f52015-11-20 13:32:24 -0800154#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700155DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800156 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700157 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
158 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
159 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
160 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800161 }
162 }
163}
164#endif
bsalomon74f681d2015-06-23 14:38:48 -0700165
kkinnunen179a8f52015-11-20 13:32:24 -0800166static GrBackendObject get_surface_backend_texture_handle(
167 SkSurface* s, SkSurface::BackendHandleAccess a) {
168 return s->getTextureHandle(a);
169}
170static GrBackendObject get_surface_backend_render_target_handle(
171 SkSurface* s, SkSurface::BackendHandleAccess a) {
172 GrBackendObject result;
173 if (!s->getRenderTargetHandle(&result, a)) {
174 return 0;
175 }
176 return result;
177}
178
179static void test_backend_handle_access_copy_on_write(
180 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
181 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700182 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700183 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700184
185 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700186 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700187
188 // If the access mode triggers CoW, then the backend objects should reflect it.
189 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
190}
kkinnunen179a8f52015-11-20 13:32:24 -0800191DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
192 const SkSurface::BackendHandleAccess accessModes[] = {
193 SkSurface::kFlushRead_BackendHandleAccess,
194 SkSurface::kFlushWrite_BackendHandleAccess,
195 SkSurface::kDiscardWrite_BackendHandleAccess,
196 };
197 for (auto& handle_access_func :
198 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
199 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700200 auto surface(create_surface());
201 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800202 handle_access_func);
203 }
204 }
205}
206#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800208 const SkSurface::BackendHandleAccess accessModes[] = {
209 SkSurface::kFlushRead_BackendHandleAccess,
210 SkSurface::kFlushWrite_BackendHandleAccess,
211 SkSurface::kDiscardWrite_BackendHandleAccess,
212 };
213 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
214 for (auto& handle_access_func :
215 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
216 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700217 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700218 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800219 handle_access_func);
220 }
221 }
222 }
223}
224#endif
fmalitae2639082015-08-06 07:04:51 -0700225
kkinnunen179a8f52015-11-20 13:32:24 -0800226#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800227
kkinnunen179a8f52015-11-20 13:32:24 -0800228static void test_backend_handle_unique_id(
229 skiatest::Reporter* reporter, SkSurface* surface,
230 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700231 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800232 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
233 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700234 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800235 // just read access should not affect the snapshot
236 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
237
238 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
239 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700240 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800241 // expect a new image, since we claimed we would write
242 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
243
244 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
245 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700246 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800247 // expect a new(er) image, since we claimed we would write
248 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
249 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
250}
251// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700252DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800253 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700254 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800255 for (auto& handle_access_func :
256 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700257 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700258 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800259 }
260 }
261 }
262}
263#endif
264
265// Verify that the right canvas commands trigger a copy on write.
266static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000267 SkCanvas* canvas = surface->getCanvas();
268
269 const SkRect testRect =
270 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
271 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000272 SkPath testPath;
273 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
274 SkIntToScalar(2), SkIntToScalar(1)));
275
276 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
277
278 SkRegion testRegion;
279 testRegion.setRect(testIRect);
280
281
282 const SkColor testColor = 0x01020304;
283 const SkPaint testPaint;
284 const SkPoint testPoints[3] = {
285 {SkIntToScalar(0), SkIntToScalar(0)},
286 {SkIntToScalar(2), SkIntToScalar(1)},
287 {SkIntToScalar(0), SkIntToScalar(2)}
288 };
289 const size_t testPointCount = 3;
290
291 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000292 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000293 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000294
295 SkRRect testRRect;
296 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
297
298 SkString testText("Hello World");
299 const SkPoint testPoints2[] = {
300 { SkIntToScalar(0), SkIntToScalar(1) },
301 { SkIntToScalar(1), SkIntToScalar(1) },
302 { SkIntToScalar(2), SkIntToScalar(1) },
303 { SkIntToScalar(3), SkIntToScalar(1) },
304 { SkIntToScalar(4), SkIntToScalar(1) },
305 { SkIntToScalar(5), SkIntToScalar(1) },
306 { SkIntToScalar(6), SkIntToScalar(1) },
307 { SkIntToScalar(7), SkIntToScalar(1) },
308 { SkIntToScalar(8), SkIntToScalar(1) },
309 { SkIntToScalar(9), SkIntToScalar(1) },
310 { SkIntToScalar(10), SkIntToScalar(1) },
311 };
312
313#define EXPECT_COPY_ON_WRITE(command) \
314 { \
reed9ce9d672016-03-17 10:51:11 -0700315 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
316 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000317 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700318 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
319 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000320 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
321 }
322
323 EXPECT_COPY_ON_WRITE(clear(testColor))
324 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
325 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
326 testPaint))
327 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
328 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
329 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
330 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
331 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700332 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700333 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000334 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
335 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
336 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700337 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000338 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800339}
340DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700341 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800342}
343#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700344DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800345 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700346 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700347 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700348 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000349}
kkinnunen179a8f52015-11-20 13:32:24 -0800350#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000351
kkinnunen179a8f52015-11-20 13:32:24 -0800352static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
353 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000354 // This test succeeds by not triggering an assertion.
355 // The test verifies that the surface remains writable (usable) after
356 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000357 SkCanvas* canvas = surface->getCanvas();
358 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700359 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000360 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000361}
kkinnunen179a8f52015-11-20 13:32:24 -0800362DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700363 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800364}
365#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700366DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800367 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700368 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700369 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800370 }
371}
372#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000373
junov@chromium.orgb516a412013-05-01 22:49:59 +0000374#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800375static void test_crbug263329(skiatest::Reporter* reporter,
376 SkSurface* surface1,
377 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000378 // This is a regression test for crbug.com/263329
379 // Bug was caused by onCopyOnWrite releasing the old surface texture
380 // back to the scratch texture pool even though the texture is used
381 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000382 SkCanvas* canvas1 = surface1->getCanvas();
383 SkCanvas* canvas2 = surface2->getCanvas();
384 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700385 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000386 // Trigger copy on write, new backing is a scratch texture
387 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700388 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000389 // Trigger copy on write, old backing should not be returned to scratch
390 // pool because it is held by image2
391 canvas1->clear(3);
392
393 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700394 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000395 // Trigger copy on write on surface2. The new backing store should not
396 // be recycling a texture that is held by an existing image.
397 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700398 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800399 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000400 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800401 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
402 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
403 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
404 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
405 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000406}
egdanielab527a52016-06-28 08:07:26 -0700407DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800408 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700409 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
410 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700411 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800412 }
413}
414#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000415
kkinnunen179a8f52015-11-20 13:32:24 -0800416DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700417 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700418 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500419 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800420 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500421 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800422}
423#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700424DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800425 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700426 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700427 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500428
429 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
430 GrBackendObject textureHandle = image->getTextureHandle(false);
431 REPORTER_ASSERT(reporter, 0 != textureHandle);
kkinnunen179a8f52015-11-20 13:32:24 -0800432 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500433 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
434 REPORTER_ASSERT(reporter, textureHandle == image->getTextureHandle(false));
junov@chromium.orgda904742013-05-01 22:38:16 +0000435 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000436}
kkinnunen179a8f52015-11-20 13:32:24 -0800437#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800438
kkinnunen179a8f52015-11-20 13:32:24 -0800439#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800440#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800441#include "SkGpuDevice.h"
442#include "SkImage_Gpu.h"
443#include "SkSurface_Gpu.h"
444
reede8f30622016-03-23 18:59:25 -0700445static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
446 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500447
448 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
449 ->asRenderTargetProxy();
450 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800451}
452
bsalomon5ec26ae2016-02-25 08:33:02 -0800453static SkBudgeted is_budgeted(SkImage* image) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400454 return ((SkImage_Gpu*)image)->peekProxy()->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800455}
456
reed9ce9d672016-03-17 10:51:11 -0700457static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
458 return is_budgeted(image.get());
459}
460
egdanielab527a52016-06-28 08:07:26 -0700461DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800462 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400463 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
464 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), budgeted, info));
465 SkASSERT(surface);
466 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800467
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400468 sk_sp<SkImage> image(surface->makeImageSnapshot());
bsalomoneaaaf0b2015-01-23 08:08:04 -0800469
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400470 // Initially the image shares a texture with the surface, and the
471 // the budgets should always match.
472 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
473 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800474
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400475 // Now trigger copy-on-write
476 surface->getCanvas()->clear(SK_ColorBLUE);
bsalomoneaaaf0b2015-01-23 08:08:04 -0800477
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400478 // They don't share a texture anymore but the budgets should still match.
479 REPORTER_ASSERT(reporter, budgeted == is_budgeted(surface));
480 REPORTER_ASSERT(reporter, budgeted == is_budgeted(image));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800481 }
482}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000483#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000484
kkinnunen179a8f52015-11-20 13:32:24 -0800485static void test_no_canvas1(skiatest::Reporter* reporter,
486 SkSurface* surface,
487 SkSurface::ContentChangeMode mode) {
488 // Test passes by not asserting
489 surface->notifyContentWillChange(mode);
490 SkDEBUGCODE(surface->validate();)
491}
492static void test_no_canvas2(skiatest::Reporter* reporter,
493 SkSurface* surface,
494 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000495 // Verifies the robustness of SkSurface for handling use cases where calls
496 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700497 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
498 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800499 SkDEBUGCODE(image1->validate();)
500 SkDEBUGCODE(surface->validate();)
501 surface->notifyContentWillChange(mode);
502 SkDEBUGCODE(image1->validate();)
503 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700504 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
505 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800506 SkDEBUGCODE(image2->validate();)
507 SkDEBUGCODE(surface->validate();)
508 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000509}
kkinnunen179a8f52015-11-20 13:32:24 -0800510DEF_TEST(SurfaceNoCanvas, reporter) {
511 SkSurface::ContentChangeMode modes[] =
512 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
513 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
514 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700515 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800516 }
517 }
518}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000519#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700520DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800521 SkSurface::ContentChangeMode modes[] =
522 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
523 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
524 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
525 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700526 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700527 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700528 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000529 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000530 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000531}
kkinnunen179a8f52015-11-20 13:32:24 -0800532#endif
reed9cd016e2016-01-30 10:01:06 -0800533
534static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800535 SkPixmap surfacePM;
536 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800537
reed9ce9d672016-03-17 10:51:11 -0700538 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800539 SkPixmap pm;
540 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800541
reed6ceeebd2016-03-09 14:26:26 -0800542 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800543
544 // trigger a copy-on-write
545 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700546 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800547 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
548
reed6ceeebd2016-03-09 14:26:26 -0800549 SkPixmap pm2;
550 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
551 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800552}
553
554DEF_TEST(surface_rowbytes, reporter) {
555 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
556
reede8f30622016-03-23 18:59:25 -0700557 auto surf0(SkSurface::MakeRaster(info));
558 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800559
560 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700561 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
562 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800563
564 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700565 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800566 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700567 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800568 REPORTER_ASSERT(reporter, nullptr == s);
569}
bsalomone63ffef2016-02-05 07:17:34 -0800570
fmalita03912f12016-07-06 06:22:06 -0700571DEF_TEST(surface_raster_zeroinitialized, reporter) {
572 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
573 SkPixmap pixmap;
574 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
575
576 for (int i = 0; i < pixmap.info().width(); ++i) {
577 for (int j = 0; j < pixmap.info().height(); ++j) {
578 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
579 }
580 }
581}
582
bsalomone63ffef2016-02-05 07:17:34 -0800583#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700584static sk_sp<SkSurface> create_gpu_surface_backend_texture(
585 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
586 const int kWidth = 10;
587 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400588 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700589 sk_memset32(pixels.get(), color, kWidth * kHeight);
590 GrBackendTextureDesc desc;
591 desc.fConfig = kRGBA_8888_GrPixelConfig;
592 desc.fWidth = kWidth;
593 desc.fHeight = kHeight;
594 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
595 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700596 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700597 desc.fSampleCnt = sampleCnt;
598 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
599 if (!surface) {
600 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
601 return nullptr;
602 }
603 *outTexture = desc.fTextureHandle;
604 return surface;
605}
bsalomone63ffef2016-02-05 07:17:34 -0800606
ericrkc4025182016-05-04 12:01:58 -0700607static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
608 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
609 const int kWidth = 10;
610 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400611 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700612 sk_memset32(pixels.get(), color, kWidth * kHeight);
613 GrBackendTextureDesc desc;
614 desc.fConfig = kRGBA_8888_GrPixelConfig;
615 desc.fWidth = kWidth;
616 desc.fHeight = kHeight;
617 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
618 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700619 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700620 desc.fSampleCnt = sampleCnt;
621 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
622 nullptr);
623 if (!surface) {
624 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
625 return nullptr;
626 }
627 *outTexture = desc.fTextureHandle;
628 return surface;
629}
630
631static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500632 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700633 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800634 if (!surface) {
635 ERRORF(reporter, "Could not create GPU SkSurface.");
636 return;
637 }
638 int w = surface->width();
639 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400640 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700641 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800642
Robert Phillips2c862492017-01-18 10:08:39 -0500643 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
644 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800645 ERRORF(reporter, "Could access render target of GPU SkSurface.");
646 return;
647 }
bsalomon2fba8092016-02-05 13:47:06 -0800648 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500649
650 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
651 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800652 for (int y = 0; y < h; ++y) {
653 for (int x = 0; x < w; ++x) {
654 uint32_t pixel = pixels.get()[y * w + x];
655 if (pixel != expectedValue) {
656 SkString msg;
657 if (expectedValue) {
658 msg = "SkSurface should have left render target unmodified";
659 } else {
660 msg = "SkSurface should have cleared the render target";
661 }
662 ERRORF(reporter,
663 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
664 expectedValue, x, y);
665 return;
666 }
667 }
668 }
669}
670
bsalomon758586c2016-04-06 14:02:39 -0700671DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700672 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700673
Robert Phillips2c862492017-01-18 10:08:39 -0500674 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700675 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500676 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
677 },
678 [] (SkSurface* s){
679 sk_sp<SkImage> i(s->makeImageSnapshot());
680 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500681 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500682 GrContext* context = gpuImage->context();
683 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
684 gpuImage->refColorSpace());
685 }
bsalomone63ffef2016-02-05 07:17:34 -0800686 };
ericrkc4025182016-05-04 12:01:58 -0700687
Robert Phillips2c862492017-01-18 10:08:39 -0500688 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700689 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800690 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700691 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800692 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
693 }
694 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700695 const uint32_t kOrigColor = 0xABABABAB;
696 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
697 &create_gpu_surface_backend_texture_as_render_target}) {
698 GrBackendObject textureObject;
699 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
700 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
701 surface.reset();
702 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
703 }
704 }
705}
bsalomone63ffef2016-02-05 07:17:34 -0800706
ericrkc4025182016-05-04 12:01:58 -0700707static void test_surface_draw_partially(
708 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
709 const int kW = surface->width();
710 const int kH = surface->height();
711 SkPaint paint;
712 const SkColor kRectColor = ~origColor | 0xFF000000;
713 paint.setColor(kRectColor);
714 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
715 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400716 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700717 sk_memset32(pixels.get(), ~origColor, kW * kH);
718 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
719 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
720 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
721 bool stop = false;
722 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
723 (origColor >> 0 & 0xFF),
724 (origColor >> 8 & 0xFF),
725 (origColor >> 16 & 0xFF));
726 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
727 (kRectColor >> 16 & 0xFF),
728 (kRectColor >> 8 & 0xFF),
729 (kRectColor >> 0 & 0xFF));
730 for (int y = 0; y < kH/2 && !stop; ++y) {
731 for (int x = 0; x < kW && !stop; ++x) {
732 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
733 if (rectColorPM != pixels[x + y * kW]) {
734 stop = true;
735 }
736 }
737 }
738 stop = false;
739 for (int y = kH/2; y < kH && !stop; ++y) {
740 for (int x = 0; x < kW && !stop; ++x) {
741 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
742 if (origColorPM != pixels[x + y * kW]) {
743 stop = true;
744 }
745 }
746 }
747}
bsalomone63ffef2016-02-05 07:17:34 -0800748
egdanielab527a52016-06-28 08:07:26 -0700749DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700750 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700751 if (!gpu) {
752 return;
753 }
754 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800755
ericrkc4025182016-05-04 12:01:58 -0700756 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
757 &create_gpu_surface_backend_texture_as_render_target}) {
758 // Validate that we can draw to the canvas and that the original texture color is
759 // preserved in pixels that aren't rendered to via the surface.
760 // This works only for non-multisampled case.
761 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700762 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700763 if (surface) {
764 test_surface_draw_partially(reporter, surface, kOrigColor);
765 surface.reset();
766 gpu->deleteTestingOnlyBackendTexture(textureObject);
767 }
768 }
769}
770
771
772DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700773 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700774 if (!gpu) {
775 return;
776 }
777 static const uint32_t kOrigColor = 0xFFAABBCC;
778
779 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
780 &create_gpu_surface_backend_texture_as_render_target}) {
781 for (int sampleCnt : {0, 4, 8}) {
782 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700783 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700784
785 if (!surface && sampleCnt > 0) {
786 // Certain platforms don't support MSAA, skip these.
787 continue;
788 }
789
790 // Validate that we can attach a stencil buffer to an SkSurface created by either of
791 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400792 GrRenderTarget* rt = surface->getCanvas()
793 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700794 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700795 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700796 gpu->deleteTestingOnlyBackendTexture(textureObject);
797 }
bsalomone63ffef2016-02-05 07:17:34 -0800798 }
799}
800#endif
brianosman0e22eb82016-08-30 07:07:59 -0700801
802static void test_surface_creation_and_snapshot_with_color_space(
803 skiatest::Reporter* reporter,
804 const char* prefix,
805 bool f16Support,
806 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
807
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500808 auto srgbColorSpace = SkColorSpace::MakeSRGB();
809 auto adobeColorSpace = SkColorSpace_Base::MakeNamed(SkColorSpace_Base::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700810 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
811 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400812 SkColorSpaceTransferFn oddGamma;
813 oddGamma.fA = 1.0f;
814 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
815 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400816 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500817 auto linearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosman0e22eb82016-08-30 07:07:59 -0700818
819 const struct {
820 SkColorType fColorType;
821 sk_sp<SkColorSpace> fColorSpace;
822 bool fShouldWork;
823 const char* fDescription;
824 } testConfigs[] = {
825 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
826 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
827 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
828 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
829 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Brian Osmaneb21ef62016-11-01 16:30:21 -0400830 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700831 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
832 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
833 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
834 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
835 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
836 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
837 };
838
839 for (auto& testConfig : testConfigs) {
840 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
841 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
842 testConfig.fColorSpace);
843
844 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
845 // any surface of that type:
846 bool shouldWork = testConfig.fShouldWork &&
847 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
848
849 auto surface(surfaceMaker(info));
850 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
851
852 if (shouldWork && surface) {
853 sk_sp<SkImage> image(surface->makeImageSnapshot());
854 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
855 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
856 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
857 fullTestName.c_str());
858 }
859 }
860}
861
862DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
863 auto surfaceMaker = [](const SkImageInfo& info) {
864 return SkSurface::MakeRaster(info);
865 };
866
867 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
868}
869
870#if SK_SUPPORT_GPU
871DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
872 GrContext* context = ctxInfo.grContext();
873 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
874 auto surfaceMaker = [context](const SkImageInfo& info) {
875 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
876 };
877
878 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
879
880 std::vector<GrBackendObject> textureHandles;
881 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
882 GrBackendTextureDesc desc;
883 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
884 desc.fWidth = 10;
885 desc.fHeight = 10;
886 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
887 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
888 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
889
890 if (!desc.fTextureHandle) {
891 return sk_sp<SkSurface>(nullptr);
892 }
893 textureHandles.push_back(desc.fTextureHandle);
894
895 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
896 nullptr);
897 };
898
899 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
900 wrappedSurfaceMaker);
901
902 for (auto textureHandle : textureHandles) {
903 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
904 }
905}
906#endif
Matt Sarett22886c42016-11-22 11:31:41 -0500907
908static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -0500909 SkOverdrawCanvas canvas(surface->getCanvas());
910 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -0500911 sk_sp<SkImage> image = surface->makeImageSnapshot();
912
913 SkBitmap bitmap;
914 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
915 bitmap.lockPixels();
916 for (int y = 0; y < 10; y++) {
917 for (int x = 0; x < 10; x++) {
918 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
919 }
920 }
921}
922
923DEF_TEST(OverdrawSurface_Raster, r) {
924 sk_sp<SkSurface> surface = create_surface();
925 test_overdraw_surface(r, surface.get());
926}
927
928#if SK_SUPPORT_GPU
929DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
930 GrContext* context = ctxInfo.grContext();
931 sk_sp<SkSurface> surface = create_gpu_surface(context);
932 test_overdraw_surface(r, surface.get());
933}
934#endif