Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
pictureshadertile.cpp
Go to the documentation of this file.
1/*
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
8#include "gm/gm.h"
16#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
24
26constexpr SkScalar kFillSize = 100;
27constexpr unsigned kRowSize = 6;
28
29constexpr struct {
32} tiles[] = {
33 { 0, 0, 1, 1, 0, 0 },
34 { -0.5f, -0.5f, 1, 1, 0, 0 },
35 { 0.5f, 0.5f, 1, 1, 0, 0 },
36
37 { 0, 0, 1.5f, 1.5f, 0, 0 },
38 { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 },
39 { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 },
40
41 { 0, 0, 0.5f, 0.5f, 0, 0 },
42 { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 },
43 { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 },
44
45 { 0, 0, 1, 1, 0.5f, 0.5f },
46 { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f },
47 { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f },
48
49 { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f },
50 { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
51 { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
52
53 { 0, 0, 1.5f, 1, 0, 0 },
54 { -0.5f, -0.5f, 1.5f, 1, 0, 0 },
55 { 0.5f, 0.5f, 1.5f, 1, 0, 0 },
56
57 { 0, 0, 0.5f, 1, 0, 0 },
58 { 0.25f, 0.25f, 0.5f, 1, 0, 0 },
59 { -0.25f, -0.25f, 0.5f, 1, 0, 0 },
60
61 { 0, 0, 1, 1.5f, 0, 0 },
62 { -0.5f, -0.5f, 1, 1.5f, 0, 0 },
63 { 0.5f, 0.5f, 1, 1.5f, 0, 0 },
64
65 { 0, 0, 1, 0.5f, 0, 0 },
66 { 0.25f, 0.25f, 1, 0.5f, 0, 0 },
67 { -0.25f, -0.25f, 1, 0.5f, 0, 0 },
68};
69
70static void draw_scene(SkCanvas* canvas, SkScalar pictureSize) {
71 canvas->clear(SK_ColorWHITE);
72
75 paint.setAntiAlias(true);
76
77 paint.setColor(SK_ColorGREEN);
78 canvas->drawCircle(pictureSize / 4, pictureSize / 4, pictureSize / 4, paint);
79 paint.setColor(SK_ColorBLUE);
80 canvas->drawRect(SkRect::MakeXYWH(pictureSize / 2, pictureSize / 2,
81 pictureSize / 2, pictureSize / 2), paint);
82
83 paint.setColor(SK_ColorRED);
84 canvas->drawLine(pictureSize / 2, pictureSize * 1 / 3,
85 pictureSize / 2, pictureSize * 2 / 3, paint);
86 canvas->drawLine(pictureSize * 1 / 3, pictureSize / 2,
87 pictureSize * 2 / 3, pictureSize / 2, paint);
88
89 paint.setColor(SK_ColorBLACK);
91 canvas->drawRect(SkRect::MakeWH(pictureSize, pictureSize), paint);
92}
93
95protected:
96 SkString getName() const override { return SkString("pictureshadertile"); }
97
98 SkISize getISize() override { return SkISize::Make(800, 600); }
99
100 void onOnceBeforeDraw() override {
101 SkPictureRecorder recorder;
102 SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPictureSize);
103 draw_scene(pictureCanvas, kPictureSize);
104 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
105
106 SkPoint offset = SkPoint::Make(100, 100);
107 pictureCanvas = recorder.beginRecording(SkRect::MakeXYWH(offset.x(), offset.y(),
109 pictureCanvas->translate(offset.x(), offset.y());
110 draw_scene(pictureCanvas, kPictureSize);
111 sk_sp<SkPicture> offsetPicture(recorder.finishRecordingAsPicture());
112
113 for (unsigned i = 0; i < std::size(tiles); ++i) {
115 tiles[i].y * kPictureSize,
116 tiles[i].w * kPictureSize,
117 tiles[i].h * kPictureSize);
118 SkMatrix localMatrix;
119 localMatrix.setTranslate(tiles[i].offsetX * kPictureSize,
121 localMatrix.postScale(kFillSize / (2 * kPictureSize),
122 kFillSize / (2 * kPictureSize));
123
124 sk_sp<SkPicture> pictureRef = picture;
125 SkRect* tilePtr = &tile;
126
128 // When the tile == picture bounds, exercise the picture + offset path.
129 pictureRef = offsetPicture;
130 tilePtr = nullptr;
131 }
132
133 fShaders[i] = pictureRef->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
134 SkFilterMode::kNearest, &localMatrix, tilePtr);
135 }
136 }
137
138 void onDraw(SkCanvas* canvas) override {
139 canvas->clear(SK_ColorBLACK);
140
142 paint.setStyle(SkPaint::kFill_Style);
143
144 for (unsigned i = 0; i < std::size(fShaders); ++i) {
145 paint.setShader(fShaders[i]);
146
147 canvas->save();
148 canvas->translate((i % kRowSize) * kFillSize * 1.1f,
149 (i / kRowSize) * kFillSize * 1.1f);
151 canvas->restore();
152 }
153 }
154
155private:
156 sk_sp<SkShader> fShaders[std::size(tiles)];
157
158 using INHERITED = GM;
159};
160
161DEF_GM(return new PictureShaderTileGM;)
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
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SK_Scalar1
Definition SkScalar.h:18
SkString getName() const override
SkISize getISize() override
void onOnceBeforeDraw() override
void onDraw(SkCanvas *canvas) override
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
int save()
Definition SkCanvas.cpp:451
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
SkMatrix & postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition SkMatrix.cpp:360
SkMatrix & setTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:254
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
SkScalar offsetX
constexpr SkScalar kPictureSize
SkScalar w
SkScalar offsetY
constexpr SkScalar kFillSize
SkScalar y
static void draw_scene(SkCanvas *canvas, SkScalar pictureSize)
constexpr struct @268 tiles[]
SkScalar x
constexpr unsigned kRowSize
SkScalar h
Point offset
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkPoint Make(float x, float y)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609