Flutter Engine
The Flutter Engine
clippedbitmapshaders.cpp
Go to the documentation of this file.
1/*
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 */
7
8#include "gm/gm.h"
14#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
21
22namespace {
23
24// This GM draws a 3x3 grid (with the center element excluded) of rectangles
25// filled with a bitmap shader. The bitmap shader is transformed so that the
26// pattern cell is at the center (excluded) region.
27//
28// In Repeat and Mirror mode, this tests that the bitmap shader still draws
29// even though the pattern cell is outside the clip.
30//
31// In Clamp mode, this tests that the clamp is handled properly. For PDF,
32// (and possibly other exported formats) this also "tests" that the image itself
33// is not stored (well, you'll need to open it up with an external tool to
34// verify that).
35
36static SkBitmap create_bitmap() {
37 SkBitmap bmp;
38 bmp.allocN32Pixels(2, 2);
39 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
44
45 return bmp;
46}
47
48constexpr SkScalar RECT_SIZE = 64;
49constexpr SkScalar SLIDE_SIZE = 300;
50
51class ClippedBitmapShadersGM : public skiagm::GM {
52public:
53 ClippedBitmapShadersGM(SkTileMode mode, bool hq=false)
54 : fMode(mode), fHQ(hq) {
55 }
56
57protected:
59 bool fHQ;
60
61 SkString getName() const override {
62 SkString descriptor;
63 switch (fMode) {
65 descriptor = "tile";
66 break;
68 descriptor = "mirror";
69 break;
71 descriptor = "clamp";
72 break;
74 descriptor = "decal";
75 break;
76 }
77 descriptor.prepend("clipped-bitmap-shaders-");
78 if (fHQ) {
79 descriptor.append("-hq");
80 }
81 return descriptor;
82 }
83
84 SkISize getISize() override { return {300, 300}; }
85
86 void onDraw(SkCanvas* canvas) override {
87 SkBitmap bmp = create_bitmap();
88 SkMatrix s;
89 s.reset();
90 s.setScale(8, 8);
91 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
93 paint.setShader(bmp.makeShader(fMode, fMode,
96 s));
97
98 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
99 for (int i = 0; i < 3; i++) {
100 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
101 for (int j = 0; j < 3; j++) {
102 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
103 if (i == 1 && j == 1) {
104 continue; // skip center element
105 }
106 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
107 RECT_SIZE, RECT_SIZE);
108 canvas->save();
109 canvas->clipRect(rect);
110 canvas->drawRect(rect, paint);
111 canvas->restore();
112 }
113 }
114 }
115
116private:
117 using INHERITED = GM;
118};
119} // namespace
120
121DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
122DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
123DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
124
125DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
126DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
127DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )
SK_API SkPMColor SkPreMultiplyColor(SkColor c)
Definition: SkColor.cpp:21
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
SkTileMode
Definition: SkTileMode.h:13
constexpr int SLIDE_SIZE
void * getPixels() const
Definition: SkBitmap.h:283
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
int save()
Definition: SkCanvas.cpp:447
void append(const char text[])
Definition: SkString.h:203
void prepend(const char text[])
Definition: SkString.h:215
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
virtual SkISize getISize()=0
virtual SkString getName() const =0
virtual DrawResult onDraw(SkCanvas *, SkString *errorMsg)
Definition: gm.cpp:139
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
#define DEF_GM(CODE)
Definition: gm.h:40
static SkBitmap create_bitmap(SkIRect contentRect, SkISize fullSize, GrSurfaceOrigin origin)
Definition: lazytiling.cpp:90
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr SkCubicResampler Mitchell()
Definition: SkSize.h:16
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
SkBlendMode fMode
Definition: xfermodes.cpp:52