Roll our r64361 and r64363
[WebKit-https.git] / WebCore / platform / graphics / win / WKCACFLayer.h
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #ifndef WKCACFLayer_h
27 #define WKCACFLayer_h
28
29 #if USE(ACCELERATED_COMPOSITING)
30
31 #include "StringHash.h"
32
33 #include <wtf/RefCounted.h>
34
35 #include <QuartzCore/CACFLayer.h>
36 #include <QuartzCore/CACFVector.h>
37 #include <wtf/PassRefPtr.h>
38 #include <wtf/RetainPtr.h>
39 #include <wtf/Vector.h>
40
41 #include "GraphicsContext.h"
42 #include "PlatformString.h"
43 #include "TransformationMatrix.h"
44
45 namespace WebCore {
46
47 class WKCACFLayer;
48
49 class WKCACFLayerLayoutClient {
50 public:
51     virtual void layoutSublayersOfLayer(WKCACFLayer*) = 0;
52 protected:
53     virtual ~WKCACFLayerLayoutClient() {}
54 };
55
56 class WKCACFLayer : public RefCounted<WKCACFLayer> {
57 public:
58     enum LayerType { Layer, TransformLayer };
59     enum FilterType { Linear, Nearest, Trilinear };
60     enum ContentsGravityType { Center, Top, Bottom, Left, Right, TopLeft, TopRight, 
61                                BottomLeft, BottomRight, Resize, ResizeAspect, ResizeAspectFill };
62
63     static PassRefPtr<WKCACFLayer> create(LayerType);
64     static WKCACFLayer* layer(CACFLayerRef layer) { return static_cast<WKCACFLayer*>(CACFLayerGetUserData(layer)); }
65
66     virtual ~WKCACFLayer();
67
68     virtual void setNeedsRender() { }
69
70     virtual void drawInContext(PlatformGraphicsContext*) { }
71
72     void setLayoutClient(WKCACFLayerLayoutClient*);
73     WKCACFLayerLayoutClient* layoutClient() const { return m_layoutClient; }
74     void setNeedsLayout() { CACFLayerSetNeedsLayout(layer()); }
75
76     void setNeedsDisplay(const CGRect* dirtyRect = 0)
77     {
78         internalSetNeedsDisplay(dirtyRect);
79         setNeedsCommit();
80     }
81
82     // Makes this layer the root when the passed context is rendered
83     void becomeRootLayerForContext(CACFContextRef);
84
85     static RetainPtr<CFTypeRef> cfValue(float value) { return RetainPtr<CFTypeRef>(AdoptCF, CFNumberCreate(0, kCFNumberFloat32Type, &value)); }
86     static RetainPtr<CFTypeRef> cfValue(const TransformationMatrix& value)
87     {
88         CATransform3D t;
89         t.m11 = value.m11();
90         t.m12 = value.m12();
91         t.m13 = value.m13();
92         t.m14 = value.m14(); 
93         t.m21 = value.m21();
94         t.m22 = value.m22();
95         t.m23 = value.m23();
96         t.m24 = value.m24(); 
97         t.m31 = value.m31();
98         t.m32 = value.m32();
99         t.m33 = value.m33();
100         t.m34 = value.m34(); 
101         t.m41 = value.m41();
102         t.m42 = value.m42();
103         t.m43 = value.m43();
104         t.m44 = value.m44(); 
105         return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreateTransform(t));
106     }
107     static RetainPtr<CFTypeRef> cfValue(const FloatPoint& value)
108     {
109         CGPoint p;
110         p.x = value.x(); p.y = value.y();
111         return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreatePoint(p));
112     }
113     static RetainPtr<CFTypeRef> cfValue(const FloatRect& rect)
114     {
115         CGRect r;
116         r.origin.x = rect.x();
117         r.origin.y = rect.y();
118         r.size.width = rect.width();
119         r.size.height = rect.height();
120         CGFloat v[4] = { CGRectGetMinX(r), CGRectGetMinY(r), CGRectGetMaxX(r), CGRectGetMaxY(r) };
121         return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreate(4, v));
122     }
123     static RetainPtr<CFTypeRef> cfValue(const Color& color)
124     {
125         return RetainPtr<CFTypeRef>(AdoptCF, CGColorCreateGenericRGB(color.red(), color.green(), color.blue(), color.alpha()));
126     }
127
128     bool isTransformLayer() const;
129
130     void addSublayer(PassRefPtr<WKCACFLayer> sublayer);
131     void insertSublayerAboveLayer(PassRefPtr<WKCACFLayer>, const WKCACFLayer* reference);
132     void insertSublayerBelowLayer(PassRefPtr<WKCACFLayer>, const WKCACFLayer* reference);
133     void replaceSublayer(WKCACFLayer* reference, PassRefPtr<WKCACFLayer>);
134     void adoptSublayers(WKCACFLayer* source);
135
136     void removeAllSublayers() { internalRemoveAllSublayers(); }
137     void setSublayers(const Vector<RefPtr<WKCACFLayer> >& sublayers) { internalSetSublayers(sublayers); }
138
139     void insertSublayer(PassRefPtr<WKCACFLayer> layer, size_t index) { internalInsertSublayer(layer, index); }
140
141     size_t sublayerCount() const { return internalSublayerCount(); }
142
143     void removeFromSuperlayer();
144
145     WKCACFLayer* ancestorOrSelfWithSuperlayer(WKCACFLayer*) const;
146     
147     void setAnchorPoint(const CGPoint& p) { CACFLayerSetAnchorPoint(layer(), p); setNeedsCommit(); }
148     CGPoint anchorPoint() const { return CACFLayerGetAnchorPoint(layer()); }
149
150     void setAnchorPointZ(CGFloat z) { CACFLayerSetAnchorPointZ(layer(), z); setNeedsCommit(); }
151     CGFloat anchorPointZ() const { return CACFLayerGetAnchorPointZ(layer()); }
152
153     void setBackgroundColor(CGColorRef color) { CACFLayerSetBackgroundColor(layer(), color); setNeedsCommit(); }
154     CGColorRef backgroundColor() const { return CACFLayerGetBackgroundColor(layer()); }
155
156     void setBorderColor(CGColorRef color) { CACFLayerSetBorderColor(layer(), color); setNeedsCommit(); }
157     CGColorRef borderColor() const { return CACFLayerGetBorderColor(layer()); }
158
159     void setBorderWidth(CGFloat width) { CACFLayerSetBorderWidth(layer(), width); setNeedsCommit(); }
160     CGFloat borderWidth() const { return CACFLayerGetBorderWidth(layer()); }
161
162     virtual void setBounds(const CGRect&);
163     CGRect bounds() const { return CACFLayerGetBounds(layer()); }
164
165     void setContents(CFTypeRef contents) { CACFLayerSetContents(layer(), contents); setNeedsCommit(); }
166     CFTypeRef contents() const { return CACFLayerGetContents(layer()); }
167
168     void setContentsRect(const CGRect& contentsRect) { CACFLayerSetContentsRect(layer(), contentsRect); setNeedsCommit(); }
169     CGRect contentsRect() const { return CACFLayerGetContentsRect(layer()); }
170
171     void setContentsGravity(ContentsGravityType);
172     ContentsGravityType contentsGravity() const;
173         
174     void setDoubleSided(bool b) { CACFLayerSetDoubleSided(layer(), b); setNeedsCommit(); }
175     bool doubleSided() const { return CACFLayerIsDoubleSided(layer()); }
176
177     void setEdgeAntialiasingMask(uint32_t mask) { CACFLayerSetEdgeAntialiasingMask(layer(), mask); setNeedsCommit(); }
178     uint32_t edgeAntialiasingMask() const { return CACFLayerGetEdgeAntialiasingMask(layer()); }
179
180     virtual void setFrame(const CGRect&);
181     CGRect frame() const { return CACFLayerGetFrame(layer()); }
182
183     void setHidden(bool hidden) { CACFLayerSetHidden(layer(), hidden); setNeedsCommit(); }
184     bool isHidden() const { return CACFLayerIsHidden(layer()); }
185
186     void setMasksToBounds(bool b) { CACFLayerSetMasksToBounds(layer(), b); }
187     bool masksToBounds() const { return CACFLayerGetMasksToBounds(layer()); }
188
189     void setMagnificationFilter(FilterType);
190     FilterType magnificationFilter() const;
191
192     void setMinificationFilter(FilterType);
193     FilterType minificationFilter() const;
194
195     void setMinificationFilterBias(float bias) { CACFLayerSetMinificationFilterBias(layer(), bias); }
196     float minificationFilterBias() const { return CACFLayerGetMinificationFilterBias(layer()); }
197
198     void setName(const String& name) { CACFLayerSetName(layer(), RetainPtr<CFStringRef>(AdoptCF, name.createCFString()).get()); }
199     String name() const { return CACFLayerGetName(layer()); }
200
201     void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
202
203     void setOpacity(float opacity) { CACFLayerSetOpacity(layer(), opacity); setNeedsCommit(); }
204     float opacity() const { return CACFLayerGetOpacity(layer()); }
205
206     void setOpaque(bool b) { CACFLayerSetOpaque(layer(), b); setNeedsCommit(); }
207     bool opaque() const { return CACFLayerIsOpaque(layer()); }
208
209     void setPosition(const CGPoint& position) { CACFLayerSetPosition(layer(), position); setNeedsCommit(); }
210     CGPoint position() const { return CACFLayerGetPosition(layer()); }
211
212     void setZPosition(CGFloat position) { CACFLayerSetZPosition(layer(), position); setNeedsCommit(); }
213     CGFloat zPosition() const { return CACFLayerGetZPosition(layer()); }
214
215     void setSpeed(float speed) { CACFLayerSetSpeed(layer(), speed); }
216     CFTimeInterval speed() const { return CACFLayerGetSpeed(layer()); }
217
218     void setTimeOffset(CFTimeInterval t) { CACFLayerSetTimeOffset(layer(), t); }
219     CFTimeInterval timeOffset() const { return CACFLayerGetTimeOffset(layer()); }
220
221     WKCACFLayer* rootLayer() const;
222
223     void setSublayerTransform(const CATransform3D& transform) { CACFLayerSetSublayerTransform(layer(), transform); setNeedsCommit(); }
224     CATransform3D sublayerTransform() const { return CACFLayerGetSublayerTransform(layer()); }
225
226     WKCACFLayer* superlayer() const;
227
228     void setTransform(const CATransform3D& transform) { CACFLayerSetTransform(layer(), transform); setNeedsCommit(); }
229     CATransform3D transform() const { return CACFLayerGetTransform(layer()); }
230
231     void setGeometryFlipped(bool flipped) { CACFLayerSetGeometryFlipped(layer(), flipped); setNeedsCommit(); }
232     bool geometryFlipped() const { return CACFLayerIsGeometryFlipped(layer()); }
233
234 #ifndef NDEBUG
235     // Print the tree from the root. Also does consistency checks
236     void printTree() const;
237 #endif
238
239 protected:
240     WKCACFLayer(LayerType);
241
242     void setNeedsCommit();
243
244     CACFLayerRef layer() const { return m_layer.get(); }
245     // This should only be called from removeFromSuperlayer.
246     void removeSublayer(const WKCACFLayer*);
247
248     // Methods to be overridden for sublayer and rendering management
249     virtual WKCACFLayer* internalSublayerAtIndex(int) const;
250
251     // Returns the index of the passed layer in this layer's sublayers list
252     // or -1 if not found
253     virtual int internalIndexOfSublayer(const WKCACFLayer*);
254
255     virtual size_t internalSublayerCount() const;
256     virtual void internalInsertSublayer(PassRefPtr<WKCACFLayer>, size_t index);
257     virtual void internalRemoveAllSublayers();
258     virtual void internalSetSublayers(const Vector<RefPtr<WKCACFLayer> >&);
259
260     virtual void internalSetNeedsDisplay(const CGRect* dirtyRect);
261
262 #ifndef NDEBUG
263     // Print this layer and its children to the console
264     void printLayer(int indent) const;
265 #endif
266
267 private:
268     static void layoutSublayersProc(CACFLayerRef);
269
270     RetainPtr<CACFLayerRef> m_layer;
271     WKCACFLayerLayoutClient* m_layoutClient;
272     bool m_needsDisplayOnBoundsChange;
273 };
274
275 }
276
277 #endif // USE(ACCELERATED_COMPOSITING)
278
279 #endif // WKCACFLayer_h