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;
53 class PostedSwapQueue;
58 // This class abstracts the creation of the 3D context for the compositor. It is
60 class COMPOSITOR_EXPORT ContextFactory {
62 virtual ~ContextFactory() {}
64 // Gets the global instance.
65 static ContextFactory* GetInstance();
67 // Sets the global instance. Caller keeps ownership.
68 // If this function isn't called (for tests), a "default" factory will be
69 // created on the first call of GetInstance.
70 static void SetInstance(ContextFactory* instance);
72 // Creates an output surface for the given compositor. The factory may keep
73 // per-compositor data (e.g. a shared context), that needs to be cleaned up
74 // by calling RemoveCompositor when the compositor gets destroyed.
75 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(
76 Compositor* compositor, bool software_fallback) = 0;
78 // Creates a reflector that copies the content of the |mirrored_compositor|
79 // onto |mirroing_layer|.
80 virtual scoped_refptr<Reflector> CreateReflector(
81 Compositor* mirrored_compositor,
82 Layer* mirroring_layer) = 0;
83 // Removes the reflector, which stops the mirroring.
84 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0;
86 // Returns a reference to the offscreen context provider used by the
87 // compositor. This provider is bound and used on whichever thread the
88 // compositor is rendering from.
89 virtual scoped_refptr<cc::ContextProvider>
90 OffscreenCompositorContextProvider() = 0;
92 // Return a reference to a shared offscreen context provider usable from the
93 // main thread. This may be the same as OffscreenCompositorContextProvider()
94 // depending on the compositor's threading configuration. This provider will
95 // be bound to the main thread.
96 virtual scoped_refptr<cc::ContextProvider>
97 SharedMainThreadContextProvider() = 0;
99 // Destroys per-compositor data.
100 virtual void RemoveCompositor(Compositor* compositor) = 0;
102 // When true, the factory uses test contexts that do not do real GL
104 virtual bool DoesCreateTestContexts() = 0;
107 // Texture provide an abstraction over the external texture that can be passed
109 class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> {
111 Texture(bool flipped, const gfx::Size& size, float device_scale_factor);
113 bool flipped() const { return flipped_; }
114 gfx::Size size() const { return size_; }
115 float device_scale_factor() const { return device_scale_factor_; }
117 virtual unsigned int PrepareTexture() = 0;
119 // Replaces the texture with the texture from the specified mailbox.
120 virtual void Consume(const gpu::Mailbox& mailbox,
121 const gfx::Size& new_size) {}
123 // Moves the texture into the mailbox and returns the mailbox name.
124 // The texture must have been previously consumed from a mailbox.
125 virtual gpu::Mailbox Produce();
129 gfx::Size size_; // in pixel
132 friend class base::RefCounted<Texture>;
135 float device_scale_factor_;
137 DISALLOW_COPY_AND_ASSIGN(Texture);
140 // This class represents a lock on the compositor, that can be used to prevent
141 // commits to the compositor tree while we're waiting for an asynchronous
142 // event. The typical use case is when waiting for a renderer to produce a frame
143 // at the right size. The caller keeps a reference on this object, and drops the
144 // reference once it desires to release the lock.
145 // Note however that the lock is cancelled after a short timeout to ensure
146 // responsiveness of the UI, so the compositor tree should be kept in a
147 // "reasonable" state while the lock is held.
148 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
149 class COMPOSITOR_EXPORT CompositorLock
150 : public base::RefCounted<CompositorLock>,
151 public base::SupportsWeakPtr<CompositorLock> {
153 friend class base::RefCounted<CompositorLock>;
154 friend class Compositor;
156 explicit CompositorLock(Compositor* compositor);
161 Compositor* compositor_;
162 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
165 // Compositor object to take care of GPU painting.
166 // A Browser compositor object is responsible for generating the final
167 // displayable form of pixels comprising a single widget's contents. It draws an
168 // appropriately transformed texture for each transformed view in the widget's
170 class COMPOSITOR_EXPORT Compositor
171 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
172 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
174 explicit Compositor(gfx::AcceleratedWidget widget);
175 virtual ~Compositor();
177 static void Initialize();
178 static bool WasInitializedWithThread();
179 static scoped_refptr<base::MessageLoopProxy> GetCompositorMessageLoop();
180 static void Terminate();
181 static void SetSharedBitmapManager(cc::SharedBitmapManager* manager);
183 // Schedules a redraw of the layer tree associated with this compositor.
186 // Sets the root of the layer tree drawn by this Compositor. The root layer
187 // must have no parent. The compositor's root layer is reset if the root layer
188 // is destroyed. NULL can be passed to reset the root layer, in which case the
189 // compositor will stop drawing anything.
190 // The Compositor does not own the root layer.
191 const Layer* root_layer() const { return root_layer_; }
192 Layer* root_layer() { return root_layer_; }
193 void SetRootLayer(Layer* root_layer);
195 // Called when we need the compositor to preserve the alpha channel in the
196 // output for situations when we want to render transparently atop something
197 // else, e.g. Aero glass.
198 void SetHostHasTransparentBackground(bool host_has_transparent_background);
200 // The scale factor of the device that this compositor is
201 // compositing layers on.
202 float device_scale_factor() const { return device_scale_factor_; }
204 // Draws the scene created by the layer tree and any visual effects.
207 // Where possible, draws are scissored to a damage region calculated from
208 // changes to layer properties. This bypasses that and indicates that
209 // the whole frame needs to be drawn.
210 void ScheduleFullRedraw();
212 // Schedule redraw and append damage_rect to the damage region calculated
213 // from changes to layer properties.
214 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
216 void SetLatencyInfo(const LatencyInfo& latency_info);
218 // Sets the compositor's device scale factor and size.
219 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
221 // Returns the size of the widget that is being drawn to in pixel coordinates.
222 const gfx::Size& size() const { return size_; }
224 // Sets the background color used for areas that aren't covered by
226 void SetBackgroundColor(SkColor color);
228 // Returns the widget for this compositor.
229 gfx::AcceleratedWidget widget() const { return widget_; }
231 // Returns the vsync manager for this compositor.
232 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
234 // Compositor does not own observers. It is the responsibility of the
235 // observer to remove itself when it is done observing.
236 void AddObserver(CompositorObserver* observer);
237 void RemoveObserver(CompositorObserver* observer);
238 bool HasObserver(CompositorObserver* observer);
240 // Creates a compositor lock. Returns NULL if it is not possible to lock at
241 // this time (i.e. we're waiting to complete a previous unlock).
242 scoped_refptr<CompositorLock> GetCompositorLock();
244 // Internal functions, called back by command-buffer contexts on swap buffer
247 // Signals swap has been posted.
248 void OnSwapBuffersPosted();
250 // Signals swap has completed.
251 void OnSwapBuffersComplete();
253 // Signals swap has aborted (e.g. lost context).
254 void OnSwapBuffersAborted();
256 // LayerTreeHostClient implementation.
257 virtual void WillBeginMainFrame(int frame_id) OVERRIDE {}
258 virtual void DidBeginMainFrame() OVERRIDE {}
259 virtual void Animate(base::TimeTicks frame_begin_time) OVERRIDE {}
260 virtual void Layout() OVERRIDE;
261 virtual void ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
262 float page_scale) OVERRIDE {}
263 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback)
265 virtual void DidInitializeOutputSurface(bool success) OVERRIDE {}
266 virtual void WillCommit() OVERRIDE {}
267 virtual void DidCommit() OVERRIDE;
268 virtual void DidCommitAndDrawFrame() OVERRIDE;
269 virtual void DidCompleteSwapBuffers() OVERRIDE;
270 virtual scoped_refptr<cc::ContextProvider>
271 OffscreenContextProvider() OVERRIDE;
273 // cc::LayerTreeHostSingleThreadClient implementation.
274 virtual void ScheduleComposite() OVERRIDE;
275 virtual void ScheduleAnimation() OVERRIDE;
276 virtual void DidPostSwapBuffers() OVERRIDE;
277 virtual void DidAbortSwapBuffers() OVERRIDE;
279 int last_started_frame() { return last_started_frame_; }
280 int last_ended_frame() { return last_ended_frame_; }
282 bool IsLocked() { return compositor_lock_ != NULL; }
284 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
285 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
288 friend class base::RefCounted<Compositor>;
289 friend class CompositorLock;
291 // Called by CompositorLock.
292 void UnlockCompositor();
294 // Called to release any pending CompositorLock
295 void CancelCompositorLock();
297 // Notifies the compositor that compositing is complete.
302 // The root of the Layer tree drawn by this compositor.
305 ObserverList<CompositorObserver> observer_list_;
307 gfx::AcceleratedWidget widget_;
308 scoped_refptr<cc::Layer> root_web_layer_;
309 scoped_ptr<cc::LayerTreeHost> host_;
311 // The manager of vsync parameters for this compositor.
312 scoped_refptr<CompositorVSyncManager> vsync_manager_;
314 // Used to verify that we have at most one draw swap in flight.
315 scoped_ptr<PostedSwapQueue> posted_swaps_;
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 next_draw_is_resize_;
326 bool disable_schedule_composite_;
328 CompositorLock* compositor_lock_;
330 // Prevent more than one draw from being scheduled.
331 bool defer_draw_scheduling_;
333 // Used to prevent Draw()s while a composite is in progress.
334 bool waiting_on_compositing_end_;
335 bool draw_on_compositing_end_;
337 base::WeakPtrFactory<Compositor> schedule_draw_factory_;
339 DISALLOW_COPY_AND_ASSIGN(Compositor);
344 #endif // UI_COMPOSITOR_COMPOSITOR_H_