7243508c32ab19c856e4d4abcc89679f76405ee1
[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 <wtf/RefCounted.h>
32
33 #include <QuartzCore/CACFLayer.h>
34 #include <QuartzCore/CACFVector.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RetainPtr.h>
37 #include <wtf/Vector.h>
38 #include <wtf/text/StringHash.h>
39
40 #include "GraphicsContext.h"
41 #include "PlatformString.h"
42 #include "TransformationMatrix.h"
43
44 namespace WebCore {
45
46 class WKCACFLayer;
47
48 class WKCACFLayerLayoutClient {
49 public:
50     virtual void layoutSublayersOfLayer(WKCACFLayer*) = 0;
51 protected:
52     virtual ~WKCACFLayerLayoutClient() {}
53 };
54
55 class WKCACFLayer : public RefCounted<WKCACFLayer> {
56 public:
57     enum LayerType { Layer, TransformLayer };
58     enum FilterType { Linear, Nearest, Trilinear };
59     enum ContentsGravityType { Center, Top, Bottom, Left, Right, TopLeft, TopRight, 
60                                BottomLeft, BottomRight, Resize, ResizeAspect, ResizeAspectFill };
61
62     static PassRefPtr<WKCACFLayer> create(LayerType);
63     static WKCACFLayer* layer(CACFLayerRef layer)
64     {
65         ASSERT(CACFLayerGetUserData(layer) != reinterpret_cast<void*>(0xDEADBEEF));
66         return static_cast<WKCACFLayer*>(CACFLayerGetUserData(layer));
67     }
68
69     virtual ~WKCACFLayer();
70
71     virtual void setNeedsRender() { }
72
73     virtual void drawInContext(PlatformGraphicsContext*) { }
74
75     void setLayoutClient(WKCACFLayerLayoutClient*);
76     WKCACFLayerLayoutClient* layoutClient() const { return m_layoutClient; }
77     void setNeedsLayout() { CACFLayerSetNeedsLayout(layer()); }
78
79     void setNeedsDisplay(const CGRect* dirtyRect = 0)
80     {
81         internalSetNeedsDisplay(dirtyRect);
82         setNeedsCommit();
83     }
84
85     // Makes this layer the root when the passed context is rendered
86     void becomeRootLayerForContext(CACFContextRef);
87
88     static RetainPtr<CFTypeRef> cfValue(float value) { return RetainPtr<CFTypeRef>(AdoptCF, CFNumberCreate(0, kCFNumberFloat32Type, &value)); }
89     static RetainPtr<CFTypeRef> cfValue(const TransformationMatrix& value)
90     {
91         CATransform3D t;
92         t.m11 = value.m11();
93         t.m12 = value.m12();
94         t.m13 = value.m13();
95         t.m14 = value.m14(); 
96         t.m21 = value.m21();
97         t.m22 = value.m22();
98         t.m23 = value.m23();
99         t.m24 = value.m24(); 
100         t.m31 = value.m31();
101         t.m32 = value.m32();
102         t.m33 = value.m33();
103         t.m34 = value.m34(); 
104         t.m41 = value.m41();
105         t.m42 = value.m42();
106         t.m43 = value.m43();
107         t.m44 = value.m44(); 
108         return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreateTransform(t));
109     }
110     static RetainPtr<CFTypeRef> cfValue(const FloatPoint& value)
111     {
112         CGPoint p;
113         p.x = value.x(); p.y = value.y();
114         return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreatePoint(p));
115     }
116     static RetainPtr<CFTypeRef> cfValue(const FloatRect& rect)
117     {
118         CGRect r;
119         r.origin.x = rect.x();
120         r.origin.y = rect.y();
121         r.size.width = rect.width();
122         r.size.height = rect.height();
123         CGFloat v[4] = { CGRectGetMinX(r), CGRectGetMinY(r), CGRectGetMaxX(r), CGRectGetMaxY(r) };
124         return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreate(4, v));
125     }
126     static RetainPtr<CFTypeRef> cfValue(const Color& color)
127     {
128         return RetainPtr<CFTypeRef>(AdoptCF, CGColorCreateGenericRGB(color.red(), color.green(), color.blue(), color.alpha()));
129     }
130
131     bool isTransformLayer() const;
132
133     void addSublayer(PassRefPtr<WKCACFLayer> sublayer);
134     void insertSublayerAboveLayer(PassRefPtr<WKCACFLayer>, const WKCACFLayer* reference);
135     void insertSublayerBelowLayer(PassRefPtr<WKCACFLayer>, const WKCACFLayer* reference);
136     void replaceSublayer(WKCACFLayer* reference, PassRefPtr<WKCACFLayer>);
137     void adoptSublayers(WKCACFLayer* source);
138
139     void removeAllSublayers() { internalRemoveAllSublayers(); }
140     void setSublayers(const Vector<RefPtr<WKCACFLayer> >& sublayers)
141     {
142         internalSetSublayers(sublayers);
143         checkLayerConsistency();
144     }
145
146     void insertSublayer(PassRefPtr<WKCACFLayer> layer, size_t index) { internalInsertSublayer(layer, index); }
147
148     size_t sublayerCount() const { return internalSublayerCount(); }
149
150     void removeFromSuperlayer();
151
152     WKCACFLayer* ancestorOrSelfWithSuperlayer(WKCACFLayer*) const;
153     
154     void setAnchorPoint(const CGPoint& p) { CACFLayerSetAnchorPoint(layer(), p); setNeedsCommit(); }
155     CGPoint anchorPoint() const { return CACFLayerGetAnchorPoint(layer()); }
156
157     void setAnchorPointZ(CGFloat z) { CACFLayerSetAnchorPointZ(layer(), z); setNeedsCommit(); }
158     CGFloat anchorPointZ() const { return CACFLayerGetAnchorPointZ(layer()); }
159
160     void setBackgroundColor(CGColorRef color) { CACFLayerSetBackgroundColor(layer(), color); setNeedsCommit(); }
161     CGColorRef backgroundColor() const { return CACFLayerGetBackgroundColor(layer()); }
162
163     void setBorderColor(CGColorRef color) { CACFLayerSetBorderColor(layer(), color); setNeedsCommit(); }
164     CGColorRef borderColor() const { return CACFLayerGetBorderColor(layer()); }
165
166     void setBorderWidth(CGFloat width) { CACFLayerSetBorderWidth(layer(), width); setNeedsCommit(); }
167     CGFloat borderWidth() const { return CACFLayerGetBorderWidth(layer()); }
168
169     virtual void setBounds(const CGRect&);
170     CGRect bounds() const { return CACFLayerGetBounds(layer()); }
171
172     void setContents(CFTypeRef contents) { CACFLayerSetContents(layer(), contents); setNeedsCommit(); }
173     CFTypeRef contents() const { return CACFLayerGetContents(layer()); }
174
175     void setContentsRect(const CGRect& contentsRect) { CACFLayerSetContentsRect(layer(), contentsRect); setNeedsCommit(); }
176     CGRect contentsRect() const { return CACFLayerGetContentsRect(layer()); }
177
178     void setContentsGravity(ContentsGravityType);
179     ContentsGravityType contentsGravity() const;
180         
181     void setDoubleSided(bool b) { CACFLayerSetDoubleSided(layer(), b); setNeedsCommit(); }
182     bool doubleSided() const { return CACFLayerIsDoubleSided(layer()); }
183
184     void setEdgeAntialiasingMask(uint32_t mask) { CACFLayerSetEdgeAntialiasingMask(layer(), mask); setNeedsCommit(); }
185     uint32_t edgeAntialiasingMask() const { return CACFLayerGetEdgeAntialiasingMask(layer()); }
186
187     virtual void setFrame(const CGRect&);
188     CGRect frame() const { return CACFLayerGetFrame(layer()); }
189
190     void setHidden(bool hidden) { CACFLayerSetHidden(layer(), hidden); setNeedsCommit(); }
191     bool isHidden() const { return CACFLayerIsHidden(layer()); }
192
193     void setMasksToBounds(bool b) { CACFLayerSetMasksToBounds(layer(), b); }
194     bool masksToBounds() const { return CACFLayerGetMasksToBounds(layer()); }
195
196     void setMagnificationFilter(FilterType);
197     FilterType magnificationFilter() const;
198
199     void setMinificationFilter(FilterType);
200     FilterType minificationFilter() const;
201
202     void setMinificationFilterBias(float bias) { CACFLayerSetMinificationFilterBias(layer(), bias); }
203     float minificationFilterBias() const { return CACFLayerGetMinificationFilterBias(layer()); }
204
205     void setName(const String& name) { CACFLayerSetName(layer(), RetainPtr<CFStringRef>(AdoptCF, name.createCFString()).get()); }
206     String name() const { return CACFLayerGetName(layer()); }
207
208     void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
209
210     void setOpacity(float opacity) { CACFLayerSetOpacity(layer(), opacity); setNeedsCommit(); }
211     float opacity() const { return CACFLayerGetOpacity(layer()); }
212
213     void setOpaque(bool b) { CACFLayerSetOpaque(layer(), b); setNeedsCommit(); }
214     bool opaque() const { return CACFLayerIsOpaque(layer()); }
215
216     void setPosition(const CGPoint& position) { CACFLayerSetPosition(layer(), position); setNeedsCommit(); }
217     CGPoint position() const { return CACFLayerGetPosition(layer()); }
218
219     void setZPosition(CGFloat position) { CACFLayerSetZPosition(layer(), position); setNeedsCommit(); }
220     CGFloat zPosition() const { return CACFLayerGetZPosition(layer()); }
221
222     void setSpeed(float speed) { CACFLayerSetSpeed(layer(), speed); }
223     CFTimeInterval speed() const { return CACFLayerGetSpeed(layer()); }
224
225     void setTimeOffset(CFTimeInterval t) { CACFLayerSetTimeOffset(layer(), t); }
226     CFTimeInterval timeOffset() const { return CACFLayerGetTimeOffset(layer()); }
227
228     WKCACFLayer* rootLayer() const;
229
230     void setSublayerTransform(const CATransform3D& transform) { CACFLayerSetSublayerTransform(layer(), transform); setNeedsCommit(); }
231     CATransform3D sublayerTransform() const { return CACFLayerGetSublayerTransform(layer()); }
232
233     WKCACFLayer* superlayer() const;
234
235     void setTransform(const CATransform3D& transform) { CACFLayerSetTransform(layer(), transform); setNeedsCommit(); }
236     CATransform3D transform() const { return CACFLayerGetTransform(layer()); }
237
238     void setGeometryFlipped(bool flipped) { CACFLayerSetGeometryFlipped(layer(), flipped); setNeedsCommit(); }
239     bool geometryFlipped() const { return CACFLayerIsGeometryFlipped(layer()); }
240
241 #ifndef NDEBUG
242     // Print the tree from the root. Also does consistency checks
243     void printTree() const;
244 #endif
245
246 protected:
247     WKCACFLayer(LayerType);
248
249     void setNeedsCommit();
250
251     CACFLayerRef layer() const { return m_layer.get(); }
252     // This should only be called from removeFromSuperlayer.
253     void removeSublayer(const WKCACFLayer*);
254
255     void checkLayerConsistency()
256     {
257 #ifndef NDEBUG
258         internalCheckLayerConsistency();
259 #endif
260     }
261
262     // Methods to be overridden for sublayer and rendering management
263     virtual WKCACFLayer* internalSublayerAtIndex(int) const;
264
265     // Returns the index of the passed layer in this layer's sublayers list
266     // or -1 if not found
267     virtual int internalIndexOfSublayer(const WKCACFLayer*);
268
269     virtual size_t internalSublayerCount() const;
270     virtual void internalInsertSublayer(PassRefPtr<WKCACFLayer>, size_t index);
271     virtual void internalRemoveAllSublayers();
272     virtual void internalSetSublayers(const Vector<RefPtr<WKCACFLayer> >&);
273
274     virtual void internalSetNeedsDisplay(const CGRect* dirtyRect);
275
276 #ifndef NDEBUG
277     virtual void internalCheckLayerConsistency();
278 #endif
279
280 #ifndef NDEBUG
281     // Print this layer and its children to the console
282     void printLayer(int indent) const;
283 #endif
284
285 private:
286     static void layoutSublayersProc(CACFLayerRef);
287
288     RetainPtr<CACFLayerRef> m_layer;
289     WKCACFLayerLayoutClient* m_layoutClient;
290     bool m_needsDisplayOnBoundsChange;
291 };
292
293 }
294
295 #endif // USE(ACCELERATED_COMPOSITING)
296
297 #endif // WKCACFLayer_h