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 <wtf/RefCounted.h>
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>
40 #include "GraphicsContext.h"
41 #include "PlatformString.h"
42 #include "TransformationMatrix.h"
50 class WKCACFLayerLayoutClient {
52 virtual void layoutSublayersOfLayer(WKCACFLayer*) = 0;
54 virtual ~WKCACFLayerLayoutClient() {}
57 class WKCACFLayer : public RefCounted<WKCACFLayer> {
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 };
64 static PassRefPtr<WKCACFLayer> create(LayerType);
65 static WKCACFLayer* layer(CACFLayerRef layer)
67 ASSERT(CACFLayerGetUserData(layer) != reinterpret_cast<void*>(0xDEADBEEF));
68 return static_cast<WKCACFLayer*>(CACFLayerGetUserData(layer));
71 virtual ~WKCACFLayer();
73 virtual void setNeedsRender() { }
75 virtual void drawInContext(PlatformGraphicsContext*) { }
77 void setLayoutClient(WKCACFLayerLayoutClient*);
78 WKCACFLayerLayoutClient* layoutClient() const { return m_layoutClient; }
79 void setNeedsLayout() { CACFLayerSetNeedsLayout(layer()); }
81 void setNeedsDisplay(const CGRect* dirtyRect = 0)
83 internalSetNeedsDisplay(dirtyRect);
87 // Makes this layer the root when the passed context is rendered
88 void becomeRootLayerForContext(WKCACFContext*);
90 static RetainPtr<CFTypeRef> cfValue(float value) { return RetainPtr<CFTypeRef>(AdoptCF, CFNumberCreate(0, kCFNumberFloat32Type, &value)); }
91 static RetainPtr<CFTypeRef> cfValue(const TransformationMatrix& value)
110 return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreateTransform(t));
112 static RetainPtr<CFTypeRef> cfValue(const FloatPoint& value)
115 p.x = value.x(); p.y = value.y();
116 return RetainPtr<CFTypeRef>(AdoptCF, CACFVectorCreatePoint(p));
118 static RetainPtr<CFTypeRef> cfValue(const FloatRect& rect)
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));
128 static RetainPtr<CFTypeRef> cfValue(const Color& color)
130 return RetainPtr<CFTypeRef>(AdoptCF, CGColorCreateGenericRGB(color.red(), color.green(), color.blue(), color.alpha()));
133 bool isTransformLayer() const;
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);
141 void removeAllSublayers() { internalRemoveAllSublayers(); }
142 void setSublayers(const Vector<RefPtr<WKCACFLayer> >& sublayers)
144 internalSetSublayers(sublayers);
145 checkLayerConsistency();
148 void insertSublayer(PassRefPtr<WKCACFLayer> layer, size_t index) { internalInsertSublayer(layer, index); }
150 size_t sublayerCount() const { return internalSublayerCount(); }
152 void removeFromSuperlayer();
154 WKCACFLayer* ancestorOrSelfWithSuperlayer(WKCACFLayer*) const;
156 void setAnchorPoint(const CGPoint& p) { CACFLayerSetAnchorPoint(layer(), p); setNeedsCommit(); }
157 CGPoint anchorPoint() const { return CACFLayerGetAnchorPoint(layer()); }
159 void setAnchorPointZ(CGFloat z) { CACFLayerSetAnchorPointZ(layer(), z); setNeedsCommit(); }
160 CGFloat anchorPointZ() const { return CACFLayerGetAnchorPointZ(layer()); }
162 void setBackgroundColor(CGColorRef color) { CACFLayerSetBackgroundColor(layer(), color); setNeedsCommit(); }
163 CGColorRef backgroundColor() const { return CACFLayerGetBackgroundColor(layer()); }
165 void setBorderColor(CGColorRef color) { CACFLayerSetBorderColor(layer(), color); setNeedsCommit(); }
166 CGColorRef borderColor() const { return CACFLayerGetBorderColor(layer()); }
168 void setBorderWidth(CGFloat width) { CACFLayerSetBorderWidth(layer(), width); setNeedsCommit(); }
169 CGFloat borderWidth() const { return CACFLayerGetBorderWidth(layer()); }
171 virtual void setBounds(const CGRect&);
172 CGRect bounds() const { return CACFLayerGetBounds(layer()); }
174 void setContents(CFTypeRef contents) { CACFLayerSetContents(layer(), contents); setNeedsCommit(); }
175 CFTypeRef contents() const { return CACFLayerGetContents(layer()); }
177 void setContentsRect(const CGRect& contentsRect) { CACFLayerSetContentsRect(layer(), contentsRect); setNeedsCommit(); }
178 CGRect contentsRect() const { return CACFLayerGetContentsRect(layer()); }
180 void setContentsGravity(ContentsGravityType);
181 ContentsGravityType contentsGravity() const;
183 void setDoubleSided(bool b) { CACFLayerSetDoubleSided(layer(), b); setNeedsCommit(); }
184 bool doubleSided() const { return CACFLayerIsDoubleSided(layer()); }
186 void setEdgeAntialiasingMask(uint32_t mask) { CACFLayerSetEdgeAntialiasingMask(layer(), mask); setNeedsCommit(); }
187 uint32_t edgeAntialiasingMask() const { return CACFLayerGetEdgeAntialiasingMask(layer()); }
189 virtual void setFrame(const CGRect&);
190 CGRect frame() const { return CACFLayerGetFrame(layer()); }
192 void setHidden(bool hidden) { CACFLayerSetHidden(layer(), hidden); setNeedsCommit(); }
193 bool isHidden() const { return CACFLayerIsHidden(layer()); }
195 void setMasksToBounds(bool b) { CACFLayerSetMasksToBounds(layer(), b); }
196 bool masksToBounds() const { return CACFLayerGetMasksToBounds(layer()); }
198 void setMagnificationFilter(FilterType);
199 FilterType magnificationFilter() const;
201 void setMinificationFilter(FilterType);
202 FilterType minificationFilter() const;
204 void setMinificationFilterBias(float bias) { CACFLayerSetMinificationFilterBias(layer(), bias); }
205 float minificationFilterBias() const { return CACFLayerGetMinificationFilterBias(layer()); }
207 void setName(const String& name) { CACFLayerSetName(layer(), RetainPtr<CFStringRef>(AdoptCF, name.createCFString()).get()); }
208 String name() const { return CACFLayerGetName(layer()); }
210 void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
212 void setOpacity(float opacity) { CACFLayerSetOpacity(layer(), opacity); setNeedsCommit(); }
213 float opacity() const { return CACFLayerGetOpacity(layer()); }
215 void setOpaque(bool b) { CACFLayerSetOpaque(layer(), b); setNeedsCommit(); }
216 bool opaque() const { return CACFLayerIsOpaque(layer()); }
218 void setPosition(const CGPoint& position) { CACFLayerSetPosition(layer(), position); setNeedsCommit(); }
219 CGPoint position() const { return CACFLayerGetPosition(layer()); }
221 void setZPosition(CGFloat position) { CACFLayerSetZPosition(layer(), position); setNeedsCommit(); }
222 CGFloat zPosition() const { return CACFLayerGetZPosition(layer()); }
224 void setSpeed(float speed) { CACFLayerSetSpeed(layer(), speed); }
225 CFTimeInterval speed() const { return CACFLayerGetSpeed(layer()); }
227 void setTimeOffset(CFTimeInterval t) { CACFLayerSetTimeOffset(layer(), t); }
228 CFTimeInterval timeOffset() const { return CACFLayerGetTimeOffset(layer()); }
230 WKCACFLayer* rootLayer() const;
232 void setSublayerTransform(const CATransform3D& transform) { CACFLayerSetSublayerTransform(layer(), transform); setNeedsCommit(); }
233 CATransform3D sublayerTransform() const { return CACFLayerGetSublayerTransform(layer()); }
235 WKCACFLayer* superlayer() const;
237 void setTransform(const CATransform3D& transform) { CACFLayerSetTransform(layer(), transform); setNeedsCommit(); }
238 CATransform3D transform() const { return CACFLayerGetTransform(layer()); }
240 void setGeometryFlipped(bool flipped) { CACFLayerSetGeometryFlipped(layer(), flipped); setNeedsCommit(); }
241 bool geometryFlipped() const { return CACFLayerIsGeometryFlipped(layer()); }
244 // Print the tree from the root. Also does consistency checks
245 void printTree() const;
249 WKCACFLayer(LayerType);
251 void setNeedsCommit();
253 CACFLayerRef layer() const { return m_layer.get(); }
254 // This should only be called from removeFromSuperlayer.
255 void removeSublayer(const WKCACFLayer*);
257 void checkLayerConsistency()
260 internalCheckLayerConsistency();
264 // Methods to be overridden for sublayer and rendering management
265 virtual WKCACFLayer* internalSublayerAtIndex(int) const;
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*);
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> >&);
276 virtual void internalSetNeedsDisplay(const CGRect* dirtyRect);
279 virtual void internalCheckLayerConsistency();
283 // Print this layer and its children to the console
284 void printLayer(int indent) const;
288 static void layoutSublayersProc(CACFLayerRef);
290 RetainPtr<CACFLayerRef> m_layer;
291 WKCACFLayerLayoutClient* m_layoutClient;
292 bool m_needsDisplayOnBoundsChange;
297 #endif // USE(ACCELERATED_COMPOSITING)
299 #endif // WKCACFLayer_h