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 SkLocalMatrixShader_DEFINED
9#define SkLocalMatrixShader_DEFINED
10
11#include "SkShader.h"
12#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
14
15class SkLocalMatrixShader : public SkShader {
16public:
17    SkLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix)
18    : INHERITED(&localMatrix)
19    , fProxyShader(SkRef(proxy))
20    {}
21
22    virtual size_t contextSize() const SK_OVERRIDE {
23        return fProxyShader->contextSize();
24    }
25
26    virtual BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
27                                 TileMode* mode) const SK_OVERRIDE {
28        return fProxyShader->asABitmap(bitmap, matrix, mode);
29    }
30
31    virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
32        return fProxyShader->asAGradient(info);
33    }
34
35#if SK_SUPPORT_GPU
36
37    virtual bool asFragmentProcessor(GrContext* context, const SkPaint& paint,
38                                     const SkMatrix* localMatrix, GrColor* grColor,
39                                     GrFragmentProcessor** fp) const SK_OVERRIDE {
40        SkMatrix tmp = this->getLocalMatrix();
41        if (localMatrix) {
42            tmp.preConcat(*localMatrix);
43        }
44        return fProxyShader->asFragmentProcessor(context, paint, &tmp, grColor, fp);
45    }
46
47#else
48
49    virtual bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
50                                     GrFragmentProcessor**) const SK_OVERRIDE {
51        SkDEBUGFAIL("Should not call in GPU-less build");
52        return false;
53    }
54
55#endif
56
57    virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const SK_OVERRIDE {
58        if (localMatrix) {
59            *localMatrix = this->getLocalMatrix();
60        }
61        return SkRef(fProxyShader.get());
62    }
63
64    SK_TO_STRING_OVERRIDE()
65    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShader)
66
67protected:
68#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
69    SkLocalMatrixShader(SkReadBuffer&);
70#endif
71    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
72    virtual Context* onCreateContext(const ContextRec&, void*) const SK_OVERRIDE;
73
74private:
75    SkAutoTUnref<SkShader> fProxyShader;
76
77    typedef SkShader INHERITED;
78};
79
80#endif
81