SkPictureShader.h revision e901b6de3ef8dea842008a08fc81e92fb1478d61
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
13class SkBitmap;
14class 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 */
22class SkPictureShader : public SkShader {
23public:
24    static SkPictureShader* Create(SkPicture*, TileMode, TileMode);
25    virtual ~SkPictureShader();
26
27    virtual bool validContext(const ContextRec&, SkMatrix* totalInverse) const SK_OVERRIDE;
28    virtual SkShader::Context* createContext(const ContextRec&, void* storage) const SK_OVERRIDE;
29    virtual size_t contextSize() const SK_OVERRIDE;
30
31    class PictureShaderContext : public SkShader::Context {
32    public:
33        PictureShaderContext(const SkPictureShader&, const ContextRec&, SkShader* bitmapShader);
34        virtual ~PictureShaderContext();
35
36        virtual uint32_t getFlags() const SK_OVERRIDE;
37
38        virtual ShadeProc asAShadeProc(void** ctx) SK_OVERRIDE;
39        virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
40        virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
41
42    private:
43        SkAutoTUnref<SkShader>  fBitmapShader;
44        SkShader::Context*      fBitmapShaderContext;
45        void*                   fBitmapShaderContextStorage;
46
47        typedef SkShader::Context INHERITED;
48    };
49
50    SK_TO_STRING_OVERRIDE()
51    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPictureShader)
52
53#if SK_SUPPORT_GPU
54    GrEffectRef* asNewEffect(GrContext*, const SkPaint&) const SK_OVERRIDE;
55#endif
56
57protected:
58    SkPictureShader(SkReadBuffer&);
59    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
60
61private:
62    SkPictureShader(SkPicture*, TileMode, TileMode);
63
64    SkShader* validInternal(const ContextRec&, SkMatrix* totalInverse) const;
65    SkShader* refBitmapShader(const SkMatrix&) const;
66
67    SkPicture*  fPicture;
68    TileMode    fTmx, fTmy;
69
70    mutable SkMutex                 fCachedBitmapShaderMutex;
71    mutable SkAutoTUnref<SkShader>  fCachedBitmapShader;
72    mutable SkSize                  fCachedTileScale;
73    mutable SkMatrix                fCachedLocalMatrix;
74
75    typedef SkShader INHERITED;
76};
77
78#endif // SkPictureShader_DEFINED
79