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/scrollbar.h"
31 #include "cc/input/top_controls_state.h"
32 #include "cc/layers/layer_lists.h"
33 #include "cc/output/output_surface.h"
34 #include "cc/resources/resource_format.h"
35 #include "cc/resources/scoped_ui_resource.h"
36 #include "cc/trees/layer_tree_host_client.h"
37 #include "cc/trees/layer_tree_host_common.h"
38 #include "cc/trees/layer_tree_settings.h"
39 #include "cc/trees/proxy.h"
40 #include "third_party/skia/include/core/SkColor.h"
41 #include "ui/gfx/rect.h"
45 class AnimationRegistrar
;
46 class HeadsUpDisplayLayer
;
48 class LayerTreeHostImpl
;
49 class LayerTreeHostImplClient
;
50 class LayerTreeHostSingleThreadClient
;
51 class PrioritizedResource
;
52 class PrioritizedResourceManager
;
54 class RenderingStatsInstrumentation
;
55 class ResourceProvider
;
56 class ResourceUpdateQueue
;
57 class SharedBitmapManager
;
58 class TopControlsManager
;
59 class UIResourceRequest
;
60 struct RenderingStats
;
61 struct ScrollAndScaleSet
;
63 // Provides information on an Impl's rendering capabilities back to the
65 struct CC_EXPORT RendererCapabilities
{
66 RendererCapabilities(ResourceFormat best_texture_format
,
67 bool allow_partial_texture_updates
,
69 bool using_shared_memory_resources
);
71 RendererCapabilities();
72 ~RendererCapabilities();
74 // Duplicate any modification to this list to RendererCapabilitiesImpl.
75 ResourceFormat best_texture_format
;
76 bool allow_partial_texture_updates
;
78 bool using_shared_memory_resources
;
81 class CC_EXPORT LayerTreeHost
{
83 // The SharedBitmapManager will be used on the compositor thread.
84 static scoped_ptr
<LayerTreeHost
> CreateThreaded(
85 LayerTreeHostClient
* client
,
86 SharedBitmapManager
* manager
,
87 const LayerTreeSettings
& settings
,
88 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
90 static scoped_ptr
<LayerTreeHost
> CreateSingleThreaded(
91 LayerTreeHostClient
* client
,
92 LayerTreeHostSingleThreadClient
* single_thread_client
,
93 SharedBitmapManager
* manager
,
94 const LayerTreeSettings
& settings
);
95 virtual ~LayerTreeHost();
97 void SetLayerTreeHostClientReady();
99 // LayerTreeHost interface to Proxy.
100 void WillBeginMainFrame() {
101 client_
->WillBeginMainFrame(source_frame_number_
);
103 void DidBeginMainFrame();
104 void UpdateClientAnimations(base::TimeTicks monotonic_frame_begin_time
);
105 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time
);
106 void DidStopFlinging();
108 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
109 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
111 void CommitComplete();
112 scoped_ptr
<OutputSurface
> CreateOutputSurface();
113 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
114 LayerTreeHostImplClient
* client
);
115 void DidLoseOutputSurface();
116 bool output_surface_lost() const { return output_surface_lost_
; }
117 virtual void OnCreateAndInitializeOutputSurfaceAttempted(bool success
);
118 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
119 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
120 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
121 bool UpdateLayers(ResourceUpdateQueue
* queue
);
123 LayerTreeHostClient
* client() { return client_
; }
124 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
125 return input_handler_weak_ptr_
;
128 void NotifyInputThrottledUntilCommit();
130 void Composite(base::TimeTicks frame_begin_time
);
132 void FinishAllRendering();
134 void SetDeferCommits(bool defer_commits
);
137 virtual void DidDeferCommit();
139 int source_frame_number() const { return source_frame_number_
; }
141 void SetNeedsDisplayOnAllLayers();
143 void CollectRenderingStats(RenderingStats
* stats
) const;
145 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
146 return rendering_stats_instrumentation_
.get();
149 const RendererCapabilities
& GetRendererCapabilities() const;
151 void SetNeedsAnimate();
152 virtual void SetNeedsUpdateLayers();
153 virtual void SetNeedsCommit();
154 virtual void SetNeedsFullTreeSync();
155 void SetNeedsRedraw();
156 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
157 bool CommitRequested() const;
158 bool BeginMainFrameRequested() const;
160 void SetNextCommitWaitsForActivation();
162 void SetNextCommitForcesRedraw();
164 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
166 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
167 Layer
* root_layer() { return root_layer_
.get(); }
168 const Layer
* root_layer() const { return root_layer_
.get(); }
169 const Layer
* page_scale_layer() const { return page_scale_layer_
.get(); }
170 void RegisterViewportLayers(
171 scoped_refptr
<Layer
> page_scale_layer
,
172 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
173 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
174 Layer
* inner_viewport_scroll_layer() const {
175 return inner_viewport_scroll_layer_
.get();
177 Layer
* outer_viewport_scroll_layer() const {
178 return outer_viewport_scroll_layer_
.get();
181 const LayerTreeSettings
& settings() const { return settings_
; }
183 void SetDebugState(const LayerTreeDebugState
& debug_state
);
184 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
186 bool has_gpu_rasterization_trigger() const {
187 return has_gpu_rasterization_trigger_
;
189 void set_has_gpu_rasterization_trigger(bool has_trigger
) {
190 has_gpu_rasterization_trigger_
= has_trigger
;
192 bool UseGpuRasterization() const;
194 void SetViewportSize(const gfx::Size
& device_viewport_size
);
195 void SetOverdrawBottomHeight(float overdraw_bottom_height
);
197 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
198 float overdraw_bottom_height() const { return overdraw_bottom_height_
; }
200 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
201 void SetPageScaleFactorAndLimits(float page_scale_factor
,
202 float min_page_scale_factor
,
203 float max_page_scale_factor
);
204 float page_scale_factor() const { return page_scale_factor_
; }
206 SkColor
background_color() const { return background_color_
; }
207 void set_background_color(SkColor color
) { background_color_
= color
; }
209 void set_has_transparent_background(bool transparent
) {
210 has_transparent_background_
= transparent
;
213 void SetOverhangBitmap(const SkBitmap
& bitmap
);
215 PrioritizedResourceManager
* contents_texture_manager() const {
216 return contents_texture_manager_
.get();
219 void SetVisible(bool visible
);
220 bool visible() const { return visible_
; }
222 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
225 base::TimeDelta duration
);
227 void ApplyScrollAndScale(const ScrollAndScaleSet
& info
);
228 void SetImplTransform(const gfx::Transform
& transform
);
230 // Virtual for tests.
231 virtual void StartRateLimiter();
232 virtual void StopRateLimiter();
236 bool AlwaysUsePartialTextureUpdates();
237 size_t MaxPartialTextureUpdates() const;
238 bool RequestPartialTextureUpdate();
240 void SetDeviceScaleFactor(float device_scale_factor
);
241 float device_scale_factor() const { return device_scale_factor_
; }
243 void UpdateTopControlsState(TopControlsState constraints
,
244 TopControlsState current
,
247 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
249 Proxy
* proxy() const { return proxy_
.get(); }
251 AnimationRegistrar
* animation_registrar() const {
252 return animation_registrar_
.get();
255 // Obtains a thorough dump of the LayerTreeHost as a value.
256 scoped_ptr
<base::Value
> AsValue() const;
258 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
260 // CreateUIResource creates a resource given a bitmap. The bitmap is
261 // generated via an interface function, which is called when initializing the
262 // resource and when the resource has been lost (due to lost context). The
263 // parameter of the interface is a single boolean, which indicates whether the
264 // resource has been lost or not. CreateUIResource returns an Id of the
265 // resource, which is always positive.
266 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
267 // Deletes a UI resource. May safely be called more than once.
268 virtual void DeleteUIResource(UIResourceId id
);
269 // Put the recreation of all UI resources into the resource queue after they
270 // were evicted on the impl thread.
271 void RecreateUIResources();
273 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
275 bool UsingSharedMemoryResources();
276 int id() const { return id_
; }
278 // Returns the id of the benchmark on success, 0 otherwise.
279 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
280 scoped_ptr
<base::Value
> value
,
281 const MicroBenchmark::DoneCallback
& callback
);
282 // Returns true if the message was successfully delivered and handled.
283 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
285 // When a SwapPromiseMonitor is created on the main thread, it calls
286 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
287 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
288 // to unregister itself.
289 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
290 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
292 // Call this function when you expect there to be a swap buffer.
293 // See swap_promise.h for how to use SwapPromise.
294 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
296 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
299 LayerTreeHost(LayerTreeHostClient
* client
,
300 SharedBitmapManager
* manager
,
301 const LayerTreeSettings
& settings
);
302 void InitializeThreaded(
303 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
304 void InitializeSingleThreaded(
305 LayerTreeHostSingleThreadClient
* single_thread_client
);
306 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
307 void SetOutputSurfaceLostForTesting(bool is_lost
) {
308 output_surface_lost_
= is_lost
;
311 MicroBenchmarkController micro_benchmark_controller_
;
314 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
316 void PaintLayerContents(
317 const RenderSurfaceLayerList
& render_surface_layer_list
,
318 ResourceUpdateQueue
* queue
,
319 bool* did_paint_content
,
320 bool* need_more_updates
);
321 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
322 ResourceUpdateQueue
* queue
,
323 bool* did_paint_content
,
324 bool* need_more_updates
);
325 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
326 void UpdateHudLayer();
327 void TriggerPrepaint();
329 void ReduceMemoryUsage();
331 void PrioritizeTextures(
332 const RenderSurfaceLayerList
& render_surface_layer_list
);
333 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
334 void SetPrioritiesForLayers(const RenderSurfaceLayerList
& update_list
);
335 size_t CalculateMemoryForRenderSurfaces(
336 const RenderSurfaceLayerList
& update_list
);
338 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
340 struct UIResourceClientData
{
341 UIResourceClient
* client
;
345 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
347 UIResourceClientMap ui_resource_client_map_
;
348 int next_ui_resource_id_
;
350 typedef std::list
<UIResourceRequest
> UIResourceRequestQueue
;
351 UIResourceRequestQueue ui_resource_request_queue_
;
353 void RecordGpuRasterizationHistogram();
354 void CalculateLCDTextMetricsCallback(Layer
* layer
);
356 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
359 bool needs_full_tree_sync_
;
361 base::CancelableClosure prepaint_callback_
;
363 LayerTreeHostClient
* client_
;
364 scoped_ptr
<Proxy
> proxy_
;
366 int source_frame_number_
;
367 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
369 bool output_surface_lost_
;
370 int num_failed_recreate_attempts_
;
372 scoped_refptr
<Layer
> root_layer_
;
373 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
375 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
376 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
378 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
379 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
381 const LayerTreeSettings settings_
;
382 LayerTreeDebugState debug_state_
;
384 gfx::Size device_viewport_size_
;
385 float overdraw_bottom_height_
;
386 float device_scale_factor_
;
390 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
392 float page_scale_factor_
;
393 float min_page_scale_factor_
;
394 float max_page_scale_factor_
;
395 gfx::Transform impl_transform_
;
396 bool trigger_idle_updates_
;
397 bool has_gpu_rasterization_trigger_
;
398 bool content_is_suitable_for_gpu_rasterization_
;
399 bool gpu_rasterization_histogram_recorded_
;
401 SkColor background_color_
;
402 bool has_transparent_background_
;
404 // If set, this texture is used to fill in the parts of the screen not
405 // covered by layers.
406 scoped_ptr
<ScopedUIResource
> overhang_ui_resource_
;
408 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
409 size_t partial_texture_update_requests_
;
411 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
413 struct PendingPageScaleAnimation
{
414 gfx::Vector2d target_offset
;
417 base::TimeDelta duration
;
419 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
421 bool in_paint_layer_contents_
;
423 static const int kTotalFramesToUseForLCDTextMetrics
= 50;
424 int total_frames_used_for_lcd_text_metrics_
;
426 struct LCDTextMetrics
{
428 : total_num_cc_layers(0),
429 total_num_cc_layers_can_use_lcd_text(0),
430 total_num_cc_layers_will_use_lcd_text(0) {}
432 int64 total_num_cc_layers
;
433 int64 total_num_cc_layers_can_use_lcd_text
;
434 int64 total_num_cc_layers_will_use_lcd_text
;
436 LCDTextMetrics lcd_text_metrics_
;
438 bool next_commit_forces_redraw_
;
440 scoped_refptr
<Layer
> page_scale_layer_
;
441 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
442 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
444 SharedBitmapManager
* shared_bitmap_manager_
;
446 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
447 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
449 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
454 #endif // CC_TREES_LAYER_TREE_HOST_H_