1589708bf7c706348b763e8277004cb160b202bdbrileya@google.com/*
2589708bf7c706348b763e8277004cb160b202bdbrileya@google.com * Copyright 2012 Google Inc.
3589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *
4589708bf7c706348b763e8277004cb160b202bdbrileya@google.com * Use of this source code is governed by a BSD-style license that can be
5589708bf7c706348b763e8277004cb160b202bdbrileya@google.com * found in the LICENSE file.
6589708bf7c706348b763e8277004cb160b202bdbrileya@google.com */
7589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
8589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#ifndef SkLinearGradient_DEFINED
9589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#define SkLinearGradient_DEFINED
10589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
11589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkGradientShaderPriv.h"
12f3182ebc72db2bf2e24119d5cea05f270473a491reed#include "SkNx.h"
13f3182ebc72db2bf2e24119d5cea05f270473a491reed
14f3182ebc72db2bf2e24119d5cea05f270473a491reedstruct Sk4fStorage {
15f3182ebc72db2bf2e24119d5cea05f270473a491reed    float fArray[4];
16f3182ebc72db2bf2e24119d5cea05f270473a491reed
17f3182ebc72db2bf2e24119d5cea05f270473a491reed    operator Sk4f() const {
18f3182ebc72db2bf2e24119d5cea05f270473a491reed        return Sk4f::Load(fArray);
19f3182ebc72db2bf2e24119d5cea05f270473a491reed    }
20f3182ebc72db2bf2e24119d5cea05f270473a491reed
21f3182ebc72db2bf2e24119d5cea05f270473a491reed    Sk4fStorage& operator=(const Sk4f& src) {
22f3182ebc72db2bf2e24119d5cea05f270473a491reed        src.store(fArray);
23f3182ebc72db2bf2e24119d5cea05f270473a491reed        return *this;
24f3182ebc72db2bf2e24119d5cea05f270473a491reed    }
25f3182ebc72db2bf2e24119d5cea05f270473a491reed};
26589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
27589708bf7c706348b763e8277004cb160b202bdbrileya@google.comclass SkLinearGradient : public SkGradientShaderBase {
28589708bf7c706348b763e8277004cb160b202bdbrileya@google.compublic:
29bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita    enum {
30bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita        // Temp flag for testing the 4f impl.
31bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita        kForce4fContext_PrivateFlag     = 1 << 7,
32bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita    };
33bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita
34addf2edf3da20f053daa3897cfe2c52d7369a7b1reed    SkLinearGradient(const SkPoint pts[2], const Descriptor&);
35589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
368d9f2e474ac9d175c28079357b022d31408e2fe4fmalita    size_t contextSize(const ContextRec&) const override;
3787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
3887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    class LinearGradientContext : public SkGradientShaderBase::GradientShaderBaseContext {
3987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    public:
40e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org        LinearGradientContext(const SkLinearGradient&, const ContextRec&);
4187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
4236352bf5e38f45a70ee4f4fc132a38048d38206dmtklein        void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
4387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
44f3182ebc72db2bf2e24119d5cea05f270473a491reed        struct Rec {
45f3182ebc72db2bf2e24119d5cea05f270473a491reed            Sk4fStorage fColor;
46f3182ebc72db2bf2e24119d5cea05f270473a491reed            float       fPos;
47f3182ebc72db2bf2e24119d5cea05f270473a491reed            float       fPosScale;
48f3182ebc72db2bf2e24119d5cea05f270473a491reed        };
4987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    private:
50f3182ebc72db2bf2e24119d5cea05f270473a491reed        SkTDArray<Rec>  fRecs;
51f3182ebc72db2bf2e24119d5cea05f270473a491reed        bool            fApplyAlphaAfterInterp;
52f3182ebc72db2bf2e24119d5cea05f270473a491reed
53f3182ebc72db2bf2e24119d5cea05f270473a491reed        void shade4_clamp(int x, int y, SkPMColor dstC[], int count);
54f3182ebc72db2bf2e24119d5cea05f270473a491reed        template <bool, bool> void shade4_dx_clamp(SkPMColor dstC[], int count, float fx, float dx,
55f3182ebc72db2bf2e24119d5cea05f270473a491reed                                                   float invDx, const float dither[2]);
56f3182ebc72db2bf2e24119d5cea05f270473a491reed
5787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
5887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    };
5987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
6036352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    GradientType asAGradient(GradientInfo* info) const override;
61c21b09eec91c9e263cb0b88467ea44e348ed4962bsalomon#if SK_SUPPORT_GPU
62c21b09eec91c9e263cb0b88467ea44e348ed4962bsalomon    const GrFragmentProcessor* asFragmentProcessor(GrContext*,
63c21b09eec91c9e263cb0b88467ea44e348ed4962bsalomon                                                   const SkMatrix& viewM,
64c21b09eec91c9e263cb0b88467ea44e348ed4962bsalomon                                                   const SkMatrix*,
654a339529612a43871d021877e58698e067d6c4cdbsalomon                                                   SkFilterQuality) const override;
66c21b09eec91c9e263cb0b88467ea44e348ed4962bsalomon#endif
67589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
680f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_OVERRIDE()
69589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLinearGradient)
70589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
71589708bf7c706348b763e8277004cb160b202bdbrileya@google.comprotected:
728b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    SkLinearGradient(SkReadBuffer& buffer);
7336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void flatten(SkWriteBuffer& buffer) const override;
7436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    Context* onCreateContext(const ContextRec&, void* storage) const override;
75589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
76589708bf7c706348b763e8277004cb160b202bdbrileya@google.comprivate:
77bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita    class LinearGradient4fContext;
78bc590c01b00ef79e1e1f30058e7a70a29419f2a9fmalita
799fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed    friend class SkGradientShader;
80589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    typedef SkGradientShaderBase INHERITED;
81589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    const SkPoint fStart;
82589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    const SkPoint fEnd;
83589708bf7c706348b763e8277004cb160b202bdbrileya@google.com};
84589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
85589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#endif
86