| 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 |
#include "src/shaders/gradients/SkConicalGradient.h" |
| 8 |
|
| 9 |
#include "include/core/SkColor.h" |
| 10 |
#include "include/core/SkColorSpace.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkShader.h" |
| 13 |
#include "include/core/SkTileMode.h" |
| 14 |
#include "include/effects/SkGradient.h" |
| 15 |
#include "include/private/base/SkAssert.h" |
| 16 |
#include "include/private/base/SkFloatingPoint.h" |
| 17 |
#include "include/private/base/SkTArray.h" |
| 18 |
#include "src/base/SkArenaAlloc.h" |
| 19 |
#include "src/core/SkRasterPipeline.h" |
| 20 |
#include "src/core/SkRasterPipelineOpContexts.h" |
| 21 |
#include "src/core/SkRasterPipelineOpList.h" |
| 22 |
#include "src/core/SkReadBuffer.h" |
| 23 |
#include "src/core/SkWriteBuffer.h" |
| 24 |
#include "src/shaders/SkShaderBase.h" |
| 25 |
#include "src/shaders/gradients/SkGradientBaseShader.h" |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <cmath> |
| 29 |
#include <cstdint> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
bool SkConicalGradient::FocalData::set(SkScalar r0, SkScalar r1, SkMatrix* matrix) { |
| 33 |
fIsSwapped = false; |
| 34 |
fFocalX = sk_ieee_float_divide(r0, (r0 - r1)); |
| 35 |
if (SkScalarNearlyZero(fFocalX - 1)) { |
| 36 |
// swap r0, r1 |
| 37 |
matrix->postTranslate(-1, 0); |
| 38 |
matrix->postScale(-1, 1); |
| 39 |
std::swap(r0, r1); |
| 40 |
fFocalX = 0; // because r0 is now 0 |
| 41 |
fIsSwapped = true; |
| 42 |
} |
| 43 |
|
| 44 |
// Map {focal point, (1, 0)} to {(0, 0), (1, 0)} |
| 45 |
const SkPoint from[2] = { {fFocalX, 0}, {1, 0} }; |
| 46 |
const SkPoint to[2] = { {0, 0}, {1, 0} }; |
| 47 |
const auto focalMatrix = SkMatrix::PolyToPoly(from, to); |
| 48 |
if (!focalMatrix) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
matrix->postConcat(*focalMatrix); |
| 52 |
fR1 = r1 / SkScalarAbs(1 - fFocalX); // focalMatrix has a scale of 1/(1-f) |
| 53 |
|
| 54 |
// The following transformations are just to accelerate the shader computation by saving |
| 55 |
// some arithmatic operations. |
| 56 |
if (this->isFocalOnCircle()) { |
| 57 |
matrix->postScale(0.5, 0.5); |
| 58 |
} else { |
| 59 |
matrix->postScale(fR1 / (fR1 * fR1 - 1), 1 / sqrt(SkScalarAbs(fR1 * fR1 - 1))); |
| 60 |
} |
| 61 |
matrix->postScale(SkScalarAbs(1 - fFocalX), SkScalarAbs(1 - fFocalX)); // scale |1 - f| |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
std::optional<SkMatrix> SkConicalGradient::MapToUnitX(const SkPoint &startCenter, |
| 66 |
const SkPoint &endCenter) { |
| 67 |
const SkPoint centers[2] = { startCenter, endCenter }; |
| 68 |
const SkPoint unitvec[2] = { {0, 0}, {1, 0} }; |
| 69 |
return SkMatrix::PolyToPoly(centers, unitvec); |
| 70 |
} |
| 71 |
|
| 72 |
sk_sp<SkShader> SkConicalGradient::Create(const SkPoint& c0, |
| 73 |
SkScalar r0, |
| 74 |
const SkPoint& c1, |
| 75 |
SkScalar r1, |
| 76 |
const SkGradient& desc, |
| 77 |
const SkMatrix* localMatrix) { |
| 78 |
SkMatrix gradientMatrix; |
| 79 |
Type gradientType; |
| 80 |
|
| 81 |
if (SkScalarNearlyZero((c0 - c1).length())) { |
| 82 |
if (SkScalarNearlyZero(std::max(r0, r1)) || SkScalarNearlyEqual(r0, r1)) { |
| 83 |
// Degenerate case; avoid dividing by zero. Should have been caught by caller but |
| 84 |
// just in case, recheck here. |
| 85 |
return nullptr; |
| 86 |
} |
| 87 |
// Concentric case: we can pretend we're radial (with a tiny twist). |
| 88 |
const SkScalar scale = sk_ieee_float_divide(1, std::max(r0, r1)); |
| 89 |
gradientMatrix = SkMatrix::Translate(-c1.x(), -c1.y()); |
| 90 |
gradientMatrix.postScale(scale, scale); |
| 91 |
|
| 92 |
gradientType = Type::kRadial; |
| 93 |
} else { |
| 94 |
auto mx = MapToUnitX(c0, c1); |
| 95 |
if (!mx) { |
| 96 |
// Degenerate case. |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
gradientMatrix = *mx; |
| 100 |
|
| 101 |
gradientType = SkScalarNearlyZero(r1 - r0) ? Type::kStrip : Type::kFocal; |
| 102 |
} |
| 103 |
|
| 104 |
FocalData focalData; |
| 105 |
if (gradientType == Type::kFocal) { |
| 106 |
const auto dCenter = (c0 - c1).length(); |
| 107 |
if (!focalData.set(r0 / dCenter, r1 / dCenter, &gradientMatrix)) { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
sk_sp<SkShader> s = sk_make_sp<SkConicalGradient>( |
| 113 |
c0, r0, c1, r1, desc, gradientType, gradientMatrix, focalData); |
| 114 |
return s->makeWithLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I()); |
| 115 |
} |
| 116 |
|
| 117 |
SkConicalGradient::SkConicalGradient(const SkPoint& start, |
| 118 |
SkScalar startRadius, |
| 119 |
const SkPoint& end, |
| 120 |
SkScalar endRadius, |
| 121 |
const SkGradient& desc, |
| 122 |
Type type, |
| 123 |
const SkMatrix& gradientMatrix, |
| 124 |
const FocalData& data) |
| 125 |
: SkGradientBaseShader(desc, gradientMatrix) |
| 126 |
, fCenter1(start) |
| 127 |
, fCenter2(end) |
| 128 |
, fRadius1(startRadius) |
| 129 |
, fRadius2(endRadius) |
| 130 |
, fType(type) { |
| 131 |
// this is degenerate, and should be caught by our caller |
| 132 |
SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2); |
| 133 |
if (type == Type::kFocal) { |
| 134 |
fFocalData = data; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
bool SkConicalGradient::isOpaque() const { |
| 139 |
// Because areas outside the cone are left untouched, we cannot treat the |
| 140 |
// shader as opaque even if the gradient itself is opaque. |
| 141 |
// TODO(junov): Compute whether the cone fills the plane crbug.com/222380 |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
// Returns the original non-sorted version of the gradient |
| 146 |
SkShaderBase::GradientType SkConicalGradient::asGradient(GradientInfo* info, |
| 147 |
SkMatrix* localMatrix) const { |
| 148 |
if (info) { |
| 149 |
commonAsAGradient(info); |
| 150 |
info->fPoint[0] = fCenter1; |
| 151 |
info->fPoint[1] = fCenter2; |
| 152 |
info->fRadius[0] = fRadius1; |
| 153 |
info->fRadius[1] = fRadius2; |
| 154 |
} |
| 155 |
if (localMatrix) { |
| 156 |
*localMatrix = SkMatrix::I(); |
| 157 |
} |
| 158 |
return GradientType::kConical; |
| 159 |
} |
| 160 |
|
| 161 |
sk_sp<SkFlattenable> SkConicalGradient::CreateProc(SkReadBuffer& buffer) { |
| 162 |
SkGradientScope scope; |
| 163 |
SkMatrix legacyLocalMatrix, *lmPtr = nullptr; |
| 164 |
auto grad = scope.unflatten(buffer, &legacyLocalMatrix); |
| 165 |
if (!grad) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
if (!legacyLocalMatrix.isIdentity()) { |
| 169 |
lmPtr = &legacyLocalMatrix; |
| 170 |
} |
| 171 |
SkPoint c1 = buffer.readPoint(); |
| 172 |
SkPoint c2 = buffer.readPoint(); |
| 173 |
SkScalar r1 = buffer.readScalar(); |
| 174 |
SkScalar r2 = buffer.readScalar(); |
| 175 |
|
| 176 |
if (!buffer.isValid()) { |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
return SkShaders::TwoPointConicalGradient(c1, r1, c2, r2, *grad, lmPtr); |
| 180 |
} |
| 181 |
|
| 182 |
void SkConicalGradient::flatten(SkWriteBuffer& buffer) const { |
| 183 |
this->SkGradientBaseShader::flatten(buffer); |
| 184 |
buffer.writePoint(fCenter1); |
| 185 |
buffer.writePoint(fCenter2); |
| 186 |
buffer.writeScalar(fRadius1); |
| 187 |
buffer.writeScalar(fRadius2); |
| 188 |
} |
| 189 |
|
| 190 |
void SkConicalGradient::appendGradientStages(SkArenaAlloc* alloc, |
| 191 |
SkRasterPipeline* p, |
| 192 |
SkRasterPipeline* postPipeline) const { |
| 193 |
const auto dRadius = fRadius2 - fRadius1; |
| 194 |
|
| 195 |
if (fType == Type::kRadial) { |
| 196 |
p->append(SkRasterPipelineOp::xy_to_radius); |
| 197 |
|
| 198 |
// Tiny twist: radial computes a t for [0, r2], but we want a t for [r1, r2]. |
| 199 |
auto scale = std::max(fRadius1, fRadius2) / dRadius; |
| 200 |
auto bias = -fRadius1 / dRadius; |
| 201 |
|
| 202 |
p->appendMatrix(alloc, SkMatrix::Translate(bias, 0) * SkMatrix::Scale(scale, 1)); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
if (fType == Type::kStrip) { |
| 207 |
auto* ctx = alloc->make<SkRasterPipelineContexts::Conical2PtCtx>(); |
| 208 |
SkScalar scaledR0 = fRadius1 / this->getCenterX1(); |
| 209 |
ctx->fP0 = scaledR0 * scaledR0; |
| 210 |
p->append(SkRasterPipelineOp::xy_to_2pt_conical_strip, ctx); |
| 211 |
p->append(SkRasterPipelineOp::mask_2pt_conical_nan, ctx); |
| 212 |
postPipeline->append(SkRasterPipelineOp::apply_vector_mask, &ctx->fMask); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
auto* ctx = alloc->make<SkRasterPipelineContexts::Conical2PtCtx>(); |
| 217 |
ctx->fP0 = 1 / fFocalData.fR1; |
| 218 |
ctx->fP1 = fFocalData.fFocalX; |
| 219 |
|
| 220 |
if (fFocalData.isFocalOnCircle()) { |
| 221 |
p->append(SkRasterPipelineOp::xy_to_2pt_conical_focal_on_circle); |
| 222 |
} else if (fFocalData.isWellBehaved()) { |
| 223 |
p->append(SkRasterPipelineOp::xy_to_2pt_conical_well_behaved, ctx); |
| 224 |
} else if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) { |
| 225 |
p->append(SkRasterPipelineOp::xy_to_2pt_conical_smaller, ctx); |
| 226 |
} else { |
| 227 |
p->append(SkRasterPipelineOp::xy_to_2pt_conical_greater, ctx); |
| 228 |
} |
| 229 |
|
| 230 |
if (!fFocalData.isWellBehaved()) { |
| 231 |
p->append(SkRasterPipelineOp::mask_2pt_conical_degenerates, ctx); |
| 232 |
} |
| 233 |
if (1 - fFocalData.fFocalX < 0) { |
| 234 |
p->append(SkRasterPipelineOp::negate_x); |
| 235 |
} |
| 236 |
if (!fFocalData.isNativelyFocal()) { |
| 237 |
p->append(SkRasterPipelineOp::alter_2pt_conical_compensate_focal, ctx); |
| 238 |
} |
| 239 |
if (fFocalData.isSwapped()) { |
| 240 |
p->append(SkRasterPipelineOp::alter_2pt_conical_unswap); |
| 241 |
} |
| 242 |
if (!fFocalData.isWellBehaved()) { |
| 243 |
postPipeline->append(SkRasterPipelineOp::apply_vector_mask, &ctx->fMask); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
sk_sp<SkShader> SkShaders::TwoPointConicalGradient(SkPoint start, float startRadius, |
| 248 |
SkPoint end, float endRadius, |
| 249 |
const SkGradient& grad, const SkMatrix* lm) { |
| 250 |
if (startRadius < 0 || endRadius < 0) { |
| 251 |
return nullptr; |
| 252 |
} |
| 253 |
|
| 254 |
const auto& colors = grad.colors(); |
| 255 |
const auto& interp = grad.interpolation(); |
| 256 |
if (!SkGradientBaseShader::ValidGradient(colors.colors(), colors.tileMode(), interp)) { |
| 257 |
return nullptr; |
| 258 |
} |
| 259 |
if (lm && !lm->invert(nullptr)) { |
| 260 |
return nullptr; |
| 261 |
} |
| 262 |
|
| 263 |
if (SkScalarNearlyZero((start - end).length(), SkGradientBaseShader::kDegenerateThreshold)) { |
| 264 |
// If the center positions are the same, then the gradient is the radial variant of a 2 pt |
| 265 |
// conical gradient, an actual radial gradient (startRadius == 0), or it is fully degenerate |
| 266 |
// (startRadius == endRadius). |
| 267 |
if (SkScalarNearlyEqual( |
| 268 |
startRadius, endRadius, SkGradientBaseShader::kDegenerateThreshold)) { |
| 269 |
// Degenerate case, where the interpolation region area approaches zero. The proper |
| 270 |
// behavior depends on the tile mode, which is consistent with the default degenerate |
| 271 |
// gradient behavior, except when mode = clamp and the radii > 0. |
| 272 |
if (colors.tileMode() == SkTileMode::kClamp && |
| 273 |
endRadius > SkGradientBaseShader::kDegenerateThreshold) { |
| 274 |
// The interpolation region becomes an infinitely thin ring at the radius, so the |
| 275 |
// final gradient will be the first color repeated from p=0 to 1, and then a hard |
| 276 |
// stop switching to the last color at p=1. |
| 277 |
static constexpr SkScalar circlePos[3] = {0, 1, 1}; |
| 278 |
SkColor4f front = colors.colors().front(); |
| 279 |
SkColor4f reColors[3] = {front, front, colors.colors().back()}; |
| 280 |
SkGradient::Colors newColors = { |
| 281 |
reColors, circlePos, colors.tileMode(), colors.colorSpace() |
| 282 |
}; |
| 283 |
return SkShaders::RadialGradient(start, endRadius, {newColors, interp}, lm); |
| 284 |
} else { |
| 285 |
return SkGradientBaseShader::MakeDegenerateGradient(colors); |
| 286 |
} |
| 287 |
} else if (SkScalarNearlyZero(startRadius, SkGradientBaseShader::kDegenerateThreshold)) { |
| 288 |
// We can treat this gradient as radial, which is faster. If we got here, we know |
| 289 |
// that endRadius is not equal to 0, so this produces a meaningful gradient |
| 290 |
return SkShaders::RadialGradient(start, endRadius, grad, lm); |
| 291 |
} |
| 292 |
// Else it's the 2pt conical radial variant with no degenerate radii, so fall through to the |
| 293 |
// regular 2pt constructor. |
| 294 |
} |
| 295 |
|
| 296 |
SkColor4f tmp2Colors[2]; |
| 297 |
SkSpan<const SkColor4f> c4 = colors.colors(); |
| 298 |
SkSpan<const float> pos = colors.positions(); |
| 299 |
if (c4.size() == 1) { |
| 300 |
tmp2Colors[0] = tmp2Colors[1] = c4[0]; |
| 301 |
c4 = {tmp2Colors}; |
| 302 |
pos = {}; |
| 303 |
} |
| 304 |
SkGradient::Colors newColors = { |
| 305 |
c4, pos, colors.tileMode(), colors.colorSpace() |
| 306 |
}; |
| 307 |
|
| 308 |
return SkConicalGradient::Create(start, startRadius, end, endRadius, {newColors, interp}, lm); |
| 309 |
} |
| 310 |
|
| 311 |
void SkRegisterConicalGradientShaderFlattenable() { |
| 312 |
SK_REGISTER_FLATTENABLE(SkConicalGradient); |
| 313 |
// Previous name |
| 314 |
SkFlattenable::Register("SkTwoPointConicalGradient", SkConicalGradient::CreateProc); |
| 315 |
} |