blob: 2097ab3fa6565909a4a1a9a5e1fb3bdff8a68e6d [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"
Robert Phillips2c862492017-01-18 10:08:39 -050023#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040024#include "GrRenderTargetContext.h"
kkinnunen179a8f52015-11-20 13:32:24 -080025#include "GrGpu.h"
ericrkc4025182016-05-04 12:01:58 -070026#include "GrResourceProvider.h"
brianosman0e22eb82016-08-30 07:07:59 -070027#include <vector>
junov@chromium.org995beb62013-03-28 13:49:22 +000028#endif
29
kkinnunen179a8f52015-11-20 13:32:24 -080030#include <initializer_list>
bsalomon74f681d2015-06-23 14:38:48 -070031
kkinnunen179a8f52015-11-20 13:32:24 -080032static void release_direct_surface_storage(void* pixels, void* context) {
reed982542d2014-06-27 06:48:14 -070033 SkASSERT(pixels == context);
34 sk_free(pixels);
35}
reede8f30622016-03-23 18:59:25 -070036static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType,
37 SkImageInfo* requestedInfo = nullptr) {
bsalomon74f681d2015-06-23 14:38:48 -070038 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000039 if (requestedInfo) {
40 *requestedInfo = info;
41 }
reede8f30622016-03-23 18:59:25 -070042 return SkSurface::MakeRaster(info);
junov@chromium.org995beb62013-03-28 13:49:22 +000043}
reede8f30622016-03-23 18:59:25 -070044static sk_sp<SkSurface> create_direct_surface(SkAlphaType at = kPremul_SkAlphaType,
45 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080046 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
47 if (requestedInfo) {
48 *requestedInfo = info;
49 }
50 const size_t rowBytes = info.minRowBytes();
51 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
reede8f30622016-03-23 18:59:25 -070052 return SkSurface::MakeRasterDirectReleaseProc(info, storage, rowBytes,
53 release_direct_surface_storage,
54 storage);
kkinnunen179a8f52015-11-20 13:32:24 -080055}
56#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -070057static sk_sp<SkSurface> create_gpu_surface(GrContext* context, SkAlphaType at = kPremul_SkAlphaType,
58 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080059 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
60 if (requestedInfo) {
61 *requestedInfo = info;
62 }
robertphillips7e922762016-07-26 11:38:17 -070063 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
kkinnunen179a8f52015-11-20 13:32:24 -080064}
reede8f30622016-03-23 18:59:25 -070065static sk_sp<SkSurface> create_gpu_scratch_surface(GrContext* context,
66 SkAlphaType at = kPremul_SkAlphaType,
67 SkImageInfo* requestedInfo = nullptr) {
kkinnunen179a8f52015-11-20 13:32:24 -080068 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
69 if (requestedInfo) {
70 *requestedInfo = info;
71 }
robertphillips7e922762016-07-26 11:38:17 -070072 return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
kkinnunen179a8f52015-11-20 13:32:24 -080073}
74#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000075
kkinnunen179a8f52015-11-20 13:32:24 -080076DEF_TEST(SurfaceEmpty, reporter) {
reedb2497c22014-12-31 12:31:43 -080077 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070078 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRaster(info));
79 REPORTER_ASSERT(reporter, nullptr == SkSurface::MakeRasterDirect(info, nullptr, 0));
kkinnunen179a8f52015-11-20 13:32:24 -080080
reedb2497c22014-12-31 12:31:43 -080081}
kkinnunen179a8f52015-11-20 13:32:24 -080082#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -070083DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -080084 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
85 REPORTER_ASSERT(reporter, nullptr ==
robertphillips7e922762016-07-26 11:38:17 -070086 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
kkinnunen179a8f52015-11-20 13:32:24 -080087}
88#endif
reedb2497c22014-12-31 12:31:43 -080089
kkinnunen179a8f52015-11-20 13:32:24 -080090static void test_canvas_peek(skiatest::Reporter* reporter,
reede8f30622016-03-23 18:59:25 -070091 sk_sp<SkSurface>& surface,
kkinnunen179a8f52015-11-20 13:32:24 -080092 const SkImageInfo& requestInfo,
93 bool expectPeekSuccess) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000094 const SkColor color = SK_ColorRED;
95 const SkPMColor pmcolor = SkPreMultiplyColor(color);
kkinnunen179a8f52015-11-20 13:32:24 -080096 surface->getCanvas()->clear(color);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000097
reed6ceeebd2016-03-09 14:26:26 -080098 SkPixmap pmap;
99 bool success = surface->getCanvas()->peekPixels(&pmap);
kkinnunen179a8f52015-11-20 13:32:24 -0800100 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000101
reed6ceeebd2016-03-09 14:26:26 -0800102 SkPixmap pmap2;
103 const void* addr2 = surface->peekPixels(&pmap2) ? pmap2.addr() : nullptr;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000104
kkinnunen179a8f52015-11-20 13:32:24 -0800105 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800106 REPORTER_ASSERT(reporter, requestInfo == pmap.info());
107 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= pmap.rowBytes());
108 REPORTER_ASSERT(reporter, pmcolor == *pmap.addr32());
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000109
reed6ceeebd2016-03-09 14:26:26 -0800110 REPORTER_ASSERT(reporter, pmap.addr() == pmap2.addr());
111 REPORTER_ASSERT(reporter, pmap.info() == pmap2.info());
112 REPORTER_ASSERT(reporter, pmap.rowBytes() == pmap2.rowBytes());
kkinnunen179a8f52015-11-20 13:32:24 -0800113 } else {
114 REPORTER_ASSERT(reporter, nullptr == addr2);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000115 }
116}
kkinnunen179a8f52015-11-20 13:32:24 -0800117DEF_TEST(SurfaceCanvasPeek, reporter) {
118 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
119 SkImageInfo requestInfo;
reede8f30622016-03-23 18:59:25 -0700120 auto surface(surface_func(kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800121 test_canvas_peek(reporter, surface, requestInfo, true);
122 }
123}
124#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700125DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800126 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
127 SkImageInfo requestInfo;
bsalomon8b7451a2016-05-11 06:33:06 -0700128 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, &requestInfo));
kkinnunen179a8f52015-11-20 13:32:24 -0800129 test_canvas_peek(reporter, surface, requestInfo, false);
130 }
131}
132#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000133
reede8f30622016-03-23 18:59:25 -0700134static void test_snapshot_alphatype(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
brianosman69c166d2016-08-17 14:01:05 -0700135 SkAlphaType expectedAlphaType) {
kkinnunen179a8f52015-11-20 13:32:24 -0800136 REPORTER_ASSERT(reporter, surface);
137 if (surface) {
reed9ce9d672016-03-17 10:51:11 -0700138 sk_sp<SkImage> image(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800139 REPORTER_ASSERT(reporter, image);
140 if (image) {
brianosman69c166d2016-08-17 14:01:05 -0700141 REPORTER_ASSERT(reporter, image->alphaType() == expectedAlphaType);
reed41e010c2015-06-09 12:16:53 -0700142 }
143 }
144}
kkinnunen179a8f52015-11-20 13:32:24 -0800145DEF_TEST(SurfaceSnapshotAlphaType, reporter) {
146 for (auto& surface_func : { &create_surface, &create_direct_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700147 for (auto& at: { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
148 auto surface(surface_func(at, nullptr));
149 test_snapshot_alphatype(reporter, surface, at);
bsalomon74f681d2015-06-23 14:38:48 -0700150 }
151 }
152}
kkinnunen179a8f52015-11-20 13:32:24 -0800153#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700154DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800155 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
brianosman69c166d2016-08-17 14:01:05 -0700156 // GPU doesn't support creating unpremul surfaces, so only test opaque + premul
157 for (auto& at : { kOpaque_SkAlphaType, kPremul_SkAlphaType }) {
158 auto surface(surface_func(ctxInfo.grContext(), at, nullptr));
159 test_snapshot_alphatype(reporter, surface, at);
kkinnunen179a8f52015-11-20 13:32:24 -0800160 }
161 }
162}
163#endif
bsalomon74f681d2015-06-23 14:38:48 -0700164
kkinnunen179a8f52015-11-20 13:32:24 -0800165static GrBackendObject get_surface_backend_texture_handle(
166 SkSurface* s, SkSurface::BackendHandleAccess a) {
167 return s->getTextureHandle(a);
168}
169static GrBackendObject get_surface_backend_render_target_handle(
170 SkSurface* s, SkSurface::BackendHandleAccess a) {
171 GrBackendObject result;
172 if (!s->getRenderTargetHandle(&result, a)) {
173 return 0;
174 }
175 return result;
176}
177
178static void test_backend_handle_access_copy_on_write(
179 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAccess mode,
180 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
fmalitae2639082015-08-06 07:04:51 -0700181 GrBackendObject obj1 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700182 sk_sp<SkImage> snap1(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700183
184 GrBackendObject obj2 = func(surface, mode);
reed9ce9d672016-03-17 10:51:11 -0700185 sk_sp<SkImage> snap2(surface->makeImageSnapshot());
fmalitae2639082015-08-06 07:04:51 -0700186
187 // If the access mode triggers CoW, then the backend objects should reflect it.
188 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
189}
kkinnunen179a8f52015-11-20 13:32:24 -0800190DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) {
191 const SkSurface::BackendHandleAccess accessModes[] = {
192 SkSurface::kFlushRead_BackendHandleAccess,
193 SkSurface::kFlushWrite_BackendHandleAccess,
194 SkSurface::kDiscardWrite_BackendHandleAccess,
195 };
196 for (auto& handle_access_func :
197 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
198 for (auto& accessMode : accessModes) {
reede8f30622016-03-23 18:59:25 -0700199 auto surface(create_surface());
200 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800201 handle_access_func);
202 }
203 }
204}
205#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700206DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800207 const SkSurface::BackendHandleAccess accessModes[] = {
208 SkSurface::kFlushRead_BackendHandleAccess,
209 SkSurface::kFlushWrite_BackendHandleAccess,
210 SkSurface::kDiscardWrite_BackendHandleAccess,
211 };
212 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
213 for (auto& handle_access_func :
214 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle }) {
215 for (auto& accessMode : accessModes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700216 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700217 test_backend_handle_access_copy_on_write(reporter, surface.get(), accessMode,
kkinnunen179a8f52015-11-20 13:32:24 -0800218 handle_access_func);
219 }
220 }
221 }
222}
223#endif
fmalitae2639082015-08-06 07:04:51 -0700224
kkinnunen179a8f52015-11-20 13:32:24 -0800225#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800226
kkinnunen179a8f52015-11-20 13:32:24 -0800227static void test_backend_handle_unique_id(
228 skiatest::Reporter* reporter, SkSurface* surface,
229 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
reed9ce9d672016-03-17 10:51:11 -0700230 sk_sp<SkImage> image0(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800231 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAccess);
232 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700233 sk_sp<SkImage> image1(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800234 // just read access should not affect the snapshot
235 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
236
237 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess);
238 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700239 sk_sp<SkImage> image2(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800240 // expect a new image, since we claimed we would write
241 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
242
243 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess);
244 REPORTER_ASSERT(reporter, obj != 0);
reed9ce9d672016-03-17 10:51:11 -0700245 sk_sp<SkImage> image3(surface->makeImageSnapshot());
kkinnunen179a8f52015-11-20 13:32:24 -0800246 // expect a new(er) image, since we claimed we would write
247 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
248 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
249}
250// No CPU test.
bsalomon68d91342016-04-12 09:59:58 -0700251DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800252 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
robertphillips1f3923e2016-07-21 07:17:54 -0700253 for (auto& test_func : { &test_backend_handle_unique_id }) {
kkinnunen179a8f52015-11-20 13:32:24 -0800254 for (auto& handle_access_func :
255 { &get_surface_backend_texture_handle, &get_surface_backend_render_target_handle}) {
bsalomon8b7451a2016-05-11 06:33:06 -0700256 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700257 test_func(reporter, surface.get(), handle_access_func);
kkinnunen179a8f52015-11-20 13:32:24 -0800258 }
259 }
260 }
261}
262#endif
263
264// Verify that the right canvas commands trigger a copy on write.
265static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) {
junov@chromium.org995beb62013-03-28 13:49:22 +0000266 SkCanvas* canvas = surface->getCanvas();
267
268 const SkRect testRect =
269 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
270 SkIntToScalar(4), SkIntToScalar(5));
junov@chromium.org995beb62013-03-28 13:49:22 +0000271 SkPath testPath;
272 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
273 SkIntToScalar(2), SkIntToScalar(1)));
274
275 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
276
277 SkRegion testRegion;
278 testRegion.setRect(testIRect);
279
280
281 const SkColor testColor = 0x01020304;
282 const SkPaint testPaint;
283 const SkPoint testPoints[3] = {
284 {SkIntToScalar(0), SkIntToScalar(0)},
285 {SkIntToScalar(2), SkIntToScalar(1)},
286 {SkIntToScalar(0), SkIntToScalar(2)}
287 };
288 const size_t testPointCount = 3;
289
290 SkBitmap testBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000291 testBitmap.allocN32Pixels(10, 10);
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000292 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000293
294 SkRRect testRRect;
295 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
296
297 SkString testText("Hello World");
298 const SkPoint testPoints2[] = {
299 { SkIntToScalar(0), SkIntToScalar(1) },
300 { SkIntToScalar(1), SkIntToScalar(1) },
301 { SkIntToScalar(2), SkIntToScalar(1) },
302 { SkIntToScalar(3), SkIntToScalar(1) },
303 { SkIntToScalar(4), SkIntToScalar(1) },
304 { SkIntToScalar(5), SkIntToScalar(1) },
305 { SkIntToScalar(6), SkIntToScalar(1) },
306 { SkIntToScalar(7), SkIntToScalar(1) },
307 { SkIntToScalar(8), SkIntToScalar(1) },
308 { SkIntToScalar(9), SkIntToScalar(1) },
309 { SkIntToScalar(10), SkIntToScalar(1) },
310 };
311
312#define EXPECT_COPY_ON_WRITE(command) \
313 { \
reed9ce9d672016-03-17 10:51:11 -0700314 sk_sp<SkImage> imageBefore = surface->makeImageSnapshot(); \
315 sk_sp<SkImage> aur_before(imageBefore); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000316 canvas-> command ; \
reed9ce9d672016-03-17 10:51:11 -0700317 sk_sp<SkImage> imageAfter = surface->makeImageSnapshot(); \
318 sk_sp<SkImage> aur_after(imageAfter); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000319 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
320 }
321
322 EXPECT_COPY_ON_WRITE(clear(testColor))
323 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
324 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
325 testPaint))
326 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
327 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
328 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
329 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
330 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
reede47829b2015-08-06 10:02:53 -0700331 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
halcanary96fcdcc2015-08-27 07:41:13 -0700332 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
junov@chromium.org995beb62013-03-28 13:49:22 +0000333 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
334 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
335 testPaint))
halcanary96fcdcc2015-08-27 07:41:13 -0700336 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
junov@chromium.org995beb62013-03-28 13:49:22 +0000337 testPaint))
kkinnunen179a8f52015-11-20 13:32:24 -0800338}
339DEF_TEST(SurfaceCopyOnWrite, reporter) {
reede8f30622016-03-23 18:59:25 -0700340 test_copy_on_write(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800341}
342#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700343DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800344 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700345 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700346 test_copy_on_write(reporter, surface.get());
fmalitae2639082015-08-06 07:04:51 -0700347 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000348}
kkinnunen179a8f52015-11-20 13:32:24 -0800349#endif
junov@chromium.org995beb62013-03-28 13:49:22 +0000350
kkinnunen179a8f52015-11-20 13:32:24 -0800351static void test_writable_after_snapshot_release(skiatest::Reporter* reporter,
352 SkSurface* surface) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000353 // This test succeeds by not triggering an assertion.
354 // The test verifies that the surface remains writable (usable) after
355 // acquiring and releasing a snapshot without triggering a copy on write.
junov@chromium.orgaf058352013-04-03 15:03:26 +0000356 SkCanvas* canvas = surface->getCanvas();
357 canvas->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700358 surface->makeImageSnapshot(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000359 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000360}
kkinnunen179a8f52015-11-20 13:32:24 -0800361DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) {
reede8f30622016-03-23 18:59:25 -0700362 test_writable_after_snapshot_release(reporter, create_surface().get());
kkinnunen179a8f52015-11-20 13:32:24 -0800363}
364#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700365DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800366 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700367 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700368 test_writable_after_snapshot_release(reporter, surface.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800369 }
370}
371#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000372
junov@chromium.orgb516a412013-05-01 22:49:59 +0000373#if SK_SUPPORT_GPU
kkinnunen179a8f52015-11-20 13:32:24 -0800374static void test_crbug263329(skiatest::Reporter* reporter,
375 SkSurface* surface1,
376 SkSurface* surface2) {
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000377 // This is a regression test for crbug.com/263329
378 // Bug was caused by onCopyOnWrite releasing the old surface texture
379 // back to the scratch texture pool even though the texture is used
380 // by and active SkImage_Gpu.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000381 SkCanvas* canvas1 = surface1->getCanvas();
382 SkCanvas* canvas2 = surface2->getCanvas();
383 canvas1->clear(1);
reed9ce9d672016-03-17 10:51:11 -0700384 sk_sp<SkImage> image1(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000385 // Trigger copy on write, new backing is a scratch texture
386 canvas1->clear(2);
reed9ce9d672016-03-17 10:51:11 -0700387 sk_sp<SkImage> image2(surface1->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000388 // Trigger copy on write, old backing should not be returned to scratch
389 // pool because it is held by image2
390 canvas1->clear(3);
391
392 canvas2->clear(4);
reed9ce9d672016-03-17 10:51:11 -0700393 sk_sp<SkImage> image3(surface2->makeImageSnapshot());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000394 // Trigger copy on write on surface2. The new backing store should not
395 // be recycling a texture that is held by an existing image.
396 canvas2->clear(5);
reed9ce9d672016-03-17 10:51:11 -0700397 sk_sp<SkImage> image4(surface2->makeImageSnapshot());
bsalomon84a4e5a2016-02-29 11:41:52 -0800398 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image3)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000399 // The following assertion checks crbug.com/263329
bsalomon84a4e5a2016-02-29 11:41:52 -0800400 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image2)->peekTexture());
401 REPORTER_ASSERT(reporter, as_IB(image4)->peekTexture() != as_IB(image1)->peekTexture());
402 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image2)->peekTexture());
403 REPORTER_ASSERT(reporter, as_IB(image3)->peekTexture() != as_IB(image1)->peekTexture());
404 REPORTER_ASSERT(reporter, as_IB(image2)->peekTexture() != as_IB(image1)->peekTexture());
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000405}
egdanielab527a52016-06-28 08:07:26 -0700406DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800407 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700408 auto surface1(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
409 auto surface2(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700410 test_crbug263329(reporter, surface1.get(), surface2.get());
kkinnunen179a8f52015-11-20 13:32:24 -0800411 }
412}
413#endif
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000414
kkinnunen179a8f52015-11-20 13:32:24 -0800415DEF_TEST(SurfaceGetTexture, reporter) {
reede8f30622016-03-23 18:59:25 -0700416 auto surface(create_surface());
reed9ce9d672016-03-17 10:51:11 -0700417 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500418 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800419 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500420 REPORTER_ASSERT(reporter, !as_IB(image)->isTextureBacked());
kkinnunen179a8f52015-11-20 13:32:24 -0800421}
422#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700423DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacepeekTexture_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800424 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700425 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reed9ce9d672016-03-17 10:51:11 -0700426 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6de99042017-01-31 11:31:39 -0500427
428 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
429 GrBackendObject textureHandle = image->getTextureHandle(false);
430 REPORTER_ASSERT(reporter, 0 != textureHandle);
kkinnunen179a8f52015-11-20 13:32:24 -0800431 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
Robert Phillips6de99042017-01-31 11:31:39 -0500432 REPORTER_ASSERT(reporter, as_IB(image)->isTextureBacked());
433 REPORTER_ASSERT(reporter, textureHandle == image->getTextureHandle(false));
junov@chromium.orgda904742013-05-01 22:38:16 +0000434 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000435}
kkinnunen179a8f52015-11-20 13:32:24 -0800436#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800437
kkinnunen179a8f52015-11-20 13:32:24 -0800438#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800439#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800440#include "SkGpuDevice.h"
441#include "SkImage_Gpu.h"
442#include "SkSurface_Gpu.h"
443
reede8f30622016-03-23 18:59:25 -0700444static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
445 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Robert Phillips6de99042017-01-31 11:31:39 -0500446
447 GrRenderTargetProxy* proxy = gsurf->getDevice()->accessRenderTargetContext()
448 ->asRenderTargetProxy();
449 return proxy->isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800450}
451
bsalomon5ec26ae2016-02-25 08:33:02 -0800452static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800453 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800454}
455
reed9ce9d672016-03-17 10:51:11 -0700456static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
457 return is_budgeted(image.get());
458}
459
egdanielab527a52016-06-28 08:07:26 -0700460DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800461 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800462 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
463 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700464 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800465 SkASSERT(surface);
466 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
467
reed9ce9d672016-03-17 10:51:11 -0700468 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800469
470 // Initially the image shares a texture with the surface, and the surface decides
471 // whether it is budgeted or not.
472 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
473 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
474
475 // Now trigger copy-on-write
476 surface->getCanvas()->clear(SK_ColorBLUE);
477
478 // They don't share a texture anymore. They should each have made their own budget
479 // decision.
480 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
481 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
482 }
483 }
484}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000485#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000486
kkinnunen179a8f52015-11-20 13:32:24 -0800487static void test_no_canvas1(skiatest::Reporter* reporter,
488 SkSurface* surface,
489 SkSurface::ContentChangeMode mode) {
490 // Test passes by not asserting
491 surface->notifyContentWillChange(mode);
492 SkDEBUGCODE(surface->validate();)
493}
494static void test_no_canvas2(skiatest::Reporter* reporter,
495 SkSurface* surface,
496 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000497 // Verifies the robustness of SkSurface for handling use cases where calls
498 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700499 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
500 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800501 SkDEBUGCODE(image1->validate();)
502 SkDEBUGCODE(surface->validate();)
503 surface->notifyContentWillChange(mode);
504 SkDEBUGCODE(image1->validate();)
505 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700506 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
507 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800508 SkDEBUGCODE(image2->validate();)
509 SkDEBUGCODE(surface->validate();)
510 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000511}
kkinnunen179a8f52015-11-20 13:32:24 -0800512DEF_TEST(SurfaceNoCanvas, reporter) {
513 SkSurface::ContentChangeMode modes[] =
514 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
515 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
516 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700517 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800518 }
519 }
520}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000521#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700522DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800523 SkSurface::ContentChangeMode modes[] =
524 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
525 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
526 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
527 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700528 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700529 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700530 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000531 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000532 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000533}
kkinnunen179a8f52015-11-20 13:32:24 -0800534#endif
reed9cd016e2016-01-30 10:01:06 -0800535
536static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800537 SkPixmap surfacePM;
538 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800539
reed9ce9d672016-03-17 10:51:11 -0700540 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800541 SkPixmap pm;
542 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800543
reed6ceeebd2016-03-09 14:26:26 -0800544 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800545
546 // trigger a copy-on-write
547 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700548 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800549 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
550
reed6ceeebd2016-03-09 14:26:26 -0800551 SkPixmap pm2;
552 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
553 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800554}
555
556DEF_TEST(surface_rowbytes, reporter) {
557 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
558
reede8f30622016-03-23 18:59:25 -0700559 auto surf0(SkSurface::MakeRaster(info));
560 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800561
562 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700563 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
564 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800565
566 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700567 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800568 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700569 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800570 REPORTER_ASSERT(reporter, nullptr == s);
571}
bsalomone63ffef2016-02-05 07:17:34 -0800572
fmalita03912f12016-07-06 06:22:06 -0700573DEF_TEST(surface_raster_zeroinitialized, reporter) {
574 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
575 SkPixmap pixmap;
576 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
577
578 for (int i = 0; i < pixmap.info().width(); ++i) {
579 for (int j = 0; j < pixmap.info().height(); ++j) {
580 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
581 }
582 }
583}
584
bsalomone63ffef2016-02-05 07:17:34 -0800585#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700586static sk_sp<SkSurface> create_gpu_surface_backend_texture(
587 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
588 const int kWidth = 10;
589 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400590 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700591 sk_memset32(pixels.get(), color, kWidth * kHeight);
592 GrBackendTextureDesc desc;
593 desc.fConfig = kRGBA_8888_GrPixelConfig;
594 desc.fWidth = kWidth;
595 desc.fHeight = kHeight;
596 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
597 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700598 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700599 desc.fSampleCnt = sampleCnt;
600 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
601 if (!surface) {
602 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
603 return nullptr;
604 }
605 *outTexture = desc.fTextureHandle;
606 return surface;
607}
bsalomone63ffef2016-02-05 07:17:34 -0800608
ericrkc4025182016-05-04 12:01:58 -0700609static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
610 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
611 const int kWidth = 10;
612 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400613 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700614 sk_memset32(pixels.get(), color, kWidth * kHeight);
615 GrBackendTextureDesc desc;
616 desc.fConfig = kRGBA_8888_GrPixelConfig;
617 desc.fWidth = kWidth;
618 desc.fHeight = kHeight;
619 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
620 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700621 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700622 desc.fSampleCnt = sampleCnt;
623 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
624 nullptr);
625 if (!surface) {
626 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
627 return nullptr;
628 }
629 *outTexture = desc.fTextureHandle;
630 return surface;
631}
632
633static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500634 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700635 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800636 if (!surface) {
637 ERRORF(reporter, "Could not create GPU SkSurface.");
638 return;
639 }
640 int w = surface->width();
641 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400642 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700643 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800644
Robert Phillips2c862492017-01-18 10:08:39 -0500645 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
646 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800647 ERRORF(reporter, "Could access render target of GPU SkSurface.");
648 return;
649 }
bsalomon2fba8092016-02-05 13:47:06 -0800650 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500651
652 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
653 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800654 for (int y = 0; y < h; ++y) {
655 for (int x = 0; x < w; ++x) {
656 uint32_t pixel = pixels.get()[y * w + x];
657 if (pixel != expectedValue) {
658 SkString msg;
659 if (expectedValue) {
660 msg = "SkSurface should have left render target unmodified";
661 } else {
662 msg = "SkSurface should have cleared the render target";
663 }
664 ERRORF(reporter,
665 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
666 expectedValue, x, y);
667 return;
668 }
669 }
670 }
671}
672
bsalomon758586c2016-04-06 14:02:39 -0700673DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700674 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700675
Robert Phillips2c862492017-01-18 10:08:39 -0500676 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700677 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500678 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
679 },
680 [] (SkSurface* s){
681 sk_sp<SkImage> i(s->makeImageSnapshot());
682 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
Robert Phillips6de99042017-01-31 11:31:39 -0500683 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef();
Robert Phillips2c862492017-01-18 10:08:39 -0500684 GrContext* context = gpuImage->context();
685 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
686 gpuImage->refColorSpace());
687 }
bsalomone63ffef2016-02-05 07:17:34 -0800688 };
ericrkc4025182016-05-04 12:01:58 -0700689
Robert Phillips2c862492017-01-18 10:08:39 -0500690 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700691 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800692 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700693 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800694 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
695 }
696 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700697 const uint32_t kOrigColor = 0xABABABAB;
698 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
699 &create_gpu_surface_backend_texture_as_render_target}) {
700 GrBackendObject textureObject;
701 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
702 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
703 surface.reset();
704 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
705 }
706 }
707}
bsalomone63ffef2016-02-05 07:17:34 -0800708
ericrkc4025182016-05-04 12:01:58 -0700709static void test_surface_draw_partially(
710 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
711 const int kW = surface->width();
712 const int kH = surface->height();
713 SkPaint paint;
714 const SkColor kRectColor = ~origColor | 0xFF000000;
715 paint.setColor(kRectColor);
716 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
717 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400718 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700719 sk_memset32(pixels.get(), ~origColor, kW * kH);
720 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
721 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
722 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
723 bool stop = false;
724 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
725 (origColor >> 0 & 0xFF),
726 (origColor >> 8 & 0xFF),
727 (origColor >> 16 & 0xFF));
728 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
729 (kRectColor >> 16 & 0xFF),
730 (kRectColor >> 8 & 0xFF),
731 (kRectColor >> 0 & 0xFF));
732 for (int y = 0; y < kH/2 && !stop; ++y) {
733 for (int x = 0; x < kW && !stop; ++x) {
734 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
735 if (rectColorPM != pixels[x + y * kW]) {
736 stop = true;
737 }
738 }
739 }
740 stop = false;
741 for (int y = kH/2; y < kH && !stop; ++y) {
742 for (int x = 0; x < kW && !stop; ++x) {
743 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
744 if (origColorPM != pixels[x + y * kW]) {
745 stop = true;
746 }
747 }
748 }
749}
bsalomone63ffef2016-02-05 07:17:34 -0800750
egdanielab527a52016-06-28 08:07:26 -0700751DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700752 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700753 if (!gpu) {
754 return;
755 }
756 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800757
ericrkc4025182016-05-04 12:01:58 -0700758 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
759 &create_gpu_surface_backend_texture_as_render_target}) {
760 // Validate that we can draw to the canvas and that the original texture color is
761 // preserved in pixels that aren't rendered to via the surface.
762 // This works only for non-multisampled case.
763 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700764 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700765 if (surface) {
766 test_surface_draw_partially(reporter, surface, kOrigColor);
767 surface.reset();
768 gpu->deleteTestingOnlyBackendTexture(textureObject);
769 }
770 }
771}
772
773
774DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700775 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700776 if (!gpu) {
777 return;
778 }
779 static const uint32_t kOrigColor = 0xFFAABBCC;
780
781 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
782 &create_gpu_surface_backend_texture_as_render_target}) {
783 for (int sampleCnt : {0, 4, 8}) {
784 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700785 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700786
787 if (!surface && sampleCnt > 0) {
788 // Certain platforms don't support MSAA, skip these.
789 continue;
790 }
791
792 // Validate that we can attach a stencil buffer to an SkSurface created by either of
793 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400794 GrRenderTarget* rt = surface->getCanvas()
795 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700796 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700797 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700798 gpu->deleteTestingOnlyBackendTexture(textureObject);
799 }
bsalomone63ffef2016-02-05 07:17:34 -0800800 }
801}
802#endif
brianosman0e22eb82016-08-30 07:07:59 -0700803
804static void test_surface_creation_and_snapshot_with_color_space(
805 skiatest::Reporter* reporter,
806 const char* prefix,
807 bool f16Support,
808 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
809
Brian Osman526972e2016-10-24 09:24:02 -0400810 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
811 auto adobeColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700812 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
813 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400814 SkColorSpaceTransferFn oddGamma;
815 oddGamma.fA = 1.0f;
816 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
817 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400818 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
819 auto linearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosman0e22eb82016-08-30 07:07:59 -0700820
821 const struct {
822 SkColorType fColorType;
823 sk_sp<SkColorSpace> fColorSpace;
824 bool fShouldWork;
825 const char* fDescription;
826 } testConfigs[] = {
827 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
828 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
829 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
830 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
831 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Brian Osmaneb21ef62016-11-01 16:30:21 -0400832 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700833 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
834 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
835 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
836 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
837 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
838 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
839 };
840
841 for (auto& testConfig : testConfigs) {
842 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
843 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
844 testConfig.fColorSpace);
845
846 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
847 // any surface of that type:
848 bool shouldWork = testConfig.fShouldWork &&
849 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
850
851 auto surface(surfaceMaker(info));
852 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
853
854 if (shouldWork && surface) {
855 sk_sp<SkImage> image(surface->makeImageSnapshot());
856 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
857 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
858 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
859 fullTestName.c_str());
860 }
861 }
862}
863
864DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
865 auto surfaceMaker = [](const SkImageInfo& info) {
866 return SkSurface::MakeRaster(info);
867 };
868
869 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
870}
871
872#if SK_SUPPORT_GPU
873DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
874 GrContext* context = ctxInfo.grContext();
875 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
876 auto surfaceMaker = [context](const SkImageInfo& info) {
877 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
878 };
879
880 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
881
882 std::vector<GrBackendObject> textureHandles;
883 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
884 GrBackendTextureDesc desc;
885 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
886 desc.fWidth = 10;
887 desc.fHeight = 10;
888 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
889 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
890 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
891
892 if (!desc.fTextureHandle) {
893 return sk_sp<SkSurface>(nullptr);
894 }
895 textureHandles.push_back(desc.fTextureHandle);
896
897 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
898 nullptr);
899 };
900
901 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
902 wrappedSurfaceMaker);
903
904 for (auto textureHandle : textureHandles) {
905 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
906 }
907}
908#endif
Matt Sarett22886c42016-11-22 11:31:41 -0500909
910static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -0500911 SkOverdrawCanvas canvas(surface->getCanvas());
912 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -0500913 sk_sp<SkImage> image = surface->makeImageSnapshot();
914
915 SkBitmap bitmap;
916 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
917 bitmap.lockPixels();
918 for (int y = 0; y < 10; y++) {
919 for (int x = 0; x < 10; x++) {
920 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
921 }
922 }
923}
924
925DEF_TEST(OverdrawSurface_Raster, r) {
926 sk_sp<SkSurface> surface = create_surface();
927 test_overdraw_surface(r, surface.get());
928}
929
930#if SK_SUPPORT_GPU
931DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
932 GrContext* context = ctxInfo.grContext();
933 sk_sp<SkSurface> surface = create_gpu_surface(context);
934 test_overdraw_surface(r, surface.get());
935}
936#endif