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/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.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/debug/latency_info.h"
22 #include "cc/layers/layer_lists.h"
23 #include "cc/output/output_surface.h"
24 #include "cc/scheduler/rate_limiter.h"
25 #include "cc/trees/layer_tree_host_client.h"
26 #include "cc/trees/layer_tree_host_common.h"
27 #include "cc/trees/layer_tree_settings.h"
28 #include "cc/trees/occlusion_tracker.h"
29 #include "cc/trees/proxy.h"
30 #include "skia/ext/refptr.h"
31 #include "third_party/WebKit/Source/Platform/chromium/public/WebScrollbar.h"
32 #include "third_party/skia/include/core/SkColor.h"
33 #include "third_party/skia/include/core/SkPicture.h"
34 #include "ui/gfx/rect.h"
36 #if defined(COMPILER_GCC)
37 namespace BASE_HASH_NAMESPACE
{
39 struct hash
<WebKit::WebGraphicsContext3D
*> {
40 size_t operator()(WebKit::WebGraphicsContext3D
* ptr
) const {
41 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
44 } // namespace BASE_HASH_NAMESPACE
49 class AnimationRegistrar
;
50 class HeadsUpDisplayLayer
;
52 class LayerTreeHostImpl
;
53 class LayerTreeHostImplClient
;
54 class PrioritizedResourceManager
;
55 class PrioritizedResource
;
57 class RenderingStatsInstrumentation
;
58 class ResourceProvider
;
59 class ResourceUpdateQueue
;
61 class TopControlsManager
;
62 struct RenderingStats
;
63 struct ScrollAndScaleSet
;
65 // Provides information on an Impl's rendering capabilities back to the
67 struct CC_EXPORT RendererCapabilities
{
68 RendererCapabilities();
69 ~RendererCapabilities();
71 unsigned best_texture_format
;
72 bool using_partial_swap
;
73 bool using_accelerated_painting
;
74 bool using_set_visibility
;
75 bool using_swap_complete_callback
;
76 bool using_gpu_memory_manager
;
78 bool allow_partial_texture_updates
;
79 bool using_offscreen_context3d
;
81 bool avoid_pow2_textures
;
84 class CC_EXPORT LayerTreeHost
: NON_EXPORTED_BASE(public RateLimiterClient
) {
86 static scoped_ptr
<LayerTreeHost
> Create(LayerTreeHostClient
* client
,
87 const LayerTreeSettings
& settings
,
88 scoped_ptr
<Thread
> impl_thread
);
89 virtual ~LayerTreeHost();
91 // TODO(boliu): Rename to SetLayerTreeHostClientReady.
92 void SetSurfaceReady();
94 // Returns true if any LayerTreeHost is alive.
95 static bool AnyLayerTreeHostInstanceExists();
97 void set_needs_filter_context() { needs_filter_context_
= true; }
98 bool needs_offscreen_context() const {
99 return needs_filter_context_
|| settings_
.accelerate_painting
;
102 // LayerTreeHost interface to Proxy.
103 void WillBeginFrame() { client_
->WillBeginFrame(); }
104 void DidBeginFrame();
105 void UpdateAnimations(base::TimeTicks monotonic_frame_begin_time
);
106 void DidStopFlinging();
108 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
109 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
110 gfx::Size
PinchZoomScrollbarSize(
111 WebKit::WebScrollbar::Orientation orientation
) const;
112 void SetPinchZoomScrollbarsBoundsAndPosition();
113 void CreateAndAddPinchZoomScrollbars();
115 void CommitComplete();
116 scoped_ptr
<OutputSurface
> CreateOutputSurface();
117 scoped_ptr
<InputHandlerClient
> CreateInputHandlerClient();
118 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
119 LayerTreeHostImplClient
* client
);
120 void DidLoseOutputSurface();
121 bool output_surface_lost() const { return output_surface_lost_
; }
124 CreateFailedButTryAgain
,
125 CreateFailedAndGaveUp
,
127 CreateResult
OnCreateAndInitializeOutputSurfaceAttempted(bool success
);
128 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
129 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
130 void DeleteContentsTexturesOnImplThread(ResourceProvider
* resource_provider
);
131 virtual void AcquireLayerTextures();
132 // Returns false if we should abort this frame due to initialization failure.
133 bool InitializeOutputSurfaceIfNeeded();
134 void UpdateLayers(ResourceUpdateQueue
* queue
,
135 size_t contents_memory_limit_bytes
);
137 LayerTreeHostClient
* client() { return client_
; }
139 void Composite(base::TimeTicks frame_begin_time
);
141 // Only used when compositing on the main thread.
142 void ScheduleComposite();
144 // Composites and attempts to read back the result into the provided
145 // buffer. If it wasn't possible, e.g. due to context lost, will return
147 bool CompositeAndReadback(void* pixels
, gfx::Rect rect_in_device_viewport
);
149 void FinishAllRendering();
151 void SetDeferCommits(bool defer_commits
);
154 virtual void DidDeferCommit();
156 int commit_number() const { return commit_number_
; }
158 void SetNeedsDisplayOnAllLayers();
160 void CollectRenderingStats(RenderingStats
* stats
) const;
162 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
163 return rendering_stats_instrumentation_
.get();
166 const RendererCapabilities
& GetRendererCapabilities() const;
168 void SetNeedsAnimate();
169 virtual void SetNeedsCommit();
170 virtual void SetNeedsFullTreeSync();
171 void SetNeedsRedraw();
172 void SetNeedsRedrawRect(gfx::Rect damage_rect
);
173 bool CommitRequested() const;
175 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
,
176 base::Time wall_clock_time
);
178 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
179 Layer
* root_layer() { return root_layer_
.get(); }
180 const Layer
* root_layer() const { return root_layer_
.get(); }
181 const Layer
* RootScrollLayer() const;
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
);
198 void set_background_color(SkColor color
) { background_color_
= color
; }
200 void set_has_transparent_background(bool transparent
) {
201 has_transparent_background_
= transparent
;
204 PrioritizedResourceManager
* contents_texture_manager() const {
205 return contents_texture_manager_
.get();
208 void SetVisible(bool visible
);
209 bool visible() const { return visible_
; }
211 void StartPageScaleAnimation(gfx::Vector2d target_offset
,
214 base::TimeDelta duration
);
216 void ApplyScrollAndScale(const ScrollAndScaleSet
& info
);
218 void SetImplTransform(const gfx::Transform
& transform
);
219 void SetLatencyInfo(const LatencyInfo
& latency_info
);
221 void StartRateLimiter(WebKit::WebGraphicsContext3D
* context3d
);
222 void StopRateLimiter(WebKit::WebGraphicsContext3D
* context3d
);
224 // RateLimiterClient implementation.
225 virtual void RateLimit() OVERRIDE
;
227 bool buffered_updates() const {
228 return settings_
.max_partial_texture_updates
!=
229 std::numeric_limits
<size_t>::max();
231 bool RequestPartialTextureUpdate();
233 void SetDeviceScaleFactor(float device_scale_factor
);
234 float device_scale_factor() const { return device_scale_factor_
; }
236 void UpdateTopControlsState(bool enable_hiding
,
240 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
242 Proxy
* proxy() const { return proxy_
.get(); }
244 AnimationRegistrar
* animation_registrar() const {
245 return animation_registrar_
.get();
248 skia::RefPtr
<SkPicture
> CapturePicture();
250 bool BlocksPendingCommit() const;
252 // Obtains a thorough dump of the LayerTreeHost as a value.
253 scoped_ptr
<base::Value
> AsValue() const;
255 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
258 LayerTreeHost(LayerTreeHostClient
* client
, const LayerTreeSettings
& settings
);
259 bool Initialize(scoped_ptr
<Thread
> impl_thread
);
260 bool InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
263 bool InitializeProxy(scoped_ptr
<Proxy
> proxy
);
265 bool PaintLayerContents(const LayerList
& render_surface_layer_list
,
266 ResourceUpdateQueue
* quue
);
267 bool PaintMasksForRenderSurface(Layer
* render_surface_layer
,
268 ResourceUpdateQueue
* queue
,
269 RenderingStats
* stats
);
271 void UpdateLayers(Layer
* root_layer
, ResourceUpdateQueue
* queue
);
272 void UpdateHudLayer();
273 void TriggerPrepaint();
275 void PrioritizeTextures(const LayerList
& render_surface_layer_list
,
276 OverdrawMetrics
* metrics
);
277 void SetPrioritiesForSurfaces(size_t surface_memory_bytes
);
278 void SetPrioritiesForLayers(const LayerList
& update_list
);
279 size_t CalculateMemoryForRenderSurfaces(const LayerList
& update_list
);
281 void AnimateLayers(base::TimeTicks monotonic_time
);
282 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
285 bool needs_full_tree_sync_
;
286 bool needs_filter_context_
;
288 base::CancelableClosure prepaint_callback_
;
290 LayerTreeHostClient
* client_
;
291 scoped_ptr
<Proxy
> proxy_
;
294 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
296 bool output_surface_can_be_initialized_
;
297 bool output_surface_lost_
;
298 int num_failed_recreate_attempts_
;
300 scoped_refptr
<Layer
> root_layer_
;
301 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
302 scoped_refptr
<ScrollbarLayer
> pinch_zoom_scrollbar_horizontal_
;
303 scoped_refptr
<ScrollbarLayer
> pinch_zoom_scrollbar_vertical_
;
305 scoped_ptr
<PrioritizedResourceManager
> contents_texture_manager_
;
306 scoped_ptr
<PrioritizedResource
> surface_memory_placeholder_
;
308 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
310 LayerTreeSettings settings_
;
311 LayerTreeDebugState debug_state_
;
313 gfx::Size device_viewport_size_
;
314 float overdraw_bottom_height_
;
315 float device_scale_factor_
;
319 typedef base::hash_map
<WebKit::WebGraphicsContext3D
*,
320 scoped_refptr
<RateLimiter
> > RateLimiterMap
;
321 RateLimiterMap rate_limiters_
;
323 float page_scale_factor_
;
324 float min_page_scale_factor_
;
325 float max_page_scale_factor_
;
326 gfx::Transform impl_transform_
;
327 bool trigger_idle_updates_
;
329 SkColor background_color_
;
330 bool has_transparent_background_
;
332 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
333 size_t partial_texture_update_requests_
;
335 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
337 struct PendingPageScaleAnimation
{
338 gfx::Vector2d target_offset
;
341 base::TimeDelta duration
;
343 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
345 bool in_paint_layer_contents_
;
347 LatencyInfo latency_info_
;
349 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
354 #endif // CC_TREES_LAYER_TREE_HOST_H_