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_
11 #include "base/basictypes.h"
12 #include "base/cancelable_callback.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "cc/animation/animation_events.h"
19 #include "cc/base/cc_export.h"
20 #include "cc/base/scoped_ptr_vector.h"
21 #include "cc/input/input_handler.h"
22 #include "cc/input/scrollbar.h"
23 #include "cc/input/top_controls_state.h"
24 #include "cc/layers/layer_lists.h"
25 #include "cc/output/output_surface.h"
26 #include "cc/scheduler/rate_limiter.h"
27 #include "cc/trees/layer_tree_host_client.h"
28 #include "cc/trees/layer_tree_host_common.h"
29 #include "cc/trees/layer_tree_settings.h"
30 #include "cc/trees/occlusion_tracker.h"
31 #include "cc/trees/proxy.h"
32 #include "skia/ext/refptr.h"
33 #include "third_party/skia/include/core/SkColor.h"
34 #include "third_party/skia/include/core/SkPicture.h"
35 #include "ui/base/latency_info.h"
36 #include "ui/gfx/rect.h"
38 namespace WebKit
{ class WebGraphicsContext3D
; }
40 #if defined(COMPILER_GCC)
41 namespace BASE_HASH_NAMESPACE
{
43 struct hash
<WebKit::WebGraphicsContext3D
*> {
44 size_t operator()(WebKit::WebGraphicsContext3D
* ptr
) const {
45 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
48 } // namespace BASE_HASH_NAMESPACE
53 class AnimationRegistrar
;
54 class HeadsUpDisplayLayer
;
56 class LayerTreeHostImpl
;
57 class LayerTreeHostImplClient
;
58 class PrioritizedResourceManager
;
59 class PrioritizedResource
;
61 class RenderingStatsInstrumentation
;
62 class ResourceProvider
;
63 class ResourceUpdateQueue
;
64 class TopControlsManager
;
65 struct RenderingStats
;
66 struct ScrollAndScaleSet
;
68 // Provides information on an Impl's rendering capabilities back to the
70 struct CC_EXPORT RendererCapabilities
{
71 RendererCapabilities();
72 ~RendererCapabilities();
74 unsigned best_texture_format
;
75 bool using_partial_swap
;
76 bool using_set_visibility
;
78 bool allow_partial_texture_updates
;
79 bool using_offscreen_context3d
;
81 bool avoid_pow2_textures
;
83 bool using_shared_memory_resources
;
86 class CC_EXPORT LayerTreeHost
: NON_EXPORTED_BASE(public RateLimiterClient
) {
88 static scoped_ptr
<LayerTreeHost
> Create(
89 LayerTreeHostClient
* client
,
90 const LayerTreeSettings
& settings
,
91 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
92 virtual ~LayerTreeHost();
94 void SetLayerTreeHostClientReady();
96 // Returns true if any LayerTreeHost is alive.
97 static bool AnyLayerTreeHostInstanceExists();
99 void set_needs_filter_context() { needs_filter_context_
= true; }
100 bool needs_offscreen_context() const {
101 return needs_filter_context_
;
104 // LayerTreeHost interface to Proxy.
105 void WillBeginFrame() { client_
->WillBeginFrame(); }
106 void DidBeginFrame();
107 void UpdateClientAnimations(base::TimeTicks monotonic_frame_begin_time
);
108 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time
);
109 void DidStopFlinging();
111 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
112 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
114 void CommitComplete();
115 scoped_ptr
<OutputSurface
> CreateOutputSurface();
116 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
117 LayerTreeHostImplClient
* client
);
118 void DidLoseOutputSurface();
119 bool output_surface_lost() const { return output_surface_lost_
; }
122 CreateFailedButTryAgain
,
123 CreateFailedAndGaveUp
,
125 CreateResult
OnCreateAndInitializeOutputSurfaceAttempted(bool success
);
126 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
127 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
128 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
129 virtual void AcquireLayerTextures();
130 // Returns false if we should abort this frame due to initialization failure.
131 bool InitializeOutputSurfaceIfNeeded();
132 bool UpdateLayers(ResourceUpdateQueue
* queue
,
133 size_t contents_memory_limit_bytes
);
135 LayerTreeHostClient
* client() { return client_
; }
136 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
137 return input_handler_weak_ptr_
;
140 void Composite(base::TimeTicks frame_begin_time
);
142 // Only used when compositing on the main thread.
143 void ScheduleComposite();
145 // Composites and attempts to read back the result into the provided
146 // buffer. If it wasn't possible, e.g. due to context lost, will return
148 bool CompositeAndReadback(void* pixels
, gfx::Rect rect_in_device_viewport
);
150 void FinishAllRendering();
152 void SetDeferCommits(bool defer_commits
);
155 virtual void DidDeferCommit();
157 int commit_number() const { return commit_number_
; }
159 void SetNeedsDisplayOnAllLayers();
161 void CollectRenderingStats(RenderingStats
* stats
) const;
163 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
164 return rendering_stats_instrumentation_
.get();
167 const RendererCapabilities
& GetRendererCapabilities() const;
169 void SetNeedsAnimate();
170 virtual void SetNeedsCommit();
171 virtual void SetNeedsFullTreeSync();
172 void SetNeedsRedraw();
173 void SetNeedsRedrawRect(gfx::Rect damage_rect
);
174 bool CommitRequested() const;
176 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
,
177 base::Time wall_clock_time
);
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(); }
183 const LayerTreeSettings
& settings() const { return settings_
; }
185 void SetDebugState(const LayerTreeDebugState
& debug_state
);
186 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
188 void SetViewportSize(gfx::Size device_viewport_size
);
189 void SetOverdrawBottomHeight(float overdraw_bottom_height
);
191 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
192 float overdraw_bottom_height() const { return overdraw_bottom_height_
; }
194 void SetPageScaleFactorAndLimits(float page_scale_factor
,
195 float min_page_scale_factor
,
196 float max_page_scale_factor
);
197 float page_scale_factor() const { return page_scale_factor_
; }
199 SkColor
background_color() const { return background_color_
; }
200 void set_background_color(SkColor color
) { background_color_
= color
; }
202 void set_has_transparent_background(bool transparent
) {
203 has_transparent_background_
= transparent
;
206 PrioritizedResourceManager
* contents_texture_manager() const {
207 return contents_texture_manager_
.get();
210 void SetVisible(bool visible
);
211 bool visible() const { return visible_
; }
213 void StartPageScaleAnimation(gfx::Vector2d target_offset
,
216 base::TimeDelta duration
);
218 void ApplyScrollAndScale(const ScrollAndScaleSet
& info
);
220 void SetImplTransform(const gfx::Transform
& transform
);
221 void SetLatencyInfo(const ui::LatencyInfo
& latency_info
);
223 virtual void StartRateLimiter(WebKit::WebGraphicsContext3D
* context3d
);
224 virtual void StopRateLimiter(WebKit::WebGraphicsContext3D
* context3d
);
226 // RateLimiterClient implementation.
227 virtual void RateLimit() OVERRIDE
;
229 bool buffered_updates() const {
230 return settings_
.max_partial_texture_updates
!=
231 std::numeric_limits
<size_t>::max();
233 bool RequestPartialTextureUpdate();
235 void SetDeviceScaleFactor(float device_scale_factor
);
236 float device_scale_factor() const { return device_scale_factor_
; }
238 void UpdateTopControlsState(TopControlsState constraints
,
239 TopControlsState current
,
242 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
244 Proxy
* proxy() const { return proxy_
.get(); }
246 AnimationRegistrar
* animation_registrar() const {
247 return animation_registrar_
.get();
250 skia::RefPtr
<SkPicture
> CapturePicture();
252 bool BlocksPendingCommit() const;
254 // Obtains a thorough dump of the LayerTreeHost as a value.
255 scoped_ptr
<base::Value
> AsValue() const;
257 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
259 bool UsingSharedMemoryResources();
262 LayerTreeHost(LayerTreeHostClient
* client
, const LayerTreeSettings
& settings
);
263 bool Initialize(scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
264 bool InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
267 bool InitializeProxy(scoped_ptr
<Proxy
> proxy
);
269 void PaintLayerContents(const LayerList
& render_surface_layer_list
,
270 ResourceUpdateQueue
* queue
,
271 bool* did_paint_content
,
272 bool* need_more_updates
);
273 void PaintMasksForRenderSurface(Layer
* render_surface_layer
,
274 ResourceUpdateQueue
* queue
,
275 bool* did_paint_content
,
276 bool* need_more_updates
);
277 bool UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
278 void UpdateHudLayer();
279 void TriggerPrepaint();
281 void ReduceMemoryUsage();
283 void PrioritizeTextures(const LayerList
& render_surface_layer_list
,
284 OverdrawMetrics
* metrics
);
285 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
286 void SetPrioritiesForLayers(const LayerList
& update_list
);
287 size_t CalculateMemoryForRenderSurfaces(const LayerList
& update_list
);
289 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
291 void CalculateLCDTextMetricsCallback(Layer
* layer
);
294 bool needs_full_tree_sync_
;
295 bool needs_filter_context_
;
297 base::CancelableClosure prepaint_callback_
;
299 LayerTreeHostClient
* client_
;
300 scoped_ptr
<Proxy
> proxy_
;
303 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
305 bool output_surface_can_be_initialized_
;
306 bool output_surface_lost_
;
307 int num_failed_recreate_attempts_
;
309 scoped_refptr
<Layer
> root_layer_
;
310 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
312 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
313 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
315 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
316 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
318 LayerTreeSettings settings_
;
319 LayerTreeDebugState debug_state_
;
321 gfx::Size device_viewport_size_
;
322 float overdraw_bottom_height_
;
323 float device_scale_factor_
;
327 typedef base::hash_map
<WebKit::WebGraphicsContext3D
*,
328 scoped_refptr
<RateLimiter
> > RateLimiterMap
;
329 RateLimiterMap rate_limiters_
;
331 float page_scale_factor_
;
332 float min_page_scale_factor_
;
333 float max_page_scale_factor_
;
334 gfx::Transform impl_transform_
;
335 bool trigger_idle_updates_
;
337 SkColor background_color_
;
338 bool has_transparent_background_
;
340 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
341 size_t partial_texture_update_requests_
;
343 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
345 struct PendingPageScaleAnimation
{
346 gfx::Vector2d target_offset
;
349 base::TimeDelta duration
;
351 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
353 bool in_paint_layer_contents_
;
355 ui::LatencyInfo latency_info_
;
357 static const int kTotalFramesToUseForLCDTextMetrics
= 50;
358 int total_frames_used_for_lcd_text_metrics_
;
360 struct LCDTextMetrics
{
362 : total_num_cc_layers(0),
363 total_num_cc_layers_can_use_lcd_text(0),
364 total_num_cc_layers_will_use_lcd_text(0) {}
366 int64 total_num_cc_layers
;
367 int64 total_num_cc_layers_can_use_lcd_text
;
368 int64 total_num_cc_layers_will_use_lcd_text
;
370 LCDTextMetrics lcd_text_metrics_
;
372 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
377 #endif // CC_TREES_LAYER_TREE_HOST_H_