1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CCLayerTreeHostImpl_h
6 #define CCLayerTreeHostImpl_h
8 #include "CCAnimationEvents.h"
9 #include "CCInputHandler.h"
10 #include "CCLayerSorter.h"
11 #include "CCRenderPass.h"
12 #include "CCRenderPassSink.h"
13 #include "CCRenderer.h"
15 #include <public/WebCompositorOutputSurfaceClient.h>
16 #include <wtf/PassOwnPtr.h>
17 #include <wtf/RefPtr.h>
21 class CCActiveGestureAnimation
;
22 class CCCompletionEvent
;
23 class CCDebugRectHistory
;
24 class CCFrameRateCounter
;
25 class CCHeadsUpDisplayLayerImpl
;
27 class CCLayerTreeHostImplTimeSourceAdapter
;
28 class CCPageScaleAnimation
;
29 class CCRenderPassDrawQuad
;
30 class CCResourceProvider
;
31 struct RendererCapabilities
;
32 struct CCRenderingStats
;
34 // CCLayerTreeHost->CCProxy callback interface.
35 class CCLayerTreeHostImplClient
{
37 virtual void didLoseContextOnImplThread() = 0;
38 virtual void onSwapBuffersCompleteOnImplThread() = 0;
39 virtual void onVSyncParametersChanged(double monotonicTimebase
, double intervalInSeconds
) = 0;
40 virtual void onCanDrawStateChanged(bool canDraw
) = 0;
41 virtual void setNeedsRedrawOnImplThread() = 0;
42 virtual void setNeedsCommitOnImplThread() = 0;
43 virtual void postAnimationEventsToMainThreadOnImplThread(PassOwnPtr
<CCAnimationEventsVector
>, double wallClockTime
) = 0;
46 // CCLayerTreeHostImpl owns the CCLayerImpl tree as well as associated rendering state
47 class CCLayerTreeHostImpl
: public CCInputHandlerClient
,
48 public CCRendererClient
,
49 public WebKit::WebCompositorOutputSurfaceClient
{
50 WTF_MAKE_NONCOPYABLE(CCLayerTreeHostImpl
);
51 typedef Vector
<CCLayerImpl
*> CCLayerList
;
54 static PassOwnPtr
<CCLayerTreeHostImpl
> create(const CCLayerTreeSettings
&, CCLayerTreeHostImplClient
*);
55 virtual ~CCLayerTreeHostImpl();
57 // CCInputHandlerClient implementation
58 virtual CCInputHandlerClient::ScrollStatus
scrollBegin(const IntPoint
&, CCInputHandlerClient::ScrollInputType
) OVERRIDE
;
59 virtual void scrollBy(const IntPoint
&, const IntSize
&) OVERRIDE
;
60 virtual void scrollEnd() OVERRIDE
;
61 virtual void pinchGestureBegin() OVERRIDE
;
62 virtual void pinchGestureUpdate(float, const IntPoint
&) OVERRIDE
;
63 virtual void pinchGestureEnd() OVERRIDE
;
64 virtual void startPageScaleAnimation(const IntSize
& targetPosition
, bool anchorPoint
, float pageScale
, double startTime
, double duration
) OVERRIDE
;
65 virtual CCActiveGestureAnimation
* activeGestureAnimation() OVERRIDE
{ return m_activeGestureAnimation
.get(); }
66 // To clear an active animation, pass nullptr.
67 virtual void setActiveGestureAnimation(PassOwnPtr
<CCActiveGestureAnimation
>) OVERRIDE
;
68 virtual void scheduleAnimation() OVERRIDE
;
70 struct FrameData
: public CCRenderPassSink
{
71 Vector
<IntRect
> occludingScreenSpaceRects
;
72 CCRenderPassList renderPasses
;
73 CCRenderPassIdHashMap renderPassesById
;
74 CCLayerList
* renderSurfaceLayerList
;
75 CCLayerList willDrawLayers
;
77 // CCRenderPassSink implementation.
78 virtual void appendRenderPass(PassOwnPtr
<CCRenderPass
>) OVERRIDE
;
81 // Virtual for testing.
82 virtual void beginCommit();
83 virtual void commitComplete();
84 virtual void animate(double monotonicTime
, double wallClockTime
);
86 // Returns false if problems occured preparing the frame, and we should try
87 // to avoid displaying the frame. If prepareToDraw is called,
88 // didDrawAllLayers must also be called, regardless of whether drawLayers is
89 // called between the two.
90 virtual bool prepareToDraw(FrameData
&);
91 virtual void drawLayers(const FrameData
&);
92 // Must be called if and only if prepareToDraw was called.
93 void didDrawAllLayers(const FrameData
&);
95 // CCRendererClient implementation
96 virtual const IntSize
& deviceViewportSize() const OVERRIDE
{ return m_deviceViewportSize
; }
97 virtual const CCLayerTreeSettings
& settings() const OVERRIDE
{ return m_settings
; }
98 virtual void didLoseContext() OVERRIDE
;
99 virtual void onSwapBuffersComplete() OVERRIDE
;
100 virtual void setFullRootLayerDamage() OVERRIDE
;
101 virtual void releaseContentsTextures() OVERRIDE
;
102 virtual void setMemoryAllocationLimitBytes(size_t) OVERRIDE
;
104 // WebCompositorOutputSurfaceClient implementation.
105 virtual void onVSyncParametersChanged(double monotonicTimebase
, double intervalInSeconds
) OVERRIDE
;
109 CCGraphicsContext
* context() const;
111 String
layerTreeAsText() const;
113 void finishAllRendering();
114 int sourceAnimationFrameNumber() const;
116 bool initializeRenderer(PassOwnPtr
<CCGraphicsContext
>, TextureUploaderOption
);
117 bool isContextLost();
118 CCRenderer
* renderer() { return m_renderer
.get(); }
119 const RendererCapabilities
& rendererCapabilities() const;
123 void readback(void* pixels
, const IntRect
&);
125 void setRootLayer(PassOwnPtr
<CCLayerImpl
>);
126 CCLayerImpl
* rootLayer() { return m_rootLayerImpl
.get(); }
128 void setHudLayer(CCHeadsUpDisplayLayerImpl
* layerImpl
) { m_hudLayerImpl
= layerImpl
; }
129 CCHeadsUpDisplayLayerImpl
* hudLayer() { return m_hudLayerImpl
; }
131 // Release ownership of the current layer tree and replace it with an empty
132 // tree. Returns the root layer of the detached tree.
133 PassOwnPtr
<CCLayerImpl
> detachLayerTree();
135 CCLayerImpl
* rootScrollLayer() const { return m_rootScrollLayerImpl
; }
137 bool visible() const { return m_visible
; }
138 void setVisible(bool);
140 int sourceFrameNumber() const { return m_sourceFrameNumber
; }
141 void setSourceFrameNumber(int frameNumber
) { m_sourceFrameNumber
= frameNumber
; }
143 bool contentsTexturesPurged() const { return m_contentsTexturesPurged
; }
144 void resetContentsTexturesPurged();
145 size_t memoryAllocationLimitBytes() const { return m_memoryAllocationLimitBytes
; }
147 void setViewportSize(const IntSize
& layoutViewportSize
, const IntSize
& deviceViewportSize
);
148 const IntSize
& layoutViewportSize() const { return m_layoutViewportSize
; }
150 float deviceScaleFactor() const { return m_deviceScaleFactor
; }
151 void setDeviceScaleFactor(float);
153 float pageScale() const { return m_pageScale
; }
154 void setPageScaleFactorAndLimits(float pageScale
, float minPageScale
, float maxPageScale
);
156 PassOwnPtr
<CCScrollAndScaleSet
> processScrollDeltas();
158 void startPageScaleAnimation(const IntSize
& tragetPosition
, bool useAnchor
, float scale
, double durationSec
);
160 SkColor
backgroundColor() const { return m_backgroundColor
; }
161 void setBackgroundColor(SkColor color
) { m_backgroundColor
= color
; }
163 bool hasTransparentBackground() const { return m_hasTransparentBackground
; }
164 void setHasTransparentBackground(bool transparent
) { m_hasTransparentBackground
= transparent
; }
166 bool needsAnimateLayers() const { return m_needsAnimateLayers
; }
167 void setNeedsAnimateLayers() { m_needsAnimateLayers
= true; }
169 void setNeedsRedraw();
171 void renderingStats(CCRenderingStats
&) const;
173 CCFrameRateCounter
* fpsCounter() const { return m_fpsCounter
.get(); }
174 CCDebugRectHistory
* debugRectHistory() const { return m_debugRectHistory
.get(); }
175 CCResourceProvider
* resourceProvider() const { return m_resourceProvider
.get(); }
177 class CullRenderPassesWithCachedTextures
{
179 bool shouldRemoveRenderPass(const CCRenderPassDrawQuad
&, const FrameData
&) const;
181 // Iterates from the root first, in order to remove the surfaces closest
182 // to the root with cached textures, and all surfaces that draw into
184 size_t renderPassListBegin(const CCRenderPassList
& list
) const { return list
.size() - 1; }
185 size_t renderPassListEnd(const CCRenderPassList
&) const { return 0 - 1; }
186 size_t renderPassListNext(size_t it
) const { return it
- 1; }
188 CullRenderPassesWithCachedTextures(CCRenderer
& renderer
) : m_renderer(renderer
) { }
190 CCRenderer
& m_renderer
;
193 class CullRenderPassesWithNoQuads
{
195 bool shouldRemoveRenderPass(const CCRenderPassDrawQuad
&, const FrameData
&) const;
197 // Iterates in draw order, so that when a surface is removed, and its
198 // target becomes empty, then its target can be removed also.
199 size_t renderPassListBegin(const CCRenderPassList
&) const { return 0; }
200 size_t renderPassListEnd(const CCRenderPassList
& list
) const { return list
.size(); }
201 size_t renderPassListNext(size_t it
) const { return it
+ 1; }
204 template<typename RenderPassCuller
>
205 static void removeRenderPasses(RenderPassCuller
, FrameData
&);
208 CCLayerTreeHostImpl(const CCLayerTreeSettings
&, CCLayerTreeHostImplClient
*);
210 void animatePageScale(double monotonicTime
);
211 void animateGestures(double monotonicTime
);
212 void animateScrollbars(double monotonicTime
);
214 // Exposed for testing.
215 void calculateRenderSurfaceLayerList(CCLayerList
&);
217 // Virtual for testing.
218 virtual void animateLayers(double monotonicTime
, double wallClockTime
);
220 // Virtual for testing. Measured in seconds.
221 virtual double lowFrequencyAnimationInterval() const;
223 CCLayerTreeHostImplClient
* m_client
;
224 int m_sourceFrameNumber
;
227 void computeDoubleTapZoomDeltas(CCScrollAndScaleSet
* scrollInfo
);
228 void computePinchZoomDeltas(CCScrollAndScaleSet
* scrollInfo
);
229 void makeScrollAndScaleSet(CCScrollAndScaleSet
* scrollInfo
, const IntSize
& scrollOffset
, float pageScale
);
231 void setPageScaleDelta(float);
232 void updateMaxScrollPosition();
233 void trackDamageForAllSurfaces(CCLayerImpl
* rootDrawLayer
, const CCLayerList
& renderSurfaceLayerList
);
235 // Returns false if the frame should not be displayed. This function should
236 // only be called from prepareToDraw, as didDrawAllLayers must be called
237 // if this helper function is called.
238 bool calculateRenderPasses(FrameData
&);
239 void animateLayersRecursive(CCLayerImpl
*, double monotonicTime
, double wallClockTime
, CCAnimationEventsVector
*, bool& didAnimate
, bool& needsAnimateLayers
);
240 void setBackgroundTickingEnabled(bool);
241 IntSize
contentSize() const;
243 void sendDidLoseContextRecursive(CCLayerImpl
*);
244 void clearRenderSurfaces();
245 bool ensureRenderSurfaceLayerList();
246 void clearCurrentlyScrollingLayer();
248 void animateScrollbarsRecursive(CCLayerImpl
*, double monotonicTime
);
250 void dumpRenderSurfaces(TextStream
&, int indent
, const CCLayerImpl
*) const;
252 OwnPtr
<CCGraphicsContext
> m_context
;
253 OwnPtr
<CCResourceProvider
> m_resourceProvider
;
254 OwnPtr
<CCRenderer
> m_renderer
;
255 OwnPtr
<CCLayerImpl
> m_rootLayerImpl
;
256 CCLayerImpl
* m_rootScrollLayerImpl
;
257 CCLayerImpl
* m_currentlyScrollingLayerImpl
;
258 CCHeadsUpDisplayLayerImpl
* m_hudLayerImpl
;
259 int m_scrollingLayerIdFromPreviousTree
;
260 bool m_scrollDeltaIsInScreenSpace
;
261 CCLayerTreeSettings m_settings
;
262 IntSize m_layoutViewportSize
;
263 IntSize m_deviceViewportSize
;
264 float m_deviceScaleFactor
;
266 bool m_contentsTexturesPurged
;
267 size_t m_memoryAllocationLimitBytes
;
270 float m_pageScaleDelta
;
271 float m_sentPageScaleDelta
;
272 float m_minPageScale
, m_maxPageScale
;
274 SkColor m_backgroundColor
;
275 bool m_hasTransparentBackground
;
277 // If this is true, it is necessary to traverse the layer tree ticking the animators.
278 bool m_needsAnimateLayers
;
279 bool m_pinchGestureActive
;
280 IntPoint m_previousPinchAnchor
;
282 OwnPtr
<CCPageScaleAnimation
> m_pageScaleAnimation
;
283 OwnPtr
<CCActiveGestureAnimation
> m_activeGestureAnimation
;
285 // This is used for ticking animations slowly when hidden.
286 OwnPtr
<CCLayerTreeHostImplTimeSourceAdapter
> m_timeSourceClientAdapter
;
288 CCLayerSorter m_layerSorter
;
290 // List of visible layers for the most recently prepared frame. Used for
291 // rendering and input event hit testing.
292 CCLayerList m_renderSurfaceLayerList
;
294 OwnPtr
<CCFrameRateCounter
> m_fpsCounter
;
295 OwnPtr
<CCDebugRectHistory
> m_debugRectHistory
;