Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / compositor / compositor.h
blob5a6d93290350c5f67fc0035804c3478803d49cc0
1 // Copyright (c) 2012 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 UI_COMPOSITOR_COMPOSITOR_H_
6 #define UI_COMPOSITOR_COMPOSITOR_H_
8 #include <list>
9 #include <string>
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/time/time.h"
17 #include "cc/output/begin_frame_args.h"
18 #include "cc/surfaces/surface_sequence.h"
19 #include "cc/trees/layer_tree_host_client.h"
20 #include "cc/trees/layer_tree_host_single_thread_client.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/compositor/compositor_animation_observer.h"
23 #include "ui/compositor/compositor_export.h"
24 #include "ui/compositor/compositor_observer.h"
25 #include "ui/compositor/layer_animator_collection.h"
26 #include "ui/gfx/geometry/size.h"
27 #include "ui/gfx/geometry/vector2d.h"
28 #include "ui/gfx/native_widget_types.h"
30 namespace base {
31 class MessageLoopProxy;
32 class RunLoop;
35 namespace cc {
36 class ContextProvider;
37 class Layer;
38 class LayerTreeDebugState;
39 class LayerTreeHost;
40 class RendererSettings;
41 class SharedBitmapManager;
42 class SurfaceIdAllocator;
43 class TaskGraphRunner;
46 namespace gfx {
47 class Rect;
48 class Size;
51 namespace gpu {
52 class GpuMemoryBufferManager;
53 struct Mailbox;
56 namespace ui {
58 class Compositor;
59 class CompositorVSyncManager;
60 class Layer;
61 class Reflector;
62 class Texture;
63 struct LatencyInfo;
65 // This class abstracts the creation of the 3D context for the compositor. It is
66 // a global object.
67 class COMPOSITOR_EXPORT ContextFactory {
68 public:
69 virtual ~ContextFactory() {}
71 // Creates an output surface for the given compositor. The factory may keep
72 // per-compositor data (e.g. a shared context), that needs to be cleaned up
73 // by calling RemoveCompositor when the compositor gets destroyed.
74 virtual void CreateOutputSurface(base::WeakPtr<Compositor> compositor) = 0;
76 // Creates a reflector that copies the content of the |mirrored_compositor|
77 // onto |mirroring_layer|.
78 virtual scoped_ptr<Reflector> CreateReflector(Compositor* mirrored_compositor,
79 Layer* mirroring_layer) = 0;
80 // Removes the reflector, which stops the mirroring.
81 virtual void RemoveReflector(Reflector* reflector) = 0;
83 // Return a reference to a shared offscreen context provider usable from the
84 // main thread.
85 virtual scoped_refptr<cc::ContextProvider>
86 SharedMainThreadContextProvider() = 0;
88 // Destroys per-compositor data.
89 virtual void RemoveCompositor(Compositor* compositor) = 0;
91 // When true, the factory uses test contexts that do not do real GL
92 // operations.
93 virtual bool DoesCreateTestContexts() = 0;
95 // Returns the OpenGL target to use for image textures.
96 virtual uint32 GetImageTextureTarget() = 0;
98 // Gets the shared bitmap manager for software mode.
99 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
101 // Gets the GPU memory buffer manager.
102 virtual gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0;
104 // Gets the task graph runner.
105 virtual cc::TaskGraphRunner* GetTaskGraphRunner() = 0;
107 // Creates a Surface ID allocator with a new namespace.
108 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 0;
110 // Resize the display corresponding to this compositor to a particular size.
111 virtual void ResizeDisplay(ui::Compositor* compositor,
112 const gfx::Size& size) = 0;
115 // This class represents a lock on the compositor, that can be used to prevent
116 // commits to the compositor tree while we're waiting for an asynchronous
117 // event. The typical use case is when waiting for a renderer to produce a frame
118 // at the right size. The caller keeps a reference on this object, and drops the
119 // reference once it desires to release the lock.
120 // By default, the lock will be cancelled after a short timeout to ensure
121 // responsiveness of the UI, so the compositor tree should be kept in a
122 // "reasonable" state while the lock is held. If the compositor sets
123 // locks to not time out, then the lock will remain in effect until destroyed.
124 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
125 class COMPOSITOR_EXPORT CompositorLock
126 : public base::RefCounted<CompositorLock>,
127 public base::SupportsWeakPtr<CompositorLock> {
128 private:
129 friend class base::RefCounted<CompositorLock>;
130 friend class Compositor;
132 explicit CompositorLock(Compositor* compositor);
133 ~CompositorLock();
135 void CancelLock();
137 Compositor* compositor_;
138 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
141 // This class observes BeginFrame notification from LayerTreeHost.
142 class COMPOSITOR_EXPORT CompositorBeginFrameObserver {
143 public:
144 virtual ~CompositorBeginFrameObserver() {}
145 virtual void OnSendBeginFrame(const cc::BeginFrameArgs& args) = 0;
148 // Compositor object to take care of GPU painting.
149 // A Browser compositor object is responsible for generating the final
150 // displayable form of pixels comprising a single widget's contents. It draws an
151 // appropriately transformed texture for each transformed view in the widget's
152 // view hierarchy.
153 class COMPOSITOR_EXPORT Compositor
154 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
155 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
156 public:
157 Compositor(gfx::AcceleratedWidget widget,
158 ui::ContextFactory* context_factory,
159 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
160 ~Compositor() override;
162 ui::ContextFactory* context_factory() { return context_factory_; }
164 void SetOutputSurface(scoped_ptr<cc::OutputSurface> surface);
166 // Schedules a redraw of the layer tree associated with this compositor.
167 void ScheduleDraw();
169 // Sets the root of the layer tree drawn by this Compositor. The root layer
170 // must have no parent. The compositor's root layer is reset if the root layer
171 // is destroyed. NULL can be passed to reset the root layer, in which case the
172 // compositor will stop drawing anything.
173 // The Compositor does not own the root layer.
174 const Layer* root_layer() const { return root_layer_; }
175 Layer* root_layer() { return root_layer_; }
176 void SetRootLayer(Layer* root_layer);
178 // Called when we need the compositor to preserve the alpha channel in the
179 // output for situations when we want to render transparently atop something
180 // else, e.g. Aero glass.
181 void SetHostHasTransparentBackground(bool host_has_transparent_background);
183 // The scale factor of the device that this compositor is
184 // compositing layers on.
185 float device_scale_factor() const { return device_scale_factor_; }
187 // Where possible, draws are scissored to a damage region calculated from
188 // changes to layer properties. This bypasses that and indicates that
189 // the whole frame needs to be drawn.
190 void ScheduleFullRedraw();
192 // Schedule redraw and append damage_rect to the damage region calculated
193 // from changes to layer properties.
194 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
196 // Finishes all outstanding rendering and disables swapping on this surface.
197 void FinishAllRendering();
199 // Finishes all outstanding rendering and disables swapping on this surface
200 // until it is resized.
201 void DisableSwapUntilResize();
203 void SetLatencyInfo(const LatencyInfo& latency_info);
205 // Sets the compositor's device scale factor and size.
206 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
208 // Returns the size of the widget that is being drawn to in pixel coordinates.
209 const gfx::Size& size() const { return size_; }
211 // Sets the background color used for areas that aren't covered by
212 // the |root_layer|.
213 void SetBackgroundColor(SkColor color);
215 // Sets the visibility of the underlying compositor.
216 void SetVisible(bool visible);
218 // Gets the visibility of the underlying compositor.
219 bool IsVisible();
221 // Returns the widget for this compositor.
222 gfx::AcceleratedWidget widget() const { return widget_; }
224 // Returns the vsync manager for this compositor.
225 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
227 // Returns the main thread task runner this compositor uses. Users of the
228 // compositor generally shouldn't use this.
229 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
230 return task_runner_;
233 // Compositor does not own observers. It is the responsibility of the
234 // observer to remove itself when it is done observing.
235 void AddObserver(CompositorObserver* observer);
236 void RemoveObserver(CompositorObserver* observer);
237 bool HasObserver(const CompositorObserver* observer) const;
239 void AddAnimationObserver(CompositorAnimationObserver* observer);
240 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
241 bool HasAnimationObserver(const CompositorAnimationObserver* observer) const;
243 void AddBeginFrameObserver(CompositorBeginFrameObserver* observer);
244 void RemoveBeginFrameObserver(CompositorBeginFrameObserver* observer);
246 // Change the timeout behavior for all future locks that are created. Locks
247 // should time out if there is an expectation that the compositor will be
248 // responsive.
249 void SetLocksWillTimeOut(bool locks_will_time_out) {
250 locks_will_time_out_ = locks_will_time_out;
253 // Creates a compositor lock. Returns NULL if it is not possible to lock at
254 // this time (i.e. we're waiting to complete a previous unlock).
255 scoped_refptr<CompositorLock> GetCompositorLock();
257 // Internal functions, called back by command-buffer contexts on swap buffer
258 // events.
260 // Signals swap has been posted.
261 void OnSwapBuffersPosted();
263 // Signals swap has completed.
264 void OnSwapBuffersComplete();
266 // Signals swap has aborted (e.g. lost context).
267 void OnSwapBuffersAborted();
269 // LayerTreeHostClient implementation.
270 void WillBeginMainFrame() override {}
271 void DidBeginMainFrame() override {}
272 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
273 void BeginMainFrameNotExpectedSoon() override;
274 void Layout() override;
275 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
276 const gfx::Vector2dF& outer_delta,
277 const gfx::Vector2dF& elastic_overscroll_delta,
278 float page_scale,
279 float top_controls_delta) override {}
280 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
281 float page_scale,
282 float top_controls_delta) override {}
283 void RequestNewOutputSurface() override;
284 void DidInitializeOutputSurface() override;
285 void DidFailToInitializeOutputSurface() override;
286 void WillCommit() override {}
287 void DidCommit() override;
288 void DidCommitAndDrawFrame() override;
289 void DidCompleteSwapBuffers() override;
290 void DidCompletePageScaleAnimation() override {}
291 void SendBeginFramesToChildren(const cc::BeginFrameArgs& args) override;
293 // cc::LayerTreeHostSingleThreadClient implementation.
294 void DidPostSwapBuffers() override;
295 void DidAbortSwapBuffers() override;
297 bool IsLocked() { return compositor_lock_ != NULL; }
299 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
300 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
301 const cc::RendererSettings& GetRendererSettings() const;
303 LayerAnimatorCollection* layer_animator_collection() {
304 return &layer_animator_collection_;
307 cc::SurfaceIdAllocator* surface_id_allocator() {
308 return surface_id_allocator_.get();
311 private:
312 friend class base::RefCounted<Compositor>;
313 friend class CompositorLock;
315 // Called by CompositorLock.
316 void UnlockCompositor();
318 // Called to release any pending CompositorLock
319 void CancelCompositorLock();
321 gfx::Size size_;
323 ui::ContextFactory* context_factory_;
325 // The root of the Layer tree drawn by this compositor.
326 Layer* root_layer_;
328 ObserverList<CompositorObserver, true> observer_list_;
329 ObserverList<CompositorAnimationObserver> animation_observer_list_;
330 std::list<CompositorBeginFrameObserver*> begin_frame_observer_list_;
332 gfx::AcceleratedWidget widget_;
333 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
334 scoped_refptr<cc::Layer> root_web_layer_;
335 scoped_ptr<cc::LayerTreeHost> host_;
336 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
338 // The manager of vsync parameters for this compositor.
339 scoped_refptr<CompositorVSyncManager> vsync_manager_;
341 // The device scale factor of the monitor that this compositor is compositing
342 // layers on.
343 float device_scale_factor_;
345 int last_started_frame_;
346 int last_ended_frame_;
348 bool locks_will_time_out_;
349 CompositorLock* compositor_lock_;
351 LayerAnimatorCollection layer_animator_collection_;
353 // Used to send to any new CompositorBeginFrameObserver immediately.
354 cc::BeginFrameArgs missed_begin_frame_args_;
356 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
358 DISALLOW_COPY_AND_ASSIGN(Compositor);
361 } // namespace ui
363 #endif // UI_COMPOSITOR_COMPOSITOR_H_