Debian Codegraph @

gm/constcolorprocessor.cpp

raw
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"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkMatrix.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"
24 #include "include/core/SkTileMode.h"
25 #include "include/core/SkTypeface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkGradient.h"
28 #include "include/private/gpu/ganesh/GrTypesPriv.h"
29 #include "src/core/SkCanvasPriv.h"
30 #include "src/core/SkColorData.h"
31 #include "src/gpu/ganesh/GrCanvas.h"
32 #include "src/gpu/ganesh/GrColor.h"
33 #include "src/gpu/ganesh/GrFPArgs.h"
34 #include "src/gpu/ganesh/GrFragmentProcessor.h"
35 #include "src/gpu/ganesh/GrFragmentProcessors.h"
36 #include "src/gpu/ganesh/GrPaint.h"
37 #include "src/gpu/ganesh/SkGr.h"
38 #include "src/gpu/ganesh/SurfaceDrawContext.h"
39 #include "src/gpu/ganesh/ops/GrOp.h"
40 #include "tools/ToolUtils.h"
41 #include "tools/fonts/FontToolUtils.h"
42 #include "tools/ganesh/TestOps.h"
43
44 #include <utility>
45
46 namespace skiagm {
47 /**
48 * This GM directly exercises Color and ModulateRGBA.
49 */
50 class ColorProcessor : public GpuGM {
51 public:
52 enum class TestMode {
53 kConstColor,
54 kModulateRGBA
55 };
56
57 ColorProcessor(TestMode mode) : fMode(mode) {
58 this->setBGColor(0xFFDDDDDD);
59 }
60
61 protected:
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 }
67 SkUNREACHABLE;
68 }
69
70 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
71
72 void onOnceBeforeDraw() override {
73 const SkColor4f colors[] = { {1,0,0,1}, {0,1,0,0x20/255.f}, {0,0,1,1}};
74 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
75 fShader = SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}});
76 }
77
78 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
79 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
80 if (!sdc) {
81 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
82 return DrawResult::kSkip;
83 }
84
85 constexpr GrColor kColors[] = {
86 0xFFFFFFFF,
87 0xFFFF00FF,
88 0x80000000,
89 0x00000000,
90 };
91
92 constexpr GrColor kPaintColors[] = {
93 0xFFFFFFFF,
94 0xFF0000FF,
95 0x80000080,
96 0x00000000,
97 };
98
99 SkScalar y = kPad;
100 SkScalar x = kPad;
101 SkScalar maxW = 0;
102 for (size_t paintType = 0; paintType < std::size(kPaintColors) + 1; ++paintType) {
103 for (size_t procColor = 0; procColor < std::size(kColors); ++procColor) {
104 // translate by x,y for the canvas draws and the test target draws.
105 canvas->save();
106 canvas->translate(x, y);
107
108 // rect to draw
109 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
110
111 // Create a base-layer FP for the const color processor to draw on top of.
112 std::unique_ptr<GrFragmentProcessor> baseFP;
113 if (paintType >= std::size(kPaintColors)) {
114 GrColorInfo colorInfo;
115 SkSurfaceProps props;
116 GrFPArgs args(sdc, &colorInfo, props, GrFPArgs::Scope::kDefault);
117 baseFP = GrFragmentProcessors::Make(fShader.get(), args, SkMatrix::I());
118 } else {
119 baseFP = GrFragmentProcessor::MakeColor(
120 SkPMColor4f::FromBytes_RGBA(kPaintColors[paintType]));
121 }
122
123 // Layer a color/modulation FP on top of the base layer, using various colors.
124 std::unique_ptr<GrFragmentProcessor> colorFP;
125 switch (fMode) {
126 case TestMode::kConstColor:
127 colorFP = GrFragmentProcessor::MakeColor(
128 SkPMColor4f::FromBytes_RGBA(kColors[procColor]));
129 break;
130
131 case TestMode::kModulateRGBA:
132 colorFP = GrFragmentProcessor::ModulateRGBA(
133 std::move(baseFP), SkPMColor4f::FromBytes_RGBA(kColors[procColor]));
134 break;
135 }
136
137 // Render the FP tree.
138 if (auto op = sk_gpu_test::test_ops::MakeRect(rContext,
139 std::move(colorFP),
140 renderRect.makeOffset(x, y),
141 renderRect,
142 SkMatrix::I())) {
143 sdc->addDrawOp(std::move(op));
144 }
145
146 // Draw labels for the input to the processor and the processor to the right of
147 // the test rect. The input label appears above the processor label.
148 SkFont labelFont;
149 labelFont.setTypeface(ToolUtils::DefaultPortableTypeface());
150 labelFont.setEdging(SkFont::Edging::kAntiAlias);
151 labelFont.setSize(10.f);
152 SkPaint labelPaint;
153 labelPaint.setAntiAlias(true);
154 SkString inputLabel("Input: ");
155 if (paintType >= std::size(kPaintColors)) {
156 inputLabel.append("gradient");
157 } else {
158 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
159 }
160 SkString procLabel;
161 procLabel.printf("Proc: [0x%08x]", kColors[procColor]);
162
163 SkRect inputLabelBounds;
164 // get the bounds of the text in order to position it
165 labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
166 SkTextEncoding::kUTF8, &inputLabelBounds);
167 canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
168 labelFont, labelPaint);
169 // update the bounds to reflect the offset we used to draw it.
170 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
171
172 SkRect procLabelBounds;
173 labelFont.measureText(procLabel.c_str(), procLabel.size(),
174 SkTextEncoding::kUTF8, &procLabelBounds);
175 canvas->drawString(procLabel, renderRect.fRight + kPad,
176 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
177 labelFont, labelPaint);
178 procLabelBounds.offset(renderRect.fRight + kPad,
179 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
180
181 labelPaint.setStrokeWidth(0);
182 labelPaint.setStyle(SkPaint::kStroke_Style);
183 canvas->drawRect(renderRect, labelPaint);
184
185 canvas->restore();
186
187 // update x and y for the next test case.
188 SkScalar height = renderRect.height();
189 SkScalar width = std::max(inputLabelBounds.fRight, procLabelBounds.fRight);
190 maxW = std::max(maxW, width);
191 y += height + kPad;
192 if (y + height > kHeight) {
193 y = kPad;
194 x += maxW + kPad;
195 maxW = 0;
196 }
197 }
198 }
199
200 return DrawResult::kOk;
201 }
202
203 private:
204 // Use this as a way of generating an input FP
205 sk_sp<SkShader> fShader;
206 TestMode fMode;
207
208 inline static constexpr SkScalar kPad = 10.f;
209 inline static constexpr SkScalar kRectSize = 20.f;
210 inline static constexpr int kWidth = 820;
211 inline static constexpr int kHeight = 500;
212
213 using INHERITED = GM;
214 };
215
216 DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kConstColor};)
217 DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kModulateRGBA};)
218
219 } // namespace skiagm