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_H_
6 #define CC_TREES_LAYER_TREE_HOST_H_
13 #include "base/basictypes.h"
14 #include "base/cancelable_callback.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/time/time.h"
20 #include "base/timer/timer.h"
21 #include "cc/animation/animation_events.h"
22 #include "cc/base/cc_export.h"
23 #include "cc/base/scoped_ptr_vector.h"
24 #include "cc/debug/micro_benchmark.h"
25 #include "cc/debug/micro_benchmark_controller.h"
26 #include "cc/input/input_handler.h"
27 #include "cc/input/layer_selection_bound.h"
28 #include "cc/input/scrollbar.h"
29 #include "cc/input/top_controls_state.h"
30 #include "cc/layers/layer_lists.h"
31 #include "cc/output/output_surface.h"
32 #include "cc/output/renderer_capabilities.h"
33 #include "cc/output/swap_promise.h"
34 #include "cc/resources/resource_format.h"
35 #include "cc/resources/scoped_ui_resource.h"
36 #include "cc/surfaces/surface_sequence.h"
37 #include "cc/trees/layer_tree_host_client.h"
38 #include "cc/trees/layer_tree_host_common.h"
39 #include "cc/trees/layer_tree_settings.h"
40 #include "cc/trees/proxy.h"
41 #include "cc/trees/swap_promise_monitor.h"
42 #include "third_party/skia/include/core/SkColor.h"
43 #include "ui/gfx/geometry/rect.h"
46 class GpuMemoryBufferManager
;
50 class AnimationRegistrar
;
51 class BeginFrameSource
;
52 class HeadsUpDisplayLayer
;
54 class LayerTreeHostImpl
;
55 class LayerTreeHostImplClient
;
56 class LayerTreeHostSingleThreadClient
;
57 class PrioritizedResource
;
58 class PrioritizedResourceManager
;
61 class RenderingStatsInstrumentation
;
62 class ResourceProvider
;
63 class ResourceUpdateQueue
;
64 class SharedBitmapManager
;
65 class TaskGraphRunner
;
66 class TopControlsManager
;
67 class UIResourceRequest
;
68 struct PendingPageScaleAnimation
;
69 struct RenderingStats
;
70 struct ScrollAndScaleSet
;
72 class CC_EXPORT LayerTreeHost
{
74 // TODO(sad): InitParams should be a movable type so that it can be
75 // std::move()d to the Create* functions.
76 struct CC_EXPORT InitParams
{
77 LayerTreeHostClient
* client
= nullptr;
78 SharedBitmapManager
* shared_bitmap_manager
= nullptr;
79 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
= nullptr;
80 TaskGraphRunner
* task_graph_runner
= nullptr;
81 LayerTreeSettings
const* settings
= nullptr;
82 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
;
83 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
;
89 // The SharedBitmapManager will be used on the compositor thread.
90 static scoped_ptr
<LayerTreeHost
> CreateThreaded(
91 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
94 static scoped_ptr
<LayerTreeHost
> CreateSingleThreaded(
95 LayerTreeHostSingleThreadClient
* single_thread_client
,
97 virtual ~LayerTreeHost();
99 void SetLayerTreeHostClientReady();
101 // LayerTreeHost interface to Proxy.
102 void WillBeginMainFrame();
103 void DidBeginMainFrame();
104 void BeginMainFrame(const BeginFrameArgs
& args
);
105 void BeginMainFrameNotExpectedSoon();
106 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time
);
107 void DidStopFlinging();
109 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
110 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
112 void CommitComplete();
113 void SetOutputSurface(scoped_ptr
<OutputSurface
> output_surface
);
114 void RequestNewOutputSurface();
115 void DidInitializeOutputSurface();
116 void DidFailToInitializeOutputSurface();
117 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
118 LayerTreeHostImplClient
* client
);
119 void DidLoseOutputSurface();
120 bool output_surface_lost() const { return output_surface_lost_
; }
121 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
122 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
123 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
124 bool UpdateLayers(ResourceUpdateQueue
* queue
);
126 // Called when the compositor completed page scale animation.
127 void DidCompletePageScaleAnimation();
129 LayerTreeHostClient
* client() { return client_
; }
130 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
131 return input_handler_weak_ptr_
;
134 void NotifyInputThrottledUntilCommit();
136 void Composite(base::TimeTicks frame_begin_time
);
138 void FinishAllRendering();
140 void SetDeferCommits(bool defer_commits
);
142 int source_frame_number() const { return source_frame_number_
; }
144 void SetNeedsDisplayOnAllLayers();
146 void CollectRenderingStats(RenderingStats
* stats
) const;
148 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
149 return rendering_stats_instrumentation_
.get();
152 const RendererCapabilities
& GetRendererCapabilities() const;
154 void SetNeedsAnimate();
155 virtual void SetNeedsUpdateLayers();
156 virtual void SetNeedsCommit();
157 virtual void SetNeedsFullTreeSync();
158 virtual void SetNeedsMetaInfoRecomputation(
159 bool needs_meta_info_recomputation
);
160 void SetNeedsRedraw();
161 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
162 bool CommitRequested() const;
163 bool BeginMainFrameRequested() const;
165 void SetNextCommitWaitsForActivation();
167 void SetNextCommitForcesRedraw();
169 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
171 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
172 Layer
* root_layer() { return root_layer_
.get(); }
173 const Layer
* root_layer() const { return root_layer_
.get(); }
174 const Layer
* overscroll_elasticity_layer() const {
175 return overscroll_elasticity_layer_
.get();
177 const Layer
* page_scale_layer() const { return page_scale_layer_
.get(); }
178 void RegisterViewportLayers(scoped_refptr
<Layer
> overscroll_elasticity_layer
,
179 scoped_refptr
<Layer
> page_scale_layer
,
180 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
181 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
182 Layer
* inner_viewport_scroll_layer() const {
183 return inner_viewport_scroll_layer_
.get();
185 Layer
* outer_viewport_scroll_layer() const {
186 return outer_viewport_scroll_layer_
.get();
189 void RegisterSelection(const LayerSelection
& selection
);
191 const LayerTreeSettings
& settings() const { return settings_
; }
193 void SetDebugState(const LayerTreeDebugState
& debug_state
);
194 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
196 bool has_gpu_rasterization_trigger() const {
197 return has_gpu_rasterization_trigger_
;
199 void SetHasGpuRasterizationTrigger(bool has_trigger
);
201 void SetViewportSize(const gfx::Size
& device_viewport_size
);
202 void SetTopControlsHeight(float height
, bool shrink
);
203 void SetTopControlsShownRatio(float ratio
);
205 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
207 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
208 void SetPageScaleFactorAndLimits(float page_scale_factor
,
209 float min_page_scale_factor
,
210 float max_page_scale_factor
);
211 float page_scale_factor() const { return page_scale_factor_
; }
212 gfx::Vector2dF
elastic_overscroll() const { return elastic_overscroll_
; }
214 SkColor
background_color() const { return background_color_
; }
215 void set_background_color(SkColor color
) { background_color_
= color
; }
217 void set_has_transparent_background(bool transparent
) {
218 has_transparent_background_
= transparent
;
221 PrioritizedResourceManager
* contents_texture_manager() const {
222 return contents_texture_manager_
.get();
225 void SetVisible(bool visible
);
226 bool visible() const { return visible_
; }
228 void SetThrottleFrameProduction(bool throttle
);
230 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
233 base::TimeDelta duration
);
235 void ApplyScrollAndScale(ScrollAndScaleSet
* info
);
236 void SetImplTransform(const gfx::Transform
& transform
);
238 // Virtual for tests.
239 virtual void StartRateLimiter();
240 virtual void StopRateLimiter();
244 bool AlwaysUsePartialTextureUpdates();
245 size_t MaxPartialTextureUpdates() const;
246 bool RequestPartialTextureUpdate();
248 void SetDeviceScaleFactor(float device_scale_factor
);
249 float device_scale_factor() const { return device_scale_factor_
; }
251 void UpdateTopControlsState(TopControlsState constraints
,
252 TopControlsState current
,
255 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
257 Proxy
* proxy() const { return proxy_
.get(); }
259 AnimationRegistrar
* animation_registrar() const {
260 return animation_registrar_
.get();
263 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
265 // CreateUIResource creates a resource given a bitmap. The bitmap is
266 // generated via an interface function, which is called when initializing the
267 // resource and when the resource has been lost (due to lost context). The
268 // parameter of the interface is a single boolean, which indicates whether the
269 // resource has been lost or not. CreateUIResource returns an Id of the
270 // resource, which is always positive.
271 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
272 // Deletes a UI resource. May safely be called more than once.
273 virtual void DeleteUIResource(UIResourceId id
);
274 // Put the recreation of all UI resources into the resource queue after they
275 // were evicted on the impl thread.
276 void RecreateUIResources();
278 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
280 bool UsingSharedMemoryResources();
281 int id() const { return id_
; }
283 // Returns the id of the benchmark on success, 0 otherwise.
284 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
285 scoped_ptr
<base::Value
> value
,
286 const MicroBenchmark::DoneCallback
& callback
);
287 // Returns true if the message was successfully delivered and handled.
288 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
290 // When a SwapPromiseMonitor is created on the main thread, it calls
291 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
292 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
293 // to unregister itself.
294 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
295 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
297 // Call this function when you expect there to be a swap buffer.
298 // See swap_promise.h for how to use SwapPromise.
299 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
301 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
303 size_t num_queued_swap_promises() const { return swap_promise_list_
.size(); }
305 void set_surface_id_namespace(uint32_t id_namespace
);
306 SurfaceSequence
CreateSurfaceSequence();
308 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
) const;
309 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) const;
311 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
313 PropertyTrees
* property_trees() { return &property_trees_
; }
314 bool needs_meta_info_recomputation() {
315 return needs_meta_info_recomputation_
;
318 // If this is true, only property trees will be used for main thread CDP.
319 // CDP will not be run, and verify_property_trees will be ignored.
320 bool using_only_property_trees() const {
321 return settings().impl_side_painting
;
325 explicit LayerTreeHost(InitParams
* params
);
326 void InitializeThreaded(
327 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
328 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
329 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
330 void InitializeSingleThreaded(
331 LayerTreeHostSingleThreadClient
* single_thread_client
,
332 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
333 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
334 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
335 void SetOutputSurfaceLostForTesting(bool is_lost
) {
336 output_surface_lost_
= is_lost
;
339 // shared_bitmap_manager(), gpu_memory_buffer_manager(), and
340 // task_graph_runner() return valid values only until the LayerTreeHostImpl is
341 // created in CreateLayerTreeHostImpl().
342 SharedBitmapManager
* shared_bitmap_manager() const {
343 return shared_bitmap_manager_
;
345 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
346 return gpu_memory_buffer_manager_
;
348 TaskGraphRunner
* task_graph_runner() const { return task_graph_runner_
; }
350 MicroBenchmarkController micro_benchmark_controller_
;
353 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
355 void PaintLayerContents(
356 const RenderSurfaceLayerList
& render_surface_layer_list
,
357 ResourceUpdateQueue
* queue
,
358 bool* did_paint_content
,
359 bool* need_more_updates
);
360 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
361 ResourceUpdateQueue
* queue
,
362 bool* did_paint_content
,
363 bool* need_more_updates
);
364 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
365 void UpdateHudLayer();
366 void TriggerPrepaint();
368 void ReduceMemoryUsage();
370 void PrioritizeTextures(
371 const RenderSurfaceLayerList
& render_surface_layer_list
);
372 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
373 void SetPrioritiesForLayers(const RenderSurfaceLayerList
& update_list
);
374 size_t CalculateMemoryForRenderSurfaces(
375 const RenderSurfaceLayerList
& update_list
);
377 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
379 struct UIResourceClientData
{
380 UIResourceClient
* client
;
384 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
386 UIResourceClientMap ui_resource_client_map_
;
387 int next_ui_resource_id_
;
389 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
390 UIResourceRequestQueue ui_resource_request_queue_
;
392 void RecordGpuRasterizationHistogram();
393 void CalculateLCDTextMetricsCallback(Layer
* layer
);
395 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
397 bool inside_begin_main_frame_
;
398 bool needs_full_tree_sync_
;
399 bool needs_meta_info_recomputation_
;
401 base::CancelableClosure prepaint_callback_
;
403 LayerTreeHostClient
* client_
;
404 scoped_ptr
<Proxy
> proxy_
;
406 int source_frame_number_
;
407 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
409 bool output_surface_lost_
;
411 scoped_refptr
<Layer
> root_layer_
;
412 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
414 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
415 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
417 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
418 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
420 const LayerTreeSettings settings_
;
421 LayerTreeDebugState debug_state_
;
423 gfx::Size device_viewport_size_
;
424 bool top_controls_shrink_blink_size_
;
425 float top_controls_height_
;
426 float top_controls_shown_ratio_
;
427 float device_scale_factor_
;
431 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
433 float page_scale_factor_
;
434 float min_page_scale_factor_
;
435 float max_page_scale_factor_
;
436 gfx::Vector2dF elastic_overscroll_
;
437 bool has_gpu_rasterization_trigger_
;
438 bool content_is_suitable_for_gpu_rasterization_
;
439 bool gpu_rasterization_histogram_recorded_
;
441 SkColor background_color_
;
442 bool has_transparent_background_
;
444 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
445 size_t partial_texture_update_requests_
;
447 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
449 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
451 // If set, then page scale animation has completed, but the client hasn't been
452 // notified about it yet.
453 bool did_complete_scale_animation_
;
455 bool in_paint_layer_contents_
;
458 bool next_commit_forces_redraw_
;
460 scoped_refptr
<Layer
> overscroll_elasticity_layer_
;
461 scoped_refptr
<Layer
> page_scale_layer_
;
462 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
463 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
465 LayerSelection selection_
;
467 SharedBitmapManager
* shared_bitmap_manager_
;
468 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
469 TaskGraphRunner
* task_graph_runner_
;
471 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
472 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
474 PropertyTrees property_trees_
;
476 uint32_t surface_id_namespace_
;
477 uint32_t next_surface_sequence_
;
479 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
484 #endif // CC_TREES_LAYER_TREE_HOST_H_