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 "base/basictypes.h"
10 #include "CCAnimationEvents.h"
11 #include "CCInputHandler.h"
12 #include "CCLayerSorter.h"
13 #include "CCRenderPass.h"
14 #include "CCRenderPassSink.h"
15 #include "CCRenderer.h"
17 #include <public/WebCompositorOutputSurfaceClient.h>
18 #include <wtf/PassOwnPtr.h>
19 #include <wtf/RefPtr.h>
23 class CCCompletionEvent
;
24 class CCDebugRectHistory
;
25 class CCFrameRateCounter
;
26 class CCHeadsUpDisplayLayerImpl
;
28 class CCLayerTreeHostImplTimeSourceAdapter
;
29 class CCPageScaleAnimation
;
30 class CCRenderPassDrawQuad
;
31 class CCResourceProvider
;
32 struct RendererCapabilities
;
33 struct CCRenderingStats
;
35 // CCLayerTreeHost->CCProxy callback interface.
36 class CCLayerTreeHostImplClient
{
38 virtual void didLoseContextOnImplThread() = 0;
39 virtual void onSwapBuffersCompleteOnImplThread() = 0;
40 virtual void onVSyncParametersChanged(double monotonicTimebase
, double intervalInSeconds
) = 0;
41 virtual void onCanDrawStateChanged(bool canDraw
) = 0;
42 virtual void setNeedsRedrawOnImplThread() = 0;
43 virtual void setNeedsCommitOnImplThread() = 0;
44 virtual void postAnimationEventsToMainThreadOnImplThread(scoped_ptr
<CCAnimationEventsVector
>, double wallClockTime
) = 0;
45 virtual void releaseContentsTexturesOnImplThread() = 0;
48 // CCPinchZoomViewport models the bounds and offset of the viewport that is used during a pinch-zoom operation.
49 // It tracks the layout-space dimensions of the viewport before any applied scale, and then tracks the layout-space
50 // coordinates of the viewport respecting the pinch settings.
51 class CCPinchZoomViewport
{
53 CCPinchZoomViewport();
55 float totalPageScaleFactor() const;
57 void setPageScaleFactor(float factor
) { m_pageScaleFactor
= factor
; }
58 float pageScaleFactor() const { return m_pageScaleFactor
; }
60 void setPageScaleDelta(float delta
);
61 float pageScaleDelta() const { return m_pageScaleDelta
; }
63 float minPageScaleFactor() const { return m_minPageScaleFactor
; }
64 float maxPageScaleFactor() const { return m_maxPageScaleFactor
; }
66 void setSentPageScaleDelta(float delta
) { m_sentPageScaleDelta
= delta
; }
67 float sentPageScaleDelta() const { return m_sentPageScaleDelta
; }
69 // Returns true if the passed parameters were different from those previously
71 bool setPageScaleFactorAndLimits(float pageScaleFactor
,
72 float minPageScaleFactor
,
73 float maxPageScaleFactor
);
75 // Returns the bounds and offset of the scaled and translated viewport to use for pinch-zoom.
76 FloatRect
bounds() const;
77 const FloatPoint
& scrollDelta() const { return m_pinchViewportScrollDelta
; }
79 void setLayoutViewportSize(const FloatSize
& size
) { m_layoutViewportSize
= size
; }
81 // Apply the scroll offset in layout space to the offset of the pinch-zoom viewport. The viewport cannot be
82 // scrolled outside of the layout viewport bounds. Returns the component of the scroll that is un-applied due to
84 FloatSize
applyScroll(FloatSize
&);
86 WebKit::WebTransformationMatrix
implTransform() const;
89 float m_pageScaleFactor
;
90 float m_pageScaleDelta
;
91 float m_sentPageScaleDelta
;
92 float m_maxPageScaleFactor
;
93 float m_minPageScaleFactor
;
95 FloatPoint m_pinchViewportScrollDelta
;
96 FloatSize m_layoutViewportSize
;
99 // CCLayerTreeHostImpl owns the CCLayerImpl tree as well as associated rendering state
100 class CCLayerTreeHostImpl
: public CCInputHandlerClient
,
101 public CCRendererClient
,
102 public WebKit::WebCompositorOutputSurfaceClient
{
103 typedef std::vector
<CCLayerImpl
*> CCLayerList
;
106 static scoped_ptr
<CCLayerTreeHostImpl
> create(const CCLayerTreeSettings
&, CCLayerTreeHostImplClient
*);
107 virtual ~CCLayerTreeHostImpl();
109 // CCInputHandlerClient implementation
110 virtual CCInputHandlerClient::ScrollStatus
scrollBegin(const IntPoint
&, CCInputHandlerClient::ScrollInputType
) OVERRIDE
;
111 virtual void scrollBy(const IntPoint
&, const IntSize
&) OVERRIDE
;
112 virtual void scrollEnd() OVERRIDE
;
113 virtual void pinchGestureBegin() OVERRIDE
;
114 virtual void pinchGestureUpdate(float, const IntPoint
&) OVERRIDE
;
115 virtual void pinchGestureEnd() OVERRIDE
;
116 virtual void startPageScaleAnimation(const IntSize
& targetPosition
, bool anchorPoint
, float pageScale
, double startTime
, double duration
) OVERRIDE
;
117 virtual void scheduleAnimation() OVERRIDE
;
119 struct FrameData
: public CCRenderPassSink
{
123 Vector
<IntRect
> occludingScreenSpaceRects
;
124 CCRenderPassList renderPasses
;
125 CCRenderPassIdHashMap renderPassesById
;
126 CCLayerList
* renderSurfaceLayerList
;
127 CCLayerList willDrawLayers
;
129 // CCRenderPassSink implementation.
130 virtual void appendRenderPass(scoped_ptr
<CCRenderPass
>) OVERRIDE
;
133 // Virtual for testing.
134 virtual void beginCommit();
135 virtual void commitComplete();
136 virtual void animate(double monotonicTime
, double wallClockTime
);
138 // Returns false if problems occured preparing the frame, and we should try
139 // to avoid displaying the frame. If prepareToDraw is called,
140 // didDrawAllLayers must also be called, regardless of whether drawLayers is
141 // called between the two.
142 virtual bool prepareToDraw(FrameData
&);
143 virtual void drawLayers(const FrameData
&);
144 // Must be called if and only if prepareToDraw was called.
145 void didDrawAllLayers(const FrameData
&);
147 // CCRendererClient implementation
148 virtual const IntSize
& deviceViewportSize() const OVERRIDE
;
149 virtual const CCLayerTreeSettings
& settings() const OVERRIDE
;
150 virtual void didLoseContext() OVERRIDE
;
151 virtual void onSwapBuffersComplete() OVERRIDE
;
152 virtual void setFullRootLayerDamage() OVERRIDE
;
153 virtual void releaseContentsTextures() OVERRIDE
;
154 virtual void setMemoryAllocationLimitBytes(size_t) OVERRIDE
;
156 // WebCompositorOutputSurfaceClient implementation.
157 virtual void onVSyncParametersChanged(double monotonicTimebase
, double intervalInSeconds
) OVERRIDE
;
161 CCGraphicsContext
* context() const;
163 std::string
layerTreeAsText() const;
165 void finishAllRendering();
166 int sourceAnimationFrameNumber() const;
168 bool initializeRenderer(scoped_ptr
<CCGraphicsContext
>);
169 bool isContextLost();
170 CCRenderer
* renderer() { return m_renderer
.get(); }
171 const RendererCapabilities
& rendererCapabilities() const;
175 void readback(void* pixels
, const IntRect
&);
177 void setRootLayer(PassOwnPtr
<CCLayerImpl
>);
178 CCLayerImpl
* rootLayer() { return m_rootLayerImpl
.get(); }
180 void setHudLayer(CCHeadsUpDisplayLayerImpl
* layerImpl
) { m_hudLayerImpl
= layerImpl
; }
181 CCHeadsUpDisplayLayerImpl
* hudLayer() { return m_hudLayerImpl
; }
183 // Release ownership of the current layer tree and replace it with an empty
184 // tree. Returns the root layer of the detached tree.
185 PassOwnPtr
<CCLayerImpl
> detachLayerTree();
187 CCLayerImpl
* rootScrollLayer() const { return m_rootScrollLayerImpl
; }
189 bool visible() const { return m_visible
; }
190 void setVisible(bool);
192 int sourceFrameNumber() const { return m_sourceFrameNumber
; }
193 void setSourceFrameNumber(int frameNumber
) { m_sourceFrameNumber
= frameNumber
; }
195 bool contentsTexturesPurged() const { return m_contentsTexturesPurged
; }
196 void setContentsTexturesPurged();
197 void resetContentsTexturesPurged();
198 size_t memoryAllocationLimitBytes() const { return m_memoryAllocationLimitBytes
; }
200 void setViewportSize(const IntSize
& layoutViewportSize
, const IntSize
& deviceViewportSize
);
201 const IntSize
& layoutViewportSize() const { return m_layoutViewportSize
; }
203 float deviceScaleFactor() const { return m_deviceScaleFactor
; }
204 void setDeviceScaleFactor(float);
206 float pageScaleFactor() const;
207 void setPageScaleFactorAndLimits(float pageScaleFactor
, float minPageScaleFactor
, float maxPageScaleFactor
);
209 PassOwnPtr
<CCScrollAndScaleSet
> processScrollDeltas();
210 WebKit::WebTransformationMatrix
implTransform() const;
212 void startPageScaleAnimation(const IntSize
& tragetPosition
, bool useAnchor
, float scale
, double durationSec
);
214 SkColor
backgroundColor() const { return m_backgroundColor
; }
215 void setBackgroundColor(SkColor color
) { m_backgroundColor
= color
; }
217 bool hasTransparentBackground() const { return m_hasTransparentBackground
; }
218 void setHasTransparentBackground(bool transparent
) { m_hasTransparentBackground
= transparent
; }
220 bool needsAnimateLayers() const { return m_needsAnimateLayers
; }
221 void setNeedsAnimateLayers() { m_needsAnimateLayers
= true; }
223 void setNeedsRedraw();
225 void renderingStats(CCRenderingStats
*) const;
227 void updateRootScrollLayerImplTransform();
229 CCFrameRateCounter
* fpsCounter() const { return m_fpsCounter
.get(); }
230 CCDebugRectHistory
* debugRectHistory() const { return m_debugRectHistory
.get(); }
231 CCResourceProvider
* resourceProvider() const { return m_resourceProvider
.get(); }
233 class CullRenderPassesWithCachedTextures
{
235 bool shouldRemoveRenderPass(const CCRenderPassDrawQuad
&, const FrameData
&) const;
237 // Iterates from the root first, in order to remove the surfaces closest
238 // to the root with cached textures, and all surfaces that draw into
240 size_t renderPassListBegin(const CCRenderPassList
& list
) const { return list
.size() - 1; }
241 size_t renderPassListEnd(const CCRenderPassList
&) const { return 0 - 1; }
242 size_t renderPassListNext(size_t it
) const { return it
- 1; }
244 CullRenderPassesWithCachedTextures(CCRenderer
& renderer
) : m_renderer(renderer
) { }
246 CCRenderer
& m_renderer
;
249 class CullRenderPassesWithNoQuads
{
251 bool shouldRemoveRenderPass(const CCRenderPassDrawQuad
&, const FrameData
&) const;
253 // Iterates in draw order, so that when a surface is removed, and its
254 // target becomes empty, then its target can be removed also.
255 size_t renderPassListBegin(const CCRenderPassList
&) const { return 0; }
256 size_t renderPassListEnd(const CCRenderPassList
& list
) const { return list
.size(); }
257 size_t renderPassListNext(size_t it
) const { return it
+ 1; }
260 template<typename RenderPassCuller
>
261 static void removeRenderPasses(RenderPassCuller
, FrameData
&);
264 CCLayerTreeHostImpl(const CCLayerTreeSettings
&, CCLayerTreeHostImplClient
*);
266 void animatePageScale(double monotonicTime
);
267 void animateScrollbars(double monotonicTime
);
269 // Exposed for testing.
270 void calculateRenderSurfaceLayerList(CCLayerList
&);
272 // Virtual for testing.
273 virtual void animateLayers(double monotonicTime
, double wallClockTime
);
275 // Virtual for testing.
276 virtual base::TimeDelta
lowFrequencyAnimationInterval() const;
278 CCLayerTreeHostImplClient
* m_client
;
279 int m_sourceFrameNumber
;
282 void computeDoubleTapZoomDeltas(CCScrollAndScaleSet
* scrollInfo
);
283 void computePinchZoomDeltas(CCScrollAndScaleSet
* scrollInfo
);
284 void makeScrollAndScaleSet(CCScrollAndScaleSet
* scrollInfo
, const IntSize
& scrollOffset
, float pageScale
);
286 void setPageScaleDelta(float);
287 void updateMaxScrollPosition();
288 void trackDamageForAllSurfaces(CCLayerImpl
* rootDrawLayer
, const CCLayerList
& renderSurfaceLayerList
);
290 // Returns false if the frame should not be displayed. This function should
291 // only be called from prepareToDraw, as didDrawAllLayers must be called
292 // if this helper function is called.
293 bool calculateRenderPasses(FrameData
&);
294 void animateLayersRecursive(CCLayerImpl
*, double monotonicTime
, double wallClockTime
, CCAnimationEventsVector
*, bool& didAnimate
, bool& needsAnimateLayers
);
295 void setBackgroundTickingEnabled(bool);
296 IntSize
contentSize() const;
298 void sendDidLoseContextRecursive(CCLayerImpl
*);
299 void clearRenderSurfaces();
300 bool ensureRenderSurfaceLayerList();
301 void clearCurrentlyScrollingLayer();
303 void animateScrollbarsRecursive(CCLayerImpl
*, double monotonicTime
);
305 void dumpRenderSurfaces(std::string
*, int indent
, const CCLayerImpl
*) const;
307 scoped_ptr
<CCGraphicsContext
> m_context
;
308 OwnPtr
<CCResourceProvider
> m_resourceProvider
;
309 OwnPtr
<CCRenderer
> m_renderer
;
310 OwnPtr
<CCLayerImpl
> m_rootLayerImpl
;
311 CCLayerImpl
* m_rootScrollLayerImpl
;
312 CCLayerImpl
* m_currentlyScrollingLayerImpl
;
313 CCHeadsUpDisplayLayerImpl
* m_hudLayerImpl
;
314 int m_scrollingLayerIdFromPreviousTree
;
315 bool m_scrollDeltaIsInScreenSpace
;
316 CCLayerTreeSettings m_settings
;
317 IntSize m_layoutViewportSize
;
318 IntSize m_deviceViewportSize
;
319 float m_deviceScaleFactor
;
321 bool m_contentsTexturesPurged
;
322 size_t m_memoryAllocationLimitBytes
;
324 SkColor m_backgroundColor
;
325 bool m_hasTransparentBackground
;
327 // If this is true, it is necessary to traverse the layer tree ticking the animators.
328 bool m_needsAnimateLayers
;
329 bool m_pinchGestureActive
;
330 IntPoint m_previousPinchAnchor
;
332 OwnPtr
<CCPageScaleAnimation
> m_pageScaleAnimation
;
334 // This is used for ticking animations slowly when hidden.
335 OwnPtr
<CCLayerTreeHostImplTimeSourceAdapter
> m_timeSourceClientAdapter
;
337 CCLayerSorter m_layerSorter
;
339 // List of visible layers for the most recently prepared frame. Used for
340 // rendering and input event hit testing.
341 CCLayerList m_renderSurfaceLayerList
;
343 CCPinchZoomViewport m_pinchZoomViewport
;
345 OwnPtr
<CCFrameRateCounter
> m_fpsCounter
;
346 OwnPtr
<CCDebugRectHistory
> m_debugRectHistory
;
348 size_t m_numImplThreadScrolls
;
349 size_t m_numMainThreadScrolls
;
351 DISALLOW_COPY_AND_ASSIGN(CCLayerTreeHostImpl
);