• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/SkReadBuffer.h"
12 #include "src/core/SkWriteBuffer.h"
13 #include "src/shaders/SkShaderBase.h"
14 
15 class GrFragmentProcessor;
16 class SkArenaAlloc;
17 
18 class SkLocalMatrixShader final : public SkShaderBase {
19 public:
20     template <typename T, typename... Args>
21     static std::enable_if_t<std::is_base_of_v<SkShader, T>, sk_sp<SkShader>>
MakeWrapped(const SkMatrix * localMatrix,Args &&...args)22     MakeWrapped(const SkMatrix* localMatrix, Args&&... args) {
23         auto t = sk_make_sp<T>(std::forward<Args>(args)...);
24         if (!localMatrix || localMatrix->isIdentity()) {
25             return std::move(t);
26         }
27         return sk_make_sp<SkLocalMatrixShader>(sk_sp<SkShader>(std::move(t)), *localMatrix);
28     }
29 
SkLocalMatrixShader(sk_sp<SkShader> wrapped,const SkMatrix & localMatrix)30     SkLocalMatrixShader(sk_sp<SkShader> wrapped, const SkMatrix& localMatrix)
31             : fLocalMatrix(localMatrix), fWrappedShader(std::move(wrapped)) {}
32 
33     GradientType asGradient(GradientInfo* info, SkMatrix* localMatrix) const override;
34 
35 #if defined(SK_GANESH)
36     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&,
37                                                              const MatrixRec&) const override;
38 #endif
39 #if defined(SK_GRAPHITE)
40     void addToKey(const skgpu::graphite::KeyContext&,
41                   skgpu::graphite::PaintParamsKeyBuilder*,
42                   skgpu::graphite::PipelineDataGatherer*) const override;
43 #endif
44 
makeAsALocalMatrixShader(SkMatrix * localMatrix)45     sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const override {
46         if (localMatrix) {
47             *localMatrix = fLocalMatrix;
48         }
49         return fWrappedShader;
50     }
51 
52 protected:
53     void flatten(SkWriteBuffer&) const override;
54 
55 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
56     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
57 #endif
58 
59     SkImage* onIsAImage(SkMatrix* matrix, SkTileMode* mode) const override;
60 
61     bool appendStages(const SkStageRec&, const MatrixRec&) const override;
62 
63     skvm::Color program(skvm::Builder*,
64                         skvm::Coord device,
65                         skvm::Coord local,
66                         skvm::Color paint,
67                         const MatrixRec&,
68                         const SkColorInfo& dst,
69                         skvm::Uniforms* uniforms,
70                         SkArenaAlloc*) const override;
71 
72 private:
73     SK_FLATTENABLE_HOOKS(SkLocalMatrixShader)
74 
75     SkMatrix fLocalMatrix;
76     sk_sp<SkShader> fWrappedShader;
77 
78     using INHERITED = SkShaderBase;
79 };
80 
81 #endif
82