1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
// This test only works with the GPU backend.
#include "gm.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrRenderTargetContextPriv.h"
#include "SkGrPriv.h"
#include "SkGradientShader.h"
#include "batches/GrDrawOp.h"
#include "batches/GrRectBatchFactory.h"
#include "effects/GrConstColorProcessor.h"
namespace skiagm {
/**
* This GM directly exercises GrConstColorProcessor.
*/
class ConstColorProcessor : public GM {
public:
ConstColorProcessor() {
this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
}
protected:
SkString onShortName() override {
return SkString("const_color_processor");
}
SkISize onISize() override {
return SkISize::Make(kWidth, kHeight);
}
void onOnceBeforeDraw() override {
SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
SkShader::kClamp_TileMode);
}
void onDraw(SkCanvas* canvas) override {
GrRenderTargetContext* renderTargetContext =
canvas->internal_private_accessTopLayerRenderTargetContext();
if (!renderTargetContext) {
skiagm::GM::DrawGpuOnlyMessage(canvas);
return;
}
GrContext* context = canvas->getGrContext();
if (!context) {
return;
}
constexpr GrColor kColors[] = {
0xFFFFFFFF,
0xFFFF00FF,
0x80000000,
0x00000000,
};
constexpr SkColor kPaintColors[] = {
0xFFFFFFFF,
0xFFFF0000,
0x80FF0000,
0x00000000,
};
const char* kModeStrs[] {
"kIgnore",
"kModulateRGBA",
"kModulateA",
};
GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
SkScalar y = kPad;
SkScalar x = kPad;
SkScalar maxW = 0;
for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
// translate by x,y for the canvas draws and the test target draws.
canvas->save();
canvas->translate(x, y);
const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
// rect to draw
SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
GrPaint grPaint;
SkPaint skPaint;
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
skPaint.setShader(fShader);
} else {
skPaint.setColor(kPaintColors[paintType]);
}
SkAssertResult(SkPaintToGrPaint(context, renderTargetContext, skPaint,
viewMatrix, &grPaint));
GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
GrColor4f color = GrColor4f::FromGrColor(kColors[procColor]);
sk_sp<GrFragmentProcessor> fp(GrConstColorProcessor::Make(color, mode));
grPaint.addColorFragmentProcessor(std::move(fp));
sk_sp<GrDrawOp> batch(
GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix,
renderRect, nullptr, nullptr));
renderTargetContext->priv().testingOnly_drawBatch(grPaint, GrAAType::kNone,
batch.get());
// Draw labels for the input to the processor and the processor to the right of
// the test rect. The input label appears above the processor label.
SkPaint labelPaint;
sk_tool_utils::set_portable_typeface(&labelPaint);
labelPaint.setAntiAlias(true);
labelPaint.setTextSize(10.f);
SkString inputLabel;
inputLabel.set("Input: ");
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
inputLabel.append("gradient");
} else {
inputLabel.appendf("0x%08x", kPaintColors[paintType]);
}
SkString procLabel;
procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
SkRect inputLabelBounds;
// get the bounds of the text in order to position it
labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
&inputLabelBounds);
canvas->drawText(inputLabel.c_str(), inputLabel.size(),
renderRect.fRight + kPad,
-inputLabelBounds.fTop, labelPaint);
// update the bounds to reflect the offset we used to draw it.
inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
SkRect procLabelBounds;
labelPaint.measureText(procLabel.c_str(), procLabel.size(),
&procLabelBounds);
canvas->drawText(procLabel.c_str(), procLabel.size(),
renderRect.fRight + kPad,
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
labelPaint);
procLabelBounds.offset(renderRect.fRight + kPad,
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
labelPaint.setStrokeWidth(0);
labelPaint.setStyle(SkPaint::kStroke_Style);
canvas->drawRect(renderRect, labelPaint);
canvas->restore();
// update x and y for the next test case.
SkScalar height = renderRect.height();
SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
maxW = SkTMax(maxW, width);
y += height + kPad;
if (y + height > kHeight) {
y = kPad;
x += maxW + kPad;
maxW = 0;
}
}
}
}
}
private:
// Use this as a way of generating and input FP
sk_sp<SkShader> fShader;
static constexpr SkScalar kPad = 10.f;
static constexpr SkScalar kRectSize = 20.f;
static constexpr int kWidth = 820;
static constexpr int kHeight = 500;
typedef GM INHERITED;
};
DEF_GM(return new ConstColorProcessor;)
}
#endif
|