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 CC_LAYER_TREE_HOST_IMPL_H_
6 #define CC_LAYER_TREE_HOST_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h"
11 #include "cc/animation_events.h"
12 #include "cc/animation_registrar.h"
13 #include "cc/cc_export.h"
14 #include "cc/input_handler.h"
15 #include "cc/output_surface_client.h"
16 #include "cc/render_pass.h"
17 #include "cc/render_pass_sink.h"
18 #include "cc/renderer.h"
19 #include "cc/tile_manager.h"
20 #include "cc/top_controls_manager_client.h"
21 #include "skia/ext/refptr.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "third_party/skia/include/core/SkPicture.h"
24 #include "ui/gfx/rect.h"
28 class CompletionEvent
;
29 class CompositorFrameMetadata
;
30 class DebugRectHistory
;
31 class FrameRateCounter
;
33 class LayerTreeHostImplTimeSourceAdapter
;
35 class PageScaleAnimation
;
36 class PaintTimeCounter
;
38 class RenderPassDrawQuad
;
39 class ResourceProvider
;
40 class TopControlsManager
;
41 struct RendererCapabilities
;
42 struct RenderingStats
;
44 // LayerTreeHost->Proxy callback interface.
45 class LayerTreeHostImplClient
{
47 virtual void didLoseOutputSurfaceOnImplThread() = 0;
48 virtual void onSwapBuffersCompleteOnImplThread() = 0;
49 virtual void onVSyncParametersChanged(base::TimeTicks timebase
, base::TimeDelta interval
) = 0;
50 virtual void onCanDrawStateChanged(bool canDraw
) = 0;
51 virtual void onHasPendingTreeStateChanged(bool hasPendingTree
) = 0;
52 virtual void setNeedsRedrawOnImplThread() = 0;
53 virtual void didUploadVisibleHighResolutionTileOnImplThread() = 0;
54 virtual void setNeedsCommitOnImplThread() = 0;
55 virtual void setNeedsManageTilesOnImplThread() = 0;
56 virtual void postAnimationEventsToMainThreadOnImplThread(scoped_ptr
<AnimationEventsVector
>, base::Time wallClockTime
) = 0;
57 // Returns true if resources were deleted by this call.
58 virtual bool reduceContentsTextureMemoryOnImplThread(size_t limitBytes
, int priorityCutoff
) = 0;
59 virtual void reduceWastedContentsTextureMemoryOnImplThread() = 0;
60 virtual void sendManagedMemoryStats() = 0;
61 virtual bool isInsideDraw() = 0;
62 virtual void renewTreePriority() = 0;
65 // LayerTreeHostImpl owns the LayerImpl tree as well as associated rendering state
66 class CC_EXPORT LayerTreeHostImpl
: public InputHandlerClient
,
67 public RendererClient
,
68 public TileManagerClient
,
69 public OutputSurfaceClient
,
70 public TopControlsManagerClient
{
71 typedef std::vector
<LayerImpl
*> LayerList
;
74 static scoped_ptr
<LayerTreeHostImpl
> create(const LayerTreeSettings
&, LayerTreeHostImplClient
*, Proxy
*);
75 virtual ~LayerTreeHostImpl();
77 // InputHandlerClient implementation
78 virtual InputHandlerClient::ScrollStatus
scrollBegin(gfx::Point
, InputHandlerClient::ScrollInputType
) OVERRIDE
;
79 virtual bool scrollBy(const gfx::Point
&, const gfx::Vector2d
&) OVERRIDE
;
80 virtual void scrollEnd() OVERRIDE
;
81 virtual void pinchGestureBegin() OVERRIDE
;
82 virtual void pinchGestureUpdate(float, gfx::Point
) OVERRIDE
;
83 virtual void pinchGestureEnd() OVERRIDE
;
84 virtual void startPageScaleAnimation(gfx::Vector2d targetOffset
, bool anchorPoint
, float pageScale
, base::TimeTicks startTime
, base::TimeDelta duration
) OVERRIDE
;
85 virtual void scheduleAnimation() OVERRIDE
;
86 virtual bool haveTouchEventHandlersAt(const gfx::Point
&) OVERRIDE
;
88 // TopControlsManagerClient implementation.
89 virtual void setActiveTreeNeedsUpdateDrawProperties() OVERRIDE
;
90 virtual void setNeedsRedraw() OVERRIDE
;
91 virtual bool haveRootScrollLayer() const OVERRIDE
;
92 virtual float rootScrollLayerTotalScrollY() const OVERRIDE
;
94 struct CC_EXPORT FrameData
: public RenderPassSink
{
98 std::vector
<gfx::Rect
> occludingScreenSpaceRects
;
99 std::vector
<gfx::Rect
> nonOccludingScreenSpaceRects
;
100 RenderPassList renderPasses
;
101 RenderPassIdHashMap renderPassesById
;
102 const LayerList
* renderSurfaceLayerList
;
103 LayerList willDrawLayers
;
104 bool containsIncompleteTile
;
106 // RenderPassSink implementation.
107 virtual void appendRenderPass(scoped_ptr
<RenderPass
>) OVERRIDE
;
110 // Virtual for testing.
111 virtual void beginCommit();
112 virtual void commitComplete();
113 virtual void animate(base::TimeTicks monotonicTime
, base::Time wallClockTime
);
117 // Returns false if problems occured preparing the frame, and we should try
118 // to avoid displaying the frame. If prepareToDraw is called,
119 // didDrawAllLayers must also be called, regardless of whether drawLayers is
120 // called between the two.
121 virtual bool prepareToDraw(FrameData
&);
122 virtual void drawLayers(FrameData
&);
123 // Must be called if and only if prepareToDraw was called.
124 void didDrawAllLayers(const FrameData
&);
126 // RendererClient implementation
127 virtual const gfx::Size
& deviceViewportSize() const OVERRIDE
;
128 virtual const LayerTreeSettings
& settings() const OVERRIDE
;
129 virtual void didLoseOutputSurface() OVERRIDE
;
130 virtual void onSwapBuffersComplete() OVERRIDE
;
131 virtual void setFullRootLayerDamage() OVERRIDE
;
132 virtual void setManagedMemoryPolicy(const ManagedMemoryPolicy
& policy
) OVERRIDE
;
133 virtual void enforceManagedMemoryPolicy(const ManagedMemoryPolicy
& policy
) OVERRIDE
;
134 virtual bool hasImplThread() const OVERRIDE
;
135 virtual bool shouldClearRootRenderPass() const OVERRIDE
;
136 virtual CompositorFrameMetadata
makeCompositorFrameMetadata() const OVERRIDE
;
138 // TileManagerClient implementation.
139 virtual void ScheduleManageTiles() OVERRIDE
;
140 virtual void DidUploadVisibleHighResolutionTile() OVERRIDE
;
142 // OutputSurfaceClient implementation.
143 virtual void OnVSyncParametersChanged(base::TimeTicks timebase
, base::TimeDelta interval
) OVERRIDE
;
144 virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck
&) OVERRIDE
;
146 // Called from LayerTreeImpl.
147 void OnCanDrawStateChangedForTree(LayerTreeImpl
*);
151 OutputSurface
* outputSurface() const;
153 std::string
layerTreeAsText() const;
154 std::string
layerTreeAsJson() const;
156 void finishAllRendering();
157 int sourceAnimationFrameNumber() const;
159 bool initializeRenderer(scoped_ptr
<OutputSurface
>);
160 bool isContextLost();
161 TileManager
* tileManager() { return m_tileManager
.get(); }
162 Renderer
* renderer() { return m_renderer
.get(); }
163 const RendererCapabilities
& rendererCapabilities() const;
167 void readback(void* pixels
, const gfx::Rect
&);
169 LayerTreeImpl
* activeTree() { return m_activeTree
.get(); }
170 const LayerTreeImpl
* activeTree() const { return m_activeTree
.get(); }
171 LayerTreeImpl
* pendingTree() { return m_pendingTree
.get(); }
172 const LayerTreeImpl
* pendingTree() const { return m_pendingTree
.get(); }
173 const LayerTreeImpl
* recycleTree() const { return m_recycleTree
.get(); }
174 void createPendingTree();
175 void checkForCompletedTileUploads();
176 virtual void activatePendingTreeIfNeeded();
178 // Shortcuts to layers on the active tree.
179 LayerImpl
* rootLayer() const;
180 LayerImpl
* rootScrollLayer() const;
181 LayerImpl
* currentlyScrollingLayer() const;
183 bool visible() const { return m_visible
; }
184 void setVisible(bool);
186 size_t memoryAllocationLimitBytes() const { return m_managedMemoryPolicy
.bytesLimitWhenVisible
; }
188 void setViewportSize(const gfx::Size
& layoutViewportSize
, const gfx::Size
& deviceViewportSize
);
189 const gfx::Size
& layoutViewportSize() const { return m_layoutViewportSize
; }
191 float deviceScaleFactor() const { return m_deviceScaleFactor
; }
192 void setDeviceScaleFactor(float);
194 scoped_ptr
<ScrollAndScaleSet
> processScrollDeltas();
196 void startPageScaleAnimation(gfx::Vector2d targetOffset
, bool useAnchor
, float scale
, base::TimeDelta duration
);
198 bool needsAnimateLayers() const { return !m_animationRegistrar
->active_animation_controllers().empty(); }
200 void renderingStats(RenderingStats
*) const;
202 void sendManagedMemoryStats(
203 size_t memoryVisibleBytes
,
204 size_t memoryVisibleAndNearbyBytes
,
205 size_t memoryUseBytes
);
207 FrameRateCounter
* fpsCounter() const { return m_fpsCounter
.get(); }
208 PaintTimeCounter
* paintTimeCounter() const { return m_paintTimeCounter
.get(); }
209 MemoryHistory
* memoryHistory() const { return m_memoryHistory
.get(); }
210 DebugRectHistory
* debugRectHistory() const { return m_debugRectHistory
.get(); }
211 ResourceProvider
* resourceProvider() const { return m_resourceProvider
.get(); }
212 TopControlsManager
* topControlsManager() const { return m_topControlsManager
.get(); }
214 Proxy
* proxy() const { return m_proxy
; }
216 AnimationRegistrar
* animationRegistrar() const { return m_animationRegistrar
.get(); }
218 void setDebugState(const LayerTreeDebugState
& debugState
);
219 const LayerTreeDebugState
& debugState() const { return m_debugState
; }
221 void savePaintTime(const base::TimeDelta
& totalPaintTime
);
223 class CC_EXPORT CullRenderPassesWithCachedTextures
{
225 bool shouldRemoveRenderPass(const RenderPassDrawQuad
&, const FrameData
&) const;
227 // Iterates from the root first, in order to remove the surfaces closest
228 // to the root with cached textures, and all surfaces that draw into
230 size_t renderPassListBegin(const RenderPassList
& list
) const { return list
.size() - 1; }
231 size_t renderPassListEnd(const RenderPassList
&) const { return 0 - 1; }
232 size_t renderPassListNext(size_t it
) const { return it
- 1; }
234 CullRenderPassesWithCachedTextures(Renderer
& renderer
) : m_renderer(renderer
) { }
236 Renderer
& m_renderer
;
239 class CC_EXPORT CullRenderPassesWithNoQuads
{
241 bool shouldRemoveRenderPass(const RenderPassDrawQuad
&, const FrameData
&) const;
243 // Iterates in draw order, so that when a surface is removed, and its
244 // target becomes empty, then its target can be removed also.
245 size_t renderPassListBegin(const RenderPassList
&) const { return 0; }
246 size_t renderPassListEnd(const RenderPassList
& list
) const { return list
.size(); }
247 size_t renderPassListNext(size_t it
) const { return it
+ 1; }
250 template<typename RenderPassCuller
>
251 static void removeRenderPasses(RenderPassCuller
, FrameData
&);
253 skia::RefPtr
<SkPicture
> capturePicture();
255 bool pinchGestureActive() const { return m_pinchGestureActive
; }
257 void setTreePriority(TreePriority priority
);
259 void beginNextFrame();
260 base::TimeTicks
currentFrameTime();
262 scoped_ptr
<base::Value
> asValue() const;
263 scoped_ptr
<base::Value
> activationStateAsValue() const;
264 scoped_ptr
<base::Value
> frameStateAsValue() const;
267 LayerTreeHostImpl(const LayerTreeSettings
&, LayerTreeHostImplClient
*, Proxy
*);
268 void activatePendingTree();
270 // Virtual for testing.
271 virtual void animateLayers(base::TimeTicks monotonicTime
, base::Time wallClockTime
);
272 virtual void updateAnimationState();
274 // Virtual for testing.
275 virtual base::TimeDelta
lowFrequencyAnimationInterval() const;
277 const AnimationRegistrar::AnimationControllerMap
& activeAnimationControllers() const { return m_animationRegistrar
->active_animation_controllers(); }
279 LayerTreeHostImplClient
* m_client
;
283 void animatePageScale(base::TimeTicks monotonicTime
);
284 void animateScrollbars(base::TimeTicks monotonicTime
);
286 void computeDoubleTapZoomDeltas(ScrollAndScaleSet
* scrollInfo
);
287 void computePinchZoomDeltas(ScrollAndScaleSet
* scrollInfo
);
288 void makeScrollAndScaleSet(ScrollAndScaleSet
* scrollInfo
, gfx::Vector2d scrollOffset
, float pageScale
);
290 void setPageScaleDelta(float);
291 gfx::Vector2dF
scrollLayerWithViewportSpaceDelta(LayerImpl
* layerImpl
, float scaleFromViewportToScreenSpace
, gfx::PointF viewportPoint
, gfx::Vector2dF viewportDelta
);
293 void updateMaxScrollOffset();
294 void trackDamageForAllSurfaces(LayerImpl
* rootDrawLayer
, const LayerList
& renderSurfaceLayerList
);
296 // Returns false if the frame should not be displayed. This function should
297 // only be called from prepareToDraw, as didDrawAllLayers must be called
298 // if this helper function is called.
299 bool calculateRenderPasses(FrameData
&);
300 void animateLayersRecursive(LayerImpl
*, base::TimeTicks monotonicTime
, base::Time wallClockTime
, AnimationEventsVector
*, bool& didAnimate
, bool& needsAnimateLayers
);
301 void setBackgroundTickingEnabled(bool);
303 void sendDidLoseOutputSurfaceRecursive(LayerImpl
*);
304 void clearRenderSurfaces();
305 bool ensureRenderSurfaceLayerList();
306 void clearCurrentlyScrollingLayer();
308 void animateScrollbarsRecursive(LayerImpl
*, base::TimeTicks monotonicTime
);
310 void dumpRenderSurfaces(std::string
*, int indent
, const LayerImpl
*) const;
312 static LayerImpl
* getNonCompositedContentLayerRecursive(LayerImpl
* layer
);
314 scoped_ptr
<OutputSurface
> m_outputSurface
;
315 scoped_ptr
<ResourceProvider
> m_resourceProvider
;
316 scoped_ptr
<Renderer
> m_renderer
;
317 scoped_ptr
<TileManager
> m_tileManager
;
319 // Tree currently being drawn.
320 scoped_ptr
<LayerTreeImpl
> m_activeTree
;
322 // In impl-side painting mode, tree with possibly incomplete rasterized
323 // content. May be promoted to active by activatePendingTreeIfNeeded().
324 scoped_ptr
<LayerTreeImpl
> m_pendingTree
;
326 // In impl-side painting mode, inert tree with layers that can be recycled
327 // by the next sync from the main thread.
328 scoped_ptr
<LayerTreeImpl
> m_recycleTree
;
330 bool m_didLockScrollingLayer
;
331 bool m_shouldBubbleScrolls
;
332 bool m_wheelScrolling
;
333 LayerTreeSettings m_settings
;
334 LayerTreeDebugState m_debugState
;
335 gfx::Size m_layoutViewportSize
;
336 gfx::Size m_deviceViewportSize
;
337 float m_deviceScaleFactor
;
339 ManagedMemoryPolicy m_managedMemoryPolicy
;
341 bool m_pinchGestureActive
;
342 gfx::Point m_previousPinchAnchor
;
344 // This is set by animateLayers() and used by updateAnimationState()
345 // when sending animation events to the main thread.
346 base::Time m_lastAnimationTime
;
348 scoped_ptr
<TopControlsManager
> m_topControlsManager
;
350 scoped_ptr
<PageScaleAnimation
> m_pageScaleAnimation
;
352 // This is used for ticking animations slowly when hidden.
353 scoped_ptr
<LayerTreeHostImplTimeSourceAdapter
> m_timeSourceClientAdapter
;
355 scoped_ptr
<FrameRateCounter
> m_fpsCounter
;
356 scoped_ptr
<PaintTimeCounter
> m_paintTimeCounter
;
357 scoped_ptr
<MemoryHistory
> m_memoryHistory
;
358 scoped_ptr
<DebugRectHistory
> m_debugRectHistory
;
360 int64 m_numImplThreadScrolls
;
361 int64 m_numMainThreadScrolls
;
363 int64 m_cumulativeNumLayersDrawn
;
365 int64 m_cumulativeNumMissingTiles
;
367 size_t m_lastSentMemoryVisibleBytes
;
368 size_t m_lastSentMemoryVisibleAndNearbyBytes
;
369 size_t m_lastSentMemoryUseBytes
;
371 base::TimeTicks m_currentFrameTime
;
373 scoped_ptr
<AnimationRegistrar
> m_animationRegistrar
;
375 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImpl
);
380 #endif // CC_LAYER_TREE_HOST_IMPL_H_