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/time/time.h"
15 #include "cc/trees/layer_tree_host_client.h"
16 #include "cc/trees/layer_tree_host_single_thread_client.h"
17 #include "third_party/skia/include/core/SkColor.h"
18 #include "ui/compositor/compositor_export.h"
19 #include "ui/compositor/compositor_observer.h"
20 #include "ui/gfx/native_widget_types.h"
21 #include "ui/gfx/size.h"
22 #include "ui/gfx/vector2d.h"
27 class MessageLoopProxy
;
32 class ContextProvider
;
34 class LayerTreeDebugState
;
36 class SharedBitmapManager
;
51 class CompositorVSyncManager
;
57 // This class abstracts the creation of the 3D context for the compositor. It is
59 class COMPOSITOR_EXPORT ContextFactory
{
61 virtual ~ContextFactory() {}
63 // Gets the global instance.
64 static ContextFactory
* GetInstance();
66 // Sets the global instance. Caller keeps ownership.
67 // If this function isn't called (for tests), a "default" factory will be
68 // created on the first call of GetInstance.
69 static void SetInstance(ContextFactory
* instance
);
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 scoped_ptr
<cc::OutputSurface
> CreateOutputSurface(
75 Compositor
* compositor
, bool software_fallback
) = 0;
77 // Creates a reflector that copies the content of the |mirrored_compositor|
78 // onto |mirroing_layer|.
79 virtual scoped_refptr
<Reflector
> CreateReflector(
80 Compositor
* mirrored_compositor
,
81 Layer
* mirroring_layer
) = 0;
82 // Removes the reflector, which stops the mirroring.
83 virtual void RemoveReflector(scoped_refptr
<Reflector
> reflector
) = 0;
85 // Return a reference to a shared offscreen context provider usable from the
87 virtual scoped_refptr
<cc::ContextProvider
>
88 SharedMainThreadContextProvider() = 0;
90 // Destroys per-compositor data.
91 virtual void RemoveCompositor(Compositor
* compositor
) = 0;
93 // When true, the factory uses test contexts that do not do real GL
95 virtual bool DoesCreateTestContexts() = 0;
98 // This class represents a lock on the compositor, that can be used to prevent
99 // commits to the compositor tree while we're waiting for an asynchronous
100 // event. The typical use case is when waiting for a renderer to produce a frame
101 // at the right size. The caller keeps a reference on this object, and drops the
102 // reference once it desires to release the lock.
103 // Note however that the lock is cancelled after a short timeout to ensure
104 // responsiveness of the UI, so the compositor tree should be kept in a
105 // "reasonable" state while the lock is held.
106 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
107 class COMPOSITOR_EXPORT CompositorLock
108 : public base::RefCounted
<CompositorLock
>,
109 public base::SupportsWeakPtr
<CompositorLock
> {
111 friend class base::RefCounted
<CompositorLock
>;
112 friend class Compositor
;
114 explicit CompositorLock(Compositor
* compositor
);
119 Compositor
* compositor_
;
120 DISALLOW_COPY_AND_ASSIGN(CompositorLock
);
123 // Compositor object to take care of GPU painting.
124 // A Browser compositor object is responsible for generating the final
125 // displayable form of pixels comprising a single widget's contents. It draws an
126 // appropriately transformed texture for each transformed view in the widget's
128 class COMPOSITOR_EXPORT Compositor
129 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient
),
130 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient
) {
132 explicit Compositor(gfx::AcceleratedWidget widget
);
133 virtual ~Compositor();
135 static void Initialize();
136 static bool WasInitializedWithThread();
137 static scoped_refptr
<base::MessageLoopProxy
> GetCompositorMessageLoop();
138 static void Terminate();
139 static void SetSharedBitmapManager(cc::SharedBitmapManager
* manager
);
141 // Schedules a redraw of the layer tree associated with this compositor.
144 // Sets the root of the layer tree drawn by this Compositor. The root layer
145 // must have no parent. The compositor's root layer is reset if the root layer
146 // is destroyed. NULL can be passed to reset the root layer, in which case the
147 // compositor will stop drawing anything.
148 // The Compositor does not own the root layer.
149 const Layer
* root_layer() const { return root_layer_
; }
150 Layer
* root_layer() { return root_layer_
; }
151 void SetRootLayer(Layer
* root_layer
);
153 // Called when we need the compositor to preserve the alpha channel in the
154 // output for situations when we want to render transparently atop something
155 // else, e.g. Aero glass.
156 void SetHostHasTransparentBackground(bool host_has_transparent_background
);
158 // The scale factor of the device that this compositor is
159 // compositing layers on.
160 float device_scale_factor() const { return device_scale_factor_
; }
162 // Draws the scene created by the layer tree and any visual effects.
165 // Where possible, draws are scissored to a damage region calculated from
166 // changes to layer properties. This bypasses that and indicates that
167 // the whole frame needs to be drawn.
168 void ScheduleFullRedraw();
170 // Schedule redraw and append damage_rect to the damage region calculated
171 // from changes to layer properties.
172 void ScheduleRedrawRect(const gfx::Rect
& damage_rect
);
174 void SetLatencyInfo(const LatencyInfo
& latency_info
);
176 // Sets the compositor's device scale factor and size.
177 void SetScaleAndSize(float scale
, const gfx::Size
& size_in_pixel
);
179 // Returns the size of the widget that is being drawn to in pixel coordinates.
180 const gfx::Size
& size() const { return size_
; }
182 // Sets the background color used for areas that aren't covered by
184 void SetBackgroundColor(SkColor color
);
186 // Returns the widget for this compositor.
187 gfx::AcceleratedWidget
widget() const { return widget_
; }
189 // Returns the vsync manager for this compositor.
190 scoped_refptr
<CompositorVSyncManager
> vsync_manager() const;
192 // Compositor does not own observers. It is the responsibility of the
193 // observer to remove itself when it is done observing.
194 void AddObserver(CompositorObserver
* observer
);
195 void RemoveObserver(CompositorObserver
* observer
);
196 bool HasObserver(CompositorObserver
* observer
);
198 // Creates a compositor lock. Returns NULL if it is not possible to lock at
199 // this time (i.e. we're waiting to complete a previous unlock).
200 scoped_refptr
<CompositorLock
> GetCompositorLock();
202 // Internal functions, called back by command-buffer contexts on swap buffer
205 // Signals swap has been posted.
206 void OnSwapBuffersPosted();
208 // Signals swap has completed.
209 void OnSwapBuffersComplete();
211 // Signals swap has aborted (e.g. lost context).
212 void OnSwapBuffersAborted();
214 // LayerTreeHostClient implementation.
215 virtual void WillBeginMainFrame(int frame_id
) OVERRIDE
{}
216 virtual void DidBeginMainFrame() OVERRIDE
{}
217 virtual void Animate(base::TimeTicks frame_begin_time
) OVERRIDE
{}
218 virtual void Layout() OVERRIDE
;
219 virtual void ApplyScrollAndScale(const gfx::Vector2d
& scroll_delta
,
220 float page_scale
) OVERRIDE
{}
221 virtual scoped_ptr
<cc::OutputSurface
> CreateOutputSurface(bool fallback
)
223 virtual void DidInitializeOutputSurface() OVERRIDE
{}
224 virtual void WillCommit() OVERRIDE
{}
225 virtual void DidCommit() OVERRIDE
;
226 virtual void DidCommitAndDrawFrame() OVERRIDE
;
227 virtual void DidCompleteSwapBuffers() OVERRIDE
;
229 // cc::LayerTreeHostSingleThreadClient implementation.
230 virtual void ScheduleComposite() OVERRIDE
;
231 virtual void ScheduleAnimation() OVERRIDE
;
232 virtual void DidPostSwapBuffers() OVERRIDE
;
233 virtual void DidAbortSwapBuffers() OVERRIDE
;
235 int last_started_frame() { return last_started_frame_
; }
236 int last_ended_frame() { return last_ended_frame_
; }
238 bool IsLocked() { return compositor_lock_
!= NULL
; }
240 const cc::LayerTreeDebugState
& GetLayerTreeDebugState() const;
241 void SetLayerTreeDebugState(const cc::LayerTreeDebugState
& debug_state
);
244 friend class base::RefCounted
<Compositor
>;
245 friend class CompositorLock
;
247 // Called by CompositorLock.
248 void UnlockCompositor();
250 // Called to release any pending CompositorLock
251 void CancelCompositorLock();
253 // Notifies the compositor that compositing is complete.
258 // The root of the Layer tree drawn by this compositor.
261 ObserverList
<CompositorObserver
> observer_list_
;
263 gfx::AcceleratedWidget widget_
;
264 scoped_refptr
<cc::Layer
> root_web_layer_
;
265 scoped_ptr
<cc::LayerTreeHost
> host_
;
267 // The manager of vsync parameters for this compositor.
268 scoped_refptr
<CompositorVSyncManager
> vsync_manager_
;
270 // The device scale factor of the monitor that this compositor is compositing
272 float device_scale_factor_
;
274 int last_started_frame_
;
275 int last_ended_frame_
;
277 bool next_draw_is_resize_
;
279 bool disable_schedule_composite_
;
281 CompositorLock
* compositor_lock_
;
283 // Prevent more than one draw from being scheduled.
284 bool defer_draw_scheduling_
;
286 // Used to prevent Draw()s while a composite is in progress.
287 bool waiting_on_compositing_end_
;
288 bool draw_on_compositing_end_
;
289 enum SwapState
{ SWAP_NONE
, SWAP_POSTED
, SWAP_COMPLETED
};
290 SwapState swap_state_
;
292 base::WeakPtrFactory
<Compositor
> schedule_draw_factory_
;
294 DISALLOW_COPY_AND_ASSIGN(Compositor
);
299 #endif // UI_COMPOSITOR_COMPOSITOR_H_