blob: 376217d048934697941611c24b28eea7a9ef7cd3 [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"
Kevin Lubick5c93acf2023-05-09 12:11:43 -040032#include "include/gpu/ganesh/SkSurfaceGanesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "include/utils/SkTextUtils.h"
34#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040035#include "tools/fonts/FontToolUtils.h"
Brian Salomon56c78f42021-02-03 16:56:55 -050036#include "tools/gpu/BackendSurfaceFactory.h"
reed4a8126e2014-09-22 07:29:03 -070037
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -080038#define W 800
reed4a8126e2014-09-22 07:29:03 -070039#define H 100
40
reed1a9b9642016-03-13 14:13:58 -070041static sk_sp<SkShader> make_shader() {
reed4a8126e2014-09-22 07:29:03 -070042 int a = 0x99;
43 int b = 0xBB;
44 SkPoint pts[] = { { 0, 0 }, { W, H } };
45 SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
Mike Reedfae8fce2019-04-03 10:27:45 -040046 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
reed4a8126e2014-09-22 07:29:03 -070047}
48
Robert Phillips16bf7d32020-07-07 10:20:27 -040049static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
50 const SkImageInfo& info,
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -080051 SkPixelGeometry geo,
52 SkScalar contrast,
53 SkScalar gamma) {
54 SkSurfaceProps props(0, geo, contrast, gamma);
reed4a8126e2014-09-22 07:29:03 -070055 if (ctx) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040056 return SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070057 } else {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040058 return SkSurfaces::Raster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070059 }
60}
61
62static void test_draw(SkCanvas* canvas, const char label[]) {
63 SkPaint paint;
64
65 paint.setAntiAlias(true);
reed4a8126e2014-09-22 07:29:03 -070066 paint.setDither(true);
67
reed1a9b9642016-03-13 14:13:58 -070068 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070069 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070070 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070071
72 paint.setColor(SK_ColorWHITE);
Kevin Lubicke836c3a2023-10-20 06:55:35 -040073 SkFont font(ToolUtils::DefaultPortableTypeface(), 32);
Mike Reedb579f072019-01-03 15:45:53 -050074 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
75 SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
76 SkTextUtils::kCenter_Align);
reed4a8126e2014-09-22 07:29:03 -070077}
78
79class SurfacePropsGM : public skiagm::GM {
80public:
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -080081 SurfacePropsGM() {
82 recs = {
83 {kUnknown_SkPixelGeometry,
84 "Unknown geometry, default contrast/gamma",
85 SK_GAMMA_CONTRAST,
86 SK_GAMMA_EXPONENT},
87 {kRGB_H_SkPixelGeometry,
88 "RGB_H, default contrast/gamma",
89 SK_GAMMA_CONTRAST,
90 SK_GAMMA_EXPONENT},
91 {kBGR_H_SkPixelGeometry,
92 "BGR_H, default contrast/gamma",
93 SK_GAMMA_CONTRAST,
94 SK_GAMMA_EXPONENT},
95 {kRGB_V_SkPixelGeometry,
96 "RGB_V, default contrast/gamma",
97 SK_GAMMA_CONTRAST,
98 SK_GAMMA_EXPONENT},
99 {kBGR_V_SkPixelGeometry,
100 "BGR_V, default contrast/gamma",
101 SK_GAMMA_CONTRAST,
102 SK_GAMMA_EXPONENT},
103 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 0", 0, 0},
104 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 0", 1, 0},
105 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 3.9", 0, 3.9f},
106 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 3.9", 1, 3.9f},
107 };
108 }
reed4a8126e2014-09-22 07:29:03 -0700109
110protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000111 SkString getName() const override { return SkString("surfaceprops"); }
reed4a8126e2014-09-22 07:29:03 -0700112
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -0800113 SkISize getISize() override { return SkISize::Make(W, H * recs.size()); }
reed4a8126e2014-09-22 07:29:03 -0700114
mtklein36352bf2015-03-25 18:17:31 -0700115 void onDraw(SkCanvas* canvas) override {
Robert Phillips16bf7d32020-07-07 10:20:27 -0400116 auto ctx = canvas->recordingContext();
reed4a8126e2014-09-22 07:29:03 -0700117
118 // must be opaque to have a hope of testing LCD text
brianosman0e22eb82016-08-30 07:07:59 -0700119 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
reed4a8126e2014-09-22 07:29:03 -0700120
reed4a8126e2014-09-22 07:29:03 -0700121 SkScalar x = 0;
reed7c123542016-08-18 09:30:44 -0700122 SkScalar y = 0;
123 for (const auto& rec : recs) {
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -0800124 auto surface(make_surface(ctx, info, rec.fGeo, rec.fContrast, rec.fGamma));
reed7c123542016-08-18 09:30:44 -0700125 if (!surface) {
126 SkDebugf("failed to create surface! label: %s", rec.fLabel);
127 continue;
reed4a8126e2014-09-22 07:29:03 -0700128 }
reed7c123542016-08-18 09:30:44 -0700129 test_draw(surface->getCanvas(), rec.fLabel);
Mike Reedb746b1f2021-01-06 08:43:51 -0500130 surface->draw(canvas, x, y);
reed7c123542016-08-18 09:30:44 -0700131 y += H;
reed4a8126e2014-09-22 07:29:03 -0700132 }
133 }
134
135private:
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -0800136 struct SurfacePropsInput {
137 SkPixelGeometry fGeo;
138 const char* fLabel;
139 SkScalar fContrast;
140 SkScalar fGamma;
141 };
142 std::vector<SurfacePropsInput> recs;
143
John Stiles7571f9e2020-09-02 22:42:33 -0400144 using INHERITED = GM;
reed4a8126e2014-09-22 07:29:03 -0700145};
reed4a8126e2014-09-22 07:29:03 -0700146DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -0800147
148#ifdef SK_DEBUG
149static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
Kurt Catti-Schmidt969536e2024-02-07 11:47:21 -0800150 return a == b;
reed4af267b2014-11-21 08:46:37 -0800151}
152#endif
153
154class NewSurfaceGM : public skiagm::GM {
155public:
156 NewSurfaceGM() {}
157
158protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000159 SkString getName() const override { return SkString("surfacenew"); }
reed4af267b2014-11-21 08:46:37 -0800160
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000161 SkISize getISize() override { return SkISize::Make(300, 140); }
reed4af267b2014-11-21 08:46:37 -0800162
163 static void drawInto(SkCanvas* canvas) {
164 canvas->drawColor(SK_ColorRED);
165 }
166
mtklein36352bf2015-03-25 18:17:31 -0700167 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800168 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
169
Mike Kleinea3f0142019-03-20 11:12:10 -0500170 auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
reed4af267b2014-11-21 08:46:37 -0800171 drawInto(surf->getCanvas());
172
reed9ce9d672016-03-17 10:51:11 -0700173 sk_sp<SkImage> image(surf->makeImageSnapshot());
Mike Reed8d29ab62021-01-23 18:10:39 -0500174 canvas->drawImage(image, 10, 10);
reed4af267b2014-11-21 08:46:37 -0800175
reede8f30622016-03-23 18:59:25 -0700176 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800177 drawInto(surf2->getCanvas());
178
179 // Assert that the props were communicated transitively through the first image
180 SkASSERT(equal(surf->props(), surf2->props()));
181
reed9ce9d672016-03-17 10:51:11 -0700182 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
Mike Reed8d29ab62021-01-23 18:10:39 -0500183 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10);
reed4af267b2014-11-21 08:46:37 -0800184 }
185
186private:
John Stiles7571f9e2020-09-02 22:42:33 -0400187 using INHERITED = GM;
reed4af267b2014-11-21 08:46:37 -0800188};
189DEF_GM( return new NewSurfaceGM )
Mike Reedd94abc52017-03-06 16:37:07 -0500190
191///////////////////////////////////////////////////////////////////////////////////////////////////
192
Brian Salomon56c78f42021-02-03 16:56:55 -0500193// The GPU backend may behave differently when images are snapped from wrapped textures and
194// render targets compared.
195namespace {
196enum SurfaceType {
197 kManaged,
198 kBackendTexture,
199 kBackendRenderTarget
200};
201}
202
203static sk_sp<SkSurface> make_surface(const SkImageInfo& ii, SkCanvas* canvas, SurfaceType type) {
204 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());
205 switch (type) {
206 case kManaged:
207 return ToolUtils::makeSurface(canvas, ii);
208 case kBackendTexture:
209 if (!direct) {
210 return nullptr;
211 }
212 return sk_gpu_test::MakeBackendTextureSurface(direct, ii, kTopLeft_GrSurfaceOrigin, 1);
213 case kBackendRenderTarget:
214 return sk_gpu_test::MakeBackendRenderTargetSurface(direct,
215 ii,
216 kTopLeft_GrSurfaceOrigin,
217 1);
218 }
219 return nullptr;
220}
221
222using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
223
224#define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H) \
225 DEF_SIMPLE_GM(name, canvas, W, H) { \
226 auto make = [canvas](const SkImageInfo& ii) { \
227 return make_surface(ii, canvas, SurfaceType::kManaged); \
228 }; \
229 main(canvas, MakeSurfaceFn(make)); \
230 }
231
232#define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H) \
233 DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) { \
234 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext()); \
235 if (!direct || direct->abandoned()) { \
236 *err_msg = "Requires non-abandoned GrDirectContext"; \
237 return skiagm::DrawResult::kSkip; \
238 } \
239 auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
240 main(canvas, MakeSurfaceFn(make)); \
241 return skiagm::DrawResult::kOk; \
242 }
243
244#define DEF_BET_SURFACE_TEST(name, canvas, main, W, H) \
245 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
246 SurfaceType::kBackendTexture, W, H)
247
248#define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H) \
249 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
250 SurfaceType::kBackendRenderTarget, W, H)
251
252// This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
253// render target.
254#define DEF_SURFACE_TESTS(name, canvas, W, H) \
255 static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
256 DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
257 DEF_BET_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
258 DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
259 static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
260
261DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
Mike Reedd94abc52017-03-06 16:37:07 -0500262 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
Brian Salomon56c78f42021-02-03 16:56:55 -0500263 sk_sp<SkSurface> surf = make(info);
Mike Reedd94abc52017-03-06 16:37:07 -0500264
265 surf->getCanvas()->clear(SK_ColorRED);
266 // its important that image survives longer than the next draw, so the surface will see
267 // an outstanding image, and have to decide if it should retain or discard those pixels
268 sk_sp<SkImage> image = surf->makeImageSnapshot();
269
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()->clear(SK_ColorBLUE);
274
275 // expect to see two rects: blue | red
Mike Reed8d29ab62021-01-23 18:10:39 -0500276 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
Mike Reedd94abc52017-03-06 16:37:07 -0500277}
Mike Reeda1361362017-03-07 09:37:29 -0500278
Brian Salomond63638b2021-03-05 14:00:07 -0500279// Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
280DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
281 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
282 sk_sp<SkSurface> surf = make(info);
283
284 surf->getCanvas()->clear(SK_ColorBLUE);
285 // its important that image survives longer than the next draw, so the surface will see
286 // an outstanding image, and have to decide if it should retain or discard those pixels
287 sk_sp<SkImage> image = surf->makeImageSnapshot();
288
289 surf->getCanvas()->clear(SK_ColorRED);
290 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
291 // it should not (we need the previous red pixels).
292 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
293 surf->getCanvas()->drawImage(image, 0, 0);
294
295 // expect to see two rects: blue | red
296 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
297}
298
299DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
300 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
301 sk_sp<SkSurface> surf = make(info);
302
303 surf->getCanvas()->clear(SK_ColorRED);
304 sk_sp<SkImage> image = surf->makeImageSnapshot();
305 // expect to see just red
306 canvas->drawImage(std::move(image), 0, 0);
307}
308
309// Like simple_snap_image but the surface dies before the image.
310DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
311 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
312 sk_sp<SkSurface> surf = make(info);
313
314 surf->getCanvas()->clear(SK_ColorRED);
315 sk_sp<SkImage> image = surf->makeImageSnapshot();
316 surf.reset();
317 // expect to see just red
318 canvas->drawImage(std::move(image), 0, 0);
319}
320
Brian Salomone4601b02022-03-30 10:12:28 -0400321DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75) {
322 auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
Brian Osman3f4ceac2022-10-28 15:09:34 +0000323 ? kRGBA_8888_SkColorType
Brian Salomone4601b02022-03-30 10:12:28 -0400324 : canvas->imageInfo().colorType();
325 auto ii = SkImageInfo::Make({32, 32},
326 ct,
327 kPremul_SkAlphaType,
328 canvas->imageInfo().refColorSpace());
Kevin Lubick5c93acf2023-05-09 12:11:43 -0400329 auto surface = SkSurfaces::Raster(ii);
Brian Salomone4601b02022-03-30 10:12:28 -0400330
331 auto nextImage = [&](SkColor color) {
332 surface->getCanvas()->clear(color);
333 SkPaint paint;
334 paint.setColor(~color | 0xFF000000);
335 surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
336 surface->height()*2/5.f,
337 surface->width() *3/5.f,
338 surface->height()*3/5.f),
339 paint);
340 return surface->makeImageSnapshot()->withDefaultMipmaps();
341 };
342
343 static constexpr int kPad = 8;
344 static const SkSamplingOptions kSampling{SkFilterMode::kLinear, SkMipmapMode::kLinear};
345
346 canvas->save();
347 for (int y = 0; y < 3; ++y) {
348 canvas->save();
349 SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
350 for (int x = 0; x < 2; ++x) {
351 auto image = nextImage(kColors[x]);
352 canvas->drawImage(image, 0, 0, kSampling);
353 canvas->translate(ii.width() + kPad, 0);
354 }
355 canvas->restore();
356 canvas->translate(0, ii.width() + kPad);
357 canvas->scale(.4f, .4f);
358 }
359 canvas->restore();
360}
361
Brian Salomon56c78f42021-02-03 16:56:55 -0500362DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
Mike Reeda1361362017-03-07 09:37:29 -0500363 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
Brian Salomon56c78f42021-02-03 16:56:55 -0500364 sk_sp<SkSurface> surf = make(info);
Mike Reeda1361362017-03-07 09:37:29 -0500365 surf->getCanvas()->clear(SK_ColorRED);
366 // its important that image survives longer than the next draw, so the surface will see
367 // an outstanding image, and have to decide if it should retain or discard those pixels
368 sk_sp<SkImage> image = surf->makeImageSnapshot();
369
370 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
371 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
372 // with a non-opaque paint.
373 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500374 paint.setAlphaf(0.25f);
Mike Reeda1361362017-03-07 09:37:29 -0500375 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
376 surf->getCanvas()->clear(SK_ColorBLUE);
377 surf->getCanvas()->restore();
378
379 // expect to see two rects: blue blended on red
Mike Reed8d29ab62021-01-23 18:10:39 -0500380 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
Mike Reeda1361362017-03-07 09:37:29 -0500381}
Mike Reed114bde82018-11-21 09:12:09 -0500382
Brian Salomon56c78f42021-02-03 16:56:55 -0500383DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
Mike Reed114bde82018-11-21 09:12:09 -0500384 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
Brian Salomon56c78f42021-02-03 16:56:55 -0500385 auto surf = make(info);
Mike Reed114bde82018-11-21 09:12:09 -0500386
387 const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
388
389 // noisy background
390 {
391 SkPoint pts[] = {{0, 0}, {40, 50}};
392 SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
Mike Reedfae8fce2019-04-03 10:27:45 -0400393 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
Mike Reed114bde82018-11-21 09:12:09 -0500394 SkPaint paint;
395 paint.setShader(sh);
396 surf->getCanvas()->drawPaint(paint);
397 }
398
399 // save away the right-hand strip, then clear it
400 sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
401 {
402 SkPaint paint;
403 paint.setBlendMode(SkBlendMode::kClear);
404 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
405 }
406
407 // draw the "foreground"
408 {
409 SkPaint paint;
410 paint.setColor(SK_ColorGREEN);
411 SkRect r = { 0, 10, 256, 35 };
412 while (r.fBottom < 256) {
413 surf->getCanvas()->drawRect(r, paint);
414 r.offset(0, r.height() * 2);
415 }
416 }
417
418 // apply the "fade"
419 {
420 SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
421 SkColor colors[] = {0xFF000000, 0};
Mike Reedfae8fce2019-04-03 10:27:45 -0400422 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed114bde82018-11-21 09:12:09 -0500423 SkPaint paint;
424 paint.setShader(sh);
425 paint.setBlendMode(SkBlendMode::kDstIn);
426 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
427 }
428
429 // restore the original strip, drawing it "under" the current foreground
430 {
431 SkPaint paint;
432 paint.setBlendMode(SkBlendMode::kDstOver);
433 surf->getCanvas()->drawImage(saveImg,
434 SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
Mike Reed8d29ab62021-01-23 18:10:39 -0500435 SkSamplingOptions(), &paint);
Mike Reed114bde82018-11-21 09:12:09 -0500436 }
437
438 // show it on screen
Mike Reedb746b1f2021-01-06 08:43:51 -0500439 surf->draw(canvas, 0, 0);
Mike Reed114bde82018-11-21 09:12:09 -0500440}