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/SkTileMode.h"
12 #include "src/shaders/SkShaderBase.h"
13 #include <atomic>
14 
15 class SkArenaAlloc;
16 class SkBitmap;
17 class SkPicture;
18 
19 /*
20  * An SkPictureShader can be used to draw SkPicture-based patterns.
21  *
22  * The SkPicture is first rendered into a tile, which is then used to shade the area according
23  * to specified tiling rules.
24  */
25 class SkPictureShader : public SkShaderBase {
26 public:
27     ~SkPictureShader() override;
28 
29     static sk_sp<SkShader> Make(sk_sp<SkPicture>, SkTileMode, SkTileMode, const SkMatrix*,
30                                 const SkRect*);
31 
32 #if SK_SUPPORT_GPU
33     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
34 #endif
35 
36     SkPicture* isAPicture(SkMatrix*, SkTileMode[2], SkRect* tile) const override;
37 
38 protected:
39     SkPictureShader(SkReadBuffer&);
40     void flatten(SkWriteBuffer&) const override;
41     bool onAppendStages(const SkStageRec&) const override;
42 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
43     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
44 #endif
45 
46 private:
47     SK_FLATTENABLE_HOOKS(SkPictureShader)
48 
49     SkPictureShader(sk_sp<SkPicture>, SkTileMode, SkTileMode, const SkMatrix*, const SkRect*);
50 
51     sk_sp<SkShader> refBitmapShader(const SkMatrix&, SkTCopyOnFirstWrite<SkMatrix>* localMatrix,
52                                     SkColorType dstColorType, SkColorSpace* dstColorSpace,
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     SkTileMode          fTmx, fTmy;
74 
75     const uint32_t            fUniqueID;
76     mutable std::atomic<bool> fAddedToCache;
77 
78     typedef SkShaderBase INHERITED;
79 };
80 
81 #endif // SkPictureShader_DEFINED
82