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