~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/Gradient.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
 
3
 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
 
4
 * Copyright (C) 2008 Torch Mobile, Inc.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions
 
8
 * are met:
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 *
 
15
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
16
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
18
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
19
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
20
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
22
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
23
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
25
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
26
 */
 
27
 
 
28
#ifndef Gradient_h
 
29
#define Gradient_h
 
30
 
 
31
#include "AffineTransform.h"
 
32
#include "FloatPoint.h"
 
33
#include "Generator.h"
 
34
#include "GraphicsTypes.h"
 
35
#include <wtf/PassRefPtr.h>
 
36
#include <wtf/Vector.h>
 
37
 
 
38
#if USE(CG)
 
39
 
 
40
typedef struct CGContext* CGContextRef;
 
41
 
 
42
typedef struct CGGradient* CGGradientRef;
 
43
typedef CGGradientRef PlatformGradient;
 
44
 
 
45
#elif PLATFORM(QT)
 
46
QT_BEGIN_NAMESPACE
 
47
class QGradient;
 
48
QT_END_NAMESPACE
 
49
typedef QGradient* PlatformGradient;
 
50
#elif USE(CAIRO)
 
51
typedef struct _cairo_pattern cairo_pattern_t;
 
52
typedef cairo_pattern_t* PlatformGradient;
 
53
#elif USE(SKIA)
 
54
class SkShader;
 
55
typedef class SkShader* PlatformGradient;
 
56
typedef class SkShader* PlatformPattern;
 
57
#elif PLATFORM(WX)
 
58
class wxGraphicsBrush;
 
59
typedef wxGraphicsBrush* PlatformGradient;
 
60
#else
 
61
typedef void* PlatformGradient;
 
62
#endif
 
63
 
 
64
namespace WebCore {
 
65
 
 
66
    class Color;
 
67
 
 
68
    class Gradient : public Generator {
 
69
    public:
 
70
        static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
 
71
        {
 
72
            return adoptRef(new Gradient(p0, p1));
 
73
        }
 
74
        static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio = 1)
 
75
        {
 
76
            return adoptRef(new Gradient(p0, r0, p1, r1, aspectRatio));
 
77
        }
 
78
        virtual ~Gradient();
 
79
 
 
80
        struct ColorStop;
 
81
        void addColorStop(const ColorStop&);
 
82
        void addColorStop(float, const Color&);
 
83
 
 
84
        void getColor(float value, float* r, float* g, float* b, float* a) const;
 
85
        bool hasAlpha() const;
 
86
 
 
87
        bool isRadial() const { return m_radial; }
 
88
        bool isZeroSize() const { return m_p0.x() == m_p1.x() && m_p0.y() == m_p1.y() && (!m_radial || m_r0 == m_r1); }
 
89
 
 
90
        const FloatPoint& p0() const { return m_p0; }
 
91
        const FloatPoint& p1() const { return m_p1; }
 
92
 
 
93
        void setP0(const FloatPoint& p)
 
94
        {
 
95
            if (m_p0 == p)
 
96
                return;
 
97
            
 
98
            m_p0 = p;
 
99
            
 
100
            invalidateHash();
 
101
        }
 
102
        
 
103
        void setP1(const FloatPoint& p)
 
104
        {
 
105
            if (m_p1 == p)
 
106
                return;
 
107
            
 
108
            m_p1 = p;
 
109
            
 
110
            invalidateHash();
 
111
        }
 
112
 
 
113
        float startRadius() const { return m_r0; }
 
114
        float endRadius() const { return m_r1; }
 
115
 
 
116
        void setStartRadius(float r)
 
117
        {
 
118
            if (m_r0 == r)
 
119
                return;
 
120
 
 
121
            m_r0 = r;
 
122
 
 
123
            invalidateHash();
 
124
        }
 
125
 
 
126
        void setEndRadius(float r)
 
127
        {
 
128
            if (m_r1 == r)
 
129
                return;
 
130
 
 
131
            m_r1 = r;
 
132
 
 
133
            invalidateHash();
 
134
        }
 
135
 
 
136
        float aspectRatio() const { return m_aspectRatio; }
 
137
 
 
138
#if OS(WINCE) && !PLATFORM(QT)
 
139
        const Vector<ColorStop, 2>& getStops() const;
 
140
#else
 
141
        PlatformGradient platformGradient();
 
142
#endif
 
143
 
 
144
        struct ColorStop {
 
145
            float stop;
 
146
            float red;
 
147
            float green;
 
148
            float blue;
 
149
            float alpha;
 
150
 
 
151
            ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
 
152
            ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
 
153
        };
 
154
 
 
155
        void setStopsSorted(bool s) { m_stopsSorted = s; }
 
156
        
 
157
        void setSpreadMethod(GradientSpreadMethod);
 
158
        GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
 
159
        void setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
 
160
        // Qt and CG transform the gradient at draw time
 
161
        AffineTransform gradientSpaceTransform() { return m_gradientSpaceTransformation; }
 
162
 
 
163
        virtual void fill(GraphicsContext*, const FloatRect&);
 
164
        virtual void adjustParametersForTiledDrawing(IntSize& size, FloatRect& srcRect);
 
165
 
 
166
        void setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
 
167
 
 
168
        virtual unsigned hash() const OVERRIDE;
 
169
        void invalidateHash() { m_cachedHash = 0; }
 
170
 
 
171
#if USE(CG)
 
172
        void paint(CGContextRef);
 
173
        void paint(GraphicsContext*);
 
174
#elif USE(CAIRO)
 
175
        PlatformGradient platformGradient(float globalAlpha);
 
176
#endif
 
177
 
 
178
    private:
 
179
        Gradient(const FloatPoint& p0, const FloatPoint& p1);
 
180
        Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio);
 
181
 
 
182
        void platformInit() { m_gradient = 0; }
 
183
        void platformDestroy();
 
184
 
 
185
        int findStop(float value) const;
 
186
        void sortStopsIfNecessary();
 
187
 
 
188
        // Keep any parameters relevant to rendering in sync with the structure in Gradient::hash().
 
189
        bool m_radial;
 
190
        FloatPoint m_p0;
 
191
        FloatPoint m_p1;
 
192
        float m_r0;
 
193
        float m_r1;
 
194
        float m_aspectRatio; // For elliptical gradient, width / height.
 
195
        mutable Vector<ColorStop, 2> m_stops;
 
196
        mutable bool m_stopsSorted;
 
197
        mutable int m_lastStop;
 
198
        GradientSpreadMethod m_spreadMethod;
 
199
        AffineTransform m_gradientSpaceTransformation;
 
200
 
 
201
        mutable unsigned m_cachedHash;
 
202
 
 
203
        PlatformGradient m_gradient;
 
204
 
 
205
#if USE(CAIRO)
 
206
        float m_platformGradientAlpha;
 
207
#endif
 
208
 
 
209
    };
 
210
 
 
211
} //namespace
 
212
 
 
213
#endif