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