1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkBitmapProcShader_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkBitmapProcShader_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmapProcState.h"
15a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org#include "SkSmallAllocator.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkBitmapProcShader : public SkShader {
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
199c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    SkBitmapProcShader(const SkBitmap& src, TileMode tx, TileMode ty,
209c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                       const SkMatrix* localMatrix = NULL);
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // overrides from SkShader
23b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org    virtual bool isOpaque() const SK_OVERRIDE;
2476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*) const SK_OVERRIDE;
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    virtual size_t contextSize() const SK_OVERRIDE;
2787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool CanDo(const SkBitmap&, TileMode tx, TileMode ty);
29ff21c2e0ae23da0f4742b47d4d37969a2a18bd99skia.committer@gmail.com
300f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_OVERRIDE()
31ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBitmapProcShader)
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
339de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
349de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    bool asNewEffect(GrContext*, const SkPaint&, const SkMatrix*, GrColor*, GrEffectRef**)
359de5b514d38c5b36066bcdc14fba2f7e5196d372dandov            const SK_OVERRIDE;
36e197cbf9a3e66bab926bc5e51962752dad5221a0bsalomon@google.com
3787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    class BitmapProcShaderContext : public SkShader::Context {
3887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    public:
3987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        // The context takes ownership of the state. It will call its destructor
4087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        // but will NOT free the memory.
41e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org        BitmapProcShaderContext(const SkBitmapProcShader&, const ContextRec&, SkBitmapProcState*);
4287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual ~BitmapProcShaderContext();
4387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
4487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
4587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual ShadeProc asAShadeProc(void** ctx) SK_OVERRIDE;
4687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
4787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
4887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual uint32_t getFlags() const SK_OVERRIDE { return fFlags; }
4987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
5087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    private:
5187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        SkBitmapProcState*  fState;
5287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        uint32_t            fFlags;
5387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
5487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        typedef SkShader::Context INHERITED;
5587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    };
5687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
588b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    SkBitmapProcShader(SkReadBuffer& );
598b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
60ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    virtual Context* onCreateContext(const ContextRec&, void* storage) const SK_OVERRIDE;
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    SkBitmap    fRawBitmap;   // experimental for RLE encoding
6387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    uint8_t     fTileModeX, fTileModeY;
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
657c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.comprivate:
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkShader INHERITED;
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org// Commonly used allocator. It currently is only used to allocate up to 3 objects. The total
7087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org// bytes requested is calculated using one of our large shaders, its context size plus the size of
7187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org// an Sk3DBlitter in SkDraw.cpp
7287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org// Note that some contexts may contain other contexts (e.g. for compose shaders), but we've not
7387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org// yet found a situation where the size below isn't big enough.
749f23a27ed9961fd4575864e590eda328452895e7reedtypedef SkSmallAllocator<3, 1024> SkTBlitterAllocator;
75a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org
76a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org// If alloc is non-NULL, it will be used to allocate the returned SkShader, and MUST outlive
77a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org// the SkShader.
78a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.orgSkShader* CreateBitmapShader(const SkBitmap& src, SkShader::TileMode, SkShader::TileMode,
799c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                             const SkMatrix* localMatrix, SkTBlitterAllocator* alloc);
80a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
82