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_TREES_LAYER_TREE_HOST_IMPL_H_
6 #define CC_TREES_LAYER_TREE_HOST_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time.h"
14 #include "cc/animation/animation_events.h"
15 #include "cc/animation/animation_registrar.h"
16 #include "cc/base/cc_export.h"
17 #include "cc/input/input_handler.h"
18 #include "cc/input/top_controls_manager_client.h"
19 #include "cc/layers/layer_lists.h"
20 #include "cc/layers/render_pass_sink.h"
21 #include "cc/output/output_surface_client.h"
22 #include "cc/output/renderer.h"
23 #include "cc/quads/render_pass.h"
24 #include "cc/resources/tile_manager.h"
25 #include "skia/ext/refptr.h"
26 #include "third_party/skia/include/core/SkColor.h"
27 #include "third_party/skia/include/core/SkPicture.h"
28 #include "ui/gfx/rect.h"
32 class CompletionEvent
;
33 class CompositorFrameMetadata
;
34 class DebugRectHistory
;
35 class FrameRateCounter
;
37 class LayerTreeHostImplTimeSourceAdapter
;
39 class PageScaleAnimation
;
40 class PaintTimeCounter
;
42 class RenderingStatsInstrumentation
;
43 class RenderPassDrawQuad
;
44 class ResourceProvider
;
45 class TopControlsManager
;
46 struct RendererCapabilities
;
48 // LayerTreeHost->Proxy callback interface.
49 class LayerTreeHostImplClient
{
51 virtual void DidLoseOutputSurfaceOnImplThread() = 0;
52 virtual void OnSwapBuffersCompleteOnImplThread() = 0;
53 virtual void OnVSyncParametersChanged(base::TimeTicks timebase
,
54 base::TimeDelta interval
) = 0;
55 virtual void DidVSync(base::TimeTicks frame_time
) = 0;
56 virtual void OnCanDrawStateChanged(bool can_draw
) = 0;
57 virtual void OnHasPendingTreeStateChanged(bool has_pending_tree
) = 0;
58 virtual void SetNeedsRedrawOnImplThread() = 0;
59 virtual void DidInitializeVisibleTileOnImplThread() = 0;
60 virtual void SetNeedsCommitOnImplThread() = 0;
61 virtual void SetNeedsManageTilesOnImplThread() = 0;
62 virtual void PostAnimationEventsToMainThreadOnImplThread(
63 scoped_ptr
<AnimationEventsVector
> events
,
64 base::Time wall_clock_time
) = 0;
65 // Returns true if resources were deleted by this call.
66 virtual bool ReduceContentsTextureMemoryOnImplThread(
68 int priority_cutoff
) = 0;
69 virtual void ReduceWastedContentsTextureMemoryOnImplThread() = 0;
70 virtual void SendManagedMemoryStats() = 0;
71 virtual bool IsInsideDraw() = 0;
72 virtual void RenewTreePriority() = 0;
73 virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay
) = 0;
76 virtual ~LayerTreeHostImplClient() {}
79 // LayerTreeHostImpl owns the LayerImpl trees as well as associated rendering
81 class CC_EXPORT LayerTreeHostImpl
: public InputHandlerClient
,
82 public RendererClient
,
83 public TileManagerClient
,
84 public OutputSurfaceClient
,
85 public TopControlsManagerClient
{
87 static scoped_ptr
<LayerTreeHostImpl
> Create(
88 const LayerTreeSettings
& settings
,
89 LayerTreeHostImplClient
* client
,
91 RenderingStatsInstrumentation
* rendering_stats_instrumentation
);
92 virtual ~LayerTreeHostImpl();
94 // InputHandlerClient implementation
95 virtual InputHandlerClient::ScrollStatus
ScrollBegin(
96 gfx::Point viewport_point
,
97 InputHandlerClient::ScrollInputType type
) OVERRIDE
;
98 virtual bool ScrollBy(gfx::Point viewport_point
,
99 gfx::Vector2dF scroll_delta
) OVERRIDE
;
100 virtual bool ScrollVerticallyByPage(
101 gfx::Point viewport_point
,
102 WebKit::WebScrollbar::ScrollDirection direction
) OVERRIDE
;
103 virtual void ScrollEnd() OVERRIDE
;
104 virtual void PinchGestureBegin() OVERRIDE
;
105 virtual void PinchGestureUpdate(float magnify_delta
,
106 gfx::Point anchor
) OVERRIDE
;
107 virtual void PinchGestureEnd() OVERRIDE
;
108 virtual void StartPageScaleAnimation(gfx::Vector2d target_offset
,
111 base::TimeTicks start_time
,
112 base::TimeDelta duration
) OVERRIDE
;
113 virtual void ScheduleAnimation() OVERRIDE
;
114 virtual bool HaveTouchEventHandlersAt(gfx::Point viewport_port
) OVERRIDE
;
116 // TopControlsManagerClient implementation.
117 virtual void DidChangeTopControlsPosition() OVERRIDE
;
118 virtual bool HaveRootScrollLayer() const OVERRIDE
;
120 void StartScrollbarAnimation(base::TimeTicks now
);
122 struct CC_EXPORT FrameData
: public RenderPassSink
{
124 virtual ~FrameData();
126 std::vector
<gfx::Rect
> occluding_screen_space_rects
;
127 std::vector
<gfx::Rect
> non_occluding_screen_space_rects
;
128 RenderPassList render_passes
;
129 RenderPassIdHashMap render_passes_by_id
;
130 const LayerImplList
* render_surface_layer_list
;
131 LayerImplList will_draw_layers
;
132 bool contains_incomplete_tile
;
135 // RenderPassSink implementation.
136 virtual void AppendRenderPass(scoped_ptr
<RenderPass
> render_pass
) OVERRIDE
;
139 virtual void BeginCommit();
140 virtual void CommitComplete();
141 virtual void Animate(base::TimeTicks monotonic_time
,
142 base::Time wall_clock_time
);
143 virtual void UpdateAnimationState(bool start_ready_animations
);
144 void UpdateBackgroundAnimateTicking(bool should_background_tick
);
145 void SetViewportDamage(const gfx::Rect
& damage_rect
);
148 void SetAnticipatedDrawTime(base::TimeTicks time
);
150 // Returns false if problems occured preparing the frame, and we should try
151 // to avoid displaying the frame. If PrepareToDraw is called, DidDrawAllLayers
152 // must also be called, regardless of whether DrawLayers is called between the
154 virtual bool PrepareToDraw(FrameData
* frame
,
155 gfx::Rect device_viewport_damage_rect
);
156 virtual void DrawLayers(FrameData
* frame
, base::TimeTicks frame_begin_time
);
157 // Must be called if and only if PrepareToDraw was called.
158 void DidDrawAllLayers(const FrameData
& frame
);
160 const LayerTreeSettings
& settings() const { return settings_
; }
162 // Returns the currently visible viewport size in DIP. This value excludes
163 // the URL bar and non-overlay scrollbars.
164 gfx::SizeF
VisibleViewportSize() const;
166 // RendererClient implementation
168 virtual gfx::Size
DeviceViewportSize() const OVERRIDE
;
169 virtual const LayerTreeSettings
& Settings() const OVERRIDE
;
171 virtual void DidLoseOutputSurface() OVERRIDE
;
172 virtual void OnSwapBuffersComplete() OVERRIDE
;
173 virtual void SetFullRootLayerDamage() OVERRIDE
;
174 virtual void SetManagedMemoryPolicy(const ManagedMemoryPolicy
& policy
)
176 virtual void EnforceManagedMemoryPolicy(const ManagedMemoryPolicy
& policy
)
178 virtual bool HasImplThread() const OVERRIDE
;
179 virtual bool ShouldClearRootRenderPass() const OVERRIDE
;
180 virtual CompositorFrameMetadata
MakeCompositorFrameMetadata() const OVERRIDE
;
181 virtual bool AllowPartialSwap() const OVERRIDE
;
183 // TileManagerClient implementation.
184 virtual void ScheduleManageTiles() OVERRIDE
;
185 virtual void DidInitializeVisibleTile() OVERRIDE
;
187 // OutputSurfaceClient implementation.
188 virtual void OnVSyncParametersChanged(base::TimeTicks timebase
,
189 base::TimeDelta interval
) OVERRIDE
;
190 virtual void DidVSync(base::TimeTicks frame_time
) OVERRIDE
;
191 virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck
& ack
)
194 // Called from LayerTreeImpl.
195 void OnCanDrawStateChangedForTree();
199 OutputSurface
* output_surface() const { return output_surface_
.get(); }
201 std::string
LayerTreeAsText() const;
202 std::string
LayerTreeAsJson() const;
204 void FinishAllRendering();
205 int SourceAnimationFrameNumber() const;
207 virtual bool InitializeRenderer(scoped_ptr
<OutputSurface
> output_surface
);
208 bool IsContextLost();
209 TileManager
* tile_manager() { return tile_manager_
.get(); }
210 Renderer
* renderer() { return renderer_
.get(); }
211 const RendererCapabilities
& GetRendererCapabilities() const;
213 virtual bool SwapBuffers(const FrameData
& frame
);
214 void EnableVSyncNotification(bool enable
);
216 void Readback(void* pixels
, gfx::Rect rect_in_device_viewport
);
218 LayerTreeImpl
* active_tree() { return active_tree_
.get(); }
219 const LayerTreeImpl
* active_tree() const { return active_tree_
.get(); }
220 LayerTreeImpl
* pending_tree() { return pending_tree_
.get(); }
221 const LayerTreeImpl
* pending_tree() const { return pending_tree_
.get(); }
222 const LayerTreeImpl
* recycle_tree() const { return recycle_tree_
.get(); }
223 void CreatePendingTree();
224 void CheckForCompletedTileUploads();
225 virtual bool ActivatePendingTreeIfNeeded();
227 // Shortcuts to layers on the active tree.
228 LayerImpl
* RootLayer() const;
229 LayerImpl
* RootScrollLayer() const;
230 LayerImpl
* CurrentlyScrollingLayer() const;
232 virtual void SetVisible(bool visible
);
233 bool visible() const { return visible_
; }
235 void SetNeedsCommit() { client_
->SetNeedsCommitOnImplThread(); }
236 void SetNeedsRedraw() { client_
->SetNeedsRedrawOnImplThread(); }
238 size_t memory_allocation_limit_bytes() const {
239 return managed_memory_policy_
.bytes_limit_when_visible
;
242 void SetViewportSize(gfx::Size device_viewport_size
);
243 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
245 void SetOverdrawBottomHeight(float overdraw_bottom_height
);
246 float overdraw_bottom_height() const { return overdraw_bottom_height_
; }
248 void SetDeviceScaleFactor(float device_scale_factor
);
249 float device_scale_factor() const { return device_scale_factor_
; }
251 scoped_ptr
<ScrollAndScaleSet
> ProcessScrollDeltas();
253 void StartPageScaleAnimation(gfx::Vector2d target_offset
,
256 base::TimeDelta duration
);
258 bool needs_animate_layers() const {
259 return !animation_registrar_
->active_animation_controllers().empty();
262 void SendManagedMemoryStats(
263 size_t memory_visible_bytes
,
264 size_t memory_visible_and_nearby_bytes
,
265 size_t memory_use_bytes
);
267 void set_max_memory_needed_bytes(size_t bytes
) {
268 max_memory_needed_bytes_
= bytes
;
271 FrameRateCounter
* fps_counter() {
272 return fps_counter_
.get();
274 PaintTimeCounter
* paint_time_counter() {
275 return paint_time_counter_
.get();
277 MemoryHistory
* memory_history() {
278 return memory_history_
.get();
280 DebugRectHistory
* debug_rect_history() {
281 return debug_rect_history_
.get();
283 ResourceProvider
* resource_provider() {
284 return resource_provider_
.get();
286 TopControlsManager
* top_controls_manager() {
287 return top_controls_manager_
.get();
290 Proxy
* proxy() const { return proxy_
; }
292 AnimationRegistrar
* animation_registrar() const {
293 return animation_registrar_
.get();
296 void SetDebugState(const LayerTreeDebugState
& debug_state
);
297 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
299 class CC_EXPORT CullRenderPassesWithCachedTextures
{
301 bool ShouldRemoveRenderPass(const RenderPassDrawQuad
& quad
,
302 const FrameData
& frame
) const;
304 // Iterates from the root first, in order to remove the surfaces closest
305 // to the root with cached textures, and all surfaces that draw into
307 size_t RenderPassListBegin(const RenderPassList
& list
) const {
308 return list
.size() - 1;
310 size_t RenderPassListEnd(const RenderPassList
& list
) const { return 0 - 1; }
311 size_t RenderPassListNext(size_t it
) const { return it
- 1; }
313 explicit CullRenderPassesWithCachedTextures(Renderer
* renderer
)
314 : renderer_(renderer
) {}
319 class CC_EXPORT CullRenderPassesWithNoQuads
{
321 bool ShouldRemoveRenderPass(const RenderPassDrawQuad
& quad
,
322 const FrameData
& frame
) const;
324 // Iterates in draw order, so that when a surface is removed, and its
325 // target becomes empty, then its target can be removed also.
326 size_t RenderPassListBegin(const RenderPassList
& list
) const { return 0; }
327 size_t RenderPassListEnd(const RenderPassList
& list
) const {
330 size_t RenderPassListNext(size_t it
) const { return it
+ 1; }
333 template <typename RenderPassCuller
>
334 static void RemoveRenderPasses(RenderPassCuller culler
, FrameData
* frame
);
336 skia::RefPtr
<SkPicture
> CapturePicture();
338 bool pinch_gesture_active() const { return pinch_gesture_active_
; }
340 void SetTreePriority(TreePriority priority
);
342 void BeginNextFrame();
343 base::TimeTicks
CurrentFrameTime();
345 scoped_ptr
<base::Value
> AsValue() const;
346 scoped_ptr
<base::Value
> ActivationStateAsValue() const;
347 scoped_ptr
<base::Value
> FrameStateAsValue() const;
349 bool page_scale_animation_active() const { return !!page_scale_animation_
; }
353 const LayerTreeSettings
& settings
,
354 LayerTreeHostImplClient
* client
,
356 RenderingStatsInstrumentation
* rendering_stats_instrumentation
);
357 void ActivatePendingTree();
359 // Virtual for testing.
360 virtual void AnimateLayers(base::TimeTicks monotonic_time
,
361 base::Time wall_clock_time
);
363 // Virtual for testing.
364 virtual base::TimeDelta
LowFrequencyAnimationInterval() const;
366 const AnimationRegistrar::AnimationControllerMap
&
367 active_animation_controllers() const {
368 return animation_registrar_
->active_animation_controllers();
371 LayerTreeHostImplClient
* client_
;
375 void AnimatePageScale(base::TimeTicks monotonic_time
);
376 void AnimateScrollbars(base::TimeTicks monotonic_time
);
377 void AnimateTopControls(base::TimeTicks monotonic_time
);
379 gfx::Vector2dF
ScrollLayerWithViewportSpaceDelta(
380 LayerImpl
* layer_impl
,
381 float scale_from_viewport_to_screen_space
,
382 gfx::PointF viewport_point
,
383 gfx::Vector2dF viewport_delta
);
385 void UpdateMaxScrollOffset();
386 void TrackDamageForAllSurfaces(
387 LayerImpl
* root_draw_layer
,
388 const LayerImplList
& render_surface_layer_list
);
390 // Returns false if the frame should not be displayed. This function should
391 // only be called from PrepareToDraw, as DidDrawAllLayers must be called
392 // if this helper function is called.
393 bool CalculateRenderPasses(FrameData
* frame
);
395 void SendDidLoseOutputSurfaceRecursive(LayerImpl
* current
);
396 void ClearRenderSurfaces();
397 bool EnsureRenderSurfaceLayerList();
398 void ClearCurrentlyScrollingLayer();
400 void AnimateScrollbarsRecursive(LayerImpl
* layer
,
401 base::TimeTicks time
);
403 void DumpRenderSurfaces(std::string
* str
,
405 const LayerImpl
* layer
) const;
407 static LayerImpl
* GetNonCompositedContentLayerRecursive(LayerImpl
* layer
);
409 void StartScrollbarAnimationRecursive(LayerImpl
* layer
, base::TimeTicks time
);
411 scoped_ptr
<OutputSurface
> output_surface_
;
412 scoped_ptr
<ResourceProvider
> resource_provider_
;
413 scoped_ptr
<Renderer
> renderer_
;
414 scoped_ptr
<TileManager
> tile_manager_
;
416 // Tree currently being drawn.
417 scoped_ptr
<LayerTreeImpl
> active_tree_
;
419 // In impl-side painting mode, tree with possibly incomplete rasterized
420 // content. May be promoted to active by ActivatePendingTreeIfNeeded().
421 scoped_ptr
<LayerTreeImpl
> pending_tree_
;
423 // In impl-side painting mode, inert tree with layers that can be recycled
424 // by the next sync from the main thread.
425 scoped_ptr
<LayerTreeImpl
> recycle_tree_
;
427 bool did_lock_scrolling_layer_
;
428 bool should_bubble_scrolls_
;
429 bool wheel_scrolling_
;
430 LayerTreeSettings settings_
;
431 LayerTreeDebugState debug_state_
;
432 gfx::Size device_viewport_size_
;
433 float overdraw_bottom_height_
;
434 float device_scale_factor_
;
436 ManagedMemoryPolicy managed_memory_policy_
;
438 bool pinch_gesture_active_
;
439 gfx::Point previous_pinch_anchor_
;
441 // This is set by AnimateLayers() and used by UpdateAnimationState()
442 // when sending animation events to the main thread.
443 base::Time last_animation_time_
;
445 scoped_ptr
<TopControlsManager
> top_controls_manager_
;
447 scoped_ptr
<PageScaleAnimation
> page_scale_animation_
;
449 // This is used for ticking animations slowly when hidden.
450 scoped_ptr
<LayerTreeHostImplTimeSourceAdapter
> time_source_client_adapter_
;
452 scoped_ptr
<FrameRateCounter
> fps_counter_
;
453 scoped_ptr
<PaintTimeCounter
> paint_time_counter_
;
454 scoped_ptr
<MemoryHistory
> memory_history_
;
455 scoped_ptr
<DebugRectHistory
> debug_rect_history_
;
457 // The maximum memory that would be used by the prioritized resource
458 // manager, if there were no limit on memory usage.
459 size_t max_memory_needed_bytes_
;
461 size_t last_sent_memory_visible_bytes_
;
462 size_t last_sent_memory_visible_and_nearby_bytes_
;
463 size_t last_sent_memory_use_bytes_
;
465 gfx::Rect viewport_damage_rect_
;
467 base::TimeTicks current_frame_time_
;
469 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
471 RenderingStatsInstrumentation
* rendering_stats_instrumentation_
;
473 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImpl
);
478 #endif // CC_TREES_LAYER_TREE_HOST_IMPL_H_