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_
14 #include "base/basictypes.h"
15 #include "base/cancelable_callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/time/time.h"
21 #include "base/timer/timer.h"
22 #include "cc/animation/animation_events.h"
23 #include "cc/base/cc_export.h"
24 #include "cc/base/scoped_ptr_vector.h"
25 #include "cc/base/swap_promise.h"
26 #include "cc/base/swap_promise_monitor.h"
27 #include "cc/debug/micro_benchmark.h"
28 #include "cc/debug/micro_benchmark_controller.h"
29 #include "cc/input/input_handler.h"
30 #include "cc/input/layer_selection_bound.h"
31 #include "cc/input/scrollbar.h"
32 #include "cc/input/top_controls_state.h"
33 #include "cc/layers/layer_lists.h"
34 #include "cc/output/output_surface.h"
35 #include "cc/resources/resource_format.h"
36 #include "cc/resources/scoped_ui_resource.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 "third_party/skia/include/core/SkColor.h"
42 #include "ui/gfx/geometry/rect.h"
45 class GpuMemoryBufferManager
;
49 class AnimationRegistrar
;
50 class HeadsUpDisplayLayer
;
52 class LayerTreeHostImpl
;
53 class LayerTreeHostImplClient
;
54 class LayerTreeHostSingleThreadClient
;
55 class PrioritizedResource
;
56 class PrioritizedResourceManager
;
58 class RenderingStatsInstrumentation
;
59 class ResourceProvider
;
60 class ResourceUpdateQueue
;
61 class SharedBitmapManager
;
62 class TopControlsManager
;
63 class UIResourceRequest
;
64 struct RenderingStats
;
65 struct ScrollAndScaleSet
;
67 // Provides information on an Impl's rendering capabilities back to the
69 struct CC_EXPORT RendererCapabilities
{
70 RendererCapabilities(ResourceFormat best_texture_format
,
71 bool allow_partial_texture_updates
,
73 bool using_shared_memory_resources
);
75 RendererCapabilities();
76 ~RendererCapabilities();
78 // Duplicate any modification to this list to RendererCapabilitiesImpl.
79 ResourceFormat best_texture_format
;
80 bool allow_partial_texture_updates
;
82 bool using_shared_memory_resources
;
85 class CC_EXPORT LayerTreeHost
{
87 // The SharedBitmapManager will be used on the compositor thread.
88 static scoped_ptr
<LayerTreeHost
> CreateThreaded(
89 LayerTreeHostClient
* client
,
90 SharedBitmapManager
* shared_bitmap_manager
,
91 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
92 const LayerTreeSettings
& settings
,
93 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
94 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
96 static scoped_ptr
<LayerTreeHost
> CreateSingleThreaded(
97 LayerTreeHostClient
* client
,
98 LayerTreeHostSingleThreadClient
* single_thread_client
,
99 SharedBitmapManager
* shared_bitmap_manager
,
100 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
101 const LayerTreeSettings
& settings
,
102 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
);
103 virtual ~LayerTreeHost();
105 void SetLayerTreeHostClientReady();
107 // LayerTreeHost interface to Proxy.
108 void WillBeginMainFrame() {
109 client_
->WillBeginMainFrame(source_frame_number_
);
111 void DidBeginMainFrame();
112 void BeginMainFrame(const BeginFrameArgs
& args
);
113 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time
);
114 void DidStopFlinging();
116 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
117 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
119 void CommitComplete();
120 void SetOutputSurface(scoped_ptr
<OutputSurface
> output_surface
);
121 void RequestNewOutputSurface();
122 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
123 LayerTreeHostImplClient
* client
);
124 void DidLoseOutputSurface();
125 bool output_surface_lost() const { return output_surface_lost_
; }
126 virtual void OnCreateAndInitializeOutputSurfaceAttempted(bool success
);
127 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
128 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
129 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
130 bool UpdateLayers(ResourceUpdateQueue
* queue
);
132 LayerTreeHostClient
* client() { return client_
; }
133 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
134 return input_handler_weak_ptr_
;
137 void NotifyInputThrottledUntilCommit();
139 void Composite(base::TimeTicks frame_begin_time
);
141 void FinishAllRendering();
143 void SetDeferCommits(bool defer_commits
);
146 virtual void DidDeferCommit();
148 int source_frame_number() const { return source_frame_number_
; }
150 void SetNeedsDisplayOnAllLayers();
152 void CollectRenderingStats(RenderingStats
* stats
) const;
154 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
155 return rendering_stats_instrumentation_
.get();
158 const RendererCapabilities
& GetRendererCapabilities() const;
160 void SetNeedsAnimate();
161 virtual void SetNeedsUpdateLayers();
162 virtual void SetNeedsCommit();
163 virtual void SetNeedsFullTreeSync();
164 void SetNeedsRedraw();
165 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
166 bool CommitRequested() const;
167 bool BeginMainFrameRequested() const;
169 void SetNextCommitWaitsForActivation();
171 void SetNextCommitForcesRedraw();
173 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
175 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
176 Layer
* root_layer() { return root_layer_
.get(); }
177 const Layer
* root_layer() const { return root_layer_
.get(); }
178 const Layer
* page_scale_layer() const { return page_scale_layer_
.get(); }
179 void RegisterViewportLayers(
180 scoped_refptr
<Layer
> page_scale_layer
,
181 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
182 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
183 Layer
* inner_viewport_scroll_layer() const {
184 return inner_viewport_scroll_layer_
.get();
186 Layer
* outer_viewport_scroll_layer() const {
187 return outer_viewport_scroll_layer_
.get();
190 void RegisterSelection(const LayerSelectionBound
& start
,
191 const LayerSelectionBound
& end
);
193 const LayerTreeSettings
& settings() const { return settings_
; }
195 void SetDebugState(const LayerTreeDebugState
& debug_state
);
196 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
198 bool has_gpu_rasterization_trigger() const {
199 return has_gpu_rasterization_trigger_
;
201 void SetHasGpuRasterizationTrigger(bool has_trigger
);
202 bool UseGpuRasterization() const;
204 void SetViewportSize(const gfx::Size
& device_viewport_size
);
205 void SetTopControlsLayoutHeight(float height
);
206 void SetTopControlsContentOffset(float offset
);
208 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
210 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
211 void SetPageScaleFactorAndLimits(float page_scale_factor
,
212 float min_page_scale_factor
,
213 float max_page_scale_factor
);
214 float page_scale_factor() const { return page_scale_factor_
; }
216 SkColor
background_color() const { return background_color_
; }
217 void set_background_color(SkColor color
) { background_color_
= color
; }
219 void set_has_transparent_background(bool transparent
) {
220 has_transparent_background_
= transparent
;
223 void SetOverhangBitmap(const SkBitmap
& bitmap
);
225 PrioritizedResourceManager
* contents_texture_manager() const {
226 return contents_texture_manager_
.get();
229 void SetVisible(bool visible
);
230 bool visible() const { return visible_
; }
232 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
235 base::TimeDelta duration
);
237 void ApplyScrollAndScale(ScrollAndScaleSet
* info
);
238 void SetImplTransform(const gfx::Transform
& transform
);
240 // Virtual for tests.
241 virtual void StartRateLimiter();
242 virtual void StopRateLimiter();
246 bool AlwaysUsePartialTextureUpdates();
247 size_t MaxPartialTextureUpdates() const;
248 bool RequestPartialTextureUpdate();
250 void SetDeviceScaleFactor(float device_scale_factor
);
251 float device_scale_factor() const { return device_scale_factor_
; }
253 void UpdateTopControlsState(TopControlsState constraints
,
254 TopControlsState current
,
257 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
259 Proxy
* proxy() const { return proxy_
.get(); }
261 AnimationRegistrar
* animation_registrar() const {
262 return animation_registrar_
.get();
265 // Obtains a thorough dump of the LayerTreeHost as a value.
266 void AsValueInto(base::debug::TracedValue
* value
) const;
268 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
270 // CreateUIResource creates a resource given a bitmap. The bitmap is
271 // generated via an interface function, which is called when initializing the
272 // resource and when the resource has been lost (due to lost context). The
273 // parameter of the interface is a single boolean, which indicates whether the
274 // resource has been lost or not. CreateUIResource returns an Id of the
275 // resource, which is always positive.
276 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
277 // Deletes a UI resource. May safely be called more than once.
278 virtual void DeleteUIResource(UIResourceId id
);
279 // Put the recreation of all UI resources into the resource queue after they
280 // were evicted on the impl thread.
281 void RecreateUIResources();
283 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
285 bool UsingSharedMemoryResources();
286 int id() const { return id_
; }
288 // Returns the id of the benchmark on success, 0 otherwise.
289 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
290 scoped_ptr
<base::Value
> value
,
291 const MicroBenchmark::DoneCallback
& callback
);
292 // Returns true if the message was successfully delivered and handled.
293 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
295 // When a SwapPromiseMonitor is created on the main thread, it calls
296 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
297 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
298 // to unregister itself.
299 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
300 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
302 // Call this function when you expect there to be a swap buffer.
303 // See swap_promise.h for how to use SwapPromise.
304 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
306 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
308 size_t num_queued_swap_promises() const { return swap_promise_list_
.size(); }
311 LayerTreeHost(LayerTreeHostClient
* client
,
312 SharedBitmapManager
* shared_bitmap_manager
,
313 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
314 const LayerTreeSettings
& settings
);
315 void InitializeThreaded(
316 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
317 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
318 void InitializeSingleThreaded(
319 LayerTreeHostSingleThreadClient
* single_thread_client
,
320 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
);
321 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
322 void SetOutputSurfaceLostForTesting(bool is_lost
) {
323 output_surface_lost_
= is_lost
;
326 MicroBenchmarkController micro_benchmark_controller_
;
329 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
331 void PaintLayerContents(
332 const RenderSurfaceLayerList
& render_surface_layer_list
,
333 ResourceUpdateQueue
* queue
,
334 bool* did_paint_content
,
335 bool* need_more_updates
);
336 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
337 ResourceUpdateQueue
* queue
,
338 bool* did_paint_content
,
339 bool* need_more_updates
);
340 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
341 void UpdateHudLayer();
342 void TriggerPrepaint();
344 void ReduceMemoryUsage();
346 void PrioritizeTextures(
347 const RenderSurfaceLayerList
& render_surface_layer_list
);
348 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
349 void SetPrioritiesForLayers(const RenderSurfaceLayerList
& update_list
);
350 size_t CalculateMemoryForRenderSurfaces(
351 const RenderSurfaceLayerList
& update_list
);
353 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
355 struct UIResourceClientData
{
356 UIResourceClient
* client
;
360 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
362 UIResourceClientMap ui_resource_client_map_
;
363 int next_ui_resource_id_
;
365 typedef std::list
<UIResourceRequest
> UIResourceRequestQueue
;
366 UIResourceRequestQueue ui_resource_request_queue_
;
368 void RecordGpuRasterizationHistogram();
369 void CalculateLCDTextMetricsCallback(Layer
* layer
);
371 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
373 bool inside_begin_main_frame_
;
374 bool needs_full_tree_sync_
;
376 base::CancelableClosure prepaint_callback_
;
378 LayerTreeHostClient
* client_
;
379 scoped_ptr
<Proxy
> proxy_
;
381 int source_frame_number_
;
382 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
384 bool output_surface_lost_
;
385 int num_failed_recreate_attempts_
;
387 scoped_refptr
<Layer
> root_layer_
;
388 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
390 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
391 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
393 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
394 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
396 const LayerTreeSettings settings_
;
397 LayerTreeDebugState debug_state_
;
399 gfx::Size device_viewport_size_
;
400 float top_controls_layout_height_
;
401 float top_controls_content_offset_
;
402 float device_scale_factor_
;
406 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
408 float page_scale_factor_
;
409 float min_page_scale_factor_
;
410 float max_page_scale_factor_
;
411 bool has_gpu_rasterization_trigger_
;
412 bool content_is_suitable_for_gpu_rasterization_
;
413 bool gpu_rasterization_histogram_recorded_
;
415 SkColor background_color_
;
416 bool has_transparent_background_
;
418 // If set, this texture is used to fill in the parts of the screen not
419 // covered by layers.
420 scoped_ptr
<ScopedUIResource
> overhang_ui_resource_
;
422 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
423 size_t partial_texture_update_requests_
;
425 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
427 struct PendingPageScaleAnimation
{
428 gfx::Vector2d target_offset
;
431 base::TimeDelta duration
;
433 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
435 bool in_paint_layer_contents_
;
437 static const int kTotalFramesToUseForLCDTextMetrics
= 50;
438 int total_frames_used_for_lcd_text_metrics_
;
440 struct LCDTextMetrics
{
442 : total_num_cc_layers(0),
443 total_num_cc_layers_can_use_lcd_text(0),
444 total_num_cc_layers_will_use_lcd_text(0) {}
446 int64 total_num_cc_layers
;
447 int64 total_num_cc_layers_can_use_lcd_text
;
448 int64 total_num_cc_layers_will_use_lcd_text
;
450 LCDTextMetrics lcd_text_metrics_
;
452 bool next_commit_forces_redraw_
;
454 scoped_refptr
<Layer
> page_scale_layer_
;
455 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
456 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
458 LayerSelectionBound selection_start_
;
459 LayerSelectionBound selection_end_
;
461 SharedBitmapManager
* shared_bitmap_manager_
;
462 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
464 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
465 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
467 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
472 #endif // CC_TREES_LAYER_TREE_HOST_H_