[Smart Lock] Remove the unused easyUnlockPrivate.getUserImage function.
[chromium-blink-merge.git] / cc / trees / layer_tree_host.h
blob68bc1ebeb3470a5ba864fac5ff9a52da59df6cbf
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/base/swap_promise.h"
25 #include "cc/base/swap_promise_monitor.h"
26 #include "cc/debug/micro_benchmark.h"
27 #include "cc/debug/micro_benchmark_controller.h"
28 #include "cc/input/input_handler.h"
29 #include "cc/input/layer_selection_bound.h"
30 #include "cc/input/scrollbar.h"
31 #include "cc/input/top_controls_state.h"
32 #include "cc/layers/layer_lists.h"
33 #include "cc/output/output_surface.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 "third_party/skia/include/core/SkColor.h"
42 #include "ui/gfx/geometry/rect.h"
44 namespace gpu {
45 class GpuMemoryBufferManager;
48 namespace cc {
49 class AnimationRegistrar;
50 class BeginFrameSource;
51 class HeadsUpDisplayLayer;
52 class Layer;
53 class LayerTreeHostImpl;
54 class LayerTreeHostImplClient;
55 class LayerTreeHostSingleThreadClient;
56 class PrioritizedResource;
57 class PrioritizedResourceManager;
58 class Region;
59 class RenderingStatsInstrumentation;
60 class ResourceProvider;
61 class ResourceUpdateQueue;
62 class SharedBitmapManager;
63 class TopControlsManager;
64 class UIResourceRequest;
65 struct PendingPageScaleAnimation;
66 struct RenderingStats;
67 struct ScrollAndScaleSet;
68 enum class GpuRasterizationStatus;
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 void DidInitializeOutputSurface();
128 void DidFailToInitializeOutputSurface();
129 virtual scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
130 LayerTreeHostImplClient* client);
131 void DidLoseOutputSurface();
132 bool output_surface_lost() const { return output_surface_lost_; }
133 void DidCommitAndDrawFrame() { client_->DidCommitAndDrawFrame(); }
134 void DidCompleteSwapBuffers() { client_->DidCompleteSwapBuffers(); }
135 void DeleteContentsTexturesOnImplThread(ResourceProvider* resource_provider);
136 bool UpdateLayers(ResourceUpdateQueue* queue);
138 // Called when the compositor completed page scale animation.
139 void DidCompletePageScaleAnimation();
141 LayerTreeHostClient* client() { return client_; }
142 const base::WeakPtr<InputHandler>& GetInputHandler() {
143 return input_handler_weak_ptr_;
146 void NotifyInputThrottledUntilCommit();
148 void Composite(base::TimeTicks frame_begin_time);
150 void FinishAllRendering();
152 void SetDeferCommits(bool defer_commits);
154 // Test only hook
155 virtual void DidDeferCommit();
157 int source_frame_number() const { return source_frame_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 SetNeedsUpdateLayers();
171 virtual void SetNeedsCommit();
172 virtual void SetNeedsFullTreeSync();
173 void SetNeedsRedraw();
174 void SetNeedsRedrawRect(const gfx::Rect& damage_rect);
175 bool CommitRequested() const;
176 bool BeginMainFrameRequested() const;
178 void SetNextCommitWaitsForActivation();
180 void SetNextCommitForcesRedraw();
182 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> events);
184 void SetRootLayer(scoped_refptr<Layer> root_layer);
185 Layer* root_layer() { return root_layer_.get(); }
186 const Layer* root_layer() const { return root_layer_.get(); }
187 const Layer* overscroll_elasticity_layer() const {
188 return overscroll_elasticity_layer_.get();
190 const Layer* page_scale_layer() const { return page_scale_layer_.get(); }
191 void RegisterViewportLayers(scoped_refptr<Layer> overscroll_elasticity_layer,
192 scoped_refptr<Layer> page_scale_layer,
193 scoped_refptr<Layer> inner_viewport_scroll_layer,
194 scoped_refptr<Layer> outer_viewport_scroll_layer);
195 Layer* inner_viewport_scroll_layer() const {
196 return inner_viewport_scroll_layer_.get();
198 Layer* outer_viewport_scroll_layer() const {
199 return outer_viewport_scroll_layer_.get();
202 void RegisterSelection(const LayerSelectionBound& start,
203 const LayerSelectionBound& end);
205 const LayerTreeSettings& settings() const { return settings_; }
207 void SetDebugState(const LayerTreeDebugState& debug_state);
208 const LayerTreeDebugState& debug_state() const { return debug_state_; }
210 bool has_gpu_rasterization_trigger() const {
211 return has_gpu_rasterization_trigger_;
213 void SetHasGpuRasterizationTrigger(bool has_trigger);
214 bool UseGpuRasterization() const;
215 GpuRasterizationStatus GetGpuRasterizationStatus() const;
217 void SetViewportSize(const gfx::Size& device_viewport_size);
218 void SetTopControlsShrinkBlinkSize(bool shrink);
219 void SetTopControlsHeight(float height);
220 void SetTopControlsContentOffset(float offset);
222 gfx::Size device_viewport_size() const { return device_viewport_size_; }
224 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta);
225 void SetPageScaleFactorAndLimits(float page_scale_factor,
226 float min_page_scale_factor,
227 float max_page_scale_factor);
228 float page_scale_factor() const { return page_scale_factor_; }
229 gfx::Vector2dF elastic_overscroll() const { return elastic_overscroll_; }
231 SkColor background_color() const { return background_color_; }
232 void set_background_color(SkColor color) { background_color_ = color; }
234 void set_has_transparent_background(bool transparent) {
235 has_transparent_background_ = transparent;
238 PrioritizedResourceManager* contents_texture_manager() const {
239 return contents_texture_manager_.get();
242 void SetVisible(bool visible);
243 bool visible() const { return visible_; }
245 void SetThrottleFrameProduction(bool throttle);
247 void StartPageScaleAnimation(const gfx::Vector2d& target_offset,
248 bool use_anchor,
249 float scale,
250 base::TimeDelta duration);
252 void ApplyScrollAndScale(ScrollAndScaleSet* info);
253 void SetImplTransform(const gfx::Transform& transform);
255 // Virtual for tests.
256 virtual void StartRateLimiter();
257 virtual void StopRateLimiter();
259 void RateLimit();
261 bool AlwaysUsePartialTextureUpdates();
262 size_t MaxPartialTextureUpdates() const;
263 bool RequestPartialTextureUpdate();
265 void SetDeviceScaleFactor(float device_scale_factor);
266 float device_scale_factor() const { return device_scale_factor_; }
268 void UpdateTopControlsState(TopControlsState constraints,
269 TopControlsState current,
270 bool animate);
272 HeadsUpDisplayLayer* hud_layer() const { return hud_layer_.get(); }
274 Proxy* proxy() const { return proxy_.get(); }
276 AnimationRegistrar* animation_registrar() const {
277 return animation_registrar_.get();
280 // Obtains a thorough dump of the LayerTreeHost as a value.
281 void AsValueInto(base::debug::TracedValue* value) const;
283 bool in_paint_layer_contents() const { return in_paint_layer_contents_; }
285 // CreateUIResource creates a resource given a bitmap. The bitmap is
286 // generated via an interface function, which is called when initializing the
287 // resource and when the resource has been lost (due to lost context). The
288 // parameter of the interface is a single boolean, which indicates whether the
289 // resource has been lost or not. CreateUIResource returns an Id of the
290 // resource, which is always positive.
291 virtual UIResourceId CreateUIResource(UIResourceClient* client);
292 // Deletes a UI resource. May safely be called more than once.
293 virtual void DeleteUIResource(UIResourceId id);
294 // Put the recreation of all UI resources into the resource queue after they
295 // were evicted on the impl thread.
296 void RecreateUIResources();
298 virtual gfx::Size GetUIResourceSize(UIResourceId id) const;
300 bool UsingSharedMemoryResources();
301 int id() const { return id_; }
303 // Returns the id of the benchmark on success, 0 otherwise.
304 int ScheduleMicroBenchmark(const std::string& benchmark_name,
305 scoped_ptr<base::Value> value,
306 const MicroBenchmark::DoneCallback& callback);
307 // Returns true if the message was successfully delivered and handled.
308 bool SendMessageToMicroBenchmark(int id, scoped_ptr<base::Value> value);
310 // When a SwapPromiseMonitor is created on the main thread, it calls
311 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
312 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
313 // to unregister itself.
314 void InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor);
315 void RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor);
317 // Call this function when you expect there to be a swap buffer.
318 // See swap_promise.h for how to use SwapPromise.
319 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
321 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
323 size_t num_queued_swap_promises() const { return swap_promise_list_.size(); }
325 void set_surface_id_namespace(uint32_t id_namespace);
326 SurfaceSequence CreateSurfaceSequence();
328 void SetChildrenNeedBeginFrames(bool children_need_begin_frames) const;
329 void SendBeginFramesToChildren(const BeginFrameArgs& args) const;
331 protected:
332 LayerTreeHost(LayerTreeHostClient* client,
333 SharedBitmapManager* shared_bitmap_manager,
334 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
335 const LayerTreeSettings& settings);
336 void InitializeThreaded(
337 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
338 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
339 scoped_ptr<BeginFrameSource> external_begin_frame_source);
340 void InitializeSingleThreaded(
341 LayerTreeHostSingleThreadClient* single_thread_client,
342 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
343 scoped_ptr<BeginFrameSource> external_begin_frame_source);
344 void InitializeForTesting(scoped_ptr<Proxy> proxy_for_testing);
345 void SetOutputSurfaceLostForTesting(bool is_lost) {
346 output_surface_lost_ = is_lost;
349 MicroBenchmarkController micro_benchmark_controller_;
351 private:
352 void InitializeProxy(scoped_ptr<Proxy> proxy);
354 void PaintLayerContents(
355 const RenderSurfaceLayerList& render_surface_layer_list,
356 ResourceUpdateQueue* queue,
357 bool* did_paint_content,
358 bool* need_more_updates);
359 void PaintMasksForRenderSurface(Layer* render_surface_layer,
360 ResourceUpdateQueue* queue,
361 bool* did_paint_content,
362 bool* need_more_updates);
363 bool UpdateLayers(Layer* root_layer, ResourceUpdateQueue* queue);
364 void UpdateHudLayer();
365 void TriggerPrepaint();
367 void ReduceMemoryUsage();
369 void PrioritizeTextures(
370 const RenderSurfaceLayerList& render_surface_layer_list);
371 void SetPrioritiesForSurfaces(size_t surface_memory_bytes);
372 void SetPrioritiesForLayers(const RenderSurfaceLayerList& update_list);
373 size_t CalculateMemoryForRenderSurfaces(
374 const RenderSurfaceLayerList& update_list);
376 bool AnimateLayersRecursive(Layer* current, base::TimeTicks time);
378 struct UIResourceClientData {
379 UIResourceClient* client;
380 gfx::Size size;
383 typedef base::hash_map<UIResourceId, UIResourceClientData>
384 UIResourceClientMap;
385 UIResourceClientMap ui_resource_client_map_;
386 int next_ui_resource_id_;
388 typedef std::vector<UIResourceRequest> UIResourceRequestQueue;
389 UIResourceRequestQueue ui_resource_request_queue_;
391 void RecordGpuRasterizationHistogram();
392 void CalculateLCDTextMetricsCallback(Layer* layer);
394 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
396 bool inside_begin_main_frame_;
397 bool needs_full_tree_sync_;
399 base::CancelableClosure prepaint_callback_;
401 LayerTreeHostClient* client_;
402 scoped_ptr<Proxy> proxy_;
404 int source_frame_number_;
405 scoped_ptr<RenderingStatsInstrumentation> rendering_stats_instrumentation_;
407 bool output_surface_lost_;
409 scoped_refptr<Layer> root_layer_;
410 scoped_refptr<HeadsUpDisplayLayer> hud_layer_;
412 scoped_ptr<PrioritizedResourceManager> contents_texture_manager_;
413 scoped_ptr<PrioritizedResource> surface_memory_placeholder_;
415 base::WeakPtr<InputHandler> input_handler_weak_ptr_;
416 base::WeakPtr<TopControlsManager> top_controls_manager_weak_ptr_;
418 const LayerTreeSettings settings_;
419 LayerTreeDebugState debug_state_;
421 gfx::Size device_viewport_size_;
422 bool top_controls_shrink_blink_size_;
423 float top_controls_height_;
424 float top_controls_content_offset_;
425 float device_scale_factor_;
427 bool visible_;
429 base::OneShotTimer<LayerTreeHost> rate_limit_timer_;
431 float page_scale_factor_;
432 float min_page_scale_factor_;
433 float max_page_scale_factor_;
434 gfx::Vector2dF elastic_overscroll_;
435 bool has_gpu_rasterization_trigger_;
436 bool content_is_suitable_for_gpu_rasterization_;
437 bool gpu_rasterization_histogram_recorded_;
439 SkColor background_color_;
440 bool has_transparent_background_;
442 typedef ScopedPtrVector<PrioritizedResource> TextureList;
443 size_t partial_texture_update_requests_;
445 scoped_ptr<AnimationRegistrar> animation_registrar_;
447 scoped_ptr<PendingPageScaleAnimation> pending_page_scale_animation_;
449 // If set, then page scale animation has completed, but the client hasn't been
450 // notified about it yet.
451 bool did_complete_scale_animation_;
453 bool in_paint_layer_contents_;
455 static const int kTotalFramesToUseForLCDTextMetrics = 50;
456 int total_frames_used_for_lcd_text_metrics_;
458 struct LCDTextMetrics {
459 LCDTextMetrics()
460 : total_num_cc_layers(0),
461 total_num_cc_layers_can_use_lcd_text(0),
462 total_num_cc_layers_will_use_lcd_text(0) {}
464 int64 total_num_cc_layers;
465 int64 total_num_cc_layers_can_use_lcd_text;
466 int64 total_num_cc_layers_will_use_lcd_text;
468 LCDTextMetrics lcd_text_metrics_;
469 int id_;
470 bool next_commit_forces_redraw_;
472 scoped_refptr<Layer> overscroll_elasticity_layer_;
473 scoped_refptr<Layer> page_scale_layer_;
474 scoped_refptr<Layer> inner_viewport_scroll_layer_;
475 scoped_refptr<Layer> outer_viewport_scroll_layer_;
477 LayerSelectionBound selection_start_;
478 LayerSelectionBound selection_end_;
480 SharedBitmapManager* shared_bitmap_manager_;
481 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
483 ScopedPtrVector<SwapPromise> swap_promise_list_;
484 std::set<SwapPromiseMonitor*> swap_promise_monitor_;
486 uint32_t surface_id_namespace_;
487 uint32_t next_surface_sequence_;
489 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost);
492 } // namespace cc
494 #endif // CC_TREES_LAYER_TREE_HOST_H_