1/*
2 * Copyright (C) 2010 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 PlatformCALayer_h
27#define PlatformCALayer_h
28
29#if USE(ACCELERATED_COMPOSITING)
30
31#include "GraphicsContext.h"
32#include "PlatformCAAnimation.h"
33#include "PlatformCALayerClient.h"
34#include <QuartzCore/CABase.h>
35#include <wtf/CurrentTime.h>
36#include <wtf/HashMap.h>
37#include <wtf/PassRefPtr.h>
38#include <wtf/RefCounted.h>
39#include <wtf/RetainPtr.h>
40#include <wtf/Vector.h>
41#include <wtf/text/StringHash.h>
42#include <wtf/text/WTFString.h>
43
44OBJC_CLASS AVPlayerLayer;
45
46namespace WebCore {
47
48class PlatformCALayer;
49
50typedef Vector<RefPtr<PlatformCALayer> > PlatformCALayerList;
51
52class PlatformCALayer : public RefCounted<PlatformCALayer> {
53public:
54    static CFTimeInterval currentTimeToMediaTime(double t) { return CACurrentMediaTime() + t - WTF::currentTime(); }
55
56    // LayerTypeRootLayer is used on some platforms. It has no backing store, so setNeedsDisplay
57    // should not call CACFLayerSetNeedsDisplay, but rather just notify the renderer that it
58    // has changed and should be re-rendered.
59    enum LayerType {
60        LayerTypeLayer,
61        LayerTypeWebLayer,
62        LayerTypeTransformLayer,
63        LayerTypeWebTiledLayer,
64        LayerTypeTiledBackingLayer,
65        LayerTypePageTiledBackingLayer,
66        LayerTypeRootLayer,
67        LayerTypeAVPlayerLayer,
68        LayerTypeCustom
69    };
70    enum FilterType { Linear, Nearest, Trilinear };
71
72    static PassRefPtr<PlatformCALayer> create(LayerType, PlatformCALayerClient*);
73
74    // This function passes the layer as a void* rather than a PlatformLayer because PlatformLayer
75    // is defined differently for Obj C and C++. This allows callers from both languages.
76    static PassRefPtr<PlatformCALayer> create(void* platformLayer, PlatformCALayerClient*);
77
78    PassRefPtr<PlatformCALayer> clone(PlatformCALayerClient*) const;
79
80    ~PlatformCALayer();
81
82    // This function passes the layer as a void* rather than a PlatformLayer because PlatformLayer
83    // is defined differently for Obj C and C++. This allows callers from both languages.
84    static PlatformCALayer* platformCALayer(void* platformLayer);
85
86    PlatformLayer* platformLayer() const;
87
88#if PLATFORM(WIN)
89    bool usesTiledBackingLayer() const { return false; }
90#else
91    bool usesTiledBackingLayer() const { return m_layerType == LayerTypePageTiledBackingLayer || m_layerType == LayerTypeTiledBackingLayer; }
92#endif
93
94    PlatformCALayer* rootLayer() const;
95
96    // A list of sublayers that GraphicsLayerCA should maintain as the first sublayers.
97    const PlatformCALayerList* customSublayers() const { return m_customSublayers.get(); }
98
99    static bool isValueFunctionSupported();
100
101    PlatformCALayerClient* owner() const { return m_owner; }
102    void setOwner(PlatformCALayerClient*);
103
104    void animationStarted(CFTimeInterval beginTime);
105    void ensureAnimationsSubmitted();
106
107    // Layout support
108    void setNeedsLayout();
109
110
111    void setNeedsDisplay(const FloatRect* dirtyRect = 0);
112
113    // This tells the layer tree to commit changes and perform a render, without do a setNeedsDisplay on any layer.
114    void setNeedsCommit();
115
116    void setContentsChanged();
117
118    LayerType layerType() const { return m_layerType; }
119
120    PlatformCALayer* superlayer() const;
121    void removeFromSuperlayer();
122    void setSublayers(const PlatformCALayerList&);
123    void removeAllSublayers();
124    void appendSublayer(PlatformCALayer*);
125    void insertSublayer(PlatformCALayer*, size_t index);
126    void replaceSublayer(PlatformCALayer* reference, PlatformCALayer*);
127    size_t sublayerCount() const;
128
129    // This method removes the sublayers from the source and reparents them to the current layer.
130    // Any sublayers previously in the current layer are removed.
131    void adoptSublayers(PlatformCALayer* source);
132
133    void addAnimationForKey(const String& key, PlatformCAAnimation* animation);
134    void removeAnimationForKey(const String& key);
135    PassRefPtr<PlatformCAAnimation> animationForKey(const String& key);
136
137    PlatformCALayer* mask() const;
138    void setMask(PlatformCALayer*);
139
140    bool isOpaque() const;
141    void setOpaque(bool);
142
143    FloatRect bounds() const;
144    void setBounds(const FloatRect&);
145
146    FloatPoint3D position() const;
147    void setPosition(const FloatPoint3D&);
148    void setPosition(const FloatPoint& pos) { setPosition(FloatPoint3D(pos.x(), pos.y(), 0)); }
149
150    FloatPoint3D anchorPoint() const;
151    void setAnchorPoint(const FloatPoint3D&);
152
153    TransformationMatrix transform() const;
154    void setTransform(const TransformationMatrix&);
155
156    TransformationMatrix sublayerTransform() const;
157    void setSublayerTransform(const TransformationMatrix&);
158
159    TransformationMatrix contentsTransform() const;
160    void setContentsTransform(const TransformationMatrix&);
161
162    bool isHidden() const;
163    void setHidden(bool);
164
165    bool isGeometryFlipped() const;
166    void setGeometryFlipped(bool);
167
168    bool isDoubleSided() const;
169    void setDoubleSided(bool);
170
171    bool masksToBounds() const;
172    void setMasksToBounds(bool);
173
174    bool acceleratesDrawing() const;
175    void setAcceleratesDrawing(bool);
176
177    CFTypeRef contents() const;
178    void setContents(CFTypeRef);
179
180    FloatRect contentsRect() const;
181    void setContentsRect(const FloatRect&);
182
183    void setMinificationFilter(FilterType);
184    void setMagnificationFilter(FilterType);
185
186    Color backgroundColor() const;
187    void setBackgroundColor(const Color&);
188
189    float borderWidth() const;
190    void setBorderWidth(float);
191
192    Color borderColor() const;
193    void setBorderColor(const Color&);
194
195    float opacity() const;
196    void setOpacity(float);
197
198#if ENABLE(CSS_FILTERS)
199    void setFilters(const FilterOperations&);
200    static bool filtersCanBeComposited(const FilterOperations&);
201    void copyFiltersFrom(const PlatformCALayer*);
202#endif
203
204    String name() const;
205    void setName(const String&);
206
207    FloatRect frame() const;
208    void setFrame(const FloatRect&);
209
210    float speed() const;
211    void setSpeed(float);
212
213    CFTimeInterval timeOffset() const;
214    void setTimeOffset(CFTimeInterval);
215
216    float contentsScale() const;
217    void setContentsScale(float);
218
219    TiledBacking* tiledBacking();
220
221#if PLATFORM(WIN)
222    HashMap<String, RefPtr<PlatformCAAnimation> >& animations() { return m_animations; }
223#endif
224
225#if PLATFORM(WIN) && !defined(NDEBUG)
226    void printTree() const;
227#endif
228
229#if PLATFORM(MAC) && (PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
230    void synchronouslyDisplayTilesInRect(const FloatRect&);
231#endif
232
233protected:
234    PlatformCALayer(LayerType, PlatformLayer*, PlatformCALayerClient*);
235
236private:
237    AVPlayerLayer* playerLayer() const;
238
239    PlatformCALayerClient* m_owner;
240    LayerType m_layerType;
241
242    OwnPtr<PlatformCALayerList> m_customSublayers;
243#if PLATFORM(MAC) || PLATFORM(WIN)
244    RetainPtr<PlatformLayer> m_layer;
245#endif
246
247#if PLATFORM(WIN)
248    HashMap<String, RefPtr<PlatformCAAnimation> > m_animations;
249#endif
250
251#if PLATFORM(MAC)
252#ifdef __OBJC__
253    RetainPtr<NSObject> m_delegate;
254#else
255    RetainPtr<void> m_delegate;
256#endif
257#endif
258};
259
260}
261
262#endif // USE(ACCELERATED_COMPOSITING)
263
264#endif // PlatformCALayer_h
265