2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #if USE(ACCELERATED_COMPOSITING)
31 #include "StringHash.h"
33 #include <wtf/RefCounted.h>
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>
41 #include "GraphicsContext.h"
42 #include "PlatformString.h"
43 #include "TransformationMatrix.h"
51 class WKCACFLayerLayoutClient {
53 virtual void layoutSublayersOfLayer(WKCACFLayer*) = 0;
55 virtual ~WKCACFLayerLayoutClient() {}
58 class WKCACFLayer : public RefCounted<WKCACFLayer> {
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 };
65 static PassRefPtr<WKCACFLayer> create(LayerType);
66 static WKCACFLayer* layer(CACFLayerRef layer) { return static_cast<WKCACFLayer*>(CACFLayerGetUserData(layer)); }
68 virtual ~WKCACFLayer();
70 virtual void setNeedsRender() { }
72 virtual void drawInContext(PlatformGraphicsContext*) { }
74 void setLayoutClient(WKCACFLayerLayoutClient*);
75 WKCACFLayerLayoutClient* layoutClient() const { return m_layoutClient; }
76 void setNeedsLayout() { CACFLayerSetNeedsLayout(layer()); }
78 void setNeedsDisplay(const CGRect* dirtyRect = 0)
80 internalSetNeedsDisplay(dirtyRect);
84 // Makes this layer the root when the passed context is rendered
85 void becomeRootLayerForContext(WKCACFContext*);
87 static RetainPtr<CFTypeRef> cfValue(float value) { return RetainPtr<CFTypeRef>(AdoptCF, CFNumberCreate(0, kCFNumberFloat32Type, &value)); }
88 static RetainPtr<CFTypeRef> cfValue(const TransformationMatrix& value)
107 return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreateTransform(t));
109 static RetainPtr<CFTypeRef> cfValue(const FloatPoint& value)
112 p.x = value.x(); p.y = value.y();
113 return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreatePoint(p));
115 static RetainPtr<CFTypeRef> cfValue(const FloatRect& rect)
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));
125 static RetainPtr<CFTypeRef> cfValue(const Color& color)
127 return RetainPtr<CFTypeRef>(AdoptCF, CGColorCreateGenericRGB(color.red(), color.green(), color.blue(), color.alpha()));
130 bool isTransformLayer() const;
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);
138 void removeAllSublayers() { internalRemoveAllSublayers(); }
139 void setSublayers(const Vector<RefPtr<WKCACFLayer> >& sublayers) { internalSetSublayers(sublayers); }
141 void insertSublayer(PassRefPtr<WKCACFLayer> layer, size_t index) { internalInsertSublayer(layer, index); }
143 size_t sublayerCount() const { return internalSublayerCount(); }
145 void removeFromSuperlayer();
147 WKCACFLayer* ancestorOrSelfWithSuperlayer(WKCACFLayer*) const;
149 void setAnchorPoint(const CGPoint& p) { CACFLayerSetAnchorPoint(layer(), p); setNeedsCommit(); }
150 CGPoint anchorPoint() const { return CACFLayerGetAnchorPoint(layer()); }
152 void setAnchorPointZ(CGFloat z) { CACFLayerSetAnchorPointZ(layer(), z); setNeedsCommit(); }
153 CGFloat anchorPointZ() const { return CACFLayerGetAnchorPointZ(layer()); }
155 void setBackgroundColor(CGColorRef color) { CACFLayerSetBackgroundColor(layer(), color); setNeedsCommit(); }
156 CGColorRef backgroundColor() const { return CACFLayerGetBackgroundColor(layer()); }
158 void setBorderColor(CGColorRef color) { CACFLayerSetBorderColor(layer(), color); setNeedsCommit(); }
159 CGColorRef borderColor() const { return CACFLayerGetBorderColor(layer()); }
161 void setBorderWidth(CGFloat width) { CACFLayerSetBorderWidth(layer(), width); setNeedsCommit(); }
162 CGFloat borderWidth() const { return CACFLayerGetBorderWidth(layer()); }
164 virtual void setBounds(const CGRect&);
165 CGRect bounds() const { return CACFLayerGetBounds(layer()); }
167 void setContents(CFTypeRef contents) { CACFLayerSetContents(layer(), contents); setNeedsCommit(); }
168 CFTypeRef contents() const { return CACFLayerGetContents(layer()); }
170 void setContentsRect(const CGRect& contentsRect) { CACFLayerSetContentsRect(layer(), contentsRect); setNeedsCommit(); }
171 CGRect contentsRect() const { return CACFLayerGetContentsRect(layer()); }
173 void setContentsGravity(ContentsGravityType);
174 ContentsGravityType contentsGravity() const;
176 void setDoubleSided(bool b) { CACFLayerSetDoubleSided(layer(), b); setNeedsCommit(); }
177 bool doubleSided() const { return CACFLayerIsDoubleSided(layer()); }
179 void setEdgeAntialiasingMask(uint32_t mask) { CACFLayerSetEdgeAntialiasingMask(layer(), mask); setNeedsCommit(); }
180 uint32_t edgeAntialiasingMask() const { return CACFLayerGetEdgeAntialiasingMask(layer()); }
182 virtual void setFrame(const CGRect&);
183 CGRect frame() const { return CACFLayerGetFrame(layer()); }
185 void setHidden(bool hidden) { CACFLayerSetHidden(layer(), hidden); setNeedsCommit(); }
186 bool isHidden() const { return CACFLayerIsHidden(layer()); }
188 void setMasksToBounds(bool b) { CACFLayerSetMasksToBounds(layer(), b); }
189 bool masksToBounds() const { return CACFLayerGetMasksToBounds(layer()); }
191 void setMagnificationFilter(FilterType);
192 FilterType magnificationFilter() const;
194 void setMinificationFilter(FilterType);
195 FilterType minificationFilter() const;
197 void setMinificationFilterBias(float bias) { CACFLayerSetMinificationFilterBias(layer(), bias); }
198 float minificationFilterBias() const { return CACFLayerGetMinificationFilterBias(layer()); }
200 void setName(const String& name) { CACFLayerSetName(layer(), RetainPtr<CFStringRef>(AdoptCF, name.createCFString()).get()); }
201 String name() const { return CACFLayerGetName(layer()); }
203 void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
205 void setOpacity(float opacity) { CACFLayerSetOpacity(layer(), opacity); setNeedsCommit(); }
206 float opacity() const { return CACFLayerGetOpacity(layer()); }
208 void setOpaque(bool b) { CACFLayerSetOpaque(layer(), b); setNeedsCommit(); }
209 bool opaque() const { return CACFLayerIsOpaque(layer()); }
211 void setPosition(const CGPoint& position) { CACFLayerSetPosition(layer(), position); setNeedsCommit(); }
212 CGPoint position() const { return CACFLayerGetPosition(layer()); }
214 void setZPosition(CGFloat position) { CACFLayerSetZPosition(layer(), position); setNeedsCommit(); }
215 CGFloat zPosition() const { return CACFLayerGetZPosition(layer()); }
217 void setSpeed(float speed) { CACFLayerSetSpeed(layer(), speed); }
218 CFTimeInterval speed() const { return CACFLayerGetSpeed(layer()); }
220 void setTimeOffset(CFTimeInterval t) { CACFLayerSetTimeOffset(layer(), t); }
221 CFTimeInterval timeOffset() const { return CACFLayerGetTimeOffset(layer()); }
223 WKCACFLayer* rootLayer() const;
225 void setSublayerTransform(const CATransform3D& transform) { CACFLayerSetSublayerTransform(layer(), transform); setNeedsCommit(); }
226 CATransform3D sublayerTransform() const { return CACFLayerGetSublayerTransform(layer()); }
228 WKCACFLayer* superlayer() const;
230 void setTransform(const CATransform3D& transform) { CACFLayerSetTransform(layer(), transform); setNeedsCommit(); }
231 CATransform3D transform() const { return CACFLayerGetTransform(layer()); }
233 void setGeometryFlipped(bool flipped) { CACFLayerSetGeometryFlipped(layer(), flipped); setNeedsCommit(); }
234 bool geometryFlipped() const { return CACFLayerIsGeometryFlipped(layer()); }
237 // Print the tree from the root. Also does consistency checks
238 void printTree() const;
242 WKCACFLayer(LayerType);
244 void setNeedsCommit();
246 CACFLayerRef layer() const { return m_layer.get(); }
247 // This should only be called from removeFromSuperlayer.
248 void removeSublayer(const WKCACFLayer*);
250 // Methods to be overridden for sublayer and rendering management
251 virtual WKCACFLayer* internalSublayerAtIndex(int) const;
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*);
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> >&);
262 virtual void internalSetNeedsDisplay(const CGRect* dirtyRect);
265 // Print this layer and its children to the console
266 void printLayer(int indent) const;
270 static void layoutSublayersProc(CACFLayerRef);
272 RetainPtr<CACFLayerRef> m_layer;
273 WKCACFLayerLayoutClient* m_layoutClient;
274 bool m_needsDisplayOnBoundsChange;
279 #endif // USE(ACCELERATED_COMPOSITING)
281 #endif // WKCACFLayer_h