Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_impl.h
blobdf3f23ffd8d9449c8c90ec8b739aee85f1833f75
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_IMPL_H_
6 #define CC_TREES_LAYER_TREE_HOST_IMPL_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time.h"
14 #include "cc/animation/animation_events.h"
15 #include "cc/animation/animation_registrar.h"
16 #include "cc/base/cc_export.h"
17 #include "cc/debug/latency_info.h"
18 #include "cc/input/input_handler.h"
19 #include "cc/input/top_controls_manager_client.h"
20 #include "cc/layers/layer_lists.h"
21 #include "cc/layers/render_pass_sink.h"
22 #include "cc/output/output_surface_client.h"
23 #include "cc/output/renderer.h"
24 #include "cc/quads/render_pass.h"
25 #include "cc/resources/tile_manager.h"
26 #include "skia/ext/refptr.h"
27 #include "third_party/skia/include/core/SkColor.h"
28 #include "third_party/skia/include/core/SkPicture.h"
29 #include "ui/gfx/rect.h"
31 namespace cc {
33 class CompletionEvent;
34 class CompositorFrameMetadata;
35 class DebugRectHistory;
36 class FrameRateCounter;
37 class LayerImpl;
38 class LayerTreeHostImplTimeSourceAdapter;
39 class LayerTreeImpl;
40 class PageScaleAnimation;
41 class PaintTimeCounter;
42 class MemoryHistory;
43 class RenderingStatsInstrumentation;
44 class RenderPassDrawQuad;
45 class ResourceProvider;
46 class TopControlsManager;
47 struct RendererCapabilities;
49 // LayerTreeHost->Proxy callback interface.
50 class LayerTreeHostImplClient {
51 public:
52 virtual void DidLoseOutputSurfaceOnImplThread() = 0;
53 virtual void OnSwapBuffersCompleteOnImplThread() = 0;
54 virtual void OnVSyncParametersChanged(base::TimeTicks timebase,
55 base::TimeDelta interval) = 0;
56 virtual void DidVSync(base::TimeTicks frame_time) = 0;
57 virtual void OnCanDrawStateChanged(bool can_draw) = 0;
58 virtual void OnHasPendingTreeStateChanged(bool has_pending_tree) = 0;
59 virtual void SetNeedsRedrawOnImplThread() = 0;
60 virtual void SetNeedsRedrawRectOnImplThread(gfx::Rect damage_rect) = 0;
61 virtual void DidInitializeVisibleTileOnImplThread() = 0;
62 virtual void SetNeedsCommitOnImplThread() = 0;
63 virtual void SetNeedsManageTilesOnImplThread() = 0;
64 virtual void PostAnimationEventsToMainThreadOnImplThread(
65 scoped_ptr<AnimationEventsVector> events,
66 base::Time wall_clock_time) = 0;
67 // Returns true if resources were deleted by this call.
68 virtual bool ReduceContentsTextureMemoryOnImplThread(
69 size_t limit_bytes,
70 int priority_cutoff) = 0;
71 virtual void ReduceWastedContentsTextureMemoryOnImplThread() = 0;
72 virtual void SendManagedMemoryStats() = 0;
73 virtual bool IsInsideDraw() = 0;
74 virtual void RenewTreePriority() = 0;
75 virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay) = 0;
76 virtual void DidReceiveLastInputEventForVSync(base::TimeTicks frame_time) = 0;
78 protected:
79 virtual ~LayerTreeHostImplClient() {}
82 // LayerTreeHostImpl owns the LayerImpl trees as well as associated rendering
83 // state.
84 class CC_EXPORT LayerTreeHostImpl : public InputHandlerClient,
85 public RendererClient,
86 public TileManagerClient,
87 public OutputSurfaceClient,
88 public TopControlsManagerClient {
89 public:
90 static scoped_ptr<LayerTreeHostImpl> Create(
91 const LayerTreeSettings& settings,
92 LayerTreeHostImplClient* client,
93 Proxy* proxy,
94 RenderingStatsInstrumentation* rendering_stats_instrumentation);
95 virtual ~LayerTreeHostImpl();
97 // InputHandlerClient implementation
98 virtual InputHandlerClient::ScrollStatus ScrollBegin(
99 gfx::Point viewport_point,
100 InputHandlerClient::ScrollInputType type) OVERRIDE;
101 virtual bool ScrollBy(gfx::Point viewport_point,
102 gfx::Vector2dF scroll_delta) OVERRIDE;
103 virtual bool ScrollVerticallyByPage(
104 gfx::Point viewport_point,
105 WebKit::WebScrollbar::ScrollDirection direction) OVERRIDE;
106 virtual void ScrollEnd() OVERRIDE;
107 virtual InputHandlerClient::ScrollStatus FlingScrollBegin() OVERRIDE;
108 virtual void PinchGestureBegin() OVERRIDE;
109 virtual void PinchGestureUpdate(float magnify_delta,
110 gfx::Point anchor) OVERRIDE;
111 virtual void PinchGestureEnd() OVERRIDE;
112 virtual void StartPageScaleAnimation(gfx::Vector2d target_offset,
113 bool anchor_point,
114 float page_scale,
115 base::TimeTicks start_time,
116 base::TimeDelta duration) OVERRIDE;
117 virtual void ScheduleAnimation() OVERRIDE;
118 virtual bool HaveTouchEventHandlersAt(gfx::Point viewport_port) OVERRIDE;
119 virtual void DidReceiveLastInputEventForVSync(
120 base::TimeTicks frame_time) OVERRIDE;
122 // TopControlsManagerClient implementation.
123 virtual void DidChangeTopControlsPosition() OVERRIDE;
124 virtual bool HaveRootScrollLayer() const OVERRIDE;
126 void StartScrollbarAnimation(base::TimeTicks now);
128 struct CC_EXPORT FrameData : public RenderPassSink {
129 FrameData();
130 virtual ~FrameData();
132 std::vector<gfx::Rect> occluding_screen_space_rects;
133 std::vector<gfx::Rect> non_occluding_screen_space_rects;
134 RenderPassList render_passes;
135 RenderPassIdHashMap render_passes_by_id;
136 const LayerImplList* render_surface_layer_list;
137 LayerImplList will_draw_layers;
138 bool contains_incomplete_tile;
139 bool has_no_damage;
140 LatencyInfo latency_info;
142 // RenderPassSink implementation.
143 virtual void AppendRenderPass(scoped_ptr<RenderPass> render_pass) OVERRIDE;
146 virtual void BeginCommit();
147 virtual void CommitComplete();
148 virtual void Animate(base::TimeTicks monotonic_time,
149 base::Time wall_clock_time);
150 virtual void UpdateAnimationState(bool start_ready_animations);
151 void UpdateBackgroundAnimateTicking(bool should_background_tick);
152 void SetViewportDamage(gfx::Rect damage_rect);
154 void ManageTiles();
155 void SetAnticipatedDrawTime(base::TimeTicks time);
157 // Returns false if problems occured preparing the frame, and we should try
158 // to avoid displaying the frame. If PrepareToDraw is called, DidDrawAllLayers
159 // must also be called, regardless of whether DrawLayers is called between the
160 // two.
161 virtual bool PrepareToDraw(FrameData* frame,
162 gfx::Rect device_viewport_damage_rect);
163 virtual void DrawLayers(FrameData* frame, base::TimeTicks frame_begin_time);
164 // Must be called if and only if PrepareToDraw was called.
165 void DidDrawAllLayers(const FrameData& frame);
167 const LayerTreeSettings& settings() const { return settings_; }
169 // Returns the currently visible viewport size in DIP. This value excludes
170 // the URL bar and non-overlay scrollbars.
171 gfx::SizeF VisibleViewportSize() const;
173 // RendererClient implementation
174 private:
175 virtual gfx::Size DeviceViewportSize() const OVERRIDE;
176 virtual const LayerTreeSettings& Settings() const OVERRIDE;
177 public:
178 virtual void DidLoseOutputSurface() OVERRIDE;
179 virtual void OnSwapBuffersComplete() OVERRIDE;
180 virtual void SetFullRootLayerDamage() OVERRIDE;
181 virtual void SetManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
182 OVERRIDE;
183 virtual void EnforceManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
184 OVERRIDE;
185 virtual bool HasImplThread() const OVERRIDE;
186 virtual bool ShouldClearRootRenderPass() const OVERRIDE;
187 virtual CompositorFrameMetadata MakeCompositorFrameMetadata() const OVERRIDE;
188 virtual bool AllowPartialSwap() const OVERRIDE;
190 // TileManagerClient implementation.
191 virtual void ScheduleManageTiles() OVERRIDE;
192 virtual void DidInitializeVisibleTile() OVERRIDE;
194 // OutputSurfaceClient implementation.
195 virtual void SetNeedsRedrawRect(gfx::Rect rect) OVERRIDE;
196 virtual void OnVSyncParametersChanged(base::TimeTicks timebase,
197 base::TimeDelta interval) OVERRIDE;
198 virtual void DidVSync(base::TimeTicks frame_time) OVERRIDE;
199 virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck& ack)
200 OVERRIDE;
202 // Called from LayerTreeImpl.
203 void OnCanDrawStateChangedForTree();
205 // Implementation
206 bool CanDraw();
207 OutputSurface* output_surface() const { return output_surface_.get(); }
209 std::string LayerTreeAsText() const;
210 std::string LayerTreeAsJson() const;
212 void FinishAllRendering();
213 int SourceAnimationFrameNumber() const;
215 virtual bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface);
216 bool IsContextLost();
217 TileManager* tile_manager() { return tile_manager_.get(); }
218 Renderer* renderer() { return renderer_.get(); }
219 const RendererCapabilities& GetRendererCapabilities() const;
221 virtual bool SwapBuffers(const FrameData& frame);
222 void EnableVSyncNotification(bool enable);
224 void Readback(void* pixels, gfx::Rect rect_in_device_viewport);
226 LayerTreeImpl* active_tree() { return active_tree_.get(); }
227 const LayerTreeImpl* active_tree() const { return active_tree_.get(); }
228 LayerTreeImpl* pending_tree() { return pending_tree_.get(); }
229 const LayerTreeImpl* pending_tree() const { return pending_tree_.get(); }
230 const LayerTreeImpl* recycle_tree() const { return recycle_tree_.get(); }
231 void CreatePendingTree();
232 void CheckForCompletedTileUploads();
233 virtual bool ActivatePendingTreeIfNeeded();
235 // Shortcuts to layers on the active tree.
236 LayerImpl* RootLayer() const;
237 LayerImpl* RootScrollLayer() const;
238 LayerImpl* CurrentlyScrollingLayer() const;
240 virtual void SetVisible(bool visible);
241 bool visible() const { return visible_; }
243 void SetNeedsCommit() { client_->SetNeedsCommitOnImplThread(); }
244 void SetNeedsRedraw() { client_->SetNeedsRedrawOnImplThread(); }
246 size_t memory_allocation_limit_bytes() const {
247 return managed_memory_policy_.bytes_limit_when_visible;
250 void SetViewportSize(gfx::Size device_viewport_size);
251 gfx::Size device_viewport_size() const { return device_viewport_size_; }
253 void SetOverdrawBottomHeight(float overdraw_bottom_height);
254 float overdraw_bottom_height() const { return overdraw_bottom_height_; }
256 void SetDeviceScaleFactor(float device_scale_factor);
257 float device_scale_factor() const { return device_scale_factor_; }
259 scoped_ptr<ScrollAndScaleSet> ProcessScrollDeltas();
261 bool needs_animate_layers() const {
262 return !animation_registrar_->active_animation_controllers().empty();
265 void SendManagedMemoryStats(
266 size_t memory_visible_bytes,
267 size_t memory_visible_and_nearby_bytes,
268 size_t memory_use_bytes);
270 void set_max_memory_needed_bytes(size_t bytes) {
271 max_memory_needed_bytes_ = bytes;
274 FrameRateCounter* fps_counter() {
275 return fps_counter_.get();
277 PaintTimeCounter* paint_time_counter() {
278 return paint_time_counter_.get();
280 MemoryHistory* memory_history() {
281 return memory_history_.get();
283 DebugRectHistory* debug_rect_history() {
284 return debug_rect_history_.get();
286 ResourceProvider* resource_provider() {
287 return resource_provider_.get();
289 TopControlsManager* top_controls_manager() {
290 return top_controls_manager_.get();
293 Proxy* proxy() const { return proxy_; }
295 AnimationRegistrar* animation_registrar() const {
296 return animation_registrar_.get();
299 void SetDebugState(const LayerTreeDebugState& debug_state);
300 const LayerTreeDebugState& debug_state() const { return debug_state_; }
302 class CC_EXPORT CullRenderPassesWithCachedTextures {
303 public:
304 bool ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
305 const FrameData& frame) const;
307 // Iterates from the root first, in order to remove the surfaces closest
308 // to the root with cached textures, and all surfaces that draw into
309 // them.
310 size_t RenderPassListBegin(const RenderPassList& list) const {
311 return list.size() - 1;
313 size_t RenderPassListEnd(const RenderPassList& list) const { return 0 - 1; }
314 size_t RenderPassListNext(size_t it) const { return it - 1; }
316 explicit CullRenderPassesWithCachedTextures(Renderer* renderer)
317 : renderer_(renderer) {}
318 private:
319 Renderer* renderer_;
322 class CC_EXPORT CullRenderPassesWithNoQuads {
323 public:
324 bool ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
325 const FrameData& frame) const;
327 // Iterates in draw order, so that when a surface is removed, and its
328 // target becomes empty, then its target can be removed also.
329 size_t RenderPassListBegin(const RenderPassList& list) const { return 0; }
330 size_t RenderPassListEnd(const RenderPassList& list) const {
331 return list.size();
333 size_t RenderPassListNext(size_t it) const { return it + 1; }
336 template <typename RenderPassCuller>
337 static void RemoveRenderPasses(RenderPassCuller culler, FrameData* frame);
339 skia::RefPtr<SkPicture> CapturePicture();
341 bool pinch_gesture_active() const { return pinch_gesture_active_; }
343 void SetTreePriority(TreePriority priority);
345 void BeginNextFrame();
346 base::TimeTicks CurrentFrameTimeTicks();
347 base::Time CurrentFrameTime();
349 scoped_ptr<base::Value> AsValue() const;
350 scoped_ptr<base::Value> ActivationStateAsValue() const;
351 scoped_ptr<base::Value> FrameStateAsValue() const;
353 bool page_scale_animation_active() const { return !!page_scale_animation_; }
355 protected:
356 LayerTreeHostImpl(
357 const LayerTreeSettings& settings,
358 LayerTreeHostImplClient* client,
359 Proxy* proxy,
360 RenderingStatsInstrumentation* rendering_stats_instrumentation);
361 void ActivatePendingTree();
363 // Virtual for testing.
364 virtual void AnimateLayers(base::TimeTicks monotonic_time,
365 base::Time wall_clock_time);
367 // Virtual for testing.
368 virtual base::TimeDelta LowFrequencyAnimationInterval() const;
370 const AnimationRegistrar::AnimationControllerMap&
371 active_animation_controllers() const {
372 return animation_registrar_->active_animation_controllers();
375 LayerTreeHostImplClient* client_;
376 Proxy* proxy_;
378 private:
379 void AnimatePageScale(base::TimeTicks monotonic_time);
380 void AnimateScrollbars(base::TimeTicks monotonic_time);
381 void AnimateTopControls(base::TimeTicks monotonic_time);
383 gfx::Vector2dF ScrollLayerWithViewportSpaceDelta(
384 LayerImpl* layer_impl,
385 float scale_from_viewport_to_screen_space,
386 gfx::PointF viewport_point,
387 gfx::Vector2dF viewport_delta);
389 void UpdateMaxScrollOffset();
390 void TrackDamageForAllSurfaces(
391 LayerImpl* root_draw_layer,
392 const LayerImplList& render_surface_layer_list);
394 // Returns false if the frame should not be displayed. This function should
395 // only be called from PrepareToDraw, as DidDrawAllLayers must be called
396 // if this helper function is called.
397 bool CalculateRenderPasses(FrameData* frame);
399 void SendDidLoseOutputSurfaceRecursive(LayerImpl* current);
400 void ClearRenderSurfaces();
401 bool EnsureRenderSurfaceLayerList();
402 void ClearCurrentlyScrollingLayer();
404 void AnimateScrollbarsRecursive(LayerImpl* layer,
405 base::TimeTicks time);
407 void DumpRenderSurfaces(std::string* str,
408 int indent,
409 const LayerImpl* layer) const;
411 static LayerImpl* GetNonCompositedContentLayerRecursive(LayerImpl* layer);
413 void StartScrollbarAnimationRecursive(LayerImpl* layer, base::TimeTicks time);
415 scoped_ptr<OutputSurface> output_surface_;
416 scoped_ptr<ResourceProvider> resource_provider_;
417 scoped_ptr<Renderer> renderer_;
418 scoped_ptr<TileManager> tile_manager_;
420 // Tree currently being drawn.
421 scoped_ptr<LayerTreeImpl> active_tree_;
423 // In impl-side painting mode, tree with possibly incomplete rasterized
424 // content. May be promoted to active by ActivatePendingTreeIfNeeded().
425 scoped_ptr<LayerTreeImpl> pending_tree_;
427 // In impl-side painting mode, inert tree with layers that can be recycled
428 // by the next sync from the main thread.
429 scoped_ptr<LayerTreeImpl> recycle_tree_;
431 bool did_lock_scrolling_layer_;
432 bool should_bubble_scrolls_;
433 bool wheel_scrolling_;
434 LayerTreeSettings settings_;
435 LayerTreeDebugState debug_state_;
436 gfx::Size device_viewport_size_;
437 float overdraw_bottom_height_;
438 float device_scale_factor_;
439 bool visible_;
440 ManagedMemoryPolicy managed_memory_policy_;
442 bool pinch_gesture_active_;
443 gfx::Point previous_pinch_anchor_;
445 // This is set by AnimateLayers() and used by UpdateAnimationState()
446 // when sending animation events to the main thread.
447 base::Time last_animation_time_;
449 scoped_ptr<TopControlsManager> top_controls_manager_;
451 scoped_ptr<PageScaleAnimation> page_scale_animation_;
453 // This is used for ticking animations slowly when hidden.
454 scoped_ptr<LayerTreeHostImplTimeSourceAdapter> time_source_client_adapter_;
456 scoped_ptr<FrameRateCounter> fps_counter_;
457 scoped_ptr<PaintTimeCounter> paint_time_counter_;
458 scoped_ptr<MemoryHistory> memory_history_;
459 scoped_ptr<DebugRectHistory> debug_rect_history_;
461 // The maximum memory that would be used by the prioritized resource
462 // manager, if there were no limit on memory usage.
463 size_t max_memory_needed_bytes_;
465 size_t last_sent_memory_visible_bytes_;
466 size_t last_sent_memory_visible_and_nearby_bytes_;
467 size_t last_sent_memory_use_bytes_;
469 gfx::Rect viewport_damage_rect_;
471 base::TimeTicks current_frame_timeticks_;
472 base::Time current_frame_time_;
474 scoped_ptr<AnimationRegistrar> animation_registrar_;
476 RenderingStatsInstrumentation* rendering_stats_instrumentation_;
478 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImpl);
481 } // namespace cc
483 #endif // CC_TREES_LAYER_TREE_HOST_IMPL_H_