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/rect.h"
46 class AnimationRegistrar
;
47 class HeadsUpDisplayLayer
;
49 class LayerTreeHostImpl
;
50 class LayerTreeHostImplClient
;
51 class LayerTreeHostSingleThreadClient
;
52 class PrioritizedResource
;
53 class PrioritizedResourceManager
;
55 class RenderingStatsInstrumentation
;
56 class ResourceProvider
;
57 class ResourceUpdateQueue
;
58 class SharedBitmapManager
;
59 class TopControlsManager
;
60 class UIResourceRequest
;
61 struct RenderingStats
;
62 struct ScrollAndScaleSet
;
64 // Provides information on an Impl's rendering capabilities back to the
66 struct CC_EXPORT RendererCapabilities
{
67 RendererCapabilities(ResourceFormat best_texture_format
,
68 bool allow_partial_texture_updates
,
70 bool using_shared_memory_resources
);
72 RendererCapabilities();
73 ~RendererCapabilities();
75 // Duplicate any modification to this list to RendererCapabilitiesImpl.
76 ResourceFormat best_texture_format
;
77 bool allow_partial_texture_updates
;
79 bool using_shared_memory_resources
;
82 class CC_EXPORT LayerTreeHost
{
84 // The SharedBitmapManager will be used on the compositor thread.
85 static scoped_ptr
<LayerTreeHost
> CreateThreaded(
86 LayerTreeHostClient
* client
,
87 SharedBitmapManager
* manager
,
88 const LayerTreeSettings
& settings
,
89 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
91 static scoped_ptr
<LayerTreeHost
> CreateSingleThreaded(
92 LayerTreeHostClient
* client
,
93 LayerTreeHostSingleThreadClient
* single_thread_client
,
94 SharedBitmapManager
* manager
,
95 const LayerTreeSettings
& settings
);
96 virtual ~LayerTreeHost();
98 void SetLayerTreeHostClientReady();
100 // LayerTreeHost interface to Proxy.
101 void WillBeginMainFrame() {
102 client_
->WillBeginMainFrame(source_frame_number_
);
104 void DidBeginMainFrame();
105 void UpdateClientAnimations(base::TimeTicks monotonic_frame_begin_time
);
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 scoped_ptr
<OutputSurface
> CreateOutputSurface();
114 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
115 LayerTreeHostImplClient
* client
);
116 void DidLoseOutputSurface();
117 bool output_surface_lost() const { return output_surface_lost_
; }
118 virtual void OnCreateAndInitializeOutputSurfaceAttempted(bool success
);
119 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
120 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
121 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
122 bool UpdateLayers(ResourceUpdateQueue
* queue
);
124 LayerTreeHostClient
* client() { return client_
; }
125 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
126 return input_handler_weak_ptr_
;
129 void NotifyInputThrottledUntilCommit();
131 void Composite(base::TimeTicks frame_begin_time
);
133 void FinishAllRendering();
135 void SetDeferCommits(bool defer_commits
);
138 virtual void DidDeferCommit();
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
* page_scale_layer() const { return page_scale_layer_
.get(); }
171 void RegisterViewportLayers(
172 scoped_refptr
<Layer
> page_scale_layer
,
173 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
174 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
175 Layer
* inner_viewport_scroll_layer() const {
176 return inner_viewport_scroll_layer_
.get();
178 Layer
* outer_viewport_scroll_layer() const {
179 return outer_viewport_scroll_layer_
.get();
182 void RegisterSelection(const LayerSelectionBound
& start
,
183 const LayerSelectionBound
& end
);
185 const LayerTreeSettings
& settings() const { return settings_
; }
187 void SetDebugState(const LayerTreeDebugState
& debug_state
);
188 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
190 bool has_gpu_rasterization_trigger() const {
191 return has_gpu_rasterization_trigger_
;
193 void SetHasGpuRasterizationTrigger(bool has_trigger
);
194 bool UseGpuRasterization() const;
196 void SetViewportSize(const gfx::Size
& device_viewport_size
);
197 void SetOverdrawBottomHeight(float overdraw_bottom_height
);
199 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
200 float overdraw_bottom_height() const { return overdraw_bottom_height_
; }
202 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
203 void SetPageScaleFactorAndLimits(float page_scale_factor
,
204 float min_page_scale_factor
,
205 float max_page_scale_factor
);
206 float page_scale_factor() const { return page_scale_factor_
; }
208 SkColor
background_color() const { return background_color_
; }
209 void set_background_color(SkColor color
) { background_color_
= color
; }
211 void set_has_transparent_background(bool transparent
) {
212 has_transparent_background_
= transparent
;
215 void SetOverhangBitmap(const SkBitmap
& bitmap
);
217 PrioritizedResourceManager
* contents_texture_manager() const {
218 return contents_texture_manager_
.get();
221 void SetVisible(bool visible
);
222 bool visible() const { return visible_
; }
224 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
227 base::TimeDelta duration
);
229 void ApplyScrollAndScale(const ScrollAndScaleSet
& info
);
230 void SetImplTransform(const gfx::Transform
& transform
);
232 // Virtual for tests.
233 virtual void StartRateLimiter();
234 virtual void StopRateLimiter();
238 bool AlwaysUsePartialTextureUpdates();
239 size_t MaxPartialTextureUpdates() const;
240 bool RequestPartialTextureUpdate();
242 void SetDeviceScaleFactor(float device_scale_factor
);
243 float device_scale_factor() const { return device_scale_factor_
; }
245 void UpdateTopControlsState(TopControlsState constraints
,
246 TopControlsState current
,
249 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
251 Proxy
* proxy() const { return proxy_
.get(); }
253 AnimationRegistrar
* animation_registrar() const {
254 return animation_registrar_
.get();
257 // Obtains a thorough dump of the LayerTreeHost as a value.
258 scoped_ptr
<base::Value
> AsValue() const;
260 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
262 // CreateUIResource creates a resource given a bitmap. The bitmap is
263 // generated via an interface function, which is called when initializing the
264 // resource and when the resource has been lost (due to lost context). The
265 // parameter of the interface is a single boolean, which indicates whether the
266 // resource has been lost or not. CreateUIResource returns an Id of the
267 // resource, which is always positive.
268 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
269 // Deletes a UI resource. May safely be called more than once.
270 virtual void DeleteUIResource(UIResourceId id
);
271 // Put the recreation of all UI resources into the resource queue after they
272 // were evicted on the impl thread.
273 void RecreateUIResources();
275 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
277 bool UsingSharedMemoryResources();
278 int id() const { return id_
; }
280 // Returns the id of the benchmark on success, 0 otherwise.
281 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
282 scoped_ptr
<base::Value
> value
,
283 const MicroBenchmark::DoneCallback
& callback
);
284 // Returns true if the message was successfully delivered and handled.
285 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
287 // When a SwapPromiseMonitor is created on the main thread, it calls
288 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
289 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
290 // to unregister itself.
291 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
292 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
294 // Call this function when you expect there to be a swap buffer.
295 // See swap_promise.h for how to use SwapPromise.
296 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
298 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
301 LayerTreeHost(LayerTreeHostClient
* client
,
302 SharedBitmapManager
* manager
,
303 const LayerTreeSettings
& settings
);
304 void InitializeThreaded(
305 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
306 void InitializeSingleThreaded(
307 LayerTreeHostSingleThreadClient
* single_thread_client
);
308 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
309 void SetOutputSurfaceLostForTesting(bool is_lost
) {
310 output_surface_lost_
= is_lost
;
313 MicroBenchmarkController micro_benchmark_controller_
;
316 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
318 void PaintLayerContents(
319 const RenderSurfaceLayerList
& render_surface_layer_list
,
320 ResourceUpdateQueue
* queue
,
321 bool* did_paint_content
,
322 bool* need_more_updates
);
323 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
324 ResourceUpdateQueue
* queue
,
325 bool* did_paint_content
,
326 bool* need_more_updates
);
327 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
328 void UpdateHudLayer();
329 void TriggerPrepaint();
331 void ReduceMemoryUsage();
333 void PrioritizeTextures(
334 const RenderSurfaceLayerList
& render_surface_layer_list
);
335 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
336 void SetPrioritiesForLayers(const RenderSurfaceLayerList
& update_list
);
337 size_t CalculateMemoryForRenderSurfaces(
338 const RenderSurfaceLayerList
& update_list
);
340 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
342 struct UIResourceClientData
{
343 UIResourceClient
* client
;
347 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
349 UIResourceClientMap ui_resource_client_map_
;
350 int next_ui_resource_id_
;
352 typedef std::list
<UIResourceRequest
> UIResourceRequestQueue
;
353 UIResourceRequestQueue ui_resource_request_queue_
;
355 void RecordGpuRasterizationHistogram();
356 void CalculateLCDTextMetricsCallback(Layer
* layer
);
358 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
361 bool needs_full_tree_sync_
;
363 base::CancelableClosure prepaint_callback_
;
365 LayerTreeHostClient
* client_
;
366 scoped_ptr
<Proxy
> proxy_
;
368 int source_frame_number_
;
369 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
371 bool output_surface_lost_
;
372 int num_failed_recreate_attempts_
;
374 scoped_refptr
<Layer
> root_layer_
;
375 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
377 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
378 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
380 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
381 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
383 const LayerTreeSettings settings_
;
384 LayerTreeDebugState debug_state_
;
386 gfx::Size device_viewport_size_
;
387 float overdraw_bottom_height_
;
388 float device_scale_factor_
;
392 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
394 float page_scale_factor_
;
395 float min_page_scale_factor_
;
396 float max_page_scale_factor_
;
397 gfx::Transform impl_transform_
;
398 bool trigger_idle_updates_
;
399 bool has_gpu_rasterization_trigger_
;
400 bool content_is_suitable_for_gpu_rasterization_
;
401 bool gpu_rasterization_histogram_recorded_
;
403 SkColor background_color_
;
404 bool has_transparent_background_
;
406 // If set, this texture is used to fill in the parts of the screen not
407 // covered by layers.
408 scoped_ptr
<ScopedUIResource
> overhang_ui_resource_
;
410 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
411 size_t partial_texture_update_requests_
;
413 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
415 struct PendingPageScaleAnimation
{
416 gfx::Vector2d target_offset
;
419 base::TimeDelta duration
;
421 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
423 bool in_paint_layer_contents_
;
425 static const int kTotalFramesToUseForLCDTextMetrics
= 50;
426 int total_frames_used_for_lcd_text_metrics_
;
428 struct LCDTextMetrics
{
430 : total_num_cc_layers(0),
431 total_num_cc_layers_can_use_lcd_text(0),
432 total_num_cc_layers_will_use_lcd_text(0) {}
434 int64 total_num_cc_layers
;
435 int64 total_num_cc_layers_can_use_lcd_text
;
436 int64 total_num_cc_layers_will_use_lcd_text
;
438 LCDTextMetrics lcd_text_metrics_
;
440 bool next_commit_forces_redraw_
;
442 scoped_refptr
<Layer
> page_scale_layer_
;
443 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
444 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
446 LayerSelectionBound selection_start_
;
447 LayerSelectionBound selection_end_
;
449 SharedBitmapManager
* shared_bitmap_manager_
;
451 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
452 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
454 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
459 #endif // CC_TREES_LAYER_TREE_HOST_H_