Make |track_| in MediaStreamTrack const. and a couple of other cosmetic changes.
[chromium-blink-merge.git] / ui / compositor / compositor.h
blobfb297685414e04402b453b92522789345f8852f8
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/native_widget_types.h"
25 #include "ui/gfx/size.h"
26 #include "ui/gfx/vector2d.h"
28 class SkBitmap;
30 namespace base {
31 class MessageLoopProxy;
32 class RunLoop;
35 namespace cc {
36 class ContextProvider;
37 class GpuMemoryBufferManager;
38 class Layer;
39 class LayerTreeDebugState;
40 class LayerTreeHost;
41 class SharedBitmapManager;
42 class SurfaceIdAllocator;
45 namespace gfx {
46 class Rect;
47 class Size;
50 namespace gpu {
51 struct Mailbox;
54 namespace ui {
56 class Compositor;
57 class CompositorVSyncManager;
58 class Layer;
59 class Reflector;
60 class Texture;
61 struct LatencyInfo;
63 // This class abstracts the creation of the 3D context for the compositor. It is
64 // a global object.
65 class COMPOSITOR_EXPORT ContextFactory {
66 public:
67 virtual ~ContextFactory() {}
69 // Creates an output surface for the given compositor. The factory may keep
70 // per-compositor data (e.g. a shared context), that needs to be cleaned up
71 // by calling RemoveCompositor when the compositor gets destroyed.
72 virtual void CreateOutputSurface(base::WeakPtr<Compositor> compositor,
73 bool software_fallback) = 0;
75 // Creates a reflector that copies the content of the |mirrored_compositor|
76 // onto |mirroing_layer|.
77 virtual scoped_refptr<Reflector> CreateReflector(
78 Compositor* mirrored_compositor,
79 Layer* mirroring_layer) = 0;
80 // Removes the reflector, which stops the mirroring.
81 virtual void RemoveReflector(scoped_refptr<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 // Gets the shared bitmap manager for software mode.
96 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
98 // Gets the GPU memory buffer manager.
99 virtual cc::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0;
101 // Gets the compositor message loop, or NULL if not using threaded
102 // compositing.
103 virtual base::MessageLoopProxy* GetCompositorMessageLoop() = 0;
105 // Creates a Surface ID allocator with a new namespace.
106 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 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 // Draws the scene created by the layer tree and any visual effects.
174 void Draw();
176 // Where possible, draws are scissored to a damage region calculated from
177 // changes to layer properties. This bypasses that and indicates that
178 // the whole frame needs to be drawn.
179 void ScheduleFullRedraw();
181 // Schedule redraw and append damage_rect to the damage region calculated
182 // from changes to layer properties.
183 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
185 // Finishes all outstanding rendering on the GPU.
186 void FinishAllRendering();
188 void SetLatencyInfo(const LatencyInfo& latency_info);
190 // Sets the compositor's device scale factor and size.
191 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
193 // Returns the size of the widget that is being drawn to in pixel coordinates.
194 const gfx::Size& size() const { return size_; }
196 // Sets the background color used for areas that aren't covered by
197 // the |root_layer|.
198 void SetBackgroundColor(SkColor color);
200 // Set the visibility of the underlying compositor.
201 void SetVisible(bool visible);
203 // Returns the widget for this compositor.
204 gfx::AcceleratedWidget widget() const { return widget_; }
206 // Returns the vsync manager for this compositor.
207 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
209 // Returns the main thread task runner this compositor uses. Users of the
210 // compositor generally shouldn't use this.
211 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
212 return task_runner_;
215 // Compositor does not own observers. It is the responsibility of the
216 // observer to remove itself when it is done observing.
217 void AddObserver(CompositorObserver* observer);
218 void RemoveObserver(CompositorObserver* observer);
219 bool HasObserver(CompositorObserver* observer);
221 void AddAnimationObserver(CompositorAnimationObserver* observer);
222 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
223 bool HasAnimationObserver(CompositorAnimationObserver* observer);
225 // Creates a compositor lock. Returns NULL if it is not possible to lock at
226 // this time (i.e. we're waiting to complete a previous unlock).
227 scoped_refptr<CompositorLock> GetCompositorLock();
229 // Internal functions, called back by command-buffer contexts on swap buffer
230 // events.
232 // Signals swap has been posted.
233 void OnSwapBuffersPosted();
235 // Signals swap has completed.
236 void OnSwapBuffersComplete();
238 // Signals swap has aborted (e.g. lost context).
239 void OnSwapBuffersAborted();
241 // LayerTreeHostClient implementation.
242 void WillBeginMainFrame(int frame_id) override {}
243 void DidBeginMainFrame() override {}
244 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
245 void Layout() override;
246 void ApplyViewportDeltas(const gfx::Vector2d& inner_delta,
247 const gfx::Vector2d& outer_delta,
248 float page_scale,
249 float top_controls_delta) override {}
250 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
251 float page_scale,
252 float top_controls_delta) override {}
253 void RequestNewOutputSurface(bool fallback) override;
254 void DidInitializeOutputSurface() override {}
255 void WillCommit() override {}
256 void DidCommit() override;
257 void DidCommitAndDrawFrame() override;
258 void DidCompleteSwapBuffers() override;
260 // cc::LayerTreeHostSingleThreadClient implementation.
261 void ScheduleComposite() override;
262 void ScheduleAnimation() override;
263 void DidPostSwapBuffers() override;
264 void DidAbortSwapBuffers() override;
266 int last_started_frame() { return last_started_frame_; }
267 int last_ended_frame() { return last_ended_frame_; }
269 bool IsLocked() { return compositor_lock_ != NULL; }
271 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
272 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
274 LayerAnimatorCollection* layer_animator_collection() {
275 return &layer_animator_collection_;
278 // Inserts a SurfaceSequence that will be satisfied on the next frame this
279 // compositor commits and swaps.
280 cc::SurfaceSequence InsertSurfaceSequenceForNextFrame();
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 // Notifies the compositor that compositing is complete.
297 void NotifyEnd();
299 gfx::Size size_;
301 ui::ContextFactory* context_factory_;
303 // The root of the Layer tree drawn by this compositor.
304 Layer* root_layer_;
306 ObserverList<CompositorObserver> observer_list_;
307 ObserverList<CompositorAnimationObserver> animation_observer_list_;
309 gfx::AcceleratedWidget widget_;
310 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
311 uint32_t surface_sequence_number_;
312 scoped_refptr<cc::Layer> root_web_layer_;
313 scoped_ptr<cc::LayerTreeHost> host_;
314 scoped_refptr<base::MessageLoopProxy> compositor_thread_loop_;
315 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
317 // The manager of vsync parameters for this compositor.
318 scoped_refptr<CompositorVSyncManager> vsync_manager_;
320 // The device scale factor of the monitor that this compositor is compositing
321 // layers on.
322 float device_scale_factor_;
324 int last_started_frame_;
325 int last_ended_frame_;
327 bool disable_schedule_composite_;
329 CompositorLock* compositor_lock_;
331 // Prevent more than one draw from being scheduled.
332 bool defer_draw_scheduling_;
334 // Used to prevent Draw()s while a composite is in progress.
335 bool waiting_on_compositing_end_;
336 bool draw_on_compositing_end_;
337 enum SwapState { SWAP_NONE, SWAP_POSTED, SWAP_COMPLETED };
338 SwapState swap_state_;
340 LayerAnimatorCollection layer_animator_collection_;
342 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
344 DISALLOW_COPY_AND_ASSIGN(Compositor);
347 } // namespace ui
349 #endif // UI_COMPOSITOR_COMPOSITOR_H_