Flutter Engine
The Flutter Engine
constcolorprocessor.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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// This test only works with the GPU backend.
9
10#include "gm/gm.h"
13#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
22#include "include/core/SkSize.h"
37#include "src/gpu/ganesh/SkGr.h"
40#include "tools/ToolUtils.h"
42#include "tools/gpu/TestOps.h"
43
44#include <utility>
45
46namespace skiagm {
47/**
48 * This GM directly exercises Color and ModulateRGBA.
49 */
50class ColorProcessor : public GpuGM {
51public:
52 enum class TestMode {
53 kConstColor,
54 kModulateRGBA
55 };
56
58 this->setBGColor(0xFFDDDDDD);
59 }
60
61protected:
62 SkString getName() const override {
63 switch (fMode) {
64 case TestMode::kConstColor: return SkString("const_color_processor");
65 case TestMode::kModulateRGBA: return SkString("modulate_rgba");
66 }
68 }
69
70 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
71
72 void onOnceBeforeDraw() override {
73 SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
74 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
75 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
77 }
78
79 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
81 if (!sdc) {
82 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
83 return DrawResult::kSkip;
84 }
85
86 constexpr GrColor kColors[] = {
87 0xFFFFFFFF,
88 0xFFFF00FF,
89 0x80000000,
90 0x00000000,
91 };
92
93 constexpr GrColor kPaintColors[] = {
94 0xFFFFFFFF,
95 0xFF0000FF,
96 0x80000080,
97 0x00000000,
98 };
99
100 SkScalar y = kPad;
101 SkScalar x = kPad;
102 SkScalar maxW = 0;
103 for (size_t paintType = 0; paintType < std::size(kPaintColors) + 1; ++paintType) {
104 for (size_t procColor = 0; procColor < std::size(kColors); ++procColor) {
105 // translate by x,y for the canvas draws and the test target draws.
106 canvas->save();
107 canvas->translate(x, y);
108
109 // rect to draw
110 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
111
112 // Create a base-layer FP for the const color processor to draw on top of.
113 std::unique_ptr<GrFragmentProcessor> baseFP;
114 if (paintType >= std::size(kPaintColors)) {
115 GrColorInfo colorInfo;
116 SkSurfaceProps props;
117 GrFPArgs args(rContext, &colorInfo, props, GrFPArgs::Scope::kDefault);
118 baseFP = GrFragmentProcessors::Make(fShader.get(), args, SkMatrix::I());
119 } else {
121 SkPMColor4f::FromBytes_RGBA(kPaintColors[paintType]));
122 }
123
124 // Layer a color/modulation FP on top of the base layer, using various colors.
125 std::unique_ptr<GrFragmentProcessor> colorFP;
126 switch (fMode) {
130 break;
131
134 std::move(baseFP), SkPMColor4f::FromBytes_RGBA(kColors[procColor]));
135 break;
136 }
137
138 // Render the FP tree.
139 if (auto op = sk_gpu_test::test_ops::MakeRect(rContext,
140 std::move(colorFP),
141 renderRect.makeOffset(x, y),
142 renderRect,
143 SkMatrix::I())) {
144 sdc->addDrawOp(std::move(op));
145 }
146
147 // Draw labels for the input to the processor and the processor to the right of
148 // the test rect. The input label appears above the processor label.
149 SkFont labelFont;
152 labelFont.setSize(10.f);
153 SkPaint labelPaint;
154 labelPaint.setAntiAlias(true);
155 SkString inputLabel("Input: ");
156 if (paintType >= std::size(kPaintColors)) {
157 inputLabel.append("gradient");
158 } else {
159 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
160 }
161 SkString procLabel;
162 procLabel.printf("Proc: [0x%08x]", kColors[procColor]);
163
164 SkRect inputLabelBounds;
165 // get the bounds of the text in order to position it
166 labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
167 SkTextEncoding::kUTF8, &inputLabelBounds);
168 canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
169 labelFont, labelPaint);
170 // update the bounds to reflect the offset we used to draw it.
171 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
172
173 SkRect procLabelBounds;
174 labelFont.measureText(procLabel.c_str(), procLabel.size(),
175 SkTextEncoding::kUTF8, &procLabelBounds);
176 canvas->drawString(procLabel, renderRect.fRight + kPad,
177 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
178 labelFont, labelPaint);
179 procLabelBounds.offset(renderRect.fRight + kPad,
180 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
181
182 labelPaint.setStrokeWidth(0);
184 canvas->drawRect(renderRect, labelPaint);
185
186 canvas->restore();
187
188 // update x and y for the next test case.
189 SkScalar height = renderRect.height();
190 SkScalar width = std::max(inputLabelBounds.fRight, procLabelBounds.fRight);
191 maxW = std::max(maxW, width);
192 y += height + kPad;
193 if (y + height > kHeight) {
194 y = kPad;
195 x += maxW + kPad;
196 maxW = 0;
197 }
198 }
199 }
200
201 return DrawResult::kOk;
202 }
203
204private:
205 // Use this as a way of generating an input FP
206 sk_sp<SkShader> fShader;
207 TestMode fMode;
208
209 inline static constexpr SkScalar kPad = 10.f;
210 inline static constexpr SkScalar kRectSize = 20.f;
211 inline static constexpr int kWidth = 820;
212 inline static constexpr int kHeight = 500;
213
214 using INHERITED = GM;
215};
216
217DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kConstColor};)
218DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kModulateRGBA};)
219
220} // namespace skiagm
uint32_t GrColor
Definition: GrColor.h:25
#define SkUNREACHABLE
Definition: SkAssert.h:135
uint32_t SkColor
Definition: SkColor.h:37
@ kUTF8
uses bytes to represent UTF-8 or ASCII
constexpr int kPad
static std::unique_ptr< GrFragmentProcessor > MakeColor(SkPMColor4f color)
static std::unique_ptr< GrFragmentProcessor > ModulateRGBA(std::unique_ptr< GrFragmentProcessor > child, const SkPMColor4f &color)
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
Definition: SkFont.h:35
void setTypeface(sk_sp< SkTypeface > tf)
Definition: SkFont.cpp:90
SkScalar measureText(const void *text, size_t byteLength, SkTextEncoding encoding, SkRect *bounds=nullptr) const
Definition: SkFont.h:336
void setEdging(Edging edging)
Definition: SkFont.cpp:121
void setSize(SkScalar textSize)
Definition: SkFont.cpp:129
@ kAntiAlias
may have transparent pixels on glyph edges
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
size_t size() const
Definition: SkString.h:131
void append(const char text[])
Definition: SkString.h:203
const char * c_str() const
Definition: SkString.h:133
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
T * get() const
Definition: SkRefCnt.h:303
DrawResult onDraw(GrRecordingContext *rContext, SkCanvas *canvas, SkString *errorMsg) override
SkString getName() const override
SkISize getISize() override
void onOnceBeforeDraw() override
Definition: gm.h:110
float SkScalar
Definition: extension.cpp:12
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double y
double x
std::unique_ptr< GrFragmentProcessor > Make(const SkMaskFilter *maskfilter, const GrFPArgs &args, const SkMatrix &ctm)
PODArray< SkColor > colors
Definition: SkRecords.h:276
sk_sp< SkTypeface > DefaultPortableTypeface()
const DlColor kColors[]
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
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 keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
GrOp::Owner MakeRect(GrRecordingContext *context, GrPaint &&paint, const SkRect &drawRect, const SkRect &localRect, const SkMatrix &localM)
Definition: TestOps.cpp:227
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition: GrCanvas.cpp:20
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
int32_t height
int32_t width
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static SkRGBA4f FromBytes_RGBA(uint32_t color)
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
void offset(float dx, float dy)
Definition: SkRect.h:1016
constexpr float height() const
Definition: SkRect.h:769
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15