1 
2 /*
3  * Copyright 2006 The Android Open Source Project
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 
10 #ifndef SkBitmapProcShader_DEFINED
11 #define SkBitmapProcShader_DEFINED
12 
13 #include "SkShader.h"
14 #include "SkSmallAllocator.h"
15 
16 struct SkBitmapProcState;
17 class SkBitmapProvider;
18 
19 class SkBitmapProcShader : public SkShader {
20 public:
21     SkBitmapProcShader(const SkBitmap& src, TileMode tx, TileMode ty,
22                        const SkMatrix* localMatrix = nullptr);
23 
24     bool isOpaque() const override;
25 
contextSize(const ContextRec &)26     size_t contextSize(const ContextRec&) const override { return ContextSize(); }
27 
28     SK_TO_STRING_OVERRIDE()
29     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBitmapProcShader)
30 
31 #if SK_SUPPORT_GPU
32     const GrFragmentProcessor* asFragmentProcessor(GrContext*, const SkMatrix& viewM,
33                                                    const SkMatrix*, SkFilterQuality) const override;
34 #endif
35 
36 protected:
37     class BitmapProcShaderContext : public SkShader::Context {
38     public:
39         // The context takes ownership of the state. It will call its destructor
40         // but will NOT free the memory.
41         BitmapProcShaderContext(const SkShader&, const ContextRec&, SkBitmapProcState*);
42         ~BitmapProcShaderContext() override;
43 
44         void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
45         ShadeProc asAShadeProc(void** ctx) override;
46 
getFlags()47         uint32_t getFlags() const override { return fFlags; }
48 
49     private:
50         SkBitmapProcState*  fState;
51         uint32_t            fFlags;
52 
53         typedef SkShader::Context INHERITED;
54     };
55 
56     void flatten(SkWriteBuffer&) const override;
57     Context* onCreateContext(const ContextRec&, void* storage) const override;
58     bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode*) const override;
59 
60     SkBitmap    fRawBitmap;   // experimental for RLE encoding
61     uint8_t     fTileModeX, fTileModeY;
62 
63 private:
64     friend class SkImageShader;
65 
66     static size_t ContextSize();
67     static Context* MakeContext(const SkShader&, TileMode tmx, TileMode tmy,
68                                 const SkBitmapProvider&, const ContextRec&, void* storage);
69 
70     typedef SkShader INHERITED;
71 };
72 
73 // Commonly used allocator. It currently is only used to allocate up to 3 objects. The total
74 // bytes requested is calculated using one of our large shaders, its context size plus the size of
75 // an Sk3DBlitter in SkDraw.cpp
76 // Note that some contexts may contain other contexts (e.g. for compose shaders), but we've not
77 // yet found a situation where the size below isn't big enough.
78 typedef SkSmallAllocator<3, 1500> SkTBlitterAllocator;
79 
80 // If alloc is non-nullptr, it will be used to allocate the returned SkShader, and MUST outlive
81 // the SkShader.
82 SkShader* SkCreateBitmapShader(const SkBitmap& src, SkShader::TileMode, SkShader::TileMode,
83                                const SkMatrix* localMatrix, SkTBlitterAllocator* alloc);
84 
85 #endif
86