1 /* 2 * Copyright 2014 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 SkLocalMatrixShader_DEFINED 9 #define SkLocalMatrixShader_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkFlattenable.h" 13 #include "include/core/SkMatrix.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkShader.h" 16 #include "include/core/SkTypes.h" 17 #include "src/shaders/SkShaderBase.h" 18 19 #include <type_traits> 20 #include <utility> 21 22 class SkArenaAlloc; 23 class SkImage; 24 class SkReadBuffer; 25 class SkWriteBuffer; 26 enum class SkTileMode; 27 struct SkStageRec; 28 29 class SkLocalMatrixShader final : public SkShaderBase { 30 public: 31 template <typename T, typename... Args> 32 static std::enable_if_t<std::is_base_of_v<SkShader, T>, sk_sp<SkShader>> MakeWrapped(const SkMatrix * localMatrix,Args &&...args)33 MakeWrapped(const SkMatrix* localMatrix, Args&&... args) { 34 auto t = sk_make_sp<T>(std::forward<Args>(args)...); 35 if (localMatrix) { 36 return t->makeWithLocalMatrix(*localMatrix); 37 } 38 return t; 39 } 40 SkLocalMatrixShader(sk_sp<SkShader> wrapped,const SkMatrix & localMatrix)41 SkLocalMatrixShader(sk_sp<SkShader> wrapped, const SkMatrix& localMatrix) 42 : fLocalMatrix(localMatrix), fWrappedShader(std::move(wrapped)) {} 43 isOpaque()44 bool isOpaque() const override { return as_SB(fWrappedShader)->isOpaque(); } 45 46 bool isConstant() const override; 47 GradientType asGradient(GradientInfo* info, SkMatrix* localMatrix) const override; type()48 ShaderType type() const override { return ShaderType::kLocalMatrix; } 49 makeAsALocalMatrixShader(SkMatrix * localMatrix)50 sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const override { 51 if (localMatrix) { 52 *localMatrix = fLocalMatrix; 53 } 54 return fWrappedShader; 55 } 56 localMatrix()57 const SkMatrix& localMatrix() const { return fLocalMatrix; } wrappedShader()58 sk_sp<SkShader> wrappedShader() const { return fWrappedShader; } 59 60 protected: 61 void flatten(SkWriteBuffer&) const override; 62 63 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT 64 Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override; 65 #endif 66 67 SkImage* onIsAImage(SkMatrix* matrix, SkTileMode* mode) const override; 68 69 bool onAsLuminanceColor(SkColor4f*) const override; 70 71 bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; 72 73 private: 74 SK_FLATTENABLE_HOOKS(SkLocalMatrixShader) 75 76 SkMatrix fLocalMatrix; 77 sk_sp<SkShader> fWrappedShader; 78 }; 79 80 /** 81 * Replaces the CTM when used. Created to support clipShaders, which have to be evaluated 82 * using the CTM that was present at the time they were specified (which may be different 83 * from the CTM at the time something is drawn through the clip. 84 */ 85 class SkCTMShader final : public SkShaderBase { 86 public: 87 SkCTMShader(sk_sp<SkShader> proxy, const SkMatrix& ctm); 88 isOpaque()89 bool isOpaque() const override { return fProxyShader->isOpaque(); } 90 91 bool isConstant() const override; 92 GradientType asGradient(GradientInfo* info, SkMatrix* localMatrix) const override; 93 type()94 ShaderType type() const override { return ShaderType::kCTM; } 95 ctm()96 const SkMatrix& ctm() const { return fCTM; } proxyShader()97 sk_sp<SkShader> proxyShader() const { return fProxyShader; } 98 99 protected: flatten(SkWriteBuffer &)100 void flatten(SkWriteBuffer&) const override { SkASSERT(false); } 101 102 bool appendStages(const SkStageRec& rec, const SkShaders::MatrixRec&) const override; 103 104 private: 105 SK_FLATTENABLE_HOOKS(SkCTMShader) 106 107 sk_sp<SkShader> fProxyShader; 108 SkMatrix fCTM; 109 }; 110 111 #endif 112