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_
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"
31 class MessageLoopProxy
;
36 class ContextProvider
;
38 class LayerTreeDebugState
;
40 class RendererSettings
;
41 class SharedBitmapManager
;
42 class SurfaceIdAllocator
;
51 class GpuMemoryBufferManager
;
58 class CompositorVSyncManager
;
64 // This class abstracts the creation of the 3D context for the compositor. It is
66 class COMPOSITOR_EXPORT ContextFactory
{
68 virtual ~ContextFactory() {}
70 // Creates an output surface for the given compositor. The factory may keep
71 // per-compositor data (e.g. a shared context), that needs to be cleaned up
72 // by calling RemoveCompositor when the compositor gets destroyed.
73 virtual void CreateOutputSurface(base::WeakPtr
<Compositor
> compositor
,
74 bool software_fallback
) = 0;
76 // Creates a reflector that copies the content of the |mirrored_compositor|
77 // onto |mirroing_layer|.
78 virtual scoped_refptr
<Reflector
> CreateReflector(
79 Compositor
* mirrored_compositor
,
80 Layer
* mirroring_layer
) = 0;
81 // Removes the reflector, which stops the mirroring.
82 virtual void RemoveReflector(scoped_refptr
<Reflector
> reflector
) = 0;
84 // Return a reference to a shared offscreen context provider usable from the
86 virtual scoped_refptr
<cc::ContextProvider
>
87 SharedMainThreadContextProvider() = 0;
89 // Destroys per-compositor data.
90 virtual void RemoveCompositor(Compositor
* compositor
) = 0;
92 // When true, the factory uses test contexts that do not do real GL
94 virtual bool DoesCreateTestContexts() = 0;
96 // Gets the shared bitmap manager for software mode.
97 virtual cc::SharedBitmapManager
* GetSharedBitmapManager() = 0;
99 // Gets the GPU memory buffer manager.
100 virtual gpu::GpuMemoryBufferManager
* GetGpuMemoryBufferManager() = 0;
102 // Gets the compositor message loop, or NULL if not using threaded
104 virtual base::MessageLoopProxy
* GetCompositorMessageLoop() = 0;
106 // Creates a Surface ID allocator with a new namespace.
107 virtual scoped_ptr
<cc::SurfaceIdAllocator
> CreateSurfaceIdAllocator() = 0;
110 // This class represents a lock on the compositor, that can be used to prevent
111 // commits to the compositor tree while we're waiting for an asynchronous
112 // event. The typical use case is when waiting for a renderer to produce a frame
113 // at the right size. The caller keeps a reference on this object, and drops the
114 // reference once it desires to release the lock.
115 // Note however that the lock is cancelled after a short timeout to ensure
116 // responsiveness of the UI, so the compositor tree should be kept in a
117 // "reasonable" state while the lock is held.
118 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
119 class COMPOSITOR_EXPORT CompositorLock
120 : public base::RefCounted
<CompositorLock
>,
121 public base::SupportsWeakPtr
<CompositorLock
> {
123 friend class base::RefCounted
<CompositorLock
>;
124 friend class Compositor
;
126 explicit CompositorLock(Compositor
* compositor
);
131 Compositor
* compositor_
;
132 DISALLOW_COPY_AND_ASSIGN(CompositorLock
);
135 // Compositor object to take care of GPU painting.
136 // A Browser compositor object is responsible for generating the final
137 // displayable form of pixels comprising a single widget's contents. It draws an
138 // appropriately transformed texture for each transformed view in the widget's
140 class COMPOSITOR_EXPORT Compositor
141 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient
),
142 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient
) {
144 Compositor(gfx::AcceleratedWidget widget
,
145 ui::ContextFactory
* context_factory
,
146 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
147 ~Compositor() override
;
149 ui::ContextFactory
* context_factory() { return context_factory_
; }
151 void SetOutputSurface(scoped_ptr
<cc::OutputSurface
> surface
);
153 // Schedules a redraw of the layer tree associated with this compositor.
156 // Sets the root of the layer tree drawn by this Compositor. The root layer
157 // must have no parent. The compositor's root layer is reset if the root layer
158 // is destroyed. NULL can be passed to reset the root layer, in which case the
159 // compositor will stop drawing anything.
160 // The Compositor does not own the root layer.
161 const Layer
* root_layer() const { return root_layer_
; }
162 Layer
* root_layer() { return root_layer_
; }
163 void SetRootLayer(Layer
* root_layer
);
165 // Called when we need the compositor to preserve the alpha channel in the
166 // output for situations when we want to render transparently atop something
167 // else, e.g. Aero glass.
168 void SetHostHasTransparentBackground(bool host_has_transparent_background
);
170 // The scale factor of the device that this compositor is
171 // compositing layers on.
172 float device_scale_factor() const { return device_scale_factor_
; }
174 // Draws the scene created by the layer tree and any visual effects.
177 // Where possible, draws are scissored to a damage region calculated from
178 // changes to layer properties. This bypasses that and indicates that
179 // the whole frame needs to be drawn.
180 void ScheduleFullRedraw();
182 // Schedule redraw and append damage_rect to the damage region calculated
183 // from changes to layer properties.
184 void ScheduleRedrawRect(const gfx::Rect
& damage_rect
);
186 // Finishes all outstanding rendering on the GPU.
187 void FinishAllRendering();
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
199 void SetBackgroundColor(SkColor color
);
201 // Set the visibility of the underlying compositor.
202 void SetVisible(bool visible
);
204 // Returns the widget for this compositor.
205 gfx::AcceleratedWidget
widget() const { return widget_
; }
207 // Returns the vsync manager for this compositor.
208 scoped_refptr
<CompositorVSyncManager
> vsync_manager() const;
210 // Returns the main thread task runner this compositor uses. Users of the
211 // compositor generally shouldn't use this.
212 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner() const {
216 // Compositor does not own observers. It is the responsibility of the
217 // observer to remove itself when it is done observing.
218 void AddObserver(CompositorObserver
* observer
);
219 void RemoveObserver(CompositorObserver
* observer
);
220 bool HasObserver(const CompositorObserver
* observer
) const;
222 void AddAnimationObserver(CompositorAnimationObserver
* observer
);
223 void RemoveAnimationObserver(CompositorAnimationObserver
* observer
);
224 bool HasAnimationObserver(const CompositorAnimationObserver
* observer
) const;
226 // Creates a compositor lock. Returns NULL if it is not possible to lock at
227 // this time (i.e. we're waiting to complete a previous unlock).
228 scoped_refptr
<CompositorLock
> GetCompositorLock();
230 // Internal functions, called back by command-buffer contexts on swap buffer
233 // Signals swap has been posted.
234 void OnSwapBuffersPosted();
236 // Signals swap has completed.
237 void OnSwapBuffersComplete();
239 // Signals swap has aborted (e.g. lost context).
240 void OnSwapBuffersAborted();
242 // LayerTreeHostClient implementation.
243 void WillBeginMainFrame(int frame_id
) override
{}
244 void DidBeginMainFrame() override
{}
245 void BeginMainFrame(const cc::BeginFrameArgs
& args
) override
;
246 void Layout() override
;
247 void ApplyViewportDeltas(const gfx::Vector2d
& inner_delta
,
248 const gfx::Vector2d
& outer_delta
,
250 float top_controls_delta
) override
{}
251 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
253 float top_controls_delta
) override
{}
254 void RequestNewOutputSurface(bool fallback
) override
;
255 void DidInitializeOutputSurface() override
{}
256 void WillCommit() override
{}
257 void DidCommit() override
;
258 void DidCommitAndDrawFrame() override
;
259 void DidCompleteSwapBuffers() override
;
261 // cc::LayerTreeHostSingleThreadClient implementation.
262 void ScheduleComposite() override
;
263 void ScheduleAnimation() override
;
264 void DidPostSwapBuffers() override
;
265 void DidAbortSwapBuffers() override
;
267 int last_started_frame() { return last_started_frame_
; }
268 int last_ended_frame() { return last_ended_frame_
; }
270 bool IsLocked() { return compositor_lock_
!= NULL
; }
272 const cc::LayerTreeDebugState
& GetLayerTreeDebugState() const;
273 void SetLayerTreeDebugState(const cc::LayerTreeDebugState
& debug_state
);
274 const cc::RendererSettings
& GetRendererSettings() const;
276 LayerAnimatorCollection
* layer_animator_collection() {
277 return &layer_animator_collection_
;
280 cc::SurfaceIdAllocator
* surface_id_allocator() {
281 return surface_id_allocator_
.get();
285 friend class base::RefCounted
<Compositor
>;
286 friend class CompositorLock
;
288 // Called by CompositorLock.
289 void UnlockCompositor();
291 // Called to release any pending CompositorLock
292 void CancelCompositorLock();
294 // Notifies the compositor that compositing is complete.
299 ui::ContextFactory
* context_factory_
;
301 // The root of the Layer tree drawn by this compositor.
304 ObserverList
<CompositorObserver
> observer_list_
;
305 ObserverList
<CompositorAnimationObserver
> animation_observer_list_
;
307 gfx::AcceleratedWidget widget_
;
308 scoped_ptr
<cc::SurfaceIdAllocator
> surface_id_allocator_
;
309 scoped_refptr
<cc::Layer
> root_web_layer_
;
310 scoped_ptr
<cc::LayerTreeHost
> host_
;
311 scoped_refptr
<base::MessageLoopProxy
> compositor_thread_loop_
;
312 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
314 // The manager of vsync parameters for this compositor.
315 scoped_refptr
<CompositorVSyncManager
> vsync_manager_
;
317 // The device scale factor of the monitor that this compositor is compositing
319 float device_scale_factor_
;
321 int last_started_frame_
;
322 int last_ended_frame_
;
324 bool disable_schedule_composite_
;
326 CompositorLock
* compositor_lock_
;
328 // Prevent more than one draw from being scheduled.
329 bool defer_draw_scheduling_
;
331 // Used to prevent Draw()s while a composite is in progress.
332 bool waiting_on_compositing_end_
;
333 bool draw_on_compositing_end_
;
334 enum SwapState
{ SWAP_NONE
, SWAP_POSTED
, SWAP_COMPLETED
};
335 SwapState swap_state_
;
337 LayerAnimatorCollection layer_animator_collection_
;
339 base::WeakPtrFactory
<Compositor
> weak_ptr_factory_
;
341 DISALLOW_COPY_AND_ASSIGN(Compositor
);
346 #endif // UI_COMPOSITOR_COMPOSITOR_H_