x11: Use scoped_ptr<> for X11 objects where it makes sense.
[chromium-blink-merge.git] / ui / compositor / compositor.h
blob47d4cd0d7e30866f980299191cd5994d386673b9
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 <string>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/time/time.h"
16 #include "cc/surfaces/surface_sequence.h"
17 #include "cc/trees/layer_tree_host_client.h"
18 #include "cc/trees/layer_tree_host_single_thread_client.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "ui/compositor/compositor_animation_observer.h"
21 #include "ui/compositor/compositor_export.h"
22 #include "ui/compositor/compositor_observer.h"
23 #include "ui/compositor/layer_animator_collection.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gfx/geometry/vector2d.h"
26 #include "ui/gfx/native_widget_types.h"
28 namespace base {
29 class MessageLoopProxy;
30 class RunLoop;
33 namespace cc {
34 class ContextProvider;
35 class Layer;
36 class LayerTreeDebugState;
37 class LayerTreeHost;
38 class RendererSettings;
39 class SharedBitmapManager;
40 class SurfaceIdAllocator;
43 namespace gfx {
44 class Rect;
45 class Size;
48 namespace gpu {
49 class GpuMemoryBufferManager;
50 struct Mailbox;
53 namespace ui {
55 class Compositor;
56 class CompositorVSyncManager;
57 class Layer;
58 class Reflector;
59 class Texture;
60 struct LatencyInfo;
62 // This class abstracts the creation of the 3D context for the compositor. It is
63 // a global object.
64 class COMPOSITOR_EXPORT ContextFactory {
65 public:
66 virtual ~ContextFactory() {}
68 // Creates an output surface for the given compositor. The factory may keep
69 // per-compositor data (e.g. a shared context), that needs to be cleaned up
70 // by calling RemoveCompositor when the compositor gets destroyed.
71 virtual void CreateOutputSurface(base::WeakPtr<Compositor> compositor) = 0;
73 // Creates a reflector that copies the content of the |mirrored_compositor|
74 // onto |mirroring_layer|.
75 virtual scoped_ptr<Reflector> CreateReflector(Compositor* mirrored_compositor,
76 Layer* mirroring_layer) = 0;
77 // Removes the reflector, which stops the mirroring.
78 virtual void RemoveReflector(Reflector* reflector) = 0;
80 // Return a reference to a shared offscreen context provider usable from the
81 // main thread.
82 virtual scoped_refptr<cc::ContextProvider>
83 SharedMainThreadContextProvider() = 0;
85 // Destroys per-compositor data.
86 virtual void RemoveCompositor(Compositor* compositor) = 0;
88 // When true, the factory uses test contexts that do not do real GL
89 // operations.
90 virtual bool DoesCreateTestContexts() = 0;
92 // Returns the OpenGL target to use for image textures.
93 virtual uint32 GetImageTextureTarget() = 0;
95 // Gets the shared bitmap manager for software mode.
96 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
98 // Gets the GPU memory buffer manager.
99 virtual gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0;
101 // Creates a Surface ID allocator with a new namespace.
102 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 0;
104 // Resize the display corresponding to this compositor to a particular size.
105 virtual void ResizeDisplay(ui::Compositor* compositor,
106 const gfx::Size& size) = 0;
109 // This class represents a lock on the compositor, that can be used to prevent
110 // commits to the compositor tree while we're waiting for an asynchronous
111 // event. The typical use case is when waiting for a renderer to produce a frame
112 // at the right size. The caller keeps a reference on this object, and drops the
113 // reference once it desires to release the lock.
114 // Note however that the lock is cancelled after a short timeout to ensure
115 // responsiveness of the UI, so the compositor tree should be kept in a
116 // "reasonable" state while the lock is held.
117 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
118 class COMPOSITOR_EXPORT CompositorLock
119 : public base::RefCounted<CompositorLock>,
120 public base::SupportsWeakPtr<CompositorLock> {
121 private:
122 friend class base::RefCounted<CompositorLock>;
123 friend class Compositor;
125 explicit CompositorLock(Compositor* compositor);
126 ~CompositorLock();
128 void CancelLock();
130 Compositor* compositor_;
131 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
134 // Compositor object to take care of GPU painting.
135 // A Browser compositor object is responsible for generating the final
136 // displayable form of pixels comprising a single widget's contents. It draws an
137 // appropriately transformed texture for each transformed view in the widget's
138 // view hierarchy.
139 class COMPOSITOR_EXPORT Compositor
140 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
141 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
142 public:
143 Compositor(gfx::AcceleratedWidget widget,
144 ui::ContextFactory* context_factory,
145 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
146 ~Compositor() override;
148 ui::ContextFactory* context_factory() { return context_factory_; }
150 void SetOutputSurface(scoped_ptr<cc::OutputSurface> surface);
152 // Schedules a redraw of the layer tree associated with this compositor.
153 void ScheduleDraw();
155 // Sets the root of the layer tree drawn by this Compositor. The root layer
156 // must have no parent. The compositor's root layer is reset if the root layer
157 // is destroyed. NULL can be passed to reset the root layer, in which case the
158 // compositor will stop drawing anything.
159 // The Compositor does not own the root layer.
160 const Layer* root_layer() const { return root_layer_; }
161 Layer* root_layer() { return root_layer_; }
162 void SetRootLayer(Layer* root_layer);
164 // Called when we need the compositor to preserve the alpha channel in the
165 // output for situations when we want to render transparently atop something
166 // else, e.g. Aero glass.
167 void SetHostHasTransparentBackground(bool host_has_transparent_background);
169 // The scale factor of the device that this compositor is
170 // compositing layers on.
171 float device_scale_factor() const { return device_scale_factor_; }
173 // Where possible, draws are scissored to a damage region calculated from
174 // changes to layer properties. This bypasses that and indicates that
175 // the whole frame needs to be drawn.
176 void ScheduleFullRedraw();
178 // Schedule redraw and append damage_rect to the damage region calculated
179 // from changes to layer properties.
180 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
182 // Finishes all outstanding rendering and disables swapping on this surface.
183 void FinishAllRendering();
185 // Finishes all outstanding rendering and disables swapping on this surface
186 // until it is resized.
187 void DisableSwapUntilResize();
189 void SetLatencyInfo(const LatencyInfo& latency_info);
191 // Sets the compositor's device scale factor and size.
192 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
194 // Returns the size of the widget that is being drawn to in pixel coordinates.
195 const gfx::Size& size() const { return size_; }
197 // Sets the background color used for areas that aren't covered by
198 // the |root_layer|.
199 void SetBackgroundColor(SkColor color);
201 // Sets the visibility of the underlying compositor.
202 void SetVisible(bool visible);
204 // Gets the visibility of the underlying compositor.
205 bool IsVisible();
207 // Returns the widget for this compositor.
208 gfx::AcceleratedWidget widget() const { return widget_; }
210 // Returns the vsync manager for this compositor.
211 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
213 // Returns the main thread task runner this compositor uses. Users of the
214 // compositor generally shouldn't use this.
215 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
216 return task_runner_;
219 // Compositor does not own observers. It is the responsibility of the
220 // observer to remove itself when it is done observing.
221 void AddObserver(CompositorObserver* observer);
222 void RemoveObserver(CompositorObserver* observer);
223 bool HasObserver(const CompositorObserver* observer) const;
225 void AddAnimationObserver(CompositorAnimationObserver* observer);
226 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
227 bool HasAnimationObserver(const CompositorAnimationObserver* observer) const;
229 // Creates a compositor lock. Returns NULL if it is not possible to lock at
230 // this time (i.e. we're waiting to complete a previous unlock).
231 scoped_refptr<CompositorLock> GetCompositorLock();
233 // Internal functions, called back by command-buffer contexts on swap buffer
234 // events.
236 // Signals swap has been posted.
237 void OnSwapBuffersPosted();
239 // Signals swap has completed.
240 void OnSwapBuffersComplete();
242 // Signals swap has aborted (e.g. lost context).
243 void OnSwapBuffersAborted();
245 // LayerTreeHostClient implementation.
246 void WillBeginMainFrame() override {}
247 void DidBeginMainFrame() override {}
248 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
249 void BeginMainFrameNotExpectedSoon() override;
250 void Layout() override;
251 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
252 const gfx::Vector2dF& outer_delta,
253 const gfx::Vector2dF& elastic_overscroll_delta,
254 float page_scale,
255 float top_controls_delta) override {}
256 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
257 float page_scale,
258 float top_controls_delta) override {}
259 void RequestNewOutputSurface() override;
260 void DidInitializeOutputSurface() override;
261 void DidFailToInitializeOutputSurface() override;
262 void WillCommit() override {}
263 void DidCommit() override;
264 void DidCommitAndDrawFrame() override;
265 void DidCompleteSwapBuffers() override;
266 void DidCompletePageScaleAnimation() override {}
268 // cc::LayerTreeHostSingleThreadClient implementation.
269 void DidPostSwapBuffers() override;
270 void DidAbortSwapBuffers() override;
272 bool IsLocked() { return compositor_lock_ != NULL; }
274 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
275 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
276 const cc::RendererSettings& GetRendererSettings() const;
278 LayerAnimatorCollection* layer_animator_collection() {
279 return &layer_animator_collection_;
282 cc::SurfaceIdAllocator* surface_id_allocator() {
283 return surface_id_allocator_.get();
286 private:
287 friend class base::RefCounted<Compositor>;
288 friend class CompositorLock;
290 // Called by CompositorLock.
291 void UnlockCompositor();
293 // Called to release any pending CompositorLock
294 void CancelCompositorLock();
296 gfx::Size size_;
298 ui::ContextFactory* context_factory_;
300 // The root of the Layer tree drawn by this compositor.
301 Layer* root_layer_;
303 ObserverList<CompositorObserver, true> observer_list_;
304 ObserverList<CompositorAnimationObserver> animation_observer_list_;
306 gfx::AcceleratedWidget widget_;
307 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
308 scoped_refptr<cc::Layer> root_web_layer_;
309 scoped_ptr<cc::LayerTreeHost> host_;
310 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
312 // The manager of vsync parameters for this compositor.
313 scoped_refptr<CompositorVSyncManager> vsync_manager_;
315 // The device scale factor of the monitor that this compositor is compositing
316 // layers on.
317 float device_scale_factor_;
319 int last_started_frame_;
320 int last_ended_frame_;
322 bool disable_schedule_composite_;
324 CompositorLock* compositor_lock_;
326 LayerAnimatorCollection layer_animator_collection_;
328 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
330 DISALLOW_COPY_AND_ASSIGN(Compositor);
333 } // namespace ui
335 #endif // UI_COMPOSITOR_COMPOSITOR_H_