Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / cc / trees / layer_tree_host.h
blobef5cb6acdc2b1d2712515356ee9a820bef55f872
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_
8 #include <limits>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/cancelable_callback.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/time/time.h"
20 #include "base/timer/timer.h"
21 #include "cc/animation/animation_events.h"
22 #include "cc/base/cc_export.h"
23 #include "cc/base/scoped_ptr_vector.h"
24 #include "cc/debug/micro_benchmark.h"
25 #include "cc/debug/micro_benchmark_controller.h"
26 #include "cc/input/input_handler.h"
27 #include "cc/input/layer_selection_bound.h"
28 #include "cc/input/scrollbar.h"
29 #include "cc/input/top_controls_state.h"
30 #include "cc/layers/layer_lists.h"
31 #include "cc/output/output_surface.h"
32 #include "cc/output/renderer_capabilities.h"
33 #include "cc/output/swap_promise.h"
34 #include "cc/resources/resource_format.h"
35 #include "cc/resources/scoped_ui_resource.h"
36 #include "cc/surfaces/surface_sequence.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 "cc/trees/swap_promise_monitor.h"
42 #include "third_party/skia/include/core/SkColor.h"
43 #include "ui/gfx/geometry/rect.h"
45 namespace gpu {
46 class GpuMemoryBufferManager;
49 namespace cc {
50 class AnimationRegistrar;
51 class BeginFrameSource;
52 class HeadsUpDisplayLayer;
53 class Layer;
54 class LayerTreeHostImpl;
55 class LayerTreeHostImplClient;
56 class LayerTreeHostSingleThreadClient;
57 class PrioritizedResource;
58 class PrioritizedResourceManager;
59 class Region;
60 class RenderingStatsInstrumentation;
61 class ResourceProvider;
62 class ResourceUpdateQueue;
63 class SharedBitmapManager;
64 class TaskGraphRunner;
65 class TopControlsManager;
66 class UIResourceRequest;
67 struct PendingPageScaleAnimation;
68 struct RenderingStats;
69 struct ScrollAndScaleSet;
70 enum class GpuRasterizationStatus;
72 class CC_EXPORT LayerTreeHost {
73 public:
74 // The SharedBitmapManager will be used on the compositor thread.
75 static scoped_ptr<LayerTreeHost> CreateThreaded(
76 LayerTreeHostClient* client,
77 SharedBitmapManager* shared_bitmap_manager,
78 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
79 TaskGraphRunner* task_graph_runner,
80 const LayerTreeSettings& settings,
81 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
82 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
83 scoped_ptr<BeginFrameSource> external_begin_frame_source);
85 static scoped_ptr<LayerTreeHost> CreateSingleThreaded(
86 LayerTreeHostClient* client,
87 LayerTreeHostSingleThreadClient* single_thread_client,
88 SharedBitmapManager* shared_bitmap_manager,
89 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
90 TaskGraphRunner* task_graph_runner,
91 const LayerTreeSettings& settings,
92 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
93 scoped_ptr<BeginFrameSource> external_begin_frame_source);
94 virtual ~LayerTreeHost();
96 void SetLayerTreeHostClientReady();
98 // LayerTreeHost interface to Proxy.
99 void WillBeginMainFrame();
100 void DidBeginMainFrame();
101 void BeginMainFrame(const BeginFrameArgs& args);
102 void BeginMainFrameNotExpectedSoon();
103 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time);
104 void DidStopFlinging();
105 void Layout();
106 void BeginCommitOnImplThread(LayerTreeHostImpl* host_impl);
107 void FinishCommitOnImplThread(LayerTreeHostImpl* host_impl);
108 void WillCommit();
109 void CommitComplete();
110 void SetOutputSurface(scoped_ptr<OutputSurface> output_surface);
111 void RequestNewOutputSurface();
112 void DidInitializeOutputSurface();
113 void DidFailToInitializeOutputSurface();
114 virtual scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
115 LayerTreeHostImplClient* client);
116 void DidLoseOutputSurface();
117 bool output_surface_lost() const { return output_surface_lost_; }
118 void DidCommitAndDrawFrame() { client_->DidCommitAndDrawFrame(); }
119 void DidCompleteSwapBuffers() { client_->DidCompleteSwapBuffers(); }
120 void DeleteContentsTexturesOnImplThread(ResourceProvider* resource_provider);
121 bool UpdateLayers(ResourceUpdateQueue* queue);
123 // Called when the compositor completed page scale animation.
124 void DidCompletePageScaleAnimation();
126 LayerTreeHostClient* client() { return client_; }
127 const base::WeakPtr<InputHandler>& GetInputHandler() {
128 return input_handler_weak_ptr_;
131 void NotifyInputThrottledUntilCommit();
133 void Composite(base::TimeTicks frame_begin_time);
135 void FinishAllRendering();
137 void SetDeferCommits(bool defer_commits);
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* overscroll_elasticity_layer() const {
170 return overscroll_elasticity_layer_.get();
172 const Layer* page_scale_layer() const { return page_scale_layer_.get(); }
173 void RegisterViewportLayers(scoped_refptr<Layer> overscroll_elasticity_layer,
174 scoped_refptr<Layer> page_scale_layer,
175 scoped_refptr<Layer> inner_viewport_scroll_layer,
176 scoped_refptr<Layer> outer_viewport_scroll_layer);
177 Layer* inner_viewport_scroll_layer() const {
178 return inner_viewport_scroll_layer_.get();
180 Layer* outer_viewport_scroll_layer() const {
181 return outer_viewport_scroll_layer_.get();
184 void RegisterSelection(const LayerSelectionBound& start,
185 const LayerSelectionBound& end);
187 const LayerTreeSettings& settings() const { return settings_; }
189 void SetDebugState(const LayerTreeDebugState& debug_state);
190 const LayerTreeDebugState& debug_state() const { return debug_state_; }
192 bool has_gpu_rasterization_trigger() const {
193 return has_gpu_rasterization_trigger_;
195 void SetHasGpuRasterizationTrigger(bool has_trigger);
196 bool UseGpuRasterization() const;
197 GpuRasterizationStatus GetGpuRasterizationStatus() const;
199 void SetViewportSize(const gfx::Size& device_viewport_size);
200 void SetTopControlsHeight(float height, bool shrink);
201 void SetTopControlsShownRatio(float ratio);
203 gfx::Size device_viewport_size() const { return device_viewport_size_; }
205 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta);
206 void SetPageScaleFactorAndLimits(float page_scale_factor,
207 float min_page_scale_factor,
208 float max_page_scale_factor);
209 float page_scale_factor() const { return page_scale_factor_; }
210 gfx::Vector2dF elastic_overscroll() const { return elastic_overscroll_; }
212 SkColor background_color() const { return background_color_; }
213 void set_background_color(SkColor color) { background_color_ = color; }
215 void set_has_transparent_background(bool transparent) {
216 has_transparent_background_ = transparent;
219 PrioritizedResourceManager* contents_texture_manager() const {
220 return contents_texture_manager_.get();
223 void SetVisible(bool visible);
224 bool visible() const { return visible_; }
226 void SetThrottleFrameProduction(bool throttle);
228 void StartPageScaleAnimation(const gfx::Vector2d& target_offset,
229 bool use_anchor,
230 float scale,
231 base::TimeDelta duration);
233 void ApplyScrollAndScale(ScrollAndScaleSet* info);
234 void SetImplTransform(const gfx::Transform& transform);
236 // Virtual for tests.
237 virtual void StartRateLimiter();
238 virtual void StopRateLimiter();
240 void RateLimit();
242 bool AlwaysUsePartialTextureUpdates();
243 size_t MaxPartialTextureUpdates() const;
244 bool RequestPartialTextureUpdate();
246 void SetDeviceScaleFactor(float device_scale_factor);
247 float device_scale_factor() const { return device_scale_factor_; }
249 void UpdateTopControlsState(TopControlsState constraints,
250 TopControlsState current,
251 bool animate);
253 HeadsUpDisplayLayer* hud_layer() const { return hud_layer_.get(); }
255 Proxy* proxy() const { return proxy_.get(); }
257 AnimationRegistrar* animation_registrar() const {
258 return animation_registrar_.get();
261 bool in_paint_layer_contents() const { return in_paint_layer_contents_; }
263 // CreateUIResource creates a resource given a bitmap. The bitmap is
264 // generated via an interface function, which is called when initializing the
265 // resource and when the resource has been lost (due to lost context). The
266 // parameter of the interface is a single boolean, which indicates whether the
267 // resource has been lost or not. CreateUIResource returns an Id of the
268 // resource, which is always positive.
269 virtual UIResourceId CreateUIResource(UIResourceClient* client);
270 // Deletes a UI resource. May safely be called more than once.
271 virtual void DeleteUIResource(UIResourceId id);
272 // Put the recreation of all UI resources into the resource queue after they
273 // were evicted on the impl thread.
274 void RecreateUIResources();
276 virtual gfx::Size GetUIResourceSize(UIResourceId id) const;
278 bool UsingSharedMemoryResources();
279 int id() const { return id_; }
281 // Returns the id of the benchmark on success, 0 otherwise.
282 int ScheduleMicroBenchmark(const std::string& benchmark_name,
283 scoped_ptr<base::Value> value,
284 const MicroBenchmark::DoneCallback& callback);
285 // Returns true if the message was successfully delivered and handled.
286 bool SendMessageToMicroBenchmark(int id, scoped_ptr<base::Value> value);
288 // When a SwapPromiseMonitor is created on the main thread, it calls
289 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
290 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
291 // to unregister itself.
292 void InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor);
293 void RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor);
295 // Call this function when you expect there to be a swap buffer.
296 // See swap_promise.h for how to use SwapPromise.
297 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
299 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
301 size_t num_queued_swap_promises() const { return swap_promise_list_.size(); }
303 void set_surface_id_namespace(uint32_t id_namespace);
304 SurfaceSequence CreateSurfaceSequence();
306 void SetChildrenNeedBeginFrames(bool children_need_begin_frames) const;
307 void SendBeginFramesToChildren(const BeginFrameArgs& args) const;
309 void SetAuthoritativeVSyncInterval(const base::TimeDelta& interval);
311 protected:
312 LayerTreeHost(LayerTreeHostClient* client,
313 SharedBitmapManager* shared_bitmap_manager,
314 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
315 TaskGraphRunner* task_graph_runner,
316 const LayerTreeSettings& settings);
317 void InitializeThreaded(
318 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
319 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
320 scoped_ptr<BeginFrameSource> external_begin_frame_source);
321 void InitializeSingleThreaded(
322 LayerTreeHostSingleThreadClient* single_thread_client,
323 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
324 scoped_ptr<BeginFrameSource> external_begin_frame_source);
325 void InitializeForTesting(scoped_ptr<Proxy> proxy_for_testing);
326 void SetOutputSurfaceLostForTesting(bool is_lost) {
327 output_surface_lost_ = is_lost;
330 MicroBenchmarkController micro_benchmark_controller_;
332 private:
333 void InitializeProxy(scoped_ptr<Proxy> proxy);
335 void PaintLayerContents(
336 const RenderSurfaceLayerList& render_surface_layer_list,
337 ResourceUpdateQueue* queue,
338 bool* did_paint_content,
339 bool* need_more_updates);
340 void PaintMasksForRenderSurface(Layer* render_surface_layer,
341 ResourceUpdateQueue* queue,
342 bool* did_paint_content,
343 bool* need_more_updates);
344 bool UpdateLayers(Layer* root_layer, ResourceUpdateQueue* queue);
345 void UpdateHudLayer();
346 void TriggerPrepaint();
348 void ReduceMemoryUsage();
350 void PrioritizeTextures(
351 const RenderSurfaceLayerList& render_surface_layer_list);
352 void SetPrioritiesForSurfaces(size_t surface_memory_bytes);
353 void SetPrioritiesForLayers(const RenderSurfaceLayerList& update_list);
354 size_t CalculateMemoryForRenderSurfaces(
355 const RenderSurfaceLayerList& update_list);
357 bool AnimateLayersRecursive(Layer* current, base::TimeTicks time);
359 struct UIResourceClientData {
360 UIResourceClient* client;
361 gfx::Size size;
364 typedef base::hash_map<UIResourceId, UIResourceClientData>
365 UIResourceClientMap;
366 UIResourceClientMap ui_resource_client_map_;
367 int next_ui_resource_id_;
369 typedef std::vector<UIResourceRequest> UIResourceRequestQueue;
370 UIResourceRequestQueue ui_resource_request_queue_;
372 void RecordGpuRasterizationHistogram();
373 void CalculateLCDTextMetricsCallback(Layer* layer);
375 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
377 bool inside_begin_main_frame_;
378 bool needs_full_tree_sync_;
380 base::CancelableClosure prepaint_callback_;
382 LayerTreeHostClient* client_;
383 scoped_ptr<Proxy> proxy_;
385 int source_frame_number_;
386 scoped_ptr<RenderingStatsInstrumentation> rendering_stats_instrumentation_;
388 bool output_surface_lost_;
390 scoped_refptr<Layer> root_layer_;
391 scoped_refptr<HeadsUpDisplayLayer> hud_layer_;
393 scoped_ptr<PrioritizedResourceManager> contents_texture_manager_;
394 scoped_ptr<PrioritizedResource> surface_memory_placeholder_;
396 base::WeakPtr<InputHandler> input_handler_weak_ptr_;
397 base::WeakPtr<TopControlsManager> top_controls_manager_weak_ptr_;
399 const LayerTreeSettings settings_;
400 LayerTreeDebugState debug_state_;
402 gfx::Size device_viewport_size_;
403 bool top_controls_shrink_blink_size_;
404 float top_controls_height_;
405 float top_controls_shown_ratio_;
406 float device_scale_factor_;
408 bool visible_;
410 base::OneShotTimer<LayerTreeHost> rate_limit_timer_;
412 float page_scale_factor_;
413 float min_page_scale_factor_;
414 float max_page_scale_factor_;
415 gfx::Vector2dF elastic_overscroll_;
416 bool has_gpu_rasterization_trigger_;
417 bool content_is_suitable_for_gpu_rasterization_;
418 bool gpu_rasterization_histogram_recorded_;
420 SkColor background_color_;
421 bool has_transparent_background_;
423 typedef ScopedPtrVector<PrioritizedResource> TextureList;
424 size_t partial_texture_update_requests_;
426 scoped_ptr<AnimationRegistrar> animation_registrar_;
428 scoped_ptr<PendingPageScaleAnimation> pending_page_scale_animation_;
430 // If set, then page scale animation has completed, but the client hasn't been
431 // notified about it yet.
432 bool did_complete_scale_animation_;
434 bool in_paint_layer_contents_;
436 int id_;
437 bool next_commit_forces_redraw_;
439 scoped_refptr<Layer> overscroll_elasticity_layer_;
440 scoped_refptr<Layer> page_scale_layer_;
441 scoped_refptr<Layer> inner_viewport_scroll_layer_;
442 scoped_refptr<Layer> outer_viewport_scroll_layer_;
444 LayerSelectionBound selection_start_;
445 LayerSelectionBound selection_end_;
447 SharedBitmapManager* shared_bitmap_manager_;
448 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
449 TaskGraphRunner* task_graph_runner_;
451 ScopedPtrVector<SwapPromise> swap_promise_list_;
452 std::set<SwapPromiseMonitor*> swap_promise_monitor_;
454 uint32_t surface_id_namespace_;
455 uint32_t next_surface_sequence_;
457 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost);
460 } // namespace cc
462 #endif // CC_TREES_LAYER_TREE_HOST_H_