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