SkBitmapProcShader.h revision ce56d965069c1649afe14319cb239e6ad670682a
15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright 2006 The Android Open Source Project
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Use of this source code is governed by a BSD-style license that can be
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * found in the LICENSE file.
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef SkBitmapProcShader_DEFINED
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define SkBitmapProcShader_DEFINED
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "SkShader.h"
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "SkBitmapProcState.h"
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "SkSmallAllocator.h"
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class SkBitmapProcShader : public SkShader {
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public:
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SkBitmapProcShader(const SkBitmap& src, TileMode tx, TileMode ty,
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                       const SkMatrix* localMatrix = NULL);
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // overrides from SkShader
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    virtual bool isOpaque() const SK_OVERRIDE;
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*) const SK_OVERRIDE;
2553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
2609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual size_t contextSize() const SK_OVERRIDE;
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    static bool CanDo(const SkBitmap&, TileMode tx, TileMode ty);
29
30    SK_TO_STRING_OVERRIDE()
31    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBitmapProcShader)
32
33#if SK_SUPPORT_GPU
34    GrEffectRef* asNewEffect(GrContext*, const SkPaint&) const SK_OVERRIDE;
35#endif
36
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 SkBitmapProcShader&, const ContextRec&, SkBitmapProcState*);
42        virtual ~BitmapProcShaderContext();
43
44        virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
45        virtual ShadeProc asAShadeProc(void** ctx) SK_OVERRIDE;
46        virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
47
48        virtual uint32_t getFlags() const SK_OVERRIDE { return fFlags; }
49
50    private:
51        SkBitmapProcState*  fState;
52        uint32_t            fFlags;
53
54        typedef SkShader::Context INHERITED;
55    };
56
57protected:
58    SkBitmapProcShader(SkReadBuffer& );
59    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
60    virtual Context* onCreateContext(const ContextRec&, void* storage) const SK_OVERRIDE;
61
62    SkBitmap    fRawBitmap;   // experimental for RLE encoding
63    uint8_t     fTileModeX, fTileModeY;
64
65private:
66    typedef SkShader INHERITED;
67};
68
69// Commonly used allocator. It currently is only used to allocate up to 3 objects. The total
70// bytes requested is calculated using one of our large shaders, its context size plus the size of
71// an Sk3DBlitter in SkDraw.cpp
72// Note that some contexts may contain other contexts (e.g. for compose shaders), but we've not
73// yet found a situation where the size below isn't big enough.
74typedef SkSmallAllocator<3, sizeof(SkBitmapProcShader) +
75                            sizeof(SkBitmapProcShader::BitmapProcShaderContext) +
76                            sizeof(SkBitmapProcState) +
77                            sizeof(void*) * 2> SkTBlitterAllocator;
78
79// If alloc is non-NULL, it will be used to allocate the returned SkShader, and MUST outlive
80// the SkShader.
81SkShader* CreateBitmapShader(const SkBitmap& src, SkShader::TileMode, SkShader::TileMode,
82                             const SkMatrix* localMatrix, SkTBlitterAllocator* alloc);
83
84#endif
85