blob: 29aee9ccf94de8eed9c1f0f9c67ccf60ee9a60b8 [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());
bsalomon84a4e5a2016-02-29 11:41:52 -0800418 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
kkinnunen179a8f52015-11-20 13:32:24 -0800419 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800420 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == nullptr);
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());
bsalomon84a4e5a2016-02-29 11:41:52 -0800427 GrTexture* texture = as_IB(image)->peekTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700428 REPORTER_ASSERT(reporter, texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000429 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
kkinnunen179a8f52015-11-20 13:32:24 -0800430 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
bsalomon84a4e5a2016-02-29 11:41:52 -0800431 REPORTER_ASSERT(reporter, as_IB(image)->peekTexture() == texture);
junov@chromium.orgda904742013-05-01 22:38:16 +0000432 }
junov@chromium.orgda904742013-05-01 22:38:16 +0000433}
kkinnunen179a8f52015-11-20 13:32:24 -0800434#endif
bsalomoneaaaf0b2015-01-23 08:08:04 -0800435
kkinnunen179a8f52015-11-20 13:32:24 -0800436#if SK_SUPPORT_GPU
bsalomon3582d3e2015-02-13 14:20:05 -0800437#include "GrGpuResourcePriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -0800438#include "SkGpuDevice.h"
439#include "SkImage_Gpu.h"
440#include "SkSurface_Gpu.h"
441
reede8f30622016-03-23 18:59:25 -0700442static SkBudgeted is_budgeted(const sk_sp<SkSurface>& surf) {
443 SkSurface_Gpu* gsurf = (SkSurface_Gpu*)surf.get();
Brian Osman11052242016-10-27 14:47:55 -0400444 return gsurf->getDevice()->accessRenderTargetContext()
445 ->accessRenderTarget()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800446}
447
bsalomon5ec26ae2016-02-25 08:33:02 -0800448static SkBudgeted is_budgeted(SkImage* image) {
bsalomon84a4e5a2016-02-29 11:41:52 -0800449 return ((SkImage_Gpu*)image)->peekTexture()->resourcePriv().isBudgeted();
bsalomoneaaaf0b2015-01-23 08:08:04 -0800450}
451
reed9ce9d672016-03-17 10:51:11 -0700452static SkBudgeted is_budgeted(const sk_sp<SkImage> image) {
453 return is_budgeted(image.get());
454}
455
egdanielab527a52016-06-28 08:07:26 -0700456DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, ctxInfo) {
bsalomoneaaaf0b2015-01-23 08:08:04 -0800457 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
bsalomon5ec26ae2016-02-25 08:33:02 -0800458 for (auto sbudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
459 for (auto ibudgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
bsalomon8b7451a2016-05-11 06:33:06 -0700460 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), sbudgeted, info));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800461 SkASSERT(surface);
462 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
463
reed9ce9d672016-03-17 10:51:11 -0700464 sk_sp<SkImage> image(surface->makeImageSnapshot(ibudgeted));
bsalomoneaaaf0b2015-01-23 08:08:04 -0800465
466 // Initially the image shares a texture with the surface, and the surface decides
467 // whether it is budgeted or not.
468 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
469 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
470
471 // Now trigger copy-on-write
472 surface->getCanvas()->clear(SK_ColorBLUE);
473
474 // They don't share a texture anymore. They should each have made their own budget
475 // decision.
476 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
477 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
478 }
479 }
480}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000481#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000482
kkinnunen179a8f52015-11-20 13:32:24 -0800483static void test_no_canvas1(skiatest::Reporter* reporter,
484 SkSurface* surface,
485 SkSurface::ContentChangeMode mode) {
486 // Test passes by not asserting
487 surface->notifyContentWillChange(mode);
488 SkDEBUGCODE(surface->validate();)
489}
490static void test_no_canvas2(skiatest::Reporter* reporter,
491 SkSurface* surface,
492 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000493 // Verifies the robustness of SkSurface for handling use cases where calls
494 // are made before a canvas is created.
reed9ce9d672016-03-17 10:51:11 -0700495 sk_sp<SkImage> image1 = surface->makeImageSnapshot();
496 sk_sp<SkImage> aur_image1(image1);
kkinnunen179a8f52015-11-20 13:32:24 -0800497 SkDEBUGCODE(image1->validate();)
498 SkDEBUGCODE(surface->validate();)
499 surface->notifyContentWillChange(mode);
500 SkDEBUGCODE(image1->validate();)
501 SkDEBUGCODE(surface->validate();)
reed9ce9d672016-03-17 10:51:11 -0700502 sk_sp<SkImage> image2 = surface->makeImageSnapshot();
503 sk_sp<SkImage> aur_image2(image2);
kkinnunen179a8f52015-11-20 13:32:24 -0800504 SkDEBUGCODE(image2->validate();)
505 SkDEBUGCODE(surface->validate();)
506 REPORTER_ASSERT(reporter, image1 != image2);
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000507}
kkinnunen179a8f52015-11-20 13:32:24 -0800508DEF_TEST(SurfaceNoCanvas, reporter) {
509 SkSurface::ContentChangeMode modes[] =
510 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
511 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
512 for (auto& mode : modes) {
reede8f30622016-03-23 18:59:25 -0700513 test_func(reporter, create_surface().get(), mode);
kkinnunen179a8f52015-11-20 13:32:24 -0800514 }
515 }
516}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000517#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700518DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, ctxInfo) {
kkinnunen179a8f52015-11-20 13:32:24 -0800519 SkSurface::ContentChangeMode modes[] =
520 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentChangeMode};
521 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface }) {
522 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) {
523 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700524 auto surface(surface_func(ctxInfo.grContext(), kPremul_SkAlphaType, nullptr));
reede8f30622016-03-23 18:59:25 -0700525 test_func(reporter, surface.get(), mode);
bsalomone904c092014-07-17 10:50:59 -0700526 }
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000527 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000528 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000529}
kkinnunen179a8f52015-11-20 13:32:24 -0800530#endif
reed9cd016e2016-01-30 10:01:06 -0800531
532static void check_rowbytes_remain_consistent(SkSurface* surface, skiatest::Reporter* reporter) {
reed6ceeebd2016-03-09 14:26:26 -0800533 SkPixmap surfacePM;
534 REPORTER_ASSERT(reporter, surface->peekPixels(&surfacePM));
reed9cd016e2016-01-30 10:01:06 -0800535
reed9ce9d672016-03-17 10:51:11 -0700536 sk_sp<SkImage> image(surface->makeImageSnapshot());
reed6ceeebd2016-03-09 14:26:26 -0800537 SkPixmap pm;
538 REPORTER_ASSERT(reporter, image->peekPixels(&pm));
reed9cd016e2016-01-30 10:01:06 -0800539
reed6ceeebd2016-03-09 14:26:26 -0800540 REPORTER_ASSERT(reporter, surfacePM.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800541
542 // trigger a copy-on-write
543 surface->getCanvas()->drawPaint(SkPaint());
reed9ce9d672016-03-17 10:51:11 -0700544 sk_sp<SkImage> image2(surface->makeImageSnapshot());
reed9cd016e2016-01-30 10:01:06 -0800545 REPORTER_ASSERT(reporter, image->uniqueID() != image2->uniqueID());
546
reed6ceeebd2016-03-09 14:26:26 -0800547 SkPixmap pm2;
548 REPORTER_ASSERT(reporter, image2->peekPixels(&pm2));
549 REPORTER_ASSERT(reporter, pm2.rowBytes() == pm.rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800550}
551
552DEF_TEST(surface_rowbytes, reporter) {
553 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
554
reede8f30622016-03-23 18:59:25 -0700555 auto surf0(SkSurface::MakeRaster(info));
556 check_rowbytes_remain_consistent(surf0.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800557
558 // specify a larger rowbytes
reede8f30622016-03-23 18:59:25 -0700559 auto surf1(SkSurface::MakeRaster(info, 500, nullptr));
560 check_rowbytes_remain_consistent(surf1.get(), reporter);
reed9cd016e2016-01-30 10:01:06 -0800561
562 // Try some illegal rowByte values
reede8f30622016-03-23 18:59:25 -0700563 auto s = SkSurface::MakeRaster(info, 396, nullptr); // needs to be at least 400
reed9cd016e2016-01-30 10:01:06 -0800564 REPORTER_ASSERT(reporter, nullptr == s);
reede8f30622016-03-23 18:59:25 -0700565 s = SkSurface::MakeRaster(info, 1 << 30, nullptr); // allocation to large
reed9cd016e2016-01-30 10:01:06 -0800566 REPORTER_ASSERT(reporter, nullptr == s);
567}
bsalomone63ffef2016-02-05 07:17:34 -0800568
fmalita03912f12016-07-06 06:22:06 -0700569DEF_TEST(surface_raster_zeroinitialized, reporter) {
570 sk_sp<SkSurface> s(SkSurface::MakeRasterN32Premul(100, 100));
571 SkPixmap pixmap;
572 REPORTER_ASSERT(reporter, s->peekPixels(&pixmap));
573
574 for (int i = 0; i < pixmap.info().width(); ++i) {
575 for (int j = 0; j < pixmap.info().height(); ++j) {
576 REPORTER_ASSERT(reporter, *pixmap.addr32(i, j) == 0);
577 }
578 }
579}
580
bsalomone63ffef2016-02-05 07:17:34 -0800581#if SK_SUPPORT_GPU
ericrkc4025182016-05-04 12:01:58 -0700582static sk_sp<SkSurface> create_gpu_surface_backend_texture(
583 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
584 const int kWidth = 10;
585 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400586 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700587 sk_memset32(pixels.get(), color, kWidth * kHeight);
588 GrBackendTextureDesc desc;
589 desc.fConfig = kRGBA_8888_GrPixelConfig;
590 desc.fWidth = kWidth;
591 desc.fHeight = kHeight;
592 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
593 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700594 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700595 desc.fSampleCnt = sampleCnt;
596 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, desc, nullptr);
597 if (!surface) {
598 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
599 return nullptr;
600 }
601 *outTexture = desc.fTextureHandle;
602 return surface;
603}
bsalomone63ffef2016-02-05 07:17:34 -0800604
ericrkc4025182016-05-04 12:01:58 -0700605static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
606 GrContext* context, int sampleCnt, uint32_t color, GrBackendObject* outTexture) {
607 const int kWidth = 10;
608 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400609 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
ericrkc4025182016-05-04 12:01:58 -0700610 sk_memset32(pixels.get(), color, kWidth * kHeight);
611 GrBackendTextureDesc desc;
612 desc.fConfig = kRGBA_8888_GrPixelConfig;
613 desc.fWidth = kWidth;
614 desc.fHeight = kHeight;
615 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
616 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
egdaniel0a3a7f72016-06-24 09:22:31 -0700617 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
ericrkc4025182016-05-04 12:01:58 -0700618 desc.fSampleCnt = sampleCnt;
619 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
620 nullptr);
621 if (!surface) {
622 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
623 return nullptr;
624 }
625 *outTexture = desc.fTextureHandle;
626 return surface;
627}
628
629static void test_surface_clear(skiatest::Reporter* reporter, sk_sp<SkSurface> surface,
Robert Phillips2c862492017-01-18 10:08:39 -0500630 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceGetter,
ericrkc4025182016-05-04 12:01:58 -0700631 uint32_t expectedValue) {
bsalomone63ffef2016-02-05 07:17:34 -0800632 if (!surface) {
633 ERRORF(reporter, "Could not create GPU SkSurface.");
634 return;
635 }
636 int w = surface->width();
637 int h = surface->height();
Ben Wagner7ecc5962016-11-02 17:07:33 -0400638 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
ericrkc4025182016-05-04 12:01:58 -0700639 sk_memset32(pixels.get(), ~expectedValue, w * h);
bsalomone63ffef2016-02-05 07:17:34 -0800640
Robert Phillips2c862492017-01-18 10:08:39 -0500641 sk_sp<GrSurfaceContext> grSurfaceContext(grSurfaceGetter(surface.get()));
642 if (!grSurfaceContext) {
bsalomone63ffef2016-02-05 07:17:34 -0800643 ERRORF(reporter, "Could access render target of GPU SkSurface.");
644 return;
645 }
bsalomon2fba8092016-02-05 13:47:06 -0800646 surface.reset();
Robert Phillips2c862492017-01-18 10:08:39 -0500647
648 SkImageInfo ii = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
649 grSurfaceContext->readPixels(ii, pixels.get(), 0, 0, 0);
bsalomone63ffef2016-02-05 07:17:34 -0800650 for (int y = 0; y < h; ++y) {
651 for (int x = 0; x < w; ++x) {
652 uint32_t pixel = pixels.get()[y * w + x];
653 if (pixel != expectedValue) {
654 SkString msg;
655 if (expectedValue) {
656 msg = "SkSurface should have left render target unmodified";
657 } else {
658 msg = "SkSurface should have cleared the render target";
659 }
660 ERRORF(reporter,
661 "%s but read 0x%08x (instead of 0x%08x) at %x,%d", msg.c_str(), pixel,
662 expectedValue, x, y);
663 return;
664 }
665 }
666 }
667}
668
bsalomon758586c2016-04-06 14:02:39 -0700669DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceClear_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700670 GrContext* context = ctxInfo.grContext();
ericrkc4025182016-05-04 12:01:58 -0700671
Robert Phillips2c862492017-01-18 10:08:39 -0500672 std::function<sk_sp<GrSurfaceContext>(SkSurface*)> grSurfaceContextGetters[] = {
egdanielab527a52016-06-28 08:07:26 -0700673 [] (SkSurface* s){
Robert Phillips2c862492017-01-18 10:08:39 -0500674 return sk_ref_sp(s->getCanvas()->internal_private_accessTopLayerRenderTargetContext());
675 },
676 [] (SkSurface* s){
677 sk_sp<SkImage> i(s->makeImageSnapshot());
678 SkImage_Gpu* gpuImage = (SkImage_Gpu *) as_IB(i);
679 sk_sp<GrSurfaceProxy> proxy = gpuImage->refProxy();
680 GrContext* context = gpuImage->context();
681 return context->contextPriv().makeWrappedSurfaceContext(std::move(proxy),
682 gpuImage->refColorSpace());
683 }
bsalomone63ffef2016-02-05 07:17:34 -0800684 };
ericrkc4025182016-05-04 12:01:58 -0700685
Robert Phillips2c862492017-01-18 10:08:39 -0500686 for (auto grSurfaceGetter : grSurfaceContextGetters) {
ericrkc4025182016-05-04 12:01:58 -0700687 // Test that non-wrapped RTs are created clear.
bsalomone63ffef2016-02-05 07:17:34 -0800688 for (auto& surface_func : {&create_gpu_surface, &create_gpu_scratch_surface}) {
reede8f30622016-03-23 18:59:25 -0700689 auto surface = surface_func(context, kPremul_SkAlphaType, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -0800690 test_surface_clear(reporter, surface, grSurfaceGetter, 0x0);
691 }
692 // Wrapped RTs are *not* supposed to clear (to allow client to partially update a surface).
ericrkc4025182016-05-04 12:01:58 -0700693 const uint32_t kOrigColor = 0xABABABAB;
694 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
695 &create_gpu_surface_backend_texture_as_render_target}) {
696 GrBackendObject textureObject;
697 auto surface = surfaceFunc(context, 0, kOrigColor, &textureObject);
698 test_surface_clear(reporter, surface, grSurfaceGetter, kOrigColor);
699 surface.reset();
700 context->getGpu()->deleteTestingOnlyBackendTexture(textureObject);
701 }
702 }
703}
bsalomone63ffef2016-02-05 07:17:34 -0800704
ericrkc4025182016-05-04 12:01:58 -0700705static void test_surface_draw_partially(
706 skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
707 const int kW = surface->width();
708 const int kH = surface->height();
709 SkPaint paint;
710 const SkColor kRectColor = ~origColor | 0xFF000000;
711 paint.setColor(kRectColor);
712 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
713 paint);
Ben Wagner7ecc5962016-11-02 17:07:33 -0400714 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kW * kH]);
ericrkc4025182016-05-04 12:01:58 -0700715 sk_memset32(pixels.get(), ~origColor, kW * kH);
716 // Read back RGBA to avoid format conversions that may not be supported on all platforms.
717 SkImageInfo readInfo = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
718 SkAssertResult(surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0));
719 bool stop = false;
720 SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
721 (origColor >> 0 & 0xFF),
722 (origColor >> 8 & 0xFF),
723 (origColor >> 16 & 0xFF));
724 SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
725 (kRectColor >> 16 & 0xFF),
726 (kRectColor >> 8 & 0xFF),
727 (kRectColor >> 0 & 0xFF));
728 for (int y = 0; y < kH/2 && !stop; ++y) {
729 for (int x = 0; x < kW && !stop; ++x) {
730 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
731 if (rectColorPM != pixels[x + y * kW]) {
732 stop = true;
733 }
734 }
735 }
736 stop = false;
737 for (int y = kH/2; y < kH && !stop; ++y) {
738 for (int x = 0; x < kW && !stop; ++x) {
739 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
740 if (origColorPM != pixels[x + y * kW]) {
741 stop = true;
742 }
743 }
744 }
745}
bsalomone63ffef2016-02-05 07:17:34 -0800746
egdanielab527a52016-06-28 08:07:26 -0700747DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfacePartialDraw_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700748 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700749 if (!gpu) {
750 return;
751 }
752 static const uint32_t kOrigColor = 0xFFAABBCC;
bsalomone63ffef2016-02-05 07:17:34 -0800753
ericrkc4025182016-05-04 12:01:58 -0700754 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
755 &create_gpu_surface_backend_texture_as_render_target}) {
756 // Validate that we can draw to the canvas and that the original texture color is
757 // preserved in pixels that aren't rendered to via the surface.
758 // This works only for non-multisampled case.
759 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700760 auto surface = surfaceFunc(ctxInfo.grContext(), 0, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700761 if (surface) {
762 test_surface_draw_partially(reporter, surface, kOrigColor);
763 surface.reset();
764 gpu->deleteTestingOnlyBackendTexture(textureObject);
765 }
766 }
767}
768
769
770DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SurfaceAttachStencil_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700771 GrGpu* gpu = ctxInfo.grContext()->getGpu();
ericrkc4025182016-05-04 12:01:58 -0700772 if (!gpu) {
773 return;
774 }
775 static const uint32_t kOrigColor = 0xFFAABBCC;
776
777 for (auto& surfaceFunc : {&create_gpu_surface_backend_texture,
778 &create_gpu_surface_backend_texture_as_render_target}) {
779 for (int sampleCnt : {0, 4, 8}) {
780 GrBackendObject textureObject;
bsalomon8b7451a2016-05-11 06:33:06 -0700781 auto surface = surfaceFunc(ctxInfo.grContext(), sampleCnt, kOrigColor, &textureObject);
ericrkc4025182016-05-04 12:01:58 -0700782
783 if (!surface && sampleCnt > 0) {
784 // Certain platforms don't support MSAA, skip these.
785 continue;
786 }
787
788 // Validate that we can attach a stencil buffer to an SkSurface created by either of
789 // our surface functions.
Brian Osman11052242016-10-27 14:47:55 -0400790 GrRenderTarget* rt = surface->getCanvas()
791 ->internal_private_accessTopLayerRenderTargetContext()->accessRenderTarget();
ericrkc4025182016-05-04 12:01:58 -0700792 REPORTER_ASSERT(reporter,
bsalomon8b7451a2016-05-11 06:33:06 -0700793 ctxInfo.grContext()->resourceProvider()->attachStencilAttachment(rt));
ericrkc4025182016-05-04 12:01:58 -0700794 gpu->deleteTestingOnlyBackendTexture(textureObject);
795 }
bsalomone63ffef2016-02-05 07:17:34 -0800796 }
797}
798#endif
brianosman0e22eb82016-08-30 07:07:59 -0700799
800static void test_surface_creation_and_snapshot_with_color_space(
801 skiatest::Reporter* reporter,
802 const char* prefix,
803 bool f16Support,
804 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) {
805
Brian Osman526972e2016-10-24 09:24:02 -0400806 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
807 auto adobeColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named);
raftias94888332016-10-18 10:02:51 -0700808 const SkMatrix44* srgbMatrix = as_CSB(srgbColorSpace)->toXYZD50();
809 SkASSERT(srgbMatrix);
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400810 SkColorSpaceTransferFn oddGamma;
811 oddGamma.fA = 1.0f;
812 oddGamma.fB = oddGamma.fC = oddGamma.fD = oddGamma.fE = oddGamma.fF = 0.0f;
813 oddGamma.fG = 4.0f;
Brian Osman526972e2016-10-24 09:24:02 -0400814 auto oddColorSpace = SkColorSpace::MakeRGB(oddGamma, *srgbMatrix);
815 auto linearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosman0e22eb82016-08-30 07:07:59 -0700816
817 const struct {
818 SkColorType fColorType;
819 sk_sp<SkColorSpace> fColorSpace;
820 bool fShouldWork;
821 const char* fDescription;
822 } testConfigs[] = {
823 { kN32_SkColorType, nullptr, true, "N32-nullptr" },
824 { kN32_SkColorType, linearColorSpace, false, "N32-linear" },
825 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" },
826 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" },
827 { kN32_SkColorType, oddColorSpace, false, "N32-odd" },
Brian Osmaneb21ef62016-11-01 16:30:21 -0400828 { kRGBA_F16_SkColorType, nullptr, true, "F16-nullptr" },
brianosman0e22eb82016-08-30 07:07:59 -0700829 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" },
830 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" },
831 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" },
832 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" },
833 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" },
834 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" },
835 };
836
837 for (auto& testConfig : testConfigs) {
838 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDescription);
839 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPremul_SkAlphaType,
840 testConfig.fColorSpace);
841
842 // For some GPU contexts (eg ANGLE), we don't have f16 support, so we should fail to create
843 // any surface of that type:
844 bool shouldWork = testConfig.fShouldWork &&
845 (f16Support || kRGBA_F16_SkColorType != testConfig.fColorType);
846
847 auto surface(surfaceMaker(info));
848 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullTestName.c_str());
849
850 if (shouldWork && surface) {
851 sk_sp<SkImage> image(surface->makeImageSnapshot());
852 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription);
853 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpace();
854 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fColorSpace.get(),
855 fullTestName.c_str());
856 }
857 }
858}
859
860DEF_TEST(SurfaceCreationWithColorSpace, reporter) {
861 auto surfaceMaker = [](const SkImageInfo& info) {
862 return SkSurface::MakeRaster(info);
863 };
864
865 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true, surfaceMaker);
866}
867
868#if SK_SUPPORT_GPU
869DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter, ctxInfo) {
870 GrContext* context = ctxInfo.grContext();
871 bool f16Support = context->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false);
872 auto surfaceMaker = [context](const SkImageInfo& info) {
873 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
874 };
875
876 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", f16Support, surfaceMaker);
877
878 std::vector<GrBackendObject> textureHandles;
879 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info) {
880 GrBackendTextureDesc desc;
881 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
882 desc.fWidth = 10;
883 desc.fHeight = 10;
884 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
885 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
886 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true);
887
888 if (!desc.fTextureHandle) {
889 return sk_sp<SkSurface>(nullptr);
890 }
891 textureHandles.push_back(desc.fTextureHandle);
892
893 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.colorSpace()),
894 nullptr);
895 };
896
897 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", f16Support,
898 wrappedSurfaceMaker);
899
900 for (auto textureHandle : textureHandles) {
901 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle);
902 }
903}
904#endif
Matt Sarett22886c42016-11-22 11:31:41 -0500905
906static void test_overdraw_surface(skiatest::Reporter* r, SkSurface* surface) {
Matt Sarette11b6142016-11-28 18:28:07 -0500907 SkOverdrawCanvas canvas(surface->getCanvas());
908 canvas.drawPaint(SkPaint());
Matt Sarett22886c42016-11-22 11:31:41 -0500909 sk_sp<SkImage> image = surface->makeImageSnapshot();
910
911 SkBitmap bitmap;
912 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
913 bitmap.lockPixels();
914 for (int y = 0; y < 10; y++) {
915 for (int x = 0; x < 10; x++) {
916 REPORTER_ASSERT(r, 1 == SkGetPackedA32(*bitmap.getAddr32(x, y)));
917 }
918 }
919}
920
921DEF_TEST(OverdrawSurface_Raster, r) {
922 sk_sp<SkSurface> surface = create_surface();
923 test_overdraw_surface(r, surface.get());
924}
925
926#if SK_SUPPORT_GPU
927DEF_GPUTEST_FOR_RENDERING_CONTEXTS(OverdrawSurface_Gpu, r, ctxInfo) {
928 GrContext* context = ctxInfo.grContext();
929 sk_sp<SkSurface> surface = create_gpu_surface(context);
930 test_overdraw_surface(r, surface.get());
931}
932#endif