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
;
71 enum class GpuRasterizationStatus
;
73 class CC_EXPORT LayerTreeHost
{
75 // The SharedBitmapManager will be used on the compositor thread.
76 static scoped_ptr
<LayerTreeHost
> CreateThreaded(
77 LayerTreeHostClient
* client
,
78 SharedBitmapManager
* shared_bitmap_manager
,
79 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
80 TaskGraphRunner
* task_graph_runner
,
81 const LayerTreeSettings
& settings
,
82 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
83 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
84 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
86 static scoped_ptr
<LayerTreeHost
> CreateSingleThreaded(
87 LayerTreeHostClient
* client
,
88 LayerTreeHostSingleThreadClient
* single_thread_client
,
89 SharedBitmapManager
* shared_bitmap_manager
,
90 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
91 TaskGraphRunner
* task_graph_runner
,
92 const LayerTreeSettings
& settings
,
93 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
94 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
95 virtual ~LayerTreeHost();
97 void SetLayerTreeHostClientReady();
99 // LayerTreeHost interface to Proxy.
100 void WillBeginMainFrame();
101 void DidBeginMainFrame();
102 void BeginMainFrame(const BeginFrameArgs
& args
);
103 void BeginMainFrameNotExpectedSoon();
104 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time
);
105 void DidStopFlinging();
107 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
108 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
110 void CommitComplete();
111 void SetOutputSurface(scoped_ptr
<OutputSurface
> output_surface
);
112 void RequestNewOutputSurface();
113 void DidInitializeOutputSurface();
114 void DidFailToInitializeOutputSurface();
115 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
116 LayerTreeHostImplClient
* client
);
117 void DidLoseOutputSurface();
118 bool output_surface_lost() const { return output_surface_lost_
; }
119 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
120 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
121 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
122 bool UpdateLayers(ResourceUpdateQueue
* queue
);
124 // Called when the compositor completed page scale animation.
125 void DidCompletePageScaleAnimation();
127 LayerTreeHostClient
* client() { return client_
; }
128 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
129 return input_handler_weak_ptr_
;
132 void NotifyInputThrottledUntilCommit();
134 void Composite(base::TimeTicks frame_begin_time
);
136 void FinishAllRendering();
138 void SetDeferCommits(bool defer_commits
);
140 int source_frame_number() const { return source_frame_number_
; }
142 void SetNeedsDisplayOnAllLayers();
144 void CollectRenderingStats(RenderingStats
* stats
) const;
146 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
147 return rendering_stats_instrumentation_
.get();
150 const RendererCapabilities
& GetRendererCapabilities() const;
152 void SetNeedsAnimate();
153 virtual void SetNeedsUpdateLayers();
154 virtual void SetNeedsCommit();
155 virtual void SetNeedsFullTreeSync();
156 void SetNeedsRedraw();
157 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
158 bool CommitRequested() const;
159 bool BeginMainFrameRequested() const;
161 void SetNextCommitWaitsForActivation();
163 void SetNextCommitForcesRedraw();
165 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
167 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
168 Layer
* root_layer() { return root_layer_
.get(); }
169 const Layer
* root_layer() const { return root_layer_
.get(); }
170 const Layer
* overscroll_elasticity_layer() const {
171 return overscroll_elasticity_layer_
.get();
173 const Layer
* page_scale_layer() const { return page_scale_layer_
.get(); }
174 void RegisterViewportLayers(scoped_refptr
<Layer
> overscroll_elasticity_layer
,
175 scoped_refptr
<Layer
> page_scale_layer
,
176 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
177 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
178 Layer
* inner_viewport_scroll_layer() const {
179 return inner_viewport_scroll_layer_
.get();
181 Layer
* outer_viewport_scroll_layer() const {
182 return outer_viewport_scroll_layer_
.get();
185 void RegisterSelection(const LayerSelection
& selection
);
187 const LayerTreeSettings
& settings() const { return settings_
; }
189 void SetDebugState(const LayerTreeDebugState
& debug_state
);
190 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
192 bool has_gpu_rasterization_trigger() const {
193 return has_gpu_rasterization_trigger_
;
195 void SetHasGpuRasterizationTrigger(bool has_trigger
);
196 bool UseGpuRasterization() const;
197 GpuRasterizationStatus
GetGpuRasterizationStatus() const;
199 void SetViewportSize(const gfx::Size
& device_viewport_size
);
200 void SetTopControlsHeight(float height
, bool shrink
);
201 void SetTopControlsShownRatio(float ratio
);
203 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
205 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
206 void SetPageScaleFactorAndLimits(float page_scale_factor
,
207 float min_page_scale_factor
,
208 float max_page_scale_factor
);
209 float page_scale_factor() const { return page_scale_factor_
; }
210 gfx::Vector2dF
elastic_overscroll() const { return elastic_overscroll_
; }
212 SkColor
background_color() const { return background_color_
; }
213 void set_background_color(SkColor color
) { background_color_
= color
; }
215 void set_has_transparent_background(bool transparent
) {
216 has_transparent_background_
= transparent
;
219 PrioritizedResourceManager
* contents_texture_manager() const {
220 return contents_texture_manager_
.get();
223 void SetVisible(bool visible
);
224 bool visible() const { return visible_
; }
226 void SetThrottleFrameProduction(bool throttle
);
228 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
231 base::TimeDelta duration
);
233 void ApplyScrollAndScale(ScrollAndScaleSet
* info
);
234 void SetImplTransform(const gfx::Transform
& transform
);
236 // Virtual for tests.
237 virtual void StartRateLimiter();
238 virtual void StopRateLimiter();
242 bool AlwaysUsePartialTextureUpdates();
243 size_t MaxPartialTextureUpdates() const;
244 bool RequestPartialTextureUpdate();
246 void SetDeviceScaleFactor(float device_scale_factor
);
247 float device_scale_factor() const { return device_scale_factor_
; }
249 void UpdateTopControlsState(TopControlsState constraints
,
250 TopControlsState current
,
253 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
255 Proxy
* proxy() const { return proxy_
.get(); }
257 AnimationRegistrar
* animation_registrar() const {
258 return animation_registrar_
.get();
261 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
263 // CreateUIResource creates a resource given a bitmap. The bitmap is
264 // generated via an interface function, which is called when initializing the
265 // resource and when the resource has been lost (due to lost context). The
266 // parameter of the interface is a single boolean, which indicates whether the
267 // resource has been lost or not. CreateUIResource returns an Id of the
268 // resource, which is always positive.
269 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
270 // Deletes a UI resource. May safely be called more than once.
271 virtual void DeleteUIResource(UIResourceId id
);
272 // Put the recreation of all UI resources into the resource queue after they
273 // were evicted on the impl thread.
274 void RecreateUIResources();
276 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
278 bool UsingSharedMemoryResources();
279 int id() const { return id_
; }
281 // Returns the id of the benchmark on success, 0 otherwise.
282 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
283 scoped_ptr
<base::Value
> value
,
284 const MicroBenchmark::DoneCallback
& callback
);
285 // Returns true if the message was successfully delivered and handled.
286 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
288 // When a SwapPromiseMonitor is created on the main thread, it calls
289 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
290 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
291 // to unregister itself.
292 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
293 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
295 // Call this function when you expect there to be a swap buffer.
296 // See swap_promise.h for how to use SwapPromise.
297 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
299 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
301 size_t num_queued_swap_promises() const { return swap_promise_list_
.size(); }
303 void set_surface_id_namespace(uint32_t id_namespace
);
304 SurfaceSequence
CreateSurfaceSequence();
306 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
) const;
307 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) const;
309 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
311 PropertyTrees
* property_trees() { return &property_trees_
; }
314 LayerTreeHost(LayerTreeHostClient
* client
,
315 SharedBitmapManager
* shared_bitmap_manager
,
316 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
317 TaskGraphRunner
* task_graph_runner
,
318 const LayerTreeSettings
& settings
);
319 void InitializeThreaded(
320 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
321 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
322 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
323 void InitializeSingleThreaded(
324 LayerTreeHostSingleThreadClient
* single_thread_client
,
325 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
326 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
327 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
328 void SetOutputSurfaceLostForTesting(bool is_lost
) {
329 output_surface_lost_
= is_lost
;
332 MicroBenchmarkController micro_benchmark_controller_
;
335 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
337 void PaintLayerContents(
338 const RenderSurfaceLayerList
& render_surface_layer_list
,
339 ResourceUpdateQueue
* queue
,
340 bool* did_paint_content
,
341 bool* need_more_updates
);
342 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
343 ResourceUpdateQueue
* queue
,
344 bool* did_paint_content
,
345 bool* need_more_updates
);
346 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
347 void UpdateHudLayer();
348 void TriggerPrepaint();
350 void ReduceMemoryUsage();
352 void PrioritizeTextures(
353 const RenderSurfaceLayerList
& render_surface_layer_list
);
354 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
355 void SetPrioritiesForLayers(const RenderSurfaceLayerList
& update_list
);
356 size_t CalculateMemoryForRenderSurfaces(
357 const RenderSurfaceLayerList
& update_list
);
359 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
361 struct UIResourceClientData
{
362 UIResourceClient
* client
;
366 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
368 UIResourceClientMap ui_resource_client_map_
;
369 int next_ui_resource_id_
;
371 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
372 UIResourceRequestQueue ui_resource_request_queue_
;
374 void RecordGpuRasterizationHistogram();
375 void CalculateLCDTextMetricsCallback(Layer
* layer
);
377 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
379 bool inside_begin_main_frame_
;
380 bool needs_full_tree_sync_
;
382 base::CancelableClosure prepaint_callback_
;
384 LayerTreeHostClient
* client_
;
385 scoped_ptr
<Proxy
> proxy_
;
387 int source_frame_number_
;
388 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
390 bool output_surface_lost_
;
392 scoped_refptr
<Layer
> root_layer_
;
393 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
395 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
396 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
398 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
399 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
401 const LayerTreeSettings settings_
;
402 LayerTreeDebugState debug_state_
;
404 gfx::Size device_viewport_size_
;
405 bool top_controls_shrink_blink_size_
;
406 float top_controls_height_
;
407 float top_controls_shown_ratio_
;
408 float device_scale_factor_
;
412 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
414 float page_scale_factor_
;
415 float min_page_scale_factor_
;
416 float max_page_scale_factor_
;
417 gfx::Vector2dF elastic_overscroll_
;
418 bool has_gpu_rasterization_trigger_
;
419 bool content_is_suitable_for_gpu_rasterization_
;
420 bool gpu_rasterization_histogram_recorded_
;
422 SkColor background_color_
;
423 bool has_transparent_background_
;
425 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
426 size_t partial_texture_update_requests_
;
428 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
430 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
432 // If set, then page scale animation has completed, but the client hasn't been
433 // notified about it yet.
434 bool did_complete_scale_animation_
;
436 bool in_paint_layer_contents_
;
439 bool next_commit_forces_redraw_
;
441 scoped_refptr
<Layer
> overscroll_elasticity_layer_
;
442 scoped_refptr
<Layer
> page_scale_layer_
;
443 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
444 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
446 LayerSelection selection_
;
448 SharedBitmapManager
* shared_bitmap_manager_
;
449 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
450 TaskGraphRunner
* task_graph_runner_
;
452 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
453 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
455 PropertyTrees property_trees_
;
457 uint32_t surface_id_namespace_
;
458 uint32_t next_surface_sequence_
;
460 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
465 #endif // CC_TREES_LAYER_TREE_HOST_H_