Support HTTP/2 drafts 14 and 15 simultaneously.
[chromium-blink-merge.git] / cc / trees / layer_tree_host.h
blob7820cc24a29764bb8eb02ffc65cdd352d7a9757b
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 <list>
10 #include <set>
11 #include <string>
12 #include <vector>
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/surfaces/surface_sequence.h"
38 #include "cc/trees/layer_tree_host_client.h"
39 #include "cc/trees/layer_tree_host_common.h"
40 #include "cc/trees/layer_tree_settings.h"
41 #include "cc/trees/proxy.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 TopControlsManager;
65 class UIResourceRequest;
66 struct RenderingStats;
67 struct ScrollAndScaleSet;
69 // Provides information on an Impl's rendering capabilities back to the
70 // LayerTreeHost.
71 struct CC_EXPORT RendererCapabilities {
72 RendererCapabilities(ResourceFormat best_texture_format,
73 bool allow_partial_texture_updates,
74 int max_texture_size,
75 bool using_shared_memory_resources);
77 RendererCapabilities();
78 ~RendererCapabilities();
80 // Duplicate any modification to this list to RendererCapabilitiesImpl.
81 ResourceFormat best_texture_format;
82 bool allow_partial_texture_updates;
83 int max_texture_size;
84 bool using_shared_memory_resources;
87 class CC_EXPORT LayerTreeHost {
88 public:
89 // The SharedBitmapManager will be used on the compositor thread.
90 static scoped_ptr<LayerTreeHost> CreateThreaded(
91 LayerTreeHostClient* client,
92 SharedBitmapManager* shared_bitmap_manager,
93 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
94 const LayerTreeSettings& settings,
95 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
96 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
97 scoped_ptr<BeginFrameSource> external_begin_frame_source);
99 static scoped_ptr<LayerTreeHost> CreateSingleThreaded(
100 LayerTreeHostClient* client,
101 LayerTreeHostSingleThreadClient* single_thread_client,
102 SharedBitmapManager* shared_bitmap_manager,
103 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
104 const LayerTreeSettings& settings,
105 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
106 scoped_ptr<BeginFrameSource> external_begin_frame_source);
107 virtual ~LayerTreeHost();
109 void SetLayerTreeHostClientReady();
111 // LayerTreeHost interface to Proxy.
112 void WillBeginMainFrame() {
113 client_->WillBeginMainFrame(source_frame_number_);
115 void DidBeginMainFrame();
116 void BeginMainFrame(const BeginFrameArgs& args);
117 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time);
118 void DidStopFlinging();
119 void Layout();
120 void BeginCommitOnImplThread(LayerTreeHostImpl* host_impl);
121 void FinishCommitOnImplThread(LayerTreeHostImpl* host_impl);
122 void WillCommit();
123 void CommitComplete();
124 void SetOutputSurface(scoped_ptr<OutputSurface> output_surface);
125 void RequestNewOutputSurface();
126 virtual scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
127 LayerTreeHostImplClient* client);
128 void DidLoseOutputSurface();
129 bool output_surface_lost() const { return output_surface_lost_; }
130 virtual void OnCreateAndInitializeOutputSurfaceAttempted(bool success);
131 void DidCommitAndDrawFrame() { client_->DidCommitAndDrawFrame(); }
132 void DidCompleteSwapBuffers() { client_->DidCompleteSwapBuffers(); }
133 void DeleteContentsTexturesOnImplThread(ResourceProvider* resource_provider);
134 bool UpdateLayers(ResourceUpdateQueue* queue);
136 LayerTreeHostClient* client() { return client_; }
137 const base::WeakPtr<InputHandler>& GetInputHandler() {
138 return input_handler_weak_ptr_;
141 void NotifyInputThrottledUntilCommit();
143 void Composite(base::TimeTicks frame_begin_time);
145 void FinishAllRendering();
147 void SetDeferCommits(bool defer_commits);
149 // Test only hook
150 virtual void DidDeferCommit();
152 int source_frame_number() const { return source_frame_number_; }
154 void SetNeedsDisplayOnAllLayers();
156 void CollectRenderingStats(RenderingStats* stats) const;
158 RenderingStatsInstrumentation* rendering_stats_instrumentation() const {
159 return rendering_stats_instrumentation_.get();
162 const RendererCapabilities& GetRendererCapabilities() const;
164 void SetNeedsAnimate();
165 virtual void SetNeedsUpdateLayers();
166 virtual void SetNeedsCommit();
167 virtual void SetNeedsFullTreeSync();
168 void SetNeedsRedraw();
169 void SetNeedsRedrawRect(const gfx::Rect& damage_rect);
170 bool CommitRequested() const;
171 bool BeginMainFrameRequested() const;
173 void SetNextCommitWaitsForActivation();
175 void SetNextCommitForcesRedraw();
177 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> events);
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(); }
182 const Layer* page_scale_layer() const { return page_scale_layer_.get(); }
183 void RegisterViewportLayers(
184 scoped_refptr<Layer> page_scale_layer,
185 scoped_refptr<Layer> inner_viewport_scroll_layer,
186 scoped_refptr<Layer> outer_viewport_scroll_layer);
187 Layer* inner_viewport_scroll_layer() const {
188 return inner_viewport_scroll_layer_.get();
190 Layer* outer_viewport_scroll_layer() const {
191 return outer_viewport_scroll_layer_.get();
194 void RegisterSelection(const LayerSelectionBound& start,
195 const LayerSelectionBound& end);
197 const LayerTreeSettings& settings() const { return settings_; }
199 void SetDebugState(const LayerTreeDebugState& debug_state);
200 const LayerTreeDebugState& debug_state() const { return debug_state_; }
202 bool has_gpu_rasterization_trigger() const {
203 return has_gpu_rasterization_trigger_;
205 void SetHasGpuRasterizationTrigger(bool has_trigger);
206 bool UseGpuRasterization() const;
208 void SetViewportSize(const gfx::Size& device_viewport_size);
209 void SetTopControlsLayoutHeight(float height);
210 void SetTopControlsContentOffset(float offset);
212 gfx::Size device_viewport_size() const { return device_viewport_size_; }
214 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta);
215 void SetPageScaleFactorAndLimits(float page_scale_factor,
216 float min_page_scale_factor,
217 float max_page_scale_factor);
218 float page_scale_factor() const { return page_scale_factor_; }
220 SkColor background_color() const { return background_color_; }
221 void set_background_color(SkColor color) { background_color_ = color; }
223 void set_has_transparent_background(bool transparent) {
224 has_transparent_background_ = transparent;
227 void SetOverhangBitmap(const SkBitmap& bitmap);
229 PrioritizedResourceManager* contents_texture_manager() const {
230 return contents_texture_manager_.get();
233 void SetVisible(bool visible);
234 bool visible() const { return visible_; }
236 void StartPageScaleAnimation(const gfx::Vector2d& target_offset,
237 bool use_anchor,
238 float scale,
239 base::TimeDelta duration);
241 void ApplyScrollAndScale(ScrollAndScaleSet* info);
242 void SetImplTransform(const gfx::Transform& transform);
244 // Virtual for tests.
245 virtual void StartRateLimiter();
246 virtual void StopRateLimiter();
248 void RateLimit();
250 bool AlwaysUsePartialTextureUpdates();
251 size_t MaxPartialTextureUpdates() const;
252 bool RequestPartialTextureUpdate();
254 void SetDeviceScaleFactor(float device_scale_factor);
255 float device_scale_factor() const { return device_scale_factor_; }
257 void UpdateTopControlsState(TopControlsState constraints,
258 TopControlsState current,
259 bool animate);
261 HeadsUpDisplayLayer* hud_layer() const { return hud_layer_.get(); }
263 Proxy* proxy() const { return proxy_.get(); }
265 AnimationRegistrar* animation_registrar() const {
266 return animation_registrar_.get();
269 // Obtains a thorough dump of the LayerTreeHost as a value.
270 void AsValueInto(base::debug::TracedValue* value) const;
272 bool in_paint_layer_contents() const { return in_paint_layer_contents_; }
274 // CreateUIResource creates a resource given a bitmap. The bitmap is
275 // generated via an interface function, which is called when initializing the
276 // resource and when the resource has been lost (due to lost context). The
277 // parameter of the interface is a single boolean, which indicates whether the
278 // resource has been lost or not. CreateUIResource returns an Id of the
279 // resource, which is always positive.
280 virtual UIResourceId CreateUIResource(UIResourceClient* client);
281 // Deletes a UI resource. May safely be called more than once.
282 virtual void DeleteUIResource(UIResourceId id);
283 // Put the recreation of all UI resources into the resource queue after they
284 // were evicted on the impl thread.
285 void RecreateUIResources();
287 virtual gfx::Size GetUIResourceSize(UIResourceId id) const;
289 bool UsingSharedMemoryResources();
290 int id() const { return id_; }
292 // Returns the id of the benchmark on success, 0 otherwise.
293 int ScheduleMicroBenchmark(const std::string& benchmark_name,
294 scoped_ptr<base::Value> value,
295 const MicroBenchmark::DoneCallback& callback);
296 // Returns true if the message was successfully delivered and handled.
297 bool SendMessageToMicroBenchmark(int id, scoped_ptr<base::Value> value);
299 // When a SwapPromiseMonitor is created on the main thread, it calls
300 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
301 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
302 // to unregister itself.
303 void InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor);
304 void RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor);
306 // Call this function when you expect there to be a swap buffer.
307 // See swap_promise.h for how to use SwapPromise.
308 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
310 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
312 size_t num_queued_swap_promises() const { return swap_promise_list_.size(); }
314 void set_surface_id_namespace(uint32_t id_namespace);
315 SurfaceSequence CreateSurfaceSequence();
317 protected:
318 LayerTreeHost(LayerTreeHostClient* client,
319 SharedBitmapManager* shared_bitmap_manager,
320 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
321 const LayerTreeSettings& settings);
322 void InitializeThreaded(
323 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
324 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
325 scoped_ptr<BeginFrameSource> external_begin_frame_source);
326 void InitializeSingleThreaded(
327 LayerTreeHostSingleThreadClient* single_thread_client,
328 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
329 scoped_ptr<BeginFrameSource> external_begin_frame_source);
330 void InitializeForTesting(scoped_ptr<Proxy> proxy_for_testing);
331 void SetOutputSurfaceLostForTesting(bool is_lost) {
332 output_surface_lost_ = is_lost;
335 MicroBenchmarkController micro_benchmark_controller_;
337 private:
338 void InitializeProxy(scoped_ptr<Proxy> proxy);
340 void PaintLayerContents(
341 const RenderSurfaceLayerList& render_surface_layer_list,
342 ResourceUpdateQueue* queue,
343 bool* did_paint_content,
344 bool* need_more_updates);
345 void PaintMasksForRenderSurface(Layer* render_surface_layer,
346 ResourceUpdateQueue* queue,
347 bool* did_paint_content,
348 bool* need_more_updates);
349 bool UpdateLayers(Layer* root_layer, ResourceUpdateQueue* queue);
350 void UpdateHudLayer();
351 void TriggerPrepaint();
353 void ReduceMemoryUsage();
355 void PrioritizeTextures(
356 const RenderSurfaceLayerList& render_surface_layer_list);
357 void SetPrioritiesForSurfaces(size_t surface_memory_bytes);
358 void SetPrioritiesForLayers(const RenderSurfaceLayerList& update_list);
359 size_t CalculateMemoryForRenderSurfaces(
360 const RenderSurfaceLayerList& update_list);
362 bool AnimateLayersRecursive(Layer* current, base::TimeTicks time);
364 struct UIResourceClientData {
365 UIResourceClient* client;
366 gfx::Size size;
369 typedef base::hash_map<UIResourceId, UIResourceClientData>
370 UIResourceClientMap;
371 UIResourceClientMap ui_resource_client_map_;
372 int next_ui_resource_id_;
374 typedef std::list<UIResourceRequest> UIResourceRequestQueue;
375 UIResourceRequestQueue ui_resource_request_queue_;
377 void RecordGpuRasterizationHistogram();
378 void CalculateLCDTextMetricsCallback(Layer* layer);
380 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
382 bool inside_begin_main_frame_;
383 bool needs_full_tree_sync_;
385 base::CancelableClosure prepaint_callback_;
387 LayerTreeHostClient* client_;
388 scoped_ptr<Proxy> proxy_;
390 int source_frame_number_;
391 scoped_ptr<RenderingStatsInstrumentation> rendering_stats_instrumentation_;
393 bool output_surface_lost_;
394 int num_failed_recreate_attempts_;
396 scoped_refptr<Layer> root_layer_;
397 scoped_refptr<HeadsUpDisplayLayer> hud_layer_;
399 scoped_ptr<PrioritizedResourceManager> contents_texture_manager_;
400 scoped_ptr<PrioritizedResource> surface_memory_placeholder_;
402 base::WeakPtr<InputHandler> input_handler_weak_ptr_;
403 base::WeakPtr<TopControlsManager> top_controls_manager_weak_ptr_;
405 const LayerTreeSettings settings_;
406 LayerTreeDebugState debug_state_;
408 gfx::Size device_viewport_size_;
409 float top_controls_layout_height_;
410 float top_controls_content_offset_;
411 float device_scale_factor_;
413 bool visible_;
415 base::OneShotTimer<LayerTreeHost> rate_limit_timer_;
417 float page_scale_factor_;
418 float min_page_scale_factor_;
419 float max_page_scale_factor_;
420 bool has_gpu_rasterization_trigger_;
421 bool content_is_suitable_for_gpu_rasterization_;
422 bool gpu_rasterization_histogram_recorded_;
424 SkColor background_color_;
425 bool has_transparent_background_;
427 // If set, this texture is used to fill in the parts of the screen not
428 // covered by layers.
429 scoped_ptr<ScopedUIResource> overhang_ui_resource_;
431 typedef ScopedPtrVector<PrioritizedResource> TextureList;
432 size_t partial_texture_update_requests_;
434 scoped_ptr<AnimationRegistrar> animation_registrar_;
436 struct PendingPageScaleAnimation {
437 gfx::Vector2d target_offset;
438 bool use_anchor;
439 float scale;
440 base::TimeDelta duration;
442 scoped_ptr<PendingPageScaleAnimation> pending_page_scale_animation_;
444 bool in_paint_layer_contents_;
446 static const int kTotalFramesToUseForLCDTextMetrics = 50;
447 int total_frames_used_for_lcd_text_metrics_;
449 struct LCDTextMetrics {
450 LCDTextMetrics()
451 : total_num_cc_layers(0),
452 total_num_cc_layers_can_use_lcd_text(0),
453 total_num_cc_layers_will_use_lcd_text(0) {}
455 int64 total_num_cc_layers;
456 int64 total_num_cc_layers_can_use_lcd_text;
457 int64 total_num_cc_layers_will_use_lcd_text;
459 LCDTextMetrics lcd_text_metrics_;
460 int id_;
461 bool next_commit_forces_redraw_;
463 scoped_refptr<Layer> page_scale_layer_;
464 scoped_refptr<Layer> inner_viewport_scroll_layer_;
465 scoped_refptr<Layer> outer_viewport_scroll_layer_;
467 LayerSelectionBound selection_start_;
468 LayerSelectionBound selection_end_;
470 SharedBitmapManager* shared_bitmap_manager_;
471 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
473 ScopedPtrVector<SwapPromise> swap_promise_list_;
474 std::set<SwapPromiseMonitor*> swap_promise_monitor_;
476 uint32_t surface_id_namespace_;
477 uint32_t next_surface_sequence_;
479 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost);
482 } // namespace cc
484 #endif // CC_TREES_LAYER_TREE_HOST_H_