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