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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Sk4fLinearGradient.h"
#include "SkColorSpaceXformer.h"
#include "SkLinearGradient.h"
#include "SkRefCnt.h"
static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) {
SkVector vec = pts[1] - pts[0];
SkScalar mag = vec.length();
SkScalar inv = mag ? SkScalarInvert(mag) : 0;
vec.scale(inv);
SkMatrix matrix;
matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
matrix.postTranslate(-pts[0].fX, -pts[0].fY);
matrix.postScale(inv, inv);
return matrix;
}
///////////////////////////////////////////////////////////////////////////////
SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc)
: SkGradientShaderBase(desc, pts_to_unit_matrix(pts))
, fStart(pts[0])
, fEnd(pts[1]) {
}
sk_sp<SkFlattenable> SkLinearGradient::CreateProc(SkReadBuffer& buffer) {
DescriptorScope desc;
if (!desc.unflatten(buffer)) {
return nullptr;
}
SkPoint pts[2];
pts[0] = buffer.readPoint();
pts[1] = buffer.readPoint();
return SkGradientShader::MakeLinear(pts, desc.fColors, std::move(desc.fColorSpace), desc.fPos,
desc.fCount, desc.fTileMode, desc.fGradFlags,
desc.fLocalMatrix);
}
void SkLinearGradient::flatten(SkWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.writePoint(fStart);
buffer.writePoint(fEnd);
}
SkShaderBase::Context* SkLinearGradient::onMakeContext(
const ContextRec& rec, SkArenaAlloc* alloc) const
{
return CheckedMakeContext<LinearGradient4fContext>(alloc, *this, rec);
}
SkShaderBase::Context* SkLinearGradient::onMakeBurstPipelineContext(
const ContextRec& rec, SkArenaAlloc* alloc) const {
// Raster pipeline has a 2-stop specialization faster than our burst.
return fColorCount > 2 ? CheckedMakeContext<LinearGradient4fContext>(alloc, *this, rec)
: nullptr;
}
void SkLinearGradient::appendGradientStages(SkArenaAlloc*, SkRasterPipeline*,
SkRasterPipeline*) const {
// No extra stage needed for linear gradients.
}
sk_sp<SkShader> SkLinearGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
SkPoint pts[2] = { fStart, fEnd };
SkSTArray<8, SkColor> xformedColors(fColorCount);
xformer->apply(xformedColors.begin(), fOrigColors, fColorCount);
return SkGradientShader::MakeLinear(pts, xformedColors.begin(), fOrigPos, fColorCount,
fTileMode, fGradFlags, &this->getLocalMatrix());
}
SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
if (info) {
commonAsAGradient(info);
info->fPoint[0] = fStart;
info->fPoint[1] = fEnd;
}
return kLinear_GradientType;
}
#if SK_SUPPORT_GPU
#include "GrColorSpaceXform.h"
#include "GrShaderCaps.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "SkGr.h"
/////////////////////////////////////////////////////////////////////
class GrLinearGradient : public GrGradientEffect {
public:
class GLSLLinearProcessor;
static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args) {
auto processor = std::unique_ptr<GrLinearGradient>(new GrLinearGradient(args));
return processor->isValid() ? std::move(processor) : nullptr;
}
const char* name() const override { return "Linear Gradient"; }
std::unique_ptr<GrFragmentProcessor> clone() const override {
return std::unique_ptr<GrFragmentProcessor>(new GrLinearGradient(*this));
}
private:
explicit GrLinearGradient(const CreateArgs& args)
: INHERITED(args, args.fShader->colorsAreOpaque()) {
this->initClassID<GrLinearGradient>();
}
explicit GrLinearGradient(const GrLinearGradient& that) : INHERITED(that) {
this->initClassID<GrLinearGradient>();
}
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
GrProcessorKeyBuilder* b) const override;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST
typedef GrGradientEffect INHERITED;
};
/////////////////////////////////////////////////////////////////////
class GrLinearGradient::GLSLLinearProcessor : public GrGradientEffect::GLSLProcessor {
public:
GLSLLinearProcessor(const GrProcessor&) {}
virtual void emitCode(EmitArgs&) override;
static void GenKey(const GrProcessor& processor, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
b->add32(GenBaseGradientKey(processor));
}
private:
typedef GrGradientEffect::GLSLProcessor INHERITED;
};
/////////////////////////////////////////////////////////////////////
GrGLSLFragmentProcessor* GrLinearGradient::onCreateGLSLInstance() const {
return new GrLinearGradient::GLSLLinearProcessor(*this);
}
void GrLinearGradient::onGetGLSLProcessorKey(const GrShaderCaps& caps,
GrProcessorKeyBuilder* b) const {
GrLinearGradient::GLSLLinearProcessor::GenKey(*this, caps, b);
}
/////////////////////////////////////////////////////////////////////
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrLinearGradient);
#if GR_TEST_UTILS
std::unique_ptr<GrFragmentProcessor> GrLinearGradient::TestCreate(GrProcessorTestData* d) {
SkPoint points[] = {{d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()},
{d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}};
RandomGradientParams params(d->fRandom);
auto shader = params.fUseColors4f ?
SkGradientShader::MakeLinear(points, params.fColors4f, params.fColorSpace, params.fStops,
params.fColorCount, params.fTileMode) :
SkGradientShader::MakeLinear(points, params.fColors, params.fStops,
params.fColorCount, params.fTileMode);
GrTest::TestAsFPArgs asFPArgs(d);
std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
GrAlwaysAssert(fp);
return fp;
}
#endif
/////////////////////////////////////////////////////////////////////
void GrLinearGradient::GLSLLinearProcessor::emitCode(EmitArgs& args) {
const GrLinearGradient& ge = args.fFp.cast<GrLinearGradient>();
this->emitUniforms(args.fUniformHandler, ge);
SkString t = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
t.append(".x");
this->emitColor(args.fFragBuilder,
args.fUniformHandler,
args.fShaderCaps,
ge,
t.c_str(),
args.fOutputColor,
args.fInputColor,
args.fTexSamplers);
}
/////////////////////////////////////////////////////////////////////
std::unique_ptr<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(
const AsFPArgs& args) const {
SkASSERT(args.fContext);
SkMatrix matrix;
if (!this->getLocalMatrix().invert(&matrix)) {
return nullptr;
}
if (args.fLocalMatrix) {
SkMatrix inv;
if (!args.fLocalMatrix->invert(&inv)) {
return nullptr;
}
matrix.postConcat(inv);
}
matrix.postConcat(fPtsToUnit);
sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(),
args.fDstColorSpace);
auto inner = GrLinearGradient::Make(GrGradientEffect::CreateArgs(
args.fContext, this, &matrix, fTileMode, std::move(colorSpaceXform),
SkToBool(args.fDstColorSpace)));
if (!inner) {
return nullptr;
}
return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
}
#endif
#ifndef SK_IGNORE_TO_STRING
void SkLinearGradient::toString(SkString* str) const {
str->append("SkLinearGradient (");
str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
this->INHERITED::toString(str);
str->append(")");
}
#endif
|