1
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkPictureShader_DEFINED
10#define SkPictureShader_DEFINED
11
12#include "SkShader.h"
13
14class SkBitmap;
15class SkPicture;
16
17/*
18 * An SkPictureShader can be used to draw SkPicture-based patterns.
19 *
20 * The SkPicture is first rendered into a tile, which is then used to shade the area according
21 * to specified tiling rules.
22 */
23class SkPictureShader : public SkShader {
24public:
25    static SkShader* Create(const SkPicture*, TileMode, TileMode, const SkMatrix*,
26                                   const SkRect*);
27
28    size_t contextSize(const ContextRec&) const override;
29
30    SK_TO_STRING_OVERRIDE()
31    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPictureShader)
32
33#if SK_SUPPORT_GPU
34    const GrFragmentProcessor* asFragmentProcessor(GrContext*,
35                                                   const SkMatrix& viewM,
36                                                   const SkMatrix*,
37                                                   SkFilterQuality) const override;
38#endif
39
40protected:
41    SkPictureShader(SkReadBuffer&);
42    void flatten(SkWriteBuffer&) const override;
43    Context* onCreateContext(const ContextRec&, void* storage) const override;
44
45private:
46    SkPictureShader(const SkPicture*, TileMode, TileMode, const SkMatrix*, const SkRect*);
47
48    SkShader* refBitmapShader(const SkMatrix&, const SkMatrix* localMatrix, const int maxTextureSize = 0) const;
49
50    SkAutoTUnref<const SkPicture> fPicture;
51    SkRect                        fTile;
52    TileMode                      fTmx, fTmy;
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        uint32_t getFlags() const override;
62
63        ShadeProc asAShadeProc(void** ctx) override;
64        void shadeSpan(int x, int y, SkPMColor dstC[], int count) 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