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 "third_party/skia/include/core/SkColor.h"
21 #include "ui/gfx/rect.h"
25 class CompletionEvent
;
26 class CompositorFrameMetadata
;
27 class DebugRectHistory
;
28 class FrameRateCounter
;
30 class LayerTreeHostImplTimeSourceAdapter
;
32 class PageScaleAnimation
;
33 class RenderPassDrawQuad
;
34 class ResourceProvider
;
35 struct RendererCapabilities
;
36 struct RenderingStats
;
38 // LayerTreeHost->Proxy callback interface.
39 class LayerTreeHostImplClient
{
41 virtual void didLoseOutputSurfaceOnImplThread() = 0;
42 virtual void onSwapBuffersCompleteOnImplThread() = 0;
43 virtual void onVSyncParametersChanged(base::TimeTicks timebase
, base::TimeDelta interval
) = 0;
44 virtual void onCanDrawStateChanged(bool canDraw
) = 0;
45 virtual void onHasPendingTreeStateChanged(bool hasPendingTree
) = 0;
46 virtual void setNeedsRedrawOnImplThread() = 0;
47 virtual void setNeedsCommitOnImplThread() = 0;
48 virtual void setNeedsManageTilesOnImplThread() = 0;
49 virtual void postAnimationEventsToMainThreadOnImplThread(scoped_ptr
<AnimationEventsVector
>, base::Time wallClockTime
) = 0;
50 // Returns true if resources were deleted by this call.
51 virtual bool reduceContentsTextureMemoryOnImplThread(size_t limitBytes
, int priorityCutoff
) = 0;
52 virtual void sendManagedMemoryStats() = 0;
55 // PinchZoomViewport models the bounds and offset of the viewport that is used during a pinch-zoom operation.
56 // It tracks the layout-space dimensions of the viewport before any applied scale, and then tracks the layout-space
57 // coordinates of the viewport respecting the pinch settings.
58 class CC_EXPORT PinchZoomViewport
{
62 float totalPageScaleFactor() const;
64 void setPageScaleFactor(float factor
) { m_pageScaleFactor
= factor
; }
65 float pageScaleFactor() const { return m_pageScaleFactor
; }
67 void setPageScaleDelta(float delta
);
68 float pageScaleDelta() const { return m_pageScaleDelta
; }
70 float minPageScaleFactor() const { return m_minPageScaleFactor
; }
71 float maxPageScaleFactor() const { return m_maxPageScaleFactor
; }
73 void setSentPageScaleDelta(float delta
) { m_sentPageScaleDelta
= delta
; }
74 float sentPageScaleDelta() const { return m_sentPageScaleDelta
; }
76 void setDeviceScaleFactor(float factor
) { m_deviceScaleFactor
= factor
; }
77 float deviceScaleFactor() const { return m_deviceScaleFactor
; }
79 // Returns true if the passed parameters were different from those previously
81 bool setPageScaleFactorAndLimits(float pageScaleFactor
,
82 float minPageScaleFactor
,
83 float maxPageScaleFactor
);
85 // Returns the bounds and offset of the scaled and translated viewport to use for pinch-zoom.
86 gfx::RectF
bounds() const;
87 const gfx::Vector2dF
& zoomedViewportOffset() const { return m_zoomedViewportOffset
; }
89 void setLayoutViewportSize(const gfx::SizeF
& size
) { m_layoutViewportSize
= size
; }
91 // Apply the scroll offset in layout space to the offset of the pinch-zoom viewport. The viewport cannot be
92 // scrolled outside of the layout viewport bounds. Returns the component of the scroll that is un-applied due to
94 gfx::Vector2dF
applyScroll(const gfx::Vector2dF
&);
96 // The implTransform goes from the origin of the unzoomedDeviceViewport to the
97 // origin of the zoomedDeviceViewport.
99 // implTransform = S[pageScale] * Tr[-zoomedDeviceViewportOffset]
100 gfx::Transform
implTransform(bool pageScalePinchZoomEnabled
) const;
103 float m_pageScaleFactor
;
104 float m_pageScaleDelta
;
105 float m_sentPageScaleDelta
;
106 float m_maxPageScaleFactor
;
107 float m_minPageScaleFactor
;
108 float m_deviceScaleFactor
;
110 gfx::Vector2dF m_zoomedViewportOffset
;
111 gfx::SizeF m_layoutViewportSize
;
114 // LayerTreeHostImpl owns the LayerImpl tree as well as associated rendering state
115 class CC_EXPORT LayerTreeHostImpl
: public InputHandlerClient
,
116 public RendererClient
,
117 public TileManagerClient
,
118 public OutputSurfaceClient
{
119 typedef std::vector
<LayerImpl
*> LayerList
;
122 static scoped_ptr
<LayerTreeHostImpl
> create(const LayerTreeSettings
&, LayerTreeHostImplClient
*, Proxy
*);
123 virtual ~LayerTreeHostImpl();
125 // InputHandlerClient implementation
126 virtual InputHandlerClient::ScrollStatus
scrollBegin(gfx::Point
, InputHandlerClient::ScrollInputType
) OVERRIDE
;
127 virtual bool scrollBy(const gfx::Point
&, const gfx::Vector2d
&) OVERRIDE
;
128 virtual void scrollEnd() OVERRIDE
;
129 virtual void pinchGestureBegin() OVERRIDE
;
130 virtual void pinchGestureUpdate(float, gfx::Point
) OVERRIDE
;
131 virtual void pinchGestureEnd() OVERRIDE
;
132 virtual void startPageScaleAnimation(gfx::Vector2d targetOffset
, bool anchorPoint
, float pageScale
, base::TimeTicks startTime
, base::TimeDelta duration
) OVERRIDE
;
133 virtual void scheduleAnimation() OVERRIDE
;
134 virtual bool haveTouchEventHandlersAt(const gfx::Point
&) OVERRIDE
;
136 struct CC_EXPORT FrameData
: public RenderPassSink
{
140 std::vector
<gfx::Rect
> occludingScreenSpaceRects
;
141 std::vector
<gfx::Rect
> nonOccludingScreenSpaceRects
;
142 RenderPassList renderPasses
;
143 RenderPassIdHashMap renderPassesById
;
144 const LayerList
* renderSurfaceLayerList
;
145 LayerList willDrawLayers
;
147 // RenderPassSink implementation.
148 virtual void appendRenderPass(scoped_ptr
<RenderPass
>) OVERRIDE
;
151 // Virtual for testing.
152 virtual void beginCommit();
153 virtual void commitComplete();
154 virtual void animate(base::TimeTicks monotonicTime
, base::Time wallClockTime
);
158 // Returns false if problems occured preparing the frame, and we should try
159 // to avoid displaying the frame. If prepareToDraw is called,
160 // didDrawAllLayers must also be called, regardless of whether drawLayers is
161 // called between the two.
162 virtual bool prepareToDraw(FrameData
&);
163 virtual void drawLayers(FrameData
&);
164 // Must be called if and only if prepareToDraw was called.
165 void didDrawAllLayers(const FrameData
&);
167 // RendererClient implementation
168 virtual const gfx::Size
& deviceViewportSize() const OVERRIDE
;
169 virtual const LayerTreeSettings
& settings() const OVERRIDE
;
170 virtual void didLoseOutputSurface() OVERRIDE
;
171 virtual void onSwapBuffersComplete() OVERRIDE
;
172 virtual void setFullRootLayerDamage() OVERRIDE
;
173 virtual void setManagedMemoryPolicy(const ManagedMemoryPolicy
& policy
) OVERRIDE
;
174 virtual void enforceManagedMemoryPolicy(const ManagedMemoryPolicy
& policy
) OVERRIDE
;
175 virtual bool hasImplThread() const OVERRIDE
;
176 virtual bool shouldClearRootRenderPass() const OVERRIDE
;
177 virtual CompositorFrameMetadata
makeCompositorFrameMetadata() const OVERRIDE
;
179 // TileManagerClient implementation.
180 virtual void ScheduleManageTiles() OVERRIDE
;
181 virtual void ScheduleCheckForCompletedSetPixels() OVERRIDE
;
183 // OutputSurfaceClient implementation.
184 virtual void OnVSyncParametersChanged(base::TimeTicks timebase
, base::TimeDelta interval
) OVERRIDE
;
185 virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck
&) OVERRIDE
;
187 // Called from LayerTreeImpl.
188 void OnCanDrawStateChangedForTree(LayerTreeImpl
*);
192 OutputSurface
* outputSurface() const;
194 std::string
layerTreeAsText() const;
195 std::string
layerTreeAsJson() const;
197 void finishAllRendering();
198 int sourceAnimationFrameNumber() const;
200 bool initializeRenderer(scoped_ptr
<OutputSurface
>);
201 bool isContextLost();
202 TileManager
* tileManager() { return m_tileManager
.get(); }
203 Renderer
* renderer() { return m_renderer
.get(); }
204 const RendererCapabilities
& rendererCapabilities() const;
208 void readback(void* pixels
, const gfx::Rect
&);
210 LayerTreeImpl
* activeTree() { return m_activeTree
.get(); }
211 const LayerTreeImpl
* activeTree() const { return m_activeTree
.get(); }
212 LayerTreeImpl
* pendingTree() { return m_pendingTree
.get(); }
213 const LayerTreeImpl
* pendingTree() const { return m_pendingTree
.get(); }
214 void createPendingTree();
215 void activatePendingTreeIfNeeded();
217 // TODO(nduca): Remove these in favor of LayerTreeImpl.
218 void setRootLayer(scoped_ptr
<LayerImpl
>);
219 LayerImpl
* rootLayer() const;
221 // Release ownership of the current layer tree and replace it with an empty
222 // tree. Returns the root layer of the detached tree.
223 scoped_ptr
<LayerImpl
> detachLayerTree();
225 LayerImpl
* rootScrollLayer() const;
227 // TOOD(nduca): This goes away when scrolling moves to LayerTreeImpl.
228 LayerImpl
* currentlyScrollingLayer() const;
230 bool visible() const { return m_visible
; }
231 void setVisible(bool);
233 bool contentsTexturesPurged() const { return m_contentsTexturesPurged
; }
234 void setContentsTexturesPurged();
235 void resetContentsTexturesPurged();
236 size_t memoryAllocationLimitBytes() const { return m_managedMemoryPolicy
.bytesLimitWhenVisible
; }
238 void setViewportSize(const gfx::Size
& layoutViewportSize
, const gfx::Size
& deviceViewportSize
);
239 const gfx::Size
& layoutViewportSize() const { return m_layoutViewportSize
; }
241 float deviceScaleFactor() const { return m_deviceScaleFactor
; }
242 void setDeviceScaleFactor(float);
244 float pageScaleFactor() const;
245 void setPageScaleFactorAndLimits(float pageScaleFactor
, float minPageScaleFactor
, float maxPageScaleFactor
);
247 scoped_ptr
<ScrollAndScaleSet
> processScrollDeltas();
248 gfx::Transform
implTransform() const;
250 void startPageScaleAnimation(gfx::Vector2d targetOffset
, bool useAnchor
, float scale
, base::TimeDelta duration
);
252 SkColor
backgroundColor() const { return m_backgroundColor
; }
253 void setBackgroundColor(SkColor color
) { m_backgroundColor
= color
; }
255 bool hasTransparentBackground() const { return m_hasTransparentBackground
; }
256 void setHasTransparentBackground(bool transparent
) { m_hasTransparentBackground
= transparent
; }
257 bool needsAnimateLayers() const { return !m_animationRegistrar
->active_animation_controllers().empty(); }
259 bool needsUpdateDrawProperties() const { return m_needsUpdateDrawProperties
; }
260 void setNeedsUpdateDrawProperties() { m_needsUpdateDrawProperties
= true; }
262 void setNeedsRedraw();
264 void renderingStats(RenderingStats
*) const;
266 void sendManagedMemoryStats(
267 size_t memoryVisibleBytes
,
268 size_t memoryVisibleAndNearbyBytes
,
269 size_t memoryUseBytes
);
271 FrameRateCounter
* fpsCounter() const { return m_fpsCounter
.get(); }
272 DebugRectHistory
* debugRectHistory() const { return m_debugRectHistory
.get(); }
273 ResourceProvider
* resourceProvider() const { return m_resourceProvider
.get(); }
275 Proxy
* proxy() const { return m_proxy
; }
277 AnimationRegistrar
* animationRegistrar() const { return m_animationRegistrar
.get(); }
279 void setDebugState(const LayerTreeDebugState
& debugState
) { m_debugState
= debugState
; }
280 const LayerTreeDebugState
& debugState() const { return m_debugState
; }
282 class CC_EXPORT CullRenderPassesWithCachedTextures
{
284 bool shouldRemoveRenderPass(const RenderPassDrawQuad
&, const FrameData
&) const;
286 // Iterates from the root first, in order to remove the surfaces closest
287 // to the root with cached textures, and all surfaces that draw into
289 size_t renderPassListBegin(const RenderPassList
& list
) const { return list
.size() - 1; }
290 size_t renderPassListEnd(const RenderPassList
&) const { return 0 - 1; }
291 size_t renderPassListNext(size_t it
) const { return it
- 1; }
293 CullRenderPassesWithCachedTextures(Renderer
& renderer
) : m_renderer(renderer
) { }
295 Renderer
& m_renderer
;
298 class CC_EXPORT CullRenderPassesWithNoQuads
{
300 bool shouldRemoveRenderPass(const RenderPassDrawQuad
&, const FrameData
&) const;
302 // Iterates in draw order, so that when a surface is removed, and its
303 // target becomes empty, then its target can be removed also.
304 size_t renderPassListBegin(const RenderPassList
&) const { return 0; }
305 size_t renderPassListEnd(const RenderPassList
& list
) const { return list
.size(); }
306 size_t renderPassListNext(size_t it
) const { return it
+ 1; }
309 template<typename RenderPassCuller
>
310 static void removeRenderPasses(RenderPassCuller
, FrameData
&);
312 float totalPageScaleFactorForTesting() const { return m_pinchZoomViewport
.totalPageScaleFactor(); }
314 const PinchZoomViewport
& pinchZoomViewport() const { return m_pinchZoomViewport
; }
317 LayerTreeHostImpl(const LayerTreeSettings
&, LayerTreeHostImplClient
*, Proxy
*);
318 void activatePendingTree();
320 // Virtual for testing.
321 virtual void animateLayers(base::TimeTicks monotonicTime
, base::Time wallClockTime
);
323 // Virtual for testing.
324 virtual base::TimeDelta
lowFrequencyAnimationInterval() const;
326 const AnimationRegistrar::AnimationControllerMap
& activeAnimationControllers() const { return m_animationRegistrar
->active_animation_controllers(); }
328 LayerTreeHostImplClient
* m_client
;
332 void animatePageScale(base::TimeTicks monotonicTime
);
333 void animateScrollbars(base::TimeTicks monotonicTime
);
335 void updateDrawProperties();
337 void computeDoubleTapZoomDeltas(ScrollAndScaleSet
* scrollInfo
);
338 void computePinchZoomDeltas(ScrollAndScaleSet
* scrollInfo
);
339 void makeScrollAndScaleSet(ScrollAndScaleSet
* scrollInfo
, gfx::Vector2d scrollOffset
, float pageScale
);
341 void setPageScaleDelta(float);
342 void updateMaxScrollOffset();
343 void trackDamageForAllSurfaces(LayerImpl
* rootDrawLayer
, const LayerList
& renderSurfaceLayerList
);
345 // Returns false if the frame should not be displayed. This function should
346 // only be called from prepareToDraw, as didDrawAllLayers must be called
347 // if this helper function is called.
348 bool calculateRenderPasses(FrameData
&);
349 void animateLayersRecursive(LayerImpl
*, base::TimeTicks monotonicTime
, base::Time wallClockTime
, AnimationEventsVector
*, bool& didAnimate
, bool& needsAnimateLayers
);
350 void setBackgroundTickingEnabled(bool);
351 gfx::Size
contentSize() const;
353 void sendDidLoseOutputSurfaceRecursive(LayerImpl
*);
354 void clearRenderSurfaces();
355 bool ensureRenderSurfaceLayerList();
356 void clearCurrentlyScrollingLayer();
358 void animateScrollbarsRecursive(LayerImpl
*, base::TimeTicks monotonicTime
);
360 void dumpRenderSurfaces(std::string
*, int indent
, const LayerImpl
*) const;
362 scoped_ptr
<OutputSurface
> m_outputSurface
;
363 scoped_ptr
<ResourceProvider
> m_resourceProvider
;
364 scoped_ptr
<Renderer
> m_renderer
;
365 scoped_ptr
<TileManager
> m_tileManager
;
367 scoped_ptr
<LayerTreeImpl
> m_pendingTree
;
368 scoped_ptr
<LayerTreeImpl
> m_activeTree
;
370 bool m_scrollDeltaIsInViewportSpace
;
371 LayerTreeSettings m_settings
;
372 LayerTreeDebugState m_debugState
;
373 gfx::Size m_layoutViewportSize
;
374 gfx::Size m_deviceViewportSize
;
375 float m_deviceScaleFactor
;
377 bool m_contentsTexturesPurged
;
378 ManagedMemoryPolicy m_managedMemoryPolicy
;
380 SkColor m_backgroundColor
;
381 bool m_hasTransparentBackground
;
383 bool m_needsUpdateDrawProperties
;
384 bool m_pinchGestureActive
;
385 gfx::Point m_previousPinchAnchor
;
387 scoped_ptr
<PageScaleAnimation
> m_pageScaleAnimation
;
389 // This is used for ticking animations slowly when hidden.
390 scoped_ptr
<LayerTreeHostImplTimeSourceAdapter
> m_timeSourceClientAdapter
;
392 PinchZoomViewport m_pinchZoomViewport
;
394 scoped_ptr
<FrameRateCounter
> m_fpsCounter
;
395 scoped_ptr
<DebugRectHistory
> m_debugRectHistory
;
397 int64 m_numImplThreadScrolls
;
398 int64 m_numMainThreadScrolls
;
400 int64 m_cumulativeNumLayersDrawn
;
402 int64 m_cumulativeNumMissingTiles
;
404 size_t m_lastSentMemoryVisibleBytes
;
405 size_t m_lastSentMemoryVisibleAndNearbyBytes
;
406 size_t m_lastSentMemoryUseBytes
;
408 scoped_ptr
<AnimationRegistrar
> m_animationRegistrar
;
410 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImpl
);
415 #endif // CC_LAYER_TREE_HOST_IMPL_H_