1 /*
2 * Copyright 2012 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 "src/shaders/gradients/SkLinearGradient.h"
9
10 #include "src/core/SkReadBuffer.h"
11 #include "src/core/SkWriteBuffer.h"
12 #include "src/shaders/gradients/Sk4fLinearGradient.h"
13
pts_to_unit_matrix(const SkPoint pts[2])14 static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) {
15 SkVector vec = pts[1] - pts[0];
16 SkScalar mag = vec.length();
17 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
18
19 vec.scale(inv);
20 SkMatrix matrix;
21 matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
22 matrix.postTranslate(-pts[0].fX, -pts[0].fY);
23 matrix.postScale(inv, inv);
24 return matrix;
25 }
26
27 ///////////////////////////////////////////////////////////////////////////////
28
SkLinearGradient(const SkPoint pts[2],const Descriptor & desc)29 SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc)
30 : SkGradientShaderBase(desc, pts_to_unit_matrix(pts))
31 , fStart(pts[0])
32 , fEnd(pts[1]) {
33 }
34
CreateProc(SkReadBuffer & buffer)35 sk_sp<SkFlattenable> SkLinearGradient::CreateProc(SkReadBuffer& buffer) {
36 DescriptorScope desc;
37 if (!desc.unflatten(buffer)) {
38 return nullptr;
39 }
40 SkPoint pts[2];
41 pts[0] = buffer.readPoint();
42 pts[1] = buffer.readPoint();
43 return SkGradientShader::MakeLinear(pts, desc.fColors, std::move(desc.fColorSpace), desc.fPos,
44 desc.fCount, desc.fTileMode, desc.fGradFlags,
45 desc.fLocalMatrix);
46 }
47
flatten(SkWriteBuffer & buffer) const48 void SkLinearGradient::flatten(SkWriteBuffer& buffer) const {
49 this->INHERITED::flatten(buffer);
50 buffer.writePoint(fStart);
51 buffer.writePoint(fEnd);
52 }
53
54 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
onMakeContext(const ContextRec & rec,SkArenaAlloc * alloc) const55 SkShaderBase::Context* SkLinearGradient::onMakeContext(
56 const ContextRec& rec, SkArenaAlloc* alloc) const
57 {
58 // make sure our colorspaces are compatible with legacy blits
59 if (!rec.isLegacyCompatible(fColorSpace.get())) {
60 return nullptr;
61 }
62 // Can't use legacy blit if we can't represent our colors as SkColors
63 if (!this->colorsCanConvertToSkColor()) {
64 return nullptr;
65 }
66
67 return fTileMode != SkTileMode::kDecal
68 ? CheckedMakeContext<LinearGradient4fContext>(alloc, *this, rec)
69 : nullptr;
70 }
71 #endif
72
appendGradientStages(SkArenaAlloc *,SkRasterPipeline *,SkRasterPipeline *) const73 void SkLinearGradient::appendGradientStages(SkArenaAlloc*, SkRasterPipeline*,
74 SkRasterPipeline*) const {
75 // No extra stage needed for linear gradients.
76 }
77
asAGradient(GradientInfo * info) const78 SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
79 if (info) {
80 commonAsAGradient(info);
81 info->fPoint[0] = fStart;
82 info->fPoint[1] = fEnd;
83 }
84 return kLinear_GradientType;
85 }
86
87 /////////////////////////////////////////////////////////////////////
88
89 #if SK_SUPPORT_GPU
90
91 #include "src/gpu/gradients/GrGradientShader.h"
92
asFragmentProcessor(const GrFPArgs & args) const93 std::unique_ptr<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(
94 const GrFPArgs& args) const {
95 return GrGradientShader::MakeLinear(*this, args);
96 }
97
98 #endif
99