• 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 "SkShader.h"
12 
13 class SkBitmap;
14 class SkPicture;
15 
16 /*
17  * An SkPictureShader can be used to draw SkPicture-based patterns.
18  *
19  * The SkPicture is first rendered into a tile, which is then used to shade the area according
20  * to specified tiling rules.
21  */
22 class SkPictureShader : public SkShader {
23 public:
24     static SkPictureShader* Create(SkPicture*, TileMode, TileMode, const SkMatrix* = NULL);
25     virtual ~SkPictureShader();
26 
27     virtual size_t contextSize() const SK_OVERRIDE;
28 
29     SK_TO_STRING_OVERRIDE()
30     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPictureShader)
31 
32     bool asNewEffect(GrContext*, const SkPaint&, const SkMatrix*, GrColor*, GrEffectRef**)
33         const SK_OVERRIDE;
34 
35 protected:
36     SkPictureShader(SkReadBuffer&);
37     virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
38     virtual Context* onCreateContext(const ContextRec&, void* storage) const SK_OVERRIDE;
39 
40 private:
41     SkPictureShader(SkPicture*, TileMode, TileMode, const SkMatrix* = NULL);
42 
43     SkShader* refBitmapShader(const SkMatrix&, const SkMatrix* localMatrix) const;
44 
45     SkPicture*  fPicture;
46     TileMode    fTmx, fTmy;
47 
48     mutable SkMutex                 fCachedBitmapShaderMutex;
49     mutable SkAutoTUnref<SkShader>  fCachedBitmapShader;
50     mutable SkSize                  fCachedTileScale;
51     mutable SkMatrix                fCachedLocalMatrix;
52 
53     class PictureShaderContext : public SkShader::Context {
54     public:
55         static Context* Create(void* storage, const SkPictureShader&, const ContextRec&,
56                                SkShader* bitmapShader);
57 
58         virtual ~PictureShaderContext();
59 
60         virtual uint32_t getFlags() const SK_OVERRIDE;
61 
62         virtual ShadeProc asAShadeProc(void** ctx) SK_OVERRIDE;
63         virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
64         virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
65 
66     private:
67         PictureShaderContext(const SkPictureShader&, const ContextRec&, SkShader* bitmapShader);
68 
69         SkAutoTUnref<SkShader>  fBitmapShader;
70         SkShader::Context*      fBitmapShaderContext;
71         void*                   fBitmapShaderContextStorage;
72 
73         typedef SkShader::Context INHERITED;
74     };
75 
76     typedef SkShader INHERITED;
77 };
78 
79 #endif // SkPictureShader_DEFINED
80