blob: c8cccc8ab1f7229961d0ff64c25f7fc313114482 [file] [log] [blame]
reed4a8126e2014-09-22 07:29:03 -07001/*
2 * Copyright 2014 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkColorSpace.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkImage.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkShader.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkSurface.h"
25#include "include/core/SkSurfaceProps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/core/SkTileMode.h"
27#include "include/core/SkTypeface.h"
28#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/effects/SkGradientShader.h"
Brian Salomon56c78f42021-02-03 16:56:55 -050030#include "include/gpu/GrDirectContext.h"
31#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "include/utils/SkTextUtils.h"
33#include "tools/ToolUtils.h"
Brian Salomon56c78f42021-02-03 16:56:55 -050034#include "tools/gpu/BackendSurfaceFactory.h"
reed4a8126e2014-09-22 07:29:03 -070035
36#define W 200
37#define H 100
38
reed1a9b9642016-03-13 14:13:58 -070039static sk_sp<SkShader> make_shader() {
reed4a8126e2014-09-22 07:29:03 -070040 int a = 0x99;
41 int b = 0xBB;
42 SkPoint pts[] = { { 0, 0 }, { W, H } };
43 SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
Mike Reedfae8fce2019-04-03 10:27:45 -040044 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
reed4a8126e2014-09-22 07:29:03 -070045}
46
Robert Phillips16bf7d32020-07-07 10:20:27 -040047static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
48 const SkImageInfo& info,
49 SkPixelGeometry geo) {
reed7c123542016-08-18 09:30:44 -070050 SkSurfaceProps props(0, geo);
reed4a8126e2014-09-22 07:29:03 -070051 if (ctx) {
reede8f30622016-03-23 18:59:25 -070052 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070053 } else {
reede8f30622016-03-23 18:59:25 -070054 return SkSurface::MakeRaster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070055 }
56}
57
58static void test_draw(SkCanvas* canvas, const char label[]) {
59 SkPaint paint;
60
61 paint.setAntiAlias(true);
reed4a8126e2014-09-22 07:29:03 -070062 paint.setDither(true);
63
reed1a9b9642016-03-13 14:13:58 -070064 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070065 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070066 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070067
68 paint.setColor(SK_ColorWHITE);
Mike Kleinea3f0142019-03-20 11:12:10 -050069 SkFont font(ToolUtils::create_portable_typeface(), 32);
Mike Reedb579f072019-01-03 15:45:53 -050070 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
71 SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
72 SkTextUtils::kCenter_Align);
reed4a8126e2014-09-22 07:29:03 -070073}
74
75class SurfacePropsGM : public skiagm::GM {
76public:
77 SurfacePropsGM() {}
78
79protected:
mtklein36352bf2015-03-25 18:17:31 -070080 SkString onShortName() override {
reed4a8126e2014-09-22 07:29:03 -070081 return SkString("surfaceprops");
82 }
83
mtklein36352bf2015-03-25 18:17:31 -070084 SkISize onISize() override {
reed7c123542016-08-18 09:30:44 -070085 return SkISize::Make(W, H * 5);
reed4a8126e2014-09-22 07:29:03 -070086 }
87
mtklein36352bf2015-03-25 18:17:31 -070088 void onDraw(SkCanvas* canvas) override {
Robert Phillips16bf7d32020-07-07 10:20:27 -040089 auto ctx = canvas->recordingContext();
reed4a8126e2014-09-22 07:29:03 -070090
91 // must be opaque to have a hope of testing LCD text
brianosman0e22eb82016-08-30 07:07:59 -070092 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
reed4a8126e2014-09-22 07:29:03 -070093
94 const struct {
95 SkPixelGeometry fGeo;
96 const char* fLabel;
scroggoe6cad6b2016-05-09 13:20:58 -070097 } recs[] = {
reed4a8126e2014-09-22 07:29:03 -070098 { kUnknown_SkPixelGeometry, "Unknown" },
99 { kRGB_H_SkPixelGeometry, "RGB_H" },
100 { kBGR_H_SkPixelGeometry, "BGR_H" },
101 { kRGB_V_SkPixelGeometry, "RGB_V" },
102 { kBGR_V_SkPixelGeometry, "BGR_V" },
103 };
halcanary9d524f22016-03-29 09:03:52 -0700104
reed4a8126e2014-09-22 07:29:03 -0700105 SkScalar x = 0;
reed7c123542016-08-18 09:30:44 -0700106 SkScalar y = 0;
107 for (const auto& rec : recs) {
108 auto surface(make_surface(ctx, info, rec.fGeo));
109 if (!surface) {
110 SkDebugf("failed to create surface! label: %s", rec.fLabel);
111 continue;
reed4a8126e2014-09-22 07:29:03 -0700112 }
reed7c123542016-08-18 09:30:44 -0700113 test_draw(surface->getCanvas(), rec.fLabel);
Mike Reedb746b1f2021-01-06 08:43:51 -0500114 surface->draw(canvas, x, y);
reed7c123542016-08-18 09:30:44 -0700115 y += H;
reed4a8126e2014-09-22 07:29:03 -0700116 }
117 }
118
119private:
John Stiles7571f9e2020-09-02 22:42:33 -0400120 using INHERITED = GM;
reed4a8126e2014-09-22 07:29:03 -0700121};
reed4a8126e2014-09-22 07:29:03 -0700122DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -0800123
124#ifdef SK_DEBUG
125static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
126 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
127}
128#endif
129
130class NewSurfaceGM : public skiagm::GM {
131public:
132 NewSurfaceGM() {}
133
134protected:
mtklein36352bf2015-03-25 18:17:31 -0700135 SkString onShortName() override {
reed4af267b2014-11-21 08:46:37 -0800136 return SkString("surfacenew");
137 }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 SkISize onISize() override {
reed4af267b2014-11-21 08:46:37 -0800140 return SkISize::Make(300, 140);
141 }
142
143 static void drawInto(SkCanvas* canvas) {
144 canvas->drawColor(SK_ColorRED);
145 }
146
mtklein36352bf2015-03-25 18:17:31 -0700147 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800148 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
149
Mike Kleinea3f0142019-03-20 11:12:10 -0500150 auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
reed4af267b2014-11-21 08:46:37 -0800151 drawInto(surf->getCanvas());
152
reed9ce9d672016-03-17 10:51:11 -0700153 sk_sp<SkImage> image(surf->makeImageSnapshot());
Mike Reed8d29ab62021-01-23 18:10:39 -0500154 canvas->drawImage(image, 10, 10);
reed4af267b2014-11-21 08:46:37 -0800155
reede8f30622016-03-23 18:59:25 -0700156 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800157 drawInto(surf2->getCanvas());
158
159 // Assert that the props were communicated transitively through the first image
160 SkASSERT(equal(surf->props(), surf2->props()));
161
reed9ce9d672016-03-17 10:51:11 -0700162 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
Mike Reed8d29ab62021-01-23 18:10:39 -0500163 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10);
reed4af267b2014-11-21 08:46:37 -0800164 }
165
166private:
John Stiles7571f9e2020-09-02 22:42:33 -0400167 using INHERITED = GM;
reed4af267b2014-11-21 08:46:37 -0800168};
169DEF_GM( return new NewSurfaceGM )
Mike Reedd94abc52017-03-06 16:37:07 -0500170
171///////////////////////////////////////////////////////////////////////////////////////////////////
172
Brian Salomon56c78f42021-02-03 16:56:55 -0500173// The GPU backend may behave differently when images are snapped from wrapped textures and
174// render targets compared.
175namespace {
176enum SurfaceType {
177 kManaged,
178 kBackendTexture,
179 kBackendRenderTarget
180};
181}
182
183static sk_sp<SkSurface> make_surface(const SkImageInfo& ii, SkCanvas* canvas, SurfaceType type) {
184 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());
185 switch (type) {
186 case kManaged:
187 return ToolUtils::makeSurface(canvas, ii);
188 case kBackendTexture:
189 if (!direct) {
190 return nullptr;
191 }
192 return sk_gpu_test::MakeBackendTextureSurface(direct, ii, kTopLeft_GrSurfaceOrigin, 1);
193 case kBackendRenderTarget:
194 return sk_gpu_test::MakeBackendRenderTargetSurface(direct,
195 ii,
196 kTopLeft_GrSurfaceOrigin,
197 1);
198 }
199 return nullptr;
200}
201
202using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
203
204#define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H) \
205 DEF_SIMPLE_GM(name, canvas, W, H) { \
206 auto make = [canvas](const SkImageInfo& ii) { \
207 return make_surface(ii, canvas, SurfaceType::kManaged); \
208 }; \
209 main(canvas, MakeSurfaceFn(make)); \
210 }
211
212#define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H) \
213 DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) { \
214 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext()); \
215 if (!direct || direct->abandoned()) { \
216 *err_msg = "Requires non-abandoned GrDirectContext"; \
217 return skiagm::DrawResult::kSkip; \
218 } \
219 auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
220 main(canvas, MakeSurfaceFn(make)); \
221 return skiagm::DrawResult::kOk; \
222 }
223
224#define DEF_BET_SURFACE_TEST(name, canvas, main, W, H) \
225 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
226 SurfaceType::kBackendTexture, W, H)
227
228#define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H) \
229 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
230 SurfaceType::kBackendRenderTarget, W, H)
231
232// This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
233// render target.
234#define DEF_SURFACE_TESTS(name, canvas, W, H) \
235 static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
236 DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
237 DEF_BET_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
238 DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
239 static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
240
241DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
Mike Reedd94abc52017-03-06 16:37:07 -0500242 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
Brian Salomon56c78f42021-02-03 16:56:55 -0500243 sk_sp<SkSurface> surf = make(info);
Mike Reedd94abc52017-03-06 16:37:07 -0500244
245 surf->getCanvas()->clear(SK_ColorRED);
246 // its important that image survives longer than the next draw, so the surface will see
247 // an outstanding image, and have to decide if it should retain or discard those pixels
248 sk_sp<SkImage> image = surf->makeImageSnapshot();
249
250 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
251 // it should not (we need the previous red pixels).
252 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
253 surf->getCanvas()->clear(SK_ColorBLUE);
254
255 // expect to see two rects: blue | red
Mike Reed8d29ab62021-01-23 18:10:39 -0500256 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
Mike Reedd94abc52017-03-06 16:37:07 -0500257}
Mike Reeda1361362017-03-07 09:37:29 -0500258
Brian Salomond63638b2021-03-05 14:00:07 -0500259// Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
260DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
261 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
262 sk_sp<SkSurface> surf = make(info);
263
264 surf->getCanvas()->clear(SK_ColorBLUE);
265 // its important that image survives longer than the next draw, so the surface will see
266 // an outstanding image, and have to decide if it should retain or discard those pixels
267 sk_sp<SkImage> image = surf->makeImageSnapshot();
268
269 surf->getCanvas()->clear(SK_ColorRED);
270 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
271 // it should not (we need the previous red pixels).
272 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
273 surf->getCanvas()->drawImage(image, 0, 0);
274
275 // expect to see two rects: blue | red
276 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
277}
278
279DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
280 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
281 sk_sp<SkSurface> surf = make(info);
282
283 surf->getCanvas()->clear(SK_ColorRED);
284 sk_sp<SkImage> image = surf->makeImageSnapshot();
285 // expect to see just red
286 canvas->drawImage(std::move(image), 0, 0);
287}
288
289// Like simple_snap_image but the surface dies before the image.
290DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
291 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
292 sk_sp<SkSurface> surf = make(info);
293
294 surf->getCanvas()->clear(SK_ColorRED);
295 sk_sp<SkImage> image = surf->makeImageSnapshot();
296 surf.reset();
297 // expect to see just red
298 canvas->drawImage(std::move(image), 0, 0);
299}
300
Brian Salomon56c78f42021-02-03 16:56:55 -0500301DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
Mike Reeda1361362017-03-07 09:37:29 -0500302 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
Brian Salomon56c78f42021-02-03 16:56:55 -0500303 sk_sp<SkSurface> surf = make(info);
Mike Reeda1361362017-03-07 09:37:29 -0500304 surf->getCanvas()->clear(SK_ColorRED);
305 // its important that image survives longer than the next draw, so the surface will see
306 // an outstanding image, and have to decide if it should retain or discard those pixels
307 sk_sp<SkImage> image = surf->makeImageSnapshot();
308
309 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
310 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
311 // with a non-opaque paint.
312 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500313 paint.setAlphaf(0.25f);
Mike Reeda1361362017-03-07 09:37:29 -0500314 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
315 surf->getCanvas()->clear(SK_ColorBLUE);
316 surf->getCanvas()->restore();
317
318 // expect to see two rects: blue blended on red
Mike Reed8d29ab62021-01-23 18:10:39 -0500319 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
Mike Reeda1361362017-03-07 09:37:29 -0500320}
Mike Reed114bde82018-11-21 09:12:09 -0500321
Brian Salomon56c78f42021-02-03 16:56:55 -0500322DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
Mike Reed114bde82018-11-21 09:12:09 -0500323 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
Brian Salomon56c78f42021-02-03 16:56:55 -0500324 auto surf = make(info);
Mike Reed114bde82018-11-21 09:12:09 -0500325
326 const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
327
328 // noisy background
329 {
330 SkPoint pts[] = {{0, 0}, {40, 50}};
331 SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
Mike Reedfae8fce2019-04-03 10:27:45 -0400332 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
Mike Reed114bde82018-11-21 09:12:09 -0500333 SkPaint paint;
334 paint.setShader(sh);
335 surf->getCanvas()->drawPaint(paint);
336 }
337
338 // save away the right-hand strip, then clear it
339 sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
340 {
341 SkPaint paint;
342 paint.setBlendMode(SkBlendMode::kClear);
343 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
344 }
345
346 // draw the "foreground"
347 {
348 SkPaint paint;
349 paint.setColor(SK_ColorGREEN);
350 SkRect r = { 0, 10, 256, 35 };
351 while (r.fBottom < 256) {
352 surf->getCanvas()->drawRect(r, paint);
353 r.offset(0, r.height() * 2);
354 }
355 }
356
357 // apply the "fade"
358 {
359 SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
360 SkColor colors[] = {0xFF000000, 0};
Mike Reedfae8fce2019-04-03 10:27:45 -0400361 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed114bde82018-11-21 09:12:09 -0500362 SkPaint paint;
363 paint.setShader(sh);
364 paint.setBlendMode(SkBlendMode::kDstIn);
365 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
366 }
367
368 // restore the original strip, drawing it "under" the current foreground
369 {
370 SkPaint paint;
371 paint.setBlendMode(SkBlendMode::kDstOver);
372 surf->getCanvas()->drawImage(saveImg,
373 SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
Mike Reed8d29ab62021-01-23 18:10:39 -0500374 SkSamplingOptions(), &paint);
Mike Reed114bde82018-11-21 09:12:09 -0500375 }
376
377 // show it on screen
Mike Reedb746b1f2021-01-06 08:43:51 -0500378 surf->draw(canvas, 0, 0);
Mike Reed114bde82018-11-21 09:12:09 -0500379}