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 "SkAtomics.h"
12 #include "SkShaderBase.h"
13 
14 class SkArenaAlloc;
15 class SkBitmap;
16 class SkPicture;
17 
18 /*
19  * An SkPictureShader can be used to draw SkPicture-based patterns.
20  *
21  * The SkPicture is first rendered into a tile, which is then used to shade the area according
22  * to specified tiling rules.
23  */
24 class SkPictureShader : public SkShaderBase {
25 public:
26     ~SkPictureShader() override;
27 
28     static sk_sp<SkShader> Make(sk_sp<SkPicture>, TileMode, TileMode, const SkMatrix*,
29                                 const SkRect*);
30 
31     SK_TO_STRING_OVERRIDE()
32     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPictureShader)
33 
34 #if SK_SUPPORT_GPU
35     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
36 #endif
37 
38 protected:
39     SkPictureShader(SkReadBuffer&);
40     void flatten(SkWriteBuffer&) const override;
41     bool onAppendStages(const StageRec&) const override;
42     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
43     sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override;
44     bool onIsRasterPipelineOnly(const SkMatrix&) const override;
45 
46 private:
47     SkPictureShader(sk_sp<SkPicture>, TileMode, TileMode, const SkMatrix*, const SkRect*,
48                     sk_sp<SkColorSpace>);
49 
50     sk_sp<SkShader> refBitmapShader(const SkMatrix&, const SkMatrix* localMatrix,
51                                     SkColorSpace* dstColorSpace,
52                                     SkMatrix* compositeLocalMatrix,
53                                     const int maxTextureSize = 0) const;
54 
55     class PictureShaderContext : public Context {
56     public:
57         PictureShaderContext(
58             const SkPictureShader&, const ContextRec&, sk_sp<SkShader> bitmapShader, SkArenaAlloc*);
59 
60         uint32_t getFlags() const override;
61 
62         void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
63 
64         sk_sp<SkShader>         fBitmapShader;
65         SkShaderBase::Context*  fBitmapShaderContext;
66         void*                   fBitmapShaderContextStorage;
67 
68         typedef Context INHERITED;
69     };
70 
71     sk_sp<SkPicture>    fPicture;
72     SkRect              fTile;
73     TileMode            fTmx, fTmy;
74 
75     // Should never be set by a public constructor.  This is only used when onMakeColorSpace()
76     // forces a deferred color space xform.
77     sk_sp<SkColorSpace>    fColorSpace;
78 
79     const uint32_t         fUniqueID;
80     mutable SkAtomic<bool> fAddedToCache;
81 
82     typedef SkShaderBase INHERITED;
83 };
84 
85 #endif // SkPictureShader_DEFINED
86