tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / Gradient.h
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 #define USE_CG_SHADING (PLATFORM(MAC) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1050)
43
44 #if USE_CG_SHADING
45 typedef struct CGShading* CGShadingRef;
46 typedef CGShadingRef PlatformGradient;
47 #else
48 typedef struct CGGradient* CGGradientRef;
49 typedef CGGradientRef PlatformGradient;
50 #endif
51
52 #elif PLATFORM(QT)
53 QT_BEGIN_NAMESPACE
54 class QGradient;
55 QT_END_NAMESPACE
56 typedef QGradient* PlatformGradient;
57 #elif USE(CAIRO)
58 typedef struct _cairo_pattern cairo_pattern_t;
59 typedef cairo_pattern_t* PlatformGradient;
60 #elif USE(SKIA)
61 class SkShader;
62 typedef class SkShader* PlatformGradient;
63 typedef class SkShader* PlatformPattern;
64 #elif PLATFORM(WX)
65 class wxGraphicsBrush;
66 typedef wxGraphicsBrush* PlatformGradient;
67 #else
68 typedef void* PlatformGradient;
69 #endif
70
71 namespace WebCore {
72
73     class Color;
74
75     class Gradient : public Generator {
76     public:
77         static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
78         {
79             return adoptRef(new Gradient(p0, p1));
80         }
81         static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio = 1)
82         {
83             return adoptRef(new Gradient(p0, r0, p1, r1, aspectRatio));
84         }
85         virtual ~Gradient();
86
87         struct ColorStop;
88         void addColorStop(const ColorStop&);
89         void addColorStop(float, const Color&);
90
91         void getColor(float value, float* r, float* g, float* b, float* a) const;
92         bool hasAlpha() const;
93
94         bool isRadial() const { return m_radial; }
95         bool isZeroSize() const { return m_p0.x() == m_p1.x() && m_p0.y() == m_p1.y() && (!m_radial || m_r0 == m_r1); }
96
97         const FloatPoint& p0() const { return m_p0; }
98         const FloatPoint& p1() const { return m_p1; }
99
100         void setP0(const FloatPoint& p)
101         {
102             if (m_p0 == p)
103                 return;
104             
105             m_p0 = p;
106             
107             invalidateHash();
108         }
109         
110         void setP1(const FloatPoint& p)
111         {
112             if (m_p1 == p)
113                 return;
114             
115             m_p1 = p;
116             
117             invalidateHash();
118         }
119
120         float startRadius() const { return m_r0; }
121         float endRadius() const { return m_r1; }
122
123         void setStartRadius(float r)
124         {
125             if (m_r0 == r)
126                 return;
127
128             m_r0 = r;
129
130             invalidateHash();
131         }
132
133         void setEndRadius(float r)
134         {
135             if (m_r1 == r)
136                 return;
137
138             m_r1 = r;
139
140             invalidateHash();
141         }
142
143         float aspectRatio() const { return m_aspectRatio; }
144
145 #if OS(WINCE) && !PLATFORM(QT)
146         const Vector<ColorStop, 2>& getStops() const;
147 #else
148         PlatformGradient platformGradient();
149 #endif
150
151         struct ColorStop {
152             float stop;
153             float red;
154             float green;
155             float blue;
156             float alpha;
157
158             ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
159             ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
160         };
161
162         void setStopsSorted(bool s) { m_stopsSorted = s; }
163         
164         void setSpreadMethod(GradientSpreadMethod);
165         GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
166         void setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
167         // Qt and CG transform the gradient at draw time
168         AffineTransform gradientSpaceTransform() { return m_gradientSpaceTransformation; }
169
170         virtual void fill(GraphicsContext*, const FloatRect&);
171         virtual void adjustParametersForTiledDrawing(IntSize& size, FloatRect& srcRect);
172
173         void setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
174
175         virtual unsigned hash() const OVERRIDE;
176         void invalidateHash() { m_cachedHash = 0; }
177
178 #if USE(CG)
179         void paint(CGContextRef);
180         void paint(GraphicsContext*);
181 #elif USE(CAIRO)
182         PlatformGradient platformGradient(float globalAlpha);
183 #endif
184
185     private:
186         Gradient(const FloatPoint& p0, const FloatPoint& p1);
187         Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio);
188
189         void platformInit() { m_gradient = 0; }
190         void platformDestroy();
191
192         int findStop(float value) const;
193         void sortStopsIfNecessary();
194
195         // Keep any parameters relevant to rendering in sync with the structure in Gradient::hash().
196         bool m_radial;
197         FloatPoint m_p0;
198         FloatPoint m_p1;
199         float m_r0;
200         float m_r1;
201         float m_aspectRatio; // For elliptical gradient, width / height.
202         mutable Vector<ColorStop, 2> m_stops;
203         mutable bool m_stopsSorted;
204         mutable int m_lastStop;
205         GradientSpreadMethod m_spreadMethod;
206         AffineTransform m_gradientSpaceTransformation;
207
208         mutable unsigned m_cachedHash;
209
210         PlatformGradient m_gradient;
211
212 #if USE(CAIRO)
213         float m_platformGradientAlpha;
214 #endif
215
216     };
217
218 } //namespace
219
220 #endif