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