Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / gpu / render_widget_compositor.h
blob185ac08e7f764bc9a278237fbfae39ac0a86fe62
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 class 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 GetSourceFrameNumber() const;
70 void SetNeedsUpdateLayers();
71 void SetNeedsCommit();
72 void NotifyInputThrottledUntilCommit();
73 const cc::Layer* GetRootLayer() const;
74 int ScheduleMicroBenchmark(
75 const std::string& name,
76 scoped_ptr<base::Value> value,
77 const base::Callback<void(scoped_ptr<base::Value>)>& callback);
78 bool SendMessageToMicroBenchmark(int id, scoped_ptr<base::Value> value);
79 void StartCompositor();
80 void SetSurfaceIdNamespace(uint32_t surface_id_namespace);
82 // WebLayerTreeView implementation.
83 virtual void setRootLayer(const blink::WebLayer& layer);
84 virtual void clearRootLayer();
85 virtual void attachCompositorAnimationTimeline(
86 blink::WebCompositorAnimationTimeline* compositor_timeline);
87 virtual void detachCompositorAnimationTimeline(
88 blink::WebCompositorAnimationTimeline* compositor_timeline);
89 virtual void setViewportSize(
90 const blink::WebSize& unused_deprecated,
91 const blink::WebSize& device_viewport_size);
92 virtual void setViewportSize(const blink::WebSize& device_viewport_size);
93 virtual blink::WebSize layoutViewportSize() const;
94 virtual blink::WebSize deviceViewportSize() const;
95 virtual blink::WebFloatPoint adjustEventPointForPinchZoom(
96 const blink::WebFloatPoint& point) const;
97 virtual void setDeviceScaleFactor(float device_scale);
98 virtual float deviceScaleFactor() const;
99 virtual void setBackgroundColor(blink::WebColor color);
100 virtual void setHasTransparentBackground(bool transparent);
101 virtual void setVisible(bool visible);
102 virtual void setPageScaleFactorAndLimits(float page_scale_factor,
103 float minimum,
104 float maximum);
105 virtual void startPageScaleAnimation(const blink::WebPoint& destination,
106 bool use_anchor,
107 float new_page_scale,
108 double duration_sec);
109 virtual void heuristicsForGpuRasterizationUpdated(bool matches_heuristics);
110 virtual void setNeedsAnimate();
111 virtual void didStopFlinging();
112 virtual void layoutAndPaintAsync(
113 blink::WebLayoutAndPaintAsyncCallback* callback);
114 virtual void compositeAndReadbackAsync(
115 blink::WebCompositeAndReadbackAsyncCallback* callback);
116 virtual void finishAllRendering();
117 virtual void setDeferCommits(bool defer_commits);
118 virtual void registerForAnimations(blink::WebLayer* layer);
119 virtual void registerViewportLayers(
120 const blink::WebLayer* overscrollElasticityLayer,
121 const blink::WebLayer* pageScaleLayer,
122 const blink::WebLayer* innerViewportScrollLayer,
123 const blink::WebLayer* outerViewportScrollLayer) override;
124 virtual void clearViewportLayers() override;
125 virtual void registerSelection(const blink::WebSelection& selection) override;
126 virtual void clearSelection() override;
127 virtual int layerTreeId() const;
128 virtual void setShowFPSCounter(bool show);
129 virtual void setShowPaintRects(bool show);
130 virtual void setShowDebugBorders(bool show);
131 virtual void setContinuousPaintingEnabled(bool enabled);
132 virtual void setShowScrollBottleneckRects(bool show);
134 virtual void updateTopControlsState(blink::WebTopControlsState constraints,
135 blink::WebTopControlsState current,
136 bool animate);
137 virtual void setTopControlsHeight(float height, bool shrink);
138 virtual void setTopControlsShownRatio(float);
139 virtual void setHidePinchScrollbarsNearMinScale(bool) override;
141 // cc::LayerTreeHostClient implementation.
142 void WillBeginMainFrame() override;
143 void DidBeginMainFrame() override;
144 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
145 void BeginMainFrameNotExpectedSoon() override;
146 void Layout() override;
147 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
148 const gfx::Vector2dF& outer_delta,
149 const gfx::Vector2dF& elastic_overscroll_delta,
150 float page_scale,
151 float top_controls_delta) override;
152 void RequestNewOutputSurface() override;
153 void DidInitializeOutputSurface() override;
154 void DidFailToInitializeOutputSurface() override;
155 void WillCommit() override;
156 void DidCommit() override;
157 void DidCommitAndDrawFrame() override;
158 void DidCompleteSwapBuffers() override;
159 void DidCompletePageScaleAnimation() override;
160 void RateLimitSharedMainThreadContext() override;
161 void RecordFrameTimingEvents(
162 scoped_ptr<cc::FrameTimingTracker::CompositeTimingSet> composite_events,
163 scoped_ptr<cc::FrameTimingTracker::MainFrameTimingSet> main_frame_events)
164 override;
166 // cc::LayerTreeHostSingleThreadClient implementation.
167 void ScheduleAnimation() override;
168 void DidPostSwapBuffers() override;
169 void DidAbortSwapBuffers() override;
171 enum {
172 OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK = 4,
173 MAX_OUTPUT_SURFACE_RETRIES = 5,
176 protected:
177 RenderWidgetCompositor(RenderWidget* widget,
178 CompositorDependencies* compositor_deps);
180 void Initialize();
181 cc::LayerTreeHost* layer_tree_host() { return layer_tree_host_.get(); }
183 private:
184 void LayoutAndUpdateLayers();
185 void InvokeLayoutAndPaintCallback();
186 bool CompositeIsSynchronous() const;
187 void SynchronouslyComposite();
189 int num_failed_recreate_attempts_;
190 RenderWidget* widget_;
191 CompositorDependencies* compositor_deps_;
192 scoped_ptr<cc::LayerTreeHost> layer_tree_host_;
194 blink::WebLayoutAndPaintAsyncCallback* layout_and_paint_async_callback_;
195 scoped_ptr<cc::CopyOutputRequest> temporary_copy_output_request_;
197 base::WeakPtrFactory<RenderWidgetCompositor> weak_factory_;
200 } // namespace content
202 #endif // CONTENT_RENDERER_GPU_RENDER_WIDGET_COMPOSITOR_H_