• 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 SkPictureShader_DEFINED
9 #define SkPictureShader_DEFINED
10 
11 #include "SkShaderBase.h"
12 #include <atomic>
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 #if SK_SUPPORT_GPU
32     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
33 #endif
34 
35 protected:
36     SkPictureShader(SkReadBuffer&);
37     void flatten(SkWriteBuffer&) const override;
38     bool onAppendStages(const StageRec&) const override;
39 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
40     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
41 #endif
42     sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override;
43 
44 private:
45     SK_FLATTENABLE_HOOKS(SkPictureShader)
46 
47     SkPictureShader(sk_sp<SkPicture>, TileMode, TileMode, const SkMatrix*, const SkRect*,
48                     sk_sp<SkColorSpace>);
49 
50     sk_sp<SkShader> refBitmapShader(const SkMatrix&, SkTCopyOnFirstWrite<SkMatrix>* localMatrix,
51                                     SkColorType dstColorType, SkColorSpace* dstColorSpace,
52                                     const int maxTextureSize = 0) const;
53 
54     class PictureShaderContext : public Context {
55     public:
56         PictureShaderContext(
57             const SkPictureShader&, const ContextRec&, sk_sp<SkShader> bitmapShader, SkArenaAlloc*);
58 
59         uint32_t getFlags() const override;
60 
61         void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
62 
63         sk_sp<SkShader>         fBitmapShader;
64         SkShaderBase::Context*  fBitmapShaderContext;
65         void*                   fBitmapShaderContextStorage;
66 
67         typedef Context INHERITED;
68     };
69 
70     sk_sp<SkPicture>    fPicture;
71     SkRect              fTile;
72     TileMode            fTmx, fTmy;
73 
74     // Should never be set by a public constructor.  This is only used when onMakeColorSpace()
75     // forces a deferred color space xform.
76     sk_sp<SkColorSpace>    fColorSpace;
77 
78     const uint32_t            fUniqueID;
79     mutable std::atomic<bool> fAddedToCache;
80 
81     typedef SkShaderBase INHERITED;
82 };
83 
84 #endif // SkPictureShader_DEFINED
85