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