Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / gpu / render_widget_compositor.h
blobae79a3082fe511371d98cfe633ae1343d042e2f4
1 // Copyright (c) 2013 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 CONTENT_RENDERER_GPU_RENDER_WIDGET_COMPOSITOR_H_
6 #define CONTENT_RENDERER_GPU_RENDER_WIDGET_COMPOSITOR_H_
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "cc/input/top_controls_state.h"
13 #include "cc/output/swap_promise.h"
14 #include "cc/trees/layer_tree_host_client.h"
15 #include "cc/trees/layer_tree_host_single_thread_client.h"
16 #include "cc/trees/layer_tree_settings.h"
17 #include "cc/trees/swap_promise_monitor.h"
18 #include "content/common/content_export.h"
19 #include "content/renderer/gpu/compositor_dependencies.h"
20 #include "third_party/WebKit/public/platform/WebLayerTreeView.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/gfx/geometry/rect.h"
24 namespace ui {
25 struct LatencyInfo;
28 namespace cc {
29 class CopyOutputRequest;
30 class InputHandler;
31 class Layer;
32 class LayerTreeHost;
35 namespace content {
36 class RenderWidget;
38 class CONTENT_EXPORT RenderWidgetCompositor
39 : NON_EXPORTED_BASE(public blink::WebLayerTreeView),
40 NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
41 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
42 public:
43 // Attempt to construct and initialize a compositor instance for the widget
44 // with the given settings. Returns NULL if initialization fails.
45 static scoped_ptr<RenderWidgetCompositor> Create(
46 RenderWidget* widget,
47 CompositorDependencies* compositor_deps);
49 virtual ~RenderWidgetCompositor();
51 const base::WeakPtr<cc::InputHandler>& GetInputHandler();
52 bool BeginMainFrameRequested() const;
53 void SetNeedsDisplayOnAllLayers();
54 void SetRasterizeOnlyVisibleContent();
55 void SetNeedsRedrawRect(gfx::Rect damage_rect);
56 // Like setNeedsRedraw but forces the frame to be drawn, without early-outs.
57 // Redraw will be forced after the next commit
58 void SetNeedsForcedRedraw();
59 // Calling CreateLatencyInfoSwapPromiseMonitor() to get a scoped
60 // LatencyInfoSwapPromiseMonitor. During the life time of the
61 // LatencyInfoSwapPromiseMonitor, if SetNeedsCommit() or
62 // SetNeedsUpdateLayers() is called on LayerTreeHost, the original latency
63 // info will be turned into a LatencyInfoSwapPromise.
64 scoped_ptr<cc::SwapPromiseMonitor> CreateLatencyInfoSwapPromiseMonitor(
65 ui::LatencyInfo* latency);
66 // Calling QueueSwapPromise() to directly queue a SwapPromise into
67 // LayerTreeHost.
68 void QueueSwapPromise(scoped_ptr<cc::SwapPromise> swap_promise);
69 int GetLayerTreeId() const;
70 int GetSourceFrameNumber() const;
71 void SetNeedsUpdateLayers();
72 void SetNeedsCommit();
73 void NotifyInputThrottledUntilCommit();
74 const cc::Layer* GetRootLayer() const;
75 int ScheduleMicroBenchmark(
76 const std::string& name,
77 scoped_ptr<base::Value> value,
78 const base::Callback<void(scoped_ptr<base::Value>)>& callback);
79 bool SendMessageToMicroBenchmark(int id, scoped_ptr<base::Value> value);
80 void StartCompositor();
82 // WebLayerTreeView implementation.
83 virtual void setRootLayer(const blink::WebLayer& layer);
84 virtual void clearRootLayer();
85 virtual void setViewportSize(
86 const blink::WebSize& unused_deprecated,
87 const blink::WebSize& device_viewport_size);
88 virtual void setViewportSize(const blink::WebSize& device_viewport_size);
89 virtual blink::WebSize layoutViewportSize() const;
90 virtual blink::WebSize deviceViewportSize() const;
91 virtual blink::WebFloatPoint adjustEventPointForPinchZoom(
92 const blink::WebFloatPoint& point) const;
93 virtual void setDeviceScaleFactor(float device_scale);
94 virtual float deviceScaleFactor() const;
95 virtual void setBackgroundColor(blink::WebColor color);
96 virtual void setHasTransparentBackground(bool transparent);
97 virtual void setVisible(bool visible);
98 virtual void setPageScaleFactorAndLimits(float page_scale_factor,
99 float minimum,
100 float maximum);
101 virtual void startPageScaleAnimation(const blink::WebPoint& destination,
102 bool use_anchor,
103 float new_page_scale,
104 double duration_sec);
105 virtual void heuristicsForGpuRasterizationUpdated(bool matches_heuristics);
106 virtual void setNeedsAnimate();
107 virtual bool commitRequested() const;
108 virtual void didStopFlinging();
109 virtual void compositeAndReadbackAsync(
110 blink::WebCompositeAndReadbackAsyncCallback* callback);
111 virtual void finishAllRendering();
112 virtual void setDeferCommits(bool defer_commits);
113 virtual void registerForAnimations(blink::WebLayer* layer);
114 virtual void registerViewportLayers(
115 const blink::WebLayer* overscrollElasticityLayer,
116 const blink::WebLayer* pageScaleLayer,
117 const blink::WebLayer* innerViewportScrollLayer,
118 const blink::WebLayer* outerViewportScrollLayer) override;
119 virtual void clearViewportLayers() override;
120 virtual void registerSelection(const blink::WebSelectionBound& start,
121 const blink::WebSelectionBound& end) override;
122 virtual void clearSelection() override;
123 virtual void setShowFPSCounter(bool show);
124 virtual void setShowPaintRects(bool show);
125 virtual void setShowDebugBorders(bool show);
126 virtual void setContinuousPaintingEnabled(bool enabled);
127 virtual void setShowScrollBottleneckRects(bool show);
129 virtual void updateTopControlsState(blink::WebTopControlsState constraints,
130 blink::WebTopControlsState current,
131 bool animate);
132 virtual void setTopControlsHeight(float height, bool shrink);
133 virtual void setTopControlsShownRatio(float);
135 // cc::LayerTreeHostClient implementation.
136 void WillBeginMainFrame() override;
137 void DidBeginMainFrame() override;
138 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
139 void BeginMainFrameNotExpectedSoon() override;
140 void Layout() override;
141 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
142 const gfx::Vector2dF& outer_delta,
143 const gfx::Vector2dF& elastic_overscroll_delta,
144 float page_scale,
145 float top_controls_delta) override;
146 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
147 float page_scale,
148 float top_controls_delta) override;
149 void RequestNewOutputSurface() override;
150 void DidInitializeOutputSurface() override;
151 void DidFailToInitializeOutputSurface() override;
152 void WillCommit() override;
153 void DidCommit() override;
154 void DidCommitAndDrawFrame() override;
155 void DidCompleteSwapBuffers() override;
156 void DidCompletePageScaleAnimation() override;
157 void RateLimitSharedMainThreadContext() override;
159 // cc::LayerTreeHostSingleThreadClient implementation.
160 void ScheduleAnimation() override;
161 void DidPostSwapBuffers() override;
162 void DidAbortSwapBuffers() override;
164 enum {
165 OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK = 4,
166 MAX_OUTPUT_SURFACE_RETRIES = 5,
169 protected:
170 RenderWidgetCompositor(RenderWidget* widget,
171 CompositorDependencies* compositor_deps);
173 void Initialize();
174 cc::LayerTreeHost* layer_tree_host() { return layer_tree_host_.get(); }
176 private:
177 int num_failed_recreate_attempts_;
178 RenderWidget* widget_;
179 CompositorDependencies* compositor_deps_;
180 scoped_ptr<cc::LayerTreeHost> layer_tree_host_;
182 scoped_ptr<cc::CopyOutputRequest> temporary_copy_output_request_;
184 base::WeakPtrFactory<RenderWidgetCompositor> weak_factory_;
187 } // namespace content
189 #endif // CONTENT_RENDERER_GPU_RENDER_WIDGET_COMPOSITOR_H_