File with simple block types.
[chromium-blink-merge.git] / cc / trees / layer_tree_host.h
blobde91b764e864398d881a0a6d409fc3b40c737579
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 PendingPageScaleAnimation;
67 struct RenderingStats;
68 struct ScrollAndScaleSet;
70 // Provides information on an Impl's rendering capabilities back to the
71 // LayerTreeHost.
72 struct CC_EXPORT RendererCapabilities {
73 RendererCapabilities(ResourceFormat best_texture_format,
74 bool allow_partial_texture_updates,
75 int max_texture_size,
76 bool using_shared_memory_resources);
78 RendererCapabilities();
79 ~RendererCapabilities();
81 // Duplicate any modification to this list to RendererCapabilitiesImpl.
82 ResourceFormat best_texture_format;
83 bool allow_partial_texture_updates;
84 int max_texture_size;
85 bool using_shared_memory_resources;
88 class CC_EXPORT LayerTreeHost {
89 public:
90 // The SharedBitmapManager will be used on the compositor thread.
91 static scoped_ptr<LayerTreeHost> CreateThreaded(
92 LayerTreeHostClient* client,
93 SharedBitmapManager* shared_bitmap_manager,
94 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
95 const LayerTreeSettings& settings,
96 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
97 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
98 scoped_ptr<BeginFrameSource> external_begin_frame_source);
100 static scoped_ptr<LayerTreeHost> CreateSingleThreaded(
101 LayerTreeHostClient* client,
102 LayerTreeHostSingleThreadClient* single_thread_client,
103 SharedBitmapManager* shared_bitmap_manager,
104 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
105 const LayerTreeSettings& settings,
106 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
107 scoped_ptr<BeginFrameSource> external_begin_frame_source);
108 virtual ~LayerTreeHost();
110 void SetLayerTreeHostClientReady();
112 // LayerTreeHost interface to Proxy.
113 void WillBeginMainFrame() {
114 client_->WillBeginMainFrame(source_frame_number_);
116 void DidBeginMainFrame();
117 void BeginMainFrame(const BeginFrameArgs& args);
118 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time);
119 void DidStopFlinging();
120 void Layout();
121 void BeginCommitOnImplThread(LayerTreeHostImpl* host_impl);
122 void FinishCommitOnImplThread(LayerTreeHostImpl* host_impl);
123 void WillCommit();
124 void CommitComplete();
125 void SetOutputSurface(scoped_ptr<OutputSurface> output_surface);
126 void RequestNewOutputSurface();
127 virtual scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
128 LayerTreeHostImplClient* client);
129 void DidLoseOutputSurface();
130 bool output_surface_lost() const { return output_surface_lost_; }
131 virtual void OnCreateAndInitializeOutputSurfaceAttempted(bool success);
132 void DidCommitAndDrawFrame() { client_->DidCommitAndDrawFrame(); }
133 void DidCompleteSwapBuffers() { client_->DidCompleteSwapBuffers(); }
134 void DeleteContentsTexturesOnImplThread(ResourceProvider* resource_provider);
135 bool UpdateLayers(ResourceUpdateQueue* queue);
137 LayerTreeHostClient* client() { return client_; }
138 const base::WeakPtr<InputHandler>& GetInputHandler() {
139 return input_handler_weak_ptr_;
142 void NotifyInputThrottledUntilCommit();
144 void Composite(base::TimeTicks frame_begin_time);
146 void FinishAllRendering();
148 void SetDeferCommits(bool defer_commits);
150 // Test only hook
151 virtual void DidDeferCommit();
153 int source_frame_number() const { return source_frame_number_; }
155 void SetNeedsDisplayOnAllLayers();
157 void CollectRenderingStats(RenderingStats* stats) const;
159 RenderingStatsInstrumentation* rendering_stats_instrumentation() const {
160 return rendering_stats_instrumentation_.get();
163 const RendererCapabilities& GetRendererCapabilities() const;
165 void SetNeedsAnimate();
166 virtual void SetNeedsUpdateLayers();
167 virtual void SetNeedsCommit();
168 virtual void SetNeedsFullTreeSync();
169 void SetNeedsRedraw();
170 void SetNeedsRedrawRect(const gfx::Rect& damage_rect);
171 bool CommitRequested() const;
172 bool BeginMainFrameRequested() const;
174 void SetNextCommitWaitsForActivation();
176 void SetNextCommitForcesRedraw();
178 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> events);
180 void SetRootLayer(scoped_refptr<Layer> root_layer);
181 Layer* root_layer() { return root_layer_.get(); }
182 const Layer* root_layer() const { return root_layer_.get(); }
183 const Layer* overscroll_elasticity_layer() const {
184 return overscroll_elasticity_layer_.get();
186 const Layer* page_scale_layer() const { return page_scale_layer_.get(); }
187 void RegisterViewportLayers(scoped_refptr<Layer> overscroll_elasticity_layer,
188 scoped_refptr<Layer> page_scale_layer,
189 scoped_refptr<Layer> inner_viewport_scroll_layer,
190 scoped_refptr<Layer> outer_viewport_scroll_layer);
191 Layer* inner_viewport_scroll_layer() const {
192 return inner_viewport_scroll_layer_.get();
194 Layer* outer_viewport_scroll_layer() const {
195 return outer_viewport_scroll_layer_.get();
198 void RegisterSelection(const LayerSelectionBound& start,
199 const LayerSelectionBound& end);
201 const LayerTreeSettings& settings() const { return settings_; }
203 void SetDebugState(const LayerTreeDebugState& debug_state);
204 const LayerTreeDebugState& debug_state() const { return debug_state_; }
206 bool has_gpu_rasterization_trigger() const {
207 return has_gpu_rasterization_trigger_;
209 void SetHasGpuRasterizationTrigger(bool has_trigger);
210 bool UseGpuRasterization() const;
212 void SetViewportSize(const gfx::Size& device_viewport_size);
213 void SetTopControlsLayoutHeight(float height);
214 void SetTopControlsContentOffset(float offset);
216 gfx::Size device_viewport_size() const { return device_viewport_size_; }
218 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta);
219 void SetPageScaleFactorAndLimits(float page_scale_factor,
220 float min_page_scale_factor,
221 float max_page_scale_factor);
222 float page_scale_factor() const { return page_scale_factor_; }
224 SkColor background_color() const { return background_color_; }
225 void set_background_color(SkColor color) { background_color_ = color; }
227 void set_has_transparent_background(bool transparent) {
228 has_transparent_background_ = transparent;
231 void SetOverhangBitmap(const SkBitmap& bitmap);
233 PrioritizedResourceManager* contents_texture_manager() const {
234 return contents_texture_manager_.get();
237 void SetVisible(bool visible);
238 bool visible() const { return visible_; }
240 void StartPageScaleAnimation(const gfx::Vector2d& target_offset,
241 bool use_anchor,
242 float scale,
243 base::TimeDelta duration);
245 void ApplyScrollAndScale(ScrollAndScaleSet* info);
246 void SetImplTransform(const gfx::Transform& transform);
248 // Virtual for tests.
249 virtual void StartRateLimiter();
250 virtual void StopRateLimiter();
252 void RateLimit();
254 bool AlwaysUsePartialTextureUpdates();
255 size_t MaxPartialTextureUpdates() const;
256 bool RequestPartialTextureUpdate();
258 void SetDeviceScaleFactor(float device_scale_factor);
259 float device_scale_factor() const { return device_scale_factor_; }
261 void UpdateTopControlsState(TopControlsState constraints,
262 TopControlsState current,
263 bool animate);
265 HeadsUpDisplayLayer* hud_layer() const { return hud_layer_.get(); }
267 Proxy* proxy() const { return proxy_.get(); }
269 AnimationRegistrar* animation_registrar() const {
270 return animation_registrar_.get();
273 // Obtains a thorough dump of the LayerTreeHost as a value.
274 void AsValueInto(base::debug::TracedValue* value) const;
276 bool in_paint_layer_contents() const { return in_paint_layer_contents_; }
278 // CreateUIResource creates a resource given a bitmap. The bitmap is
279 // generated via an interface function, which is called when initializing the
280 // resource and when the resource has been lost (due to lost context). The
281 // parameter of the interface is a single boolean, which indicates whether the
282 // resource has been lost or not. CreateUIResource returns an Id of the
283 // resource, which is always positive.
284 virtual UIResourceId CreateUIResource(UIResourceClient* client);
285 // Deletes a UI resource. May safely be called more than once.
286 virtual void DeleteUIResource(UIResourceId id);
287 // Put the recreation of all UI resources into the resource queue after they
288 // were evicted on the impl thread.
289 void RecreateUIResources();
291 virtual gfx::Size GetUIResourceSize(UIResourceId id) const;
293 bool UsingSharedMemoryResources();
294 int id() const { return id_; }
296 // Returns the id of the benchmark on success, 0 otherwise.
297 int ScheduleMicroBenchmark(const std::string& benchmark_name,
298 scoped_ptr<base::Value> value,
299 const MicroBenchmark::DoneCallback& callback);
300 // Returns true if the message was successfully delivered and handled.
301 bool SendMessageToMicroBenchmark(int id, scoped_ptr<base::Value> value);
303 // When a SwapPromiseMonitor is created on the main thread, it calls
304 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
305 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
306 // to unregister itself.
307 void InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor);
308 void RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor);
310 // Call this function when you expect there to be a swap buffer.
311 // See swap_promise.h for how to use SwapPromise.
312 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
314 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
316 size_t num_queued_swap_promises() const { return swap_promise_list_.size(); }
318 void set_surface_id_namespace(uint32_t id_namespace);
319 SurfaceSequence CreateSurfaceSequence();
321 void SetChildrenNeedBeginFrames(bool children_need_begin_frames) const;
322 void SendBeginFramesToChildren(const BeginFrameArgs& args) const;
324 protected:
325 LayerTreeHost(LayerTreeHostClient* client,
326 SharedBitmapManager* shared_bitmap_manager,
327 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
328 const LayerTreeSettings& settings);
329 void InitializeThreaded(
330 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
331 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
332 scoped_ptr<BeginFrameSource> external_begin_frame_source);
333 void InitializeSingleThreaded(
334 LayerTreeHostSingleThreadClient* single_thread_client,
335 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
336 scoped_ptr<BeginFrameSource> external_begin_frame_source);
337 void InitializeForTesting(scoped_ptr<Proxy> proxy_for_testing);
338 void SetOutputSurfaceLostForTesting(bool is_lost) {
339 output_surface_lost_ = is_lost;
342 MicroBenchmarkController micro_benchmark_controller_;
344 private:
345 void InitializeProxy(scoped_ptr<Proxy> proxy);
347 void PaintLayerContents(
348 const RenderSurfaceLayerList& render_surface_layer_list,
349 ResourceUpdateQueue* queue,
350 bool* did_paint_content,
351 bool* need_more_updates);
352 void PaintMasksForRenderSurface(Layer* render_surface_layer,
353 ResourceUpdateQueue* queue,
354 bool* did_paint_content,
355 bool* need_more_updates);
356 bool UpdateLayers(Layer* root_layer, ResourceUpdateQueue* queue);
357 void UpdateHudLayer();
358 void TriggerPrepaint();
360 void ReduceMemoryUsage();
362 void PrioritizeTextures(
363 const RenderSurfaceLayerList& render_surface_layer_list);
364 void SetPrioritiesForSurfaces(size_t surface_memory_bytes);
365 void SetPrioritiesForLayers(const RenderSurfaceLayerList& update_list);
366 size_t CalculateMemoryForRenderSurfaces(
367 const RenderSurfaceLayerList& update_list);
369 bool AnimateLayersRecursive(Layer* current, base::TimeTicks time);
371 struct UIResourceClientData {
372 UIResourceClient* client;
373 gfx::Size size;
376 typedef base::hash_map<UIResourceId, UIResourceClientData>
377 UIResourceClientMap;
378 UIResourceClientMap ui_resource_client_map_;
379 int next_ui_resource_id_;
381 typedef std::list<UIResourceRequest> UIResourceRequestQueue;
382 UIResourceRequestQueue ui_resource_request_queue_;
384 void RecordGpuRasterizationHistogram();
385 void CalculateLCDTextMetricsCallback(Layer* layer);
387 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
389 bool inside_begin_main_frame_;
390 bool needs_full_tree_sync_;
392 base::CancelableClosure prepaint_callback_;
394 LayerTreeHostClient* client_;
395 scoped_ptr<Proxy> proxy_;
397 int source_frame_number_;
398 scoped_ptr<RenderingStatsInstrumentation> rendering_stats_instrumentation_;
400 bool output_surface_lost_;
401 int num_failed_recreate_attempts_;
403 scoped_refptr<Layer> root_layer_;
404 scoped_refptr<HeadsUpDisplayLayer> hud_layer_;
406 scoped_ptr<PrioritizedResourceManager> contents_texture_manager_;
407 scoped_ptr<PrioritizedResource> surface_memory_placeholder_;
409 base::WeakPtr<InputHandler> input_handler_weak_ptr_;
410 base::WeakPtr<TopControlsManager> top_controls_manager_weak_ptr_;
412 const LayerTreeSettings settings_;
413 LayerTreeDebugState debug_state_;
415 gfx::Size device_viewport_size_;
416 float top_controls_layout_height_;
417 float top_controls_content_offset_;
418 float device_scale_factor_;
420 bool visible_;
422 base::OneShotTimer<LayerTreeHost> rate_limit_timer_;
424 float page_scale_factor_;
425 float min_page_scale_factor_;
426 float max_page_scale_factor_;
427 bool has_gpu_rasterization_trigger_;
428 bool content_is_suitable_for_gpu_rasterization_;
429 bool gpu_rasterization_histogram_recorded_;
431 SkColor background_color_;
432 bool has_transparent_background_;
434 // If set, this texture is used to fill in the parts of the screen not
435 // covered by layers.
436 scoped_ptr<ScopedUIResource> overhang_ui_resource_;
438 typedef ScopedPtrVector<PrioritizedResource> TextureList;
439 size_t partial_texture_update_requests_;
441 scoped_ptr<AnimationRegistrar> animation_registrar_;
443 scoped_ptr<PendingPageScaleAnimation> pending_page_scale_animation_;
445 bool in_paint_layer_contents_;
447 static const int kTotalFramesToUseForLCDTextMetrics = 50;
448 int total_frames_used_for_lcd_text_metrics_;
450 struct LCDTextMetrics {
451 LCDTextMetrics()
452 : total_num_cc_layers(0),
453 total_num_cc_layers_can_use_lcd_text(0),
454 total_num_cc_layers_will_use_lcd_text(0) {}
456 int64 total_num_cc_layers;
457 int64 total_num_cc_layers_can_use_lcd_text;
458 int64 total_num_cc_layers_will_use_lcd_text;
460 LCDTextMetrics lcd_text_metrics_;
461 int id_;
462 bool next_commit_forces_redraw_;
464 scoped_refptr<Layer> overscroll_elasticity_layer_;
465 scoped_refptr<Layer> page_scale_layer_;
466 scoped_refptr<Layer> inner_viewport_scroll_layer_;
467 scoped_refptr<Layer> outer_viewport_scroll_layer_;
469 LayerSelectionBound selection_start_;
470 LayerSelectionBound selection_end_;
472 SharedBitmapManager* shared_bitmap_manager_;
473 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
475 ScopedPtrVector<SwapPromise> swap_promise_list_;
476 std::set<SwapPromiseMonitor*> swap_promise_monitor_;
478 uint32_t surface_id_namespace_;
479 uint32_t next_surface_sequence_;
481 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost);
484 } // namespace cc
486 #endif // CC_TREES_LAYER_TREE_HOST_H_