| 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 "include/core/SkColor.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkShader.h" |
| 16 |
#include "include/effects/SkGradient.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "include/private/base/SkTArray.h" |
| 19 |
#include "src/core/SkReadBuffer.h" |
| 20 |
#include "src/core/SkWriteBuffer.h" |
| 21 |
#include "src/shaders/SkShaderBase.h" |
| 22 |
|
| 23 |
#include <cstdint> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
class SkArenaAlloc; |
| 27 |
class SkRasterPipeline; |
| 28 |
enum class SkTileMode; |
| 29 |
|
| 30 |
static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) { |
| 31 |
SkVector vec = pts[1] - pts[0]; |
| 32 |
SkScalar mag = vec.length(); |
| 33 |
SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 34 |
|
| 35 |
vec.scale(inv); |
| 36 |
SkMatrix matrix; |
| 37 |
matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 38 |
matrix.postTranslate(-pts[0].fX, -pts[0].fY); |
| 39 |
matrix.postScale(inv, inv); |
| 40 |
return matrix; |
| 41 |
} |
| 42 |
|
| 43 |
/////////////////////////////////////////////////////////////////////////////// |
| 44 |
|
| 45 |
SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const SkGradient& desc) |
| 46 |
: SkGradientBaseShader(desc, pts_to_unit_matrix(pts)), fStart(pts[0]), fEnd(pts[1]) {} |
| 47 |
|
| 48 |
sk_sp<SkFlattenable> SkLinearGradient::CreateProc(SkReadBuffer& buffer) { |
| 49 |
SkGradientScope scope; |
| 50 |
SkMatrix legacyLocalMatrix, *lmPtr = nullptr; |
| 51 |
auto grad = scope.unflatten(buffer, &legacyLocalMatrix); |
| 52 |
if (!grad) { |
| 53 |
return nullptr; |
| 54 |
} |
| 55 |
if (!legacyLocalMatrix.isIdentity()) { |
| 56 |
lmPtr = &legacyLocalMatrix; |
| 57 |
} |
| 58 |
SkPoint pts[2]; |
| 59 |
pts[0] = buffer.readPoint(); |
| 60 |
pts[1] = buffer.readPoint(); |
| 61 |
return SkShaders::LinearGradient(pts, *grad, lmPtr); |
| 62 |
} |
| 63 |
|
| 64 |
void SkLinearGradient::flatten(SkWriteBuffer& buffer) const { |
| 65 |
this->INHERITED::flatten(buffer); |
| 66 |
buffer.writePoint(fStart); |
| 67 |
buffer.writePoint(fEnd); |
| 68 |
} |
| 69 |
|
| 70 |
void SkLinearGradient::appendGradientStages(SkArenaAlloc*, SkRasterPipeline*, |
| 71 |
SkRasterPipeline*) const { |
| 72 |
// No extra stage needed for linear gradients. |
| 73 |
} |
| 74 |
|
| 75 |
SkShaderBase::GradientType SkLinearGradient::asGradient(GradientInfo* info, |
| 76 |
SkMatrix* localMatrix) const { |
| 77 |
if (info) { |
| 78 |
commonAsAGradient(info); |
| 79 |
info->fPoint[0] = fStart; |
| 80 |
info->fPoint[1] = fEnd; |
| 81 |
} |
| 82 |
if (localMatrix) { |
| 83 |
*localMatrix = SkMatrix::I(); |
| 84 |
} |
| 85 |
return GradientType::kLinear; |
| 86 |
} |
| 87 |
|
| 88 |
sk_sp<SkShader> SkShaders::LinearGradient(const SkPoint pts[2], const SkGradient& grad, |
| 89 |
const SkMatrix* lm) { |
| 90 |
if (!pts || !SkIsFinite((pts[1] - pts[0]).length())) { |
| 91 |
return nullptr; |
| 92 |
} |
| 93 |
|
| 94 |
GRADIENT_FACTORY_EARLY_EXIT(grad, lm); |
| 95 |
|
| 96 |
if (SkScalarNearlyZero((pts[1] - pts[0]).length(), |
| 97 |
SkGradientBaseShader::kDegenerateThreshold)) { |
| 98 |
// Degenerate gradient, the only tricky complication is when in clamp mode, the limit of |
| 99 |
// the gradient approaches two half planes of solid color (first and last). However, they |
| 100 |
// are divided by the line perpendicular to the start and end point, which becomes undefined |
| 101 |
// once start and end are exactly the same, so just use the end color for a stable solution. |
| 102 |
return SkGradientBaseShader::MakeDegenerateGradient(grad.colors()); |
| 103 |
} |
| 104 |
|
| 105 |
sk_sp<SkShader> s = sk_make_sp<SkLinearGradient>(pts, grad); |
| 106 |
return s->makeWithLocalMatrix(lm ? *lm : SkMatrix::I()); |
| 107 |
} |
| 108 |
|
| 109 |
void SkRegisterLinearGradientShaderFlattenable() { |
| 110 |
SK_REGISTER_FLATTENABLE(SkLinearGradient); |
| 111 |
} |