| 1 |
/* |
| 2 |
* Copyright 2017 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 |
#ifndef SkShaderBase_DEFINED |
| 9 |
#define SkShaderBase_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkFlattenable.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkShader.h" |
| 18 |
#include "include/core/SkSurfaceProps.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/private/base/SkNoncopyable.h" |
| 21 |
|
| 22 |
#include <cstddef> |
| 23 |
#include <cstdint> |
| 24 |
#include <optional> |
| 25 |
#include <tuple> |
| 26 |
|
| 27 |
class SkArenaAlloc; |
| 28 |
class SkColorSpace; |
| 29 |
class SkImage; |
| 30 |
class SkRuntimeEffect; |
| 31 |
class SkWriteBuffer; |
| 32 |
enum SkColorType : int; |
| 33 |
enum class SkTileMode; |
| 34 |
struct SkDeserialProcs; |
| 35 |
struct SkStageRec; |
| 36 |
|
| 37 |
namespace SkShaders { |
| 38 |
/** |
| 39 |
* This is used to accumulate matrices, starting with the CTM, when building up |
| 40 |
* SkRasterPipeline or GrFragmentProcessor by walking the SkShader tree. It avoids |
| 41 |
* adding a matrix multiply for each individual matrix. It also handles the reverse matrix |
| 42 |
* concatenation order required by Android Framework, see b/256873449. |
| 43 |
* |
| 44 |
* This also tracks the dubious concept of a "total matrix", in the legacy Context/shadeSpan system. |
| 45 |
* That includes all the matrices encountered during traversal to the current shader, including ones |
| 46 |
* that have already been applied. The total matrix represents the transformation from the current |
| 47 |
* shader's coordinate space to device space. It is dubious because it doesn't account for SkShaders |
| 48 |
* that manipulate the coordinates passed to their children, which may not even be representable by |
| 49 |
* a matrix. |
| 50 |
* |
| 51 |
* The total matrix is used for mipmap level selection and a filter downgrade optimizations in |
| 52 |
* SkImageShader and sizing of the SkImage created by SkPictureShader. If we can remove usages |
| 53 |
* of the "total matrix" and if Android Framework could be updated to not use backwards local |
| 54 |
* matrix concatenation this could just be replaced by a simple SkMatrix or SkM44 passed down |
| 55 |
* during traversal. |
| 56 |
*/ |
| 57 |
class MatrixRec { |
| 58 |
public: |
| 59 |
MatrixRec() = default; |
| 60 |
|
| 61 |
explicit MatrixRec(const SkMatrix& ctm); |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns a new MatrixRec that represents the existing total and pending matrix |
| 65 |
* pre-concat'ed with m. |
| 66 |
*/ |
| 67 |
[[nodiscard]] MatrixRec concat(const SkMatrix& m) const; |
| 68 |
|
| 69 |
/** |
| 70 |
* Appends a mul by the inverse of the pending local matrix to the pipeline. 'postInv' is an |
| 71 |
* additional matrix to post-apply to the inverted pending matrix. If the pending matrix is |
| 72 |
* not invertible the std::optional result won't have a value and the pipeline will be |
| 73 |
* unmodified. |
| 74 |
*/ |
| 75 |
[[nodiscard]] std::optional<MatrixRec> apply(const SkStageRec& rec, |
| 76 |
const SkMatrix& postInv = {}) const; |
| 77 |
|
| 78 |
/** |
| 79 |
* FP matrices work differently than SkRasterPipeline. The starting coordinates provided to the |
| 80 |
* root SkShader's FP are already in local space. So we never apply the inverse CTM. This |
| 81 |
* returns the inverted pending local matrix with the provided postInv matrix applied after it. |
| 82 |
* If the pending local matrix cannot be inverted, the boolean is false. |
| 83 |
*/ |
| 84 |
std::tuple<SkMatrix, bool> applyForFragmentProcessor(const SkMatrix& postInv) const; |
| 85 |
|
| 86 |
/** |
| 87 |
* A parent FP may need to create a FP for its child by calling |
| 88 |
* SkShaderBase::asFragmentProcessor() and then pass the result to the apply() above. |
| 89 |
* This comes up when the parent needs to ensure pending matrices are applied before the |
| 90 |
* child because the parent is going to manipulate the coordinates *after* any pending |
| 91 |
* matrix and pass the resulting coords to the child. This function gets a MatrixRec that |
| 92 |
* reflects the state after this MatrixRec has bee applied but it does not apply it! |
| 93 |
* Example: |
| 94 |
* auto childFP = fChild->asFragmentProcessor(args, mrec.applied()); |
| 95 |
* childFP = MakeAWrappingFPThatModifiesChildsCoords(std::move(childFP)); |
| 96 |
* auto [success, parentFP] = mrec.apply(std::move(childFP)); |
| 97 |
*/ |
| 98 |
MatrixRec applied() const; |
| 99 |
|
| 100 |
/** Call to indicate that the mapping from shader to device space is not known. */ |
| 101 |
void markTotalMatrixInvalid() { fTotalMatrixIsValid = false; } |
| 102 |
|
| 103 |
/** Marks the CTM as already applied; can avoid re-seeding the shader unnecessarily. */ |
| 104 |
void markCTMApplied() { fCTMApplied = true; } |
| 105 |
|
| 106 |
/** |
| 107 |
* Indicates whether the total matrix of a MatrixRec passed to a SkShader actually |
| 108 |
* represents the full transform between that shader's coordinate space and device space. |
| 109 |
*/ |
| 110 |
bool totalMatrixIsValid() const { return fTotalMatrixIsValid; } |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets the total transform from the current shader's space to device space. This may or |
| 114 |
* may not be valid. Shaders should avoid making decisions based on this matrix if |
| 115 |
* totalMatrixIsValid() is false. |
| 116 |
*/ |
| 117 |
SkMatrix totalMatrix() const { return SkMatrix::Concat(fCTM, fTotalLocalMatrix); } |
| 118 |
|
| 119 |
/** Gets the inverse of totalMatrix(), if invertible. */ |
| 120 |
std::optional<SkMatrix> totalInverse() const { |
| 121 |
return this->totalMatrix().invert(); |
| 122 |
} |
| 123 |
|
| 124 |
/** Is there a transform that has not yet been applied by a parent shader? */ |
| 125 |
bool hasPendingMatrix() const { |
| 126 |
return (!fCTMApplied && !fCTM.isIdentity()) || !fPendingLocalMatrix.isIdentity(); |
| 127 |
} |
| 128 |
|
| 129 |
/** When generating raster pipeline, have the device coordinates been seeded? */ |
| 130 |
bool rasterPipelineCoordsAreSeeded() const { return fCTMApplied; } |
| 131 |
|
| 132 |
private: |
| 133 |
MatrixRec(const SkMatrix& ctm, |
| 134 |
const SkMatrix& totalLocalMatrix, |
| 135 |
const SkMatrix& pendingLocalMatrix, |
| 136 |
bool totalIsValid, |
| 137 |
bool ctmApplied) |
| 138 |
: fCTM(ctm) |
| 139 |
, fTotalLocalMatrix(totalLocalMatrix) |
| 140 |
, fPendingLocalMatrix(pendingLocalMatrix) |
| 141 |
, fTotalMatrixIsValid(totalIsValid) |
| 142 |
, fCTMApplied(ctmApplied) {} |
| 143 |
|
| 144 |
const SkMatrix fCTM; |
| 145 |
|
| 146 |
// Concatenation of all local matrices, including those already applied. |
| 147 |
const SkMatrix fTotalLocalMatrix; |
| 148 |
|
| 149 |
// The accumulated local matrices from walking down the shader hierarchy that have NOT yet |
| 150 |
// been incorporated into the SkRasterPipeline. |
| 151 |
const SkMatrix fPendingLocalMatrix; |
| 152 |
|
| 153 |
bool fTotalMatrixIsValid = true; |
| 154 |
|
| 155 |
// Tracks whether the CTM has already been applied (and in raster pipeline whether the |
| 156 |
// device coords have been seeded.) |
| 157 |
bool fCTMApplied = false; |
| 158 |
}; |
| 159 |
|
| 160 |
} // namespace SkShaders |
| 161 |
|
| 162 |
#define SK_ALL_SHADERS(M) \ |
| 163 |
M(Blend) \ |
| 164 |
M(CTM) \ |
| 165 |
M(Color) \ |
| 166 |
M(ColorFilter) \ |
| 167 |
M(CoordClamp) \ |
| 168 |
M(Empty) \ |
| 169 |
M(GradientBase) \ |
| 170 |
M(Image) \ |
| 171 |
M(LocalMatrix) \ |
| 172 |
M(PerlinNoise) \ |
| 173 |
M(Picture) \ |
| 174 |
M(Runtime) \ |
| 175 |
M(Transform) \ |
| 176 |
M(TriColor) \ |
| 177 |
M(WorkingColorSpace) |
| 178 |
|
| 179 |
#define SK_ALL_GRADIENTS(M) \ |
| 180 |
M(Conical) \ |
| 181 |
M(Linear) \ |
| 182 |
M(Radial) \ |
| 183 |
M(Sweep) |
| 184 |
|
| 185 |
class SkShaderBase : public SkShader { |
| 186 |
public: |
| 187 |
~SkShaderBase() override; |
| 188 |
|
| 189 |
uint32_t uniqueID() const { return fUniqueID; } |
| 190 |
|
| 191 |
sk_sp<SkShader> makeInvertAlpha() const; |
| 192 |
sk_sp<SkShader> makeWithCTM(const SkMatrix&) const; // owns its own ctm |
| 193 |
|
| 194 |
/** |
| 195 |
* Returns true if the shader is guaranteed to produce only a single color |
| 196 |
* If the color parameter is non-null, it is filled in with that color. |
| 197 |
* Subclasses can override this to allow loop-hoisting optimization. |
| 198 |
*/ |
| 199 |
virtual bool isConstant(SkColor4f* color = nullptr) const { return false; } |
| 200 |
|
| 201 |
enum class ShaderType { |
| 202 |
#define M(type) k##type, |
| 203 |
SK_ALL_SHADERS(M) |
| 204 |
#undef M |
| 205 |
}; |
| 206 |
|
| 207 |
virtual ShaderType type() const = 0; |
| 208 |
|
| 209 |
enum class GradientType { |
| 210 |
kNone, |
| 211 |
#define M(type) k##type, |
| 212 |
SK_ALL_GRADIENTS(M) |
| 213 |
#undef M |
| 214 |
}; |
| 215 |
|
| 216 |
/** |
| 217 |
* If the shader subclass can be represented as a gradient, asGradient |
| 218 |
* returns the matching GradientType enum (or GradientType::kNone if it |
| 219 |
* cannot). Also, if info is not null, asGradient populates info with |
| 220 |
* the relevant (see below) parameters for the gradient. fColorCount |
| 221 |
* is both an input and output parameter. On input, it indicates how |
| 222 |
* many entries in fColors and fColorOffsets can be used, if they are |
| 223 |
* non-NULL. After asGradient has run, fColorCount indicates how |
| 224 |
* many color-offset pairs there are in the gradient. If there is |
| 225 |
* insufficient space to store all of the color-offset pairs, fColors |
| 226 |
* and fColorOffsets will not be altered. fColorOffsets specifies |
| 227 |
* where on the range of 0 to 1 to transition to the given color. |
| 228 |
* The meaning of fPoint and fRadius is dependent on the type of gradient. |
| 229 |
* |
| 230 |
* None: |
| 231 |
* info is ignored. |
| 232 |
* Color: |
| 233 |
* fColorOffsets[0] is meaningless. |
| 234 |
* Linear: |
| 235 |
* fPoint[0] and fPoint[1] are the end-points of the gradient |
| 236 |
* Radial: |
| 237 |
* fPoint[0] and fRadius[0] are the center and radius |
| 238 |
* Conical: |
| 239 |
* fPoint[0] and fRadius[0] are the center and radius of the 1st circle |
| 240 |
* fPoint[1] and fRadius[1] are the center and radius of the 2nd circle |
| 241 |
* Sweep: |
| 242 |
* fPoint[0] is the center of the sweep. |
| 243 |
* fPoint[1] x is the scale, y is the bias |
| 244 |
*/ |
| 245 |
struct GradientInfo { |
| 246 |
int fColorCount = 0; //!< In-out parameter, specifies passed size |
| 247 |
// of fColors/fColorOffsets on input, and |
| 248 |
// actual number of colors/offsets on |
| 249 |
// output. |
| 250 |
SkColor4f* fColors = nullptr; //!< The colors in the gradient. |
| 251 |
SkScalar* fColorOffsets = nullptr; //!< The unit offset for color transitions. |
| 252 |
SkPoint fPoint[2]; //!< Type specific, see above. |
| 253 |
SkScalar fRadius[2]; //!< Type specific, see above. |
| 254 |
SkTileMode fTileMode; |
| 255 |
bool fPremulInterp; |
| 256 |
}; |
| 257 |
|
| 258 |
virtual GradientType asGradient(GradientInfo* info = nullptr, |
| 259 |
SkMatrix* localMatrix = nullptr) const { |
| 260 |
return GradientType::kNone; |
| 261 |
} |
| 262 |
|
| 263 |
enum Flags { |
| 264 |
//!< set if all of the colors will be opaque |
| 265 |
kOpaqueAlpha_Flag = 1 << 0, |
| 266 |
}; |
| 267 |
|
| 268 |
/** |
| 269 |
* ContextRec acts as a parameter bundle for creating Contexts. |
| 270 |
*/ |
| 271 |
struct ContextRec { |
| 272 |
ContextRec(SkAlpha paintAlpha, |
| 273 |
const SkShaders::MatrixRec& matrixRec, |
| 274 |
SkColorType dstColorType, |
| 275 |
SkColorSpace* dstColorSpace, |
| 276 |
const SkSurfaceProps& props) |
| 277 |
: fMatrixRec(matrixRec) |
| 278 |
, fDstColorType(dstColorType) |
| 279 |
, fDstColorSpace(dstColorSpace) |
| 280 |
, fProps(props) |
| 281 |
, fPaintAlpha(paintAlpha) {} |
| 282 |
|
| 283 |
static ContextRec Concat(const ContextRec& parentRec, const SkMatrix& localM) { |
| 284 |
return {parentRec.fPaintAlpha, |
| 285 |
parentRec.fMatrixRec.concat(localM), |
| 286 |
parentRec.fDstColorType, |
| 287 |
parentRec.fDstColorSpace, |
| 288 |
parentRec.fProps}; |
| 289 |
} |
| 290 |
|
| 291 |
const SkShaders::MatrixRec fMatrixRec; |
| 292 |
SkColorType fDstColorType; // the color type of the dest surface |
| 293 |
SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any) |
| 294 |
SkSurfaceProps fProps; // props of the dest surface |
| 295 |
SkAlpha fPaintAlpha; |
| 296 |
|
| 297 |
bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const; |
| 298 |
}; |
| 299 |
|
| 300 |
class Context : public ::SkNoncopyable { |
| 301 |
public: |
| 302 |
Context(const SkShaderBase& shader, const ContextRec&); |
| 303 |
|
| 304 |
virtual ~Context(); |
| 305 |
|
| 306 |
/** |
| 307 |
* Called sometimes before drawing with this shader. Return the type of |
| 308 |
* alpha your shader will return. The default implementation returns 0. |
| 309 |
* Your subclass should override if it can (even sometimes) report a |
| 310 |
* non-zero value, since that will enable various blitters to perform |
| 311 |
* faster. |
| 312 |
*/ |
| 313 |
virtual uint32_t getFlags() const { return 0; } |
| 314 |
|
| 315 |
/** |
| 316 |
* Called for each span of the object being drawn. Your subclass should |
| 317 |
* set the appropriate colors (with premultiplied alpha) that correspond |
| 318 |
* to the specified device coordinates. |
| 319 |
*/ |
| 320 |
virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; |
| 321 |
|
| 322 |
protected: |
| 323 |
// Reference to shader, so we don't have to dupe information. |
| 324 |
const SkShaderBase& fShader; |
| 325 |
|
| 326 |
uint8_t getPaintAlpha() const { return fPaintAlpha; } |
| 327 |
|
| 328 |
private: |
| 329 |
uint8_t fPaintAlpha; |
| 330 |
}; |
| 331 |
|
| 332 |
/** |
| 333 |
* Make a context using the memory provided by the arena. |
| 334 |
* |
| 335 |
* @return pointer to context or nullptr if can't be created |
| 336 |
*/ |
| 337 |
Context* makeContext(const ContextRec&, SkArenaAlloc*) const; |
| 338 |
|
| 339 |
/** |
| 340 |
* If the shader can represent its "average" luminance in a single color, return true and |
| 341 |
* if color is not NULL, return that color. If it cannot, return false and ignore the color |
| 342 |
* parameter. |
| 343 |
* |
| 344 |
* Note: if this returns true, the returned color will always be opaque, as only the RGB |
| 345 |
* components are used to compute luminance. |
| 346 |
*/ |
| 347 |
bool asLuminanceColor(SkColor4f*) const; |
| 348 |
|
| 349 |
/** |
| 350 |
* If this returns false, then we draw nothing (do not fall back to shader context). This should |
| 351 |
* only be called on a root-level effect. It assumes that the initial device coordinates have |
| 352 |
* not yet been seeded. |
| 353 |
*/ |
| 354 |
[[nodiscard]] bool appendRootStages(const SkStageRec& rec, const SkMatrix& ctm) const; |
| 355 |
|
| 356 |
/** |
| 357 |
* Adds stages to implement this shader. To ensure that the correct input coords are present |
| 358 |
* in r,g MatrixRec::apply() must be called (unless the shader doesn't require it's input |
| 359 |
* coords). The default impl creates shadercontext and calls that (not very efficient). |
| 360 |
*/ |
| 361 |
virtual bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const = 0; |
| 362 |
|
| 363 |
virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const { |
| 364 |
return nullptr; |
| 365 |
} |
| 366 |
|
| 367 |
virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; } |
| 368 |
|
| 369 |
static Type GetFlattenableType() { return kSkShader_Type; } |
| 370 |
Type getFlattenableType() const override { return GetFlattenableType(); } |
| 371 |
|
| 372 |
static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size, |
| 373 |
const SkDeserialProcs* procs = nullptr) { |
| 374 |
return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>( |
| 375 |
SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release())); |
| 376 |
} |
| 377 |
static void RegisterFlattenables(); |
| 378 |
|
| 379 |
/** DEPRECATED. skbug.com/40040221 |
| 380 |
* If this shader can be represented by another shader + a localMatrix, return that shader and |
| 381 |
* the localMatrix. If not, return nullptr and ignore the localMatrix parameter. |
| 382 |
*/ |
| 383 |
virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const; |
| 384 |
|
| 385 |
static SkMatrix ConcatLocalMatrices(const SkMatrix& parentLM, const SkMatrix& childLM) { |
| 386 |
#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) // b/256873449 |
| 387 |
return SkMatrix::Concat(childLM, parentLM); |
| 388 |
#endif |
| 389 |
return SkMatrix::Concat(parentLM, childLM); |
| 390 |
} |
| 391 |
|
| 392 |
protected: |
| 393 |
SkShaderBase(); |
| 394 |
|
| 395 |
void flatten(SkWriteBuffer&) const override; |
| 396 |
|
| 397 |
#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
| 398 |
/** |
| 399 |
* Specialize creating a SkShader context using the supplied allocator. |
| 400 |
* @return pointer to context owned by the arena allocator. |
| 401 |
*/ |
| 402 |
virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const { |
| 403 |
return nullptr; |
| 404 |
} |
| 405 |
#endif |
| 406 |
|
| 407 |
virtual bool onAsLuminanceColor(SkColor4f*) const { |
| 408 |
return false; |
| 409 |
} |
| 410 |
|
| 411 |
private: |
| 412 |
const uint32_t fUniqueID; |
| 413 |
|
| 414 |
friend class SkShaders::MatrixRec; |
| 415 |
}; |
| 416 |
inline SkShaderBase* as_SB(SkShader* shader) { |
| 417 |
return static_cast<SkShaderBase*>(shader); |
| 418 |
} |
| 419 |
|
| 420 |
inline const SkShaderBase* as_SB(const SkShader* shader) { |
| 421 |
return static_cast<const SkShaderBase*>(shader); |
| 422 |
} |
| 423 |
|
| 424 |
inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) { |
| 425 |
return static_cast<SkShaderBase*>(shader.get()); |
| 426 |
} |
| 427 |
|
| 428 |
void SkRegisterBlendShaderFlattenable(); |
| 429 |
void SkRegisterColorShaderFlattenable(); |
| 430 |
void SkRegisterCoordClampShaderFlattenable(); |
| 431 |
void SkRegisterEmptyShaderFlattenable(); |
| 432 |
void SkRegisterPerlinNoiseShaderFlattenable(); |
| 433 |
void SkRegisterWorkingColorSpaceShaderFlattenable(); |
| 434 |
|
| 435 |
#endif // SkShaderBase_DEFINED |