Flutter Engine
The Flutter Engine
coordclampshader.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 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
8#include "gm/gm.h"
13#include "include/core/SkRect.h"
16#include "tools/DecodeUtils.h"
17#include "tools/Resources.h"
18
19DEF_SIMPLE_GM(coordclampshader, canvas, 1074, 795) {
20 auto image = ToolUtils::GetResourceAsImage("images/mandrill_256.png");
21 if (!image) {
22 return;
23 }
24 // The mandrill_512 image has a bottom row of mostly black pixels. Remove it.
27
28 auto imageShader = image->makeShader(SkFilterMode::kLinear);
29
31
32 auto drawRect = SkRect::Make(image->dimensions());
33
34 auto rotate = SkMatrix::RotateDeg(45.f, drawRect.center());
35
36 auto clampRect = drawRect.makeInset(20, 40);
37
38 canvas->translate(10, 10);
39
40 auto shader = SkShaders::CoordClamp(imageShader, clampRect);
41 paint.setShader(std::move(shader));
42 canvas->drawRect(drawRect, paint);
43
44 canvas->save();
45 canvas->translate(image->width(), 0);
46 shader = SkShaders::CoordClamp(imageShader->makeWithLocalMatrix(rotate), clampRect);
47 paint.setShader(std::move(shader));
48 canvas->drawRect(drawRect, paint);
49 canvas->restore();
50
51 canvas->save();
52 canvas->translate(0, image->height());
53 shader = SkShaders::CoordClamp(imageShader, clampRect)->makeWithLocalMatrix(rotate);
54 paint.setShader(std::move(shader));
55 canvas->drawRect(drawRect, paint);
56 canvas->restore();
57
58 canvas->save();
59 canvas->translate(image->width(), image->height());
60 shader = SkShaders::CoordClamp(imageShader->makeWithLocalMatrix(rotate), clampRect)
62 paint.setShader(std::move(shader));
63 canvas->drawRect(drawRect, paint);
64 canvas->restore();
65
66 canvas->translate(0, 2 * image->height() + 10);
67
68 static const SkSamplingOptions kSamplers[] = {
73 };
74
75 for (const auto& sampler : kSamplers) {
78 sampler,
79 SkMatrix::Scale(0.3f, 1.0));
80
81 shader = SkShaders::CoordClamp(imageShader, clampRect);
82 paint.setShader(std::move(shader));
83 canvas->drawRect(drawRect, paint);
84
85 canvas->translate(image->width() + 10, 0);
86 }
87}
static bool rotate(const SkDCubic &cubic, int zero, int index, SkDCubic &rotPath)
SkISize dimensions() const
Definition: SkImage.h:297
sk_sp< SkImage > withDefaultMipmaps() const
Definition: SkImage.cpp:305
int width() const
Definition: SkImage.h:285
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
int height() const
Definition: SkImage.h:291
virtual sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const =0
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
static SkMatrix RotateDeg(SkScalar deg)
Definition: SkMatrix.h:104
sk_sp< SkShader > makeWithLocalMatrix(const SkMatrix &) const
Definition: SkShader.cpp:26
const Paint & paint
Definition: color_source.cc:38
DEF_SIMPLE_GM(coordclampshader, canvas, 1074, 795)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkShader > CoordClamp(sk_sp< SkShader >, const SkRect &subset)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static constexpr SkSamplingOptions Aniso(int maxAniso)