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 int meta_information_sequence_number() {
145 return meta_information_sequence_number_
;
148 void IncrementMetaInformationSequenceNumber() {
149 meta_information_sequence_number_
++;
152 void SetNeedsDisplayOnAllLayers();
154 void CollectRenderingStats(RenderingStats
* stats
) const;
156 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
157 return rendering_stats_instrumentation_
.get();
160 const RendererCapabilities
& GetRendererCapabilities() const;
162 void SetNeedsAnimate();
163 virtual void SetNeedsUpdateLayers();
164 virtual void SetNeedsCommit();
165 virtual void SetNeedsFullTreeSync();
166 virtual void SetNeedsMetaInfoRecomputation(
167 bool needs_meta_info_recomputation
);
168 void SetNeedsRedraw();
169 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
170 bool CommitRequested() const;
171 bool BeginMainFrameRequested() const;
173 void SetNextCommitWaitsForActivation();
175 void SetNextCommitForcesRedraw();
177 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
179 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
180 Layer
* root_layer() { return root_layer_
.get(); }
181 const Layer
* root_layer() const { return root_layer_
.get(); }
182 const Layer
* overscroll_elasticity_layer() const {
183 return overscroll_elasticity_layer_
.get();
185 const Layer
* page_scale_layer() const { return page_scale_layer_
.get(); }
186 void RegisterViewportLayers(scoped_refptr
<Layer
> overscroll_elasticity_layer
,
187 scoped_refptr
<Layer
> page_scale_layer
,
188 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
189 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
190 Layer
* inner_viewport_scroll_layer() const {
191 return inner_viewport_scroll_layer_
.get();
193 Layer
* outer_viewport_scroll_layer() const {
194 return outer_viewport_scroll_layer_
.get();
197 void RegisterSelection(const LayerSelection
& selection
);
199 const LayerTreeSettings
& settings() const { return settings_
; }
201 void SetDebugState(const LayerTreeDebugState
& debug_state
);
202 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
204 bool has_gpu_rasterization_trigger() const {
205 return has_gpu_rasterization_trigger_
;
207 void SetHasGpuRasterizationTrigger(bool has_trigger
);
209 void SetViewportSize(const gfx::Size
& device_viewport_size
);
210 void SetTopControlsHeight(float height
, bool shrink
);
211 void SetTopControlsShownRatio(float ratio
);
213 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
215 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
216 void SetPageScaleFactorAndLimits(float page_scale_factor
,
217 float min_page_scale_factor
,
218 float max_page_scale_factor
);
219 float page_scale_factor() const { return page_scale_factor_
; }
220 gfx::Vector2dF
elastic_overscroll() const { return elastic_overscroll_
; }
222 SkColor
background_color() const { return background_color_
; }
223 void set_background_color(SkColor color
) { background_color_
= color
; }
225 void set_has_transparent_background(bool transparent
) {
226 has_transparent_background_
= transparent
;
229 PrioritizedResourceManager
* contents_texture_manager() const {
230 return contents_texture_manager_
.get();
233 void SetVisible(bool visible
);
234 bool visible() const { return visible_
; }
236 void SetThrottleFrameProduction(bool throttle
);
238 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
241 base::TimeDelta duration
);
243 void ApplyScrollAndScale(ScrollAndScaleSet
* info
);
244 void SetImplTransform(const gfx::Transform
& transform
);
246 // Virtual for tests.
247 virtual void StartRateLimiter();
248 virtual void StopRateLimiter();
252 bool AlwaysUsePartialTextureUpdates();
253 size_t MaxPartialTextureUpdates() const;
254 bool RequestPartialTextureUpdate();
256 void SetDeviceScaleFactor(float device_scale_factor
);
257 float device_scale_factor() const { return device_scale_factor_
; }
259 void UpdateTopControlsState(TopControlsState constraints
,
260 TopControlsState current
,
263 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
265 Proxy
* proxy() const { return proxy_
.get(); }
267 AnimationRegistrar
* animation_registrar() const {
268 return animation_registrar_
.get();
271 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
273 // CreateUIResource creates a resource given a bitmap. The bitmap is
274 // generated via an interface function, which is called when initializing the
275 // resource and when the resource has been lost (due to lost context). The
276 // parameter of the interface is a single boolean, which indicates whether the
277 // resource has been lost or not. CreateUIResource returns an Id of the
278 // resource, which is always positive.
279 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
280 // Deletes a UI resource. May safely be called more than once.
281 virtual void DeleteUIResource(UIResourceId id
);
282 // Put the recreation of all UI resources into the resource queue after they
283 // were evicted on the impl thread.
284 void RecreateUIResources();
286 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
288 bool UsingSharedMemoryResources();
289 int id() const { return id_
; }
291 // Returns the id of the benchmark on success, 0 otherwise.
292 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
293 scoped_ptr
<base::Value
> value
,
294 const MicroBenchmark::DoneCallback
& callback
);
295 // Returns true if the message was successfully delivered and handled.
296 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
298 // When a SwapPromiseMonitor is created on the main thread, it calls
299 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
300 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
301 // to unregister itself.
302 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
303 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
305 // Call this function when you expect there to be a swap buffer.
306 // See swap_promise.h for how to use SwapPromise.
307 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
309 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
311 size_t num_queued_swap_promises() const { return swap_promise_list_
.size(); }
313 void set_surface_id_namespace(uint32_t id_namespace
);
314 SurfaceSequence
CreateSurfaceSequence();
316 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
) const;
317 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) const;
319 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
321 PropertyTrees
* property_trees() { return &property_trees_
; }
322 bool needs_meta_info_recomputation() {
323 return needs_meta_info_recomputation_
;
326 // If this is true, only property trees will be used for main thread CDP.
327 // CDP will not be run, and verify_property_trees will be ignored.
328 bool using_only_property_trees() const {
329 return settings().impl_side_painting
;
333 explicit LayerTreeHost(InitParams
* params
);
334 void InitializeThreaded(
335 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
336 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
337 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
338 void InitializeSingleThreaded(
339 LayerTreeHostSingleThreadClient
* single_thread_client
,
340 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
341 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
342 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
343 void SetOutputSurfaceLostForTesting(bool is_lost
) {
344 output_surface_lost_
= is_lost
;
347 // shared_bitmap_manager(), gpu_memory_buffer_manager(), and
348 // task_graph_runner() return valid values only until the LayerTreeHostImpl is
349 // created in CreateLayerTreeHostImpl().
350 SharedBitmapManager
* shared_bitmap_manager() const {
351 return shared_bitmap_manager_
;
353 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
354 return gpu_memory_buffer_manager_
;
356 TaskGraphRunner
* task_graph_runner() const { return task_graph_runner_
; }
358 MicroBenchmarkController micro_benchmark_controller_
;
361 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
363 void PaintLayerContents(
364 const RenderSurfaceLayerList
& render_surface_layer_list
,
365 ResourceUpdateQueue
* queue
,
366 bool* did_paint_content
,
367 bool* need_more_updates
);
368 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
369 ResourceUpdateQueue
* queue
,
370 bool* did_paint_content
,
371 bool* need_more_updates
);
372 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
373 void UpdateHudLayer();
374 void TriggerPrepaint();
376 void ReduceMemoryUsage();
378 void PrioritizeTextures(
379 const RenderSurfaceLayerList
& render_surface_layer_list
);
380 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
381 void SetPrioritiesForLayers(const RenderSurfaceLayerList
& update_list
);
382 size_t CalculateMemoryForRenderSurfaces(
383 const RenderSurfaceLayerList
& update_list
);
385 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
387 struct UIResourceClientData
{
388 UIResourceClient
* client
;
392 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
394 UIResourceClientMap ui_resource_client_map_
;
395 int next_ui_resource_id_
;
397 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
398 UIResourceRequestQueue ui_resource_request_queue_
;
400 void RecordGpuRasterizationHistogram();
401 void CalculateLCDTextMetricsCallback(Layer
* layer
);
403 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
405 bool inside_begin_main_frame_
;
406 bool needs_full_tree_sync_
;
407 bool needs_meta_info_recomputation_
;
409 base::CancelableClosure prepaint_callback_
;
411 LayerTreeHostClient
* client_
;
412 scoped_ptr
<Proxy
> proxy_
;
414 int source_frame_number_
;
415 int meta_information_sequence_number_
;
416 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
418 bool output_surface_lost_
;
420 scoped_refptr
<Layer
> root_layer_
;
421 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
423 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
424 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
426 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
427 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
429 const LayerTreeSettings settings_
;
430 LayerTreeDebugState debug_state_
;
432 gfx::Size device_viewport_size_
;
433 bool top_controls_shrink_blink_size_
;
434 float top_controls_height_
;
435 float top_controls_shown_ratio_
;
436 float device_scale_factor_
;
440 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
442 float page_scale_factor_
;
443 float min_page_scale_factor_
;
444 float max_page_scale_factor_
;
445 gfx::Vector2dF elastic_overscroll_
;
446 bool has_gpu_rasterization_trigger_
;
447 bool content_is_suitable_for_gpu_rasterization_
;
448 bool gpu_rasterization_histogram_recorded_
;
450 SkColor background_color_
;
451 bool has_transparent_background_
;
453 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
454 size_t partial_texture_update_requests_
;
456 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
458 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
460 // If set, then page scale animation has completed, but the client hasn't been
461 // notified about it yet.
462 bool did_complete_scale_animation_
;
464 bool in_paint_layer_contents_
;
467 bool next_commit_forces_redraw_
;
469 scoped_refptr
<Layer
> overscroll_elasticity_layer_
;
470 scoped_refptr
<Layer
> page_scale_layer_
;
471 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
472 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
474 LayerSelection selection_
;
476 SharedBitmapManager
* shared_bitmap_manager_
;
477 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
478 TaskGraphRunner
* task_graph_runner_
;
480 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
481 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
483 PropertyTrees property_trees_
;
485 uint32_t surface_id_namespace_
;
486 uint32_t next_surface_sequence_
;
488 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
493 #endif // CC_TREES_LAYER_TREE_HOST_H_