xref: /aosp_15_r20/external/skia/src/shaders/SkPictureShader.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 SkPictureShader_DEFINED
9 #define SkPictureShader_DEFINED
10 
11 #include "include/core/SkFlattenable.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPicture.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkSurfaceProps.h"
19 #include "include/core/SkTypes.h"
20 #include "src/shaders/SkShaderBase.h"
21 
22 class SkArenaAlloc;
23 class SkColorSpace;
24 class SkImage;
25 class SkReadBuffer;
26 class SkShader;
27 class SkSurface;
28 class SkWriteBuffer;
29 enum SkColorType : int;
30 enum class SkFilterMode;
31 enum class SkTileMode;
32 struct SkStageRec;
33 
34 /*
35  * An SkPictureShader can be used to draw SkPicture-based patterns.
36  *
37  * The SkPicture is first rendered into a tile, which is then used to shade the area according
38  * to specified tiling rules.
39  */
40 class SkPictureShader : public SkShaderBase {
41 public:
42     static sk_sp<SkShader> Make(sk_sp<SkPicture>, SkTileMode, SkTileMode, SkFilterMode,
43                                 const SkMatrix*, const SkRect*);
44 
45     SkPictureShader(sk_sp<SkPicture>, SkTileMode, SkTileMode, SkFilterMode, const SkRect*);
46 
type()47     ShaderType type() const override { return ShaderType::kPicture; }
48 
picture()49     sk_sp<SkPicture> picture() const { return fPicture; }
tile()50     SkRect tile() const { return fTile; }
tileModeX()51     SkTileMode tileModeX() const { return fTmx; }
tileModeY()52     SkTileMode tileModeY() const { return fTmy; }
filter()53     SkFilterMode filter() const { return fFilter; }
54 
55     struct CachedImageInfo {
56         bool success;
57         SkSize tileScale;        // Additional scale factors to apply when sampling image.
58         SkMatrix matrixForDraw;  // Matrix used to produce an image from the picture
59         SkImageInfo imageInfo;
60         SkSurfaceProps props;
61 
62         static CachedImageInfo Make(const SkRect& bounds,
63                                     const SkMatrix& totalM,
64                                     SkColorType dstColorType,
65                                     SkColorSpace* dstColorSpace,
66                                     const int maxTextureSize,
67                                     const SkSurfaceProps& propsIn);
68 
69         sk_sp<SkImage> makeImage(sk_sp<SkSurface> surf, const SkPicture* pict) const;
70     };
71 
72 protected:
73     SkPictureShader(SkReadBuffer&);
74     void flatten(SkWriteBuffer&) const override;
75     bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override;
76 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
77     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
78 #endif
79 
80 private:
81     SK_FLATTENABLE_HOOKS(SkPictureShader)
82 
83     sk_sp<SkShader> rasterShader(const SkMatrix&,
84                                  SkColorType dstColorType,
85                                  SkColorSpace* dstColorSpace,
86                                  const SkSurfaceProps& props) const;
87 
88     sk_sp<SkPicture>    fPicture;
89     SkRect              fTile;
90     SkTileMode          fTmx, fTmy;
91     SkFilterMode fFilter;
92 };
93 
94 #endif // SkPictureShader_DEFINED
95