Add locking control to user flows, disallow screen locking during user creation.
[chromium-blink-merge.git] / ui / compositor / compositor.h
blobc481dc93c8a676bebbf28cdc7185d86606960b9b
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/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"
24 class SkBitmap;
26 namespace base {
27 class MessageLoopProxy;
28 class RunLoop;
31 namespace blink {
32 class WebGraphicsContext3D;
35 namespace cc {
36 class ContextProvider;
37 class Layer;
38 class LayerTreeDebugState;
39 class LayerTreeHost;
42 namespace gfx {
43 class Rect;
44 class Size;
47 namespace ui {
49 class Compositor;
50 class Layer;
51 class PostedSwapQueue;
52 class Reflector;
53 class Texture;
54 struct LatencyInfo;
56 // This class abstracts the creation of the 3D context for the compositor. It is
57 // a global object.
58 class COMPOSITOR_EXPORT ContextFactory {
59 public:
60 virtual ~ContextFactory() {}
62 // Gets the global instance.
63 static ContextFactory* GetInstance();
65 // Sets the global instance. Caller keeps ownership.
66 // If this function isn't called (for tests), a "default" factory will be
67 // created on the first call of GetInstance.
68 static void SetInstance(ContextFactory* instance);
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 scoped_ptr<cc::OutputSurface> CreateOutputSurface(
74 Compositor* compositor, 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 // Returns a reference to the offscreen context provider used by the
85 // compositor. This provider is bound and used on whichever thread the
86 // compositor is rendering from.
87 virtual scoped_refptr<cc::ContextProvider>
88 OffscreenCompositorContextProvider() = 0;
90 // Return a reference to a shared offscreen context provider usable from the
91 // main thread. This may be the same as OffscreenCompositorContextProvider()
92 // depending on the compositor's threading configuration. This provider will
93 // be bound to the main thread.
94 virtual scoped_refptr<cc::ContextProvider>
95 SharedMainThreadContextProvider() = 0;
97 // Destroys per-compositor data.
98 virtual void RemoveCompositor(Compositor* compositor) = 0;
100 // When true, the factory uses test contexts that do not do real GL
101 // operations.
102 virtual bool DoesCreateTestContexts() = 0;
105 // Texture provide an abstraction over the external texture that can be passed
106 // to a layer.
107 class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> {
108 public:
109 Texture(bool flipped, const gfx::Size& size, float device_scale_factor);
111 bool flipped() const { return flipped_; }
112 gfx::Size size() const { return size_; }
113 float device_scale_factor() const { return device_scale_factor_; }
115 virtual unsigned int PrepareTexture() = 0;
117 // Replaces the texture with the texture from the specified mailbox.
118 virtual void Consume(const std::string& mailbox_name,
119 const gfx::Size& new_size) {}
121 // Moves the texture into the mailbox and returns the mailbox name.
122 // The texture must have been previously consumed from a mailbox.
123 virtual std::string Produce();
125 protected:
126 virtual ~Texture();
127 gfx::Size size_; // in pixel
129 private:
130 friend class base::RefCounted<Texture>;
132 bool flipped_;
133 float device_scale_factor_;
135 DISALLOW_COPY_AND_ASSIGN(Texture);
138 // This class represents a lock on the compositor, that can be used to prevent
139 // commits to the compositor tree while we're waiting for an asynchronous
140 // event. The typical use case is when waiting for a renderer to produce a frame
141 // at the right size. The caller keeps a reference on this object, and drops the
142 // reference once it desires to release the lock.
143 // Note however that the lock is cancelled after a short timeout to ensure
144 // responsiveness of the UI, so the compositor tree should be kept in a
145 // "reasonable" state while the lock is held.
146 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
147 class COMPOSITOR_EXPORT CompositorLock
148 : public base::RefCounted<CompositorLock>,
149 public base::SupportsWeakPtr<CompositorLock> {
150 private:
151 friend class base::RefCounted<CompositorLock>;
152 friend class Compositor;
154 explicit CompositorLock(Compositor* compositor);
155 ~CompositorLock();
157 void CancelLock();
159 Compositor* compositor_;
160 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
163 // Compositor object to take care of GPU painting.
164 // A Browser compositor object is responsible for generating the final
165 // displayable form of pixels comprising a single widget's contents. It draws an
166 // appropriately transformed texture for each transformed view in the widget's
167 // view hierarchy.
168 class COMPOSITOR_EXPORT Compositor
169 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
170 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient),
171 public base::SupportsWeakPtr<Compositor> {
172 public:
173 explicit Compositor(gfx::AcceleratedWidget widget);
174 virtual ~Compositor();
176 static void Initialize();
177 static bool WasInitializedWithThread();
178 static scoped_refptr<base::MessageLoopProxy> GetCompositorMessageLoop();
179 static void Terminate();
181 // Schedules a redraw of the layer tree associated with this compositor.
182 void ScheduleDraw();
184 // Sets the root of the layer tree drawn by this Compositor. The root layer
185 // must have no parent. The compositor's root layer is reset if the root layer
186 // is destroyed. NULL can be passed to reset the root layer, in which case the
187 // compositor will stop drawing anything.
188 // The Compositor does not own the root layer.
189 const Layer* root_layer() const { return root_layer_; }
190 Layer* root_layer() { return root_layer_; }
191 void SetRootLayer(Layer* root_layer);
193 // Called when we need the compositor to preserve the alpha channel in the
194 // output for situations when we want to render transparently atop something
195 // else, e.g. Aero glass.
196 void SetHostHasTransparentBackground(bool host_has_transparent_background);
198 // The scale factor of the device that this compositor is
199 // compositing layers on.
200 float device_scale_factor() const { return device_scale_factor_; }
202 // Draws the scene created by the layer tree and any visual effects.
203 void Draw();
205 // Where possible, draws are scissored to a damage region calculated from
206 // changes to layer properties. This bypasses that and indicates that
207 // the whole frame needs to be drawn.
208 void ScheduleFullRedraw();
210 // Schedule redraw and append damage_rect to the damage region calculated
211 // from changes to layer properties.
212 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
214 void SetLatencyInfo(const LatencyInfo& latency_info);
216 // Reads the region |bounds_in_pixel| of the contents of the last rendered
217 // frame into the given bitmap.
218 // Returns false if the pixels could not be read.
219 bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds_in_pixel);
221 // Sets the compositor's device scale factor and size.
222 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
224 // Returns the size of the widget that is being drawn to in pixel coordinates.
225 const gfx::Size& size() const { return size_; }
227 // Sets the background color used for areas that aren't covered by
228 // the |root_layer|.
229 void SetBackgroundColor(SkColor color);
231 // Returns the widget for this compositor.
232 gfx::AcceleratedWidget widget() const { return widget_; }
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
245 // events.
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 void OnUpdateVSyncParameters(base::TimeTicks timebase,
257 base::TimeDelta interval);
259 // LayerTreeHostClient implementation.
260 virtual void WillBeginMainFrame(int frame_id) OVERRIDE {}
261 virtual void DidBeginMainFrame() OVERRIDE {}
262 virtual void Animate(double frame_begin_time) OVERRIDE {}
263 virtual void Layout() OVERRIDE;
264 virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta,
265 float page_scale) OVERRIDE {}
266 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback)
267 OVERRIDE;
268 virtual void DidInitializeOutputSurface(bool success) OVERRIDE {}
269 virtual void WillCommit() OVERRIDE {}
270 virtual void DidCommit() OVERRIDE;
271 virtual void DidCommitAndDrawFrame() OVERRIDE;
272 virtual void DidCompleteSwapBuffers() OVERRIDE;
273 virtual scoped_refptr<cc::ContextProvider>
274 OffscreenContextProvider() OVERRIDE;
276 // cc::LayerTreeHostSingleThreadClient implementation.
277 virtual void ScheduleComposite() OVERRIDE;
278 virtual void ScheduleAnimation() OVERRIDE;
279 virtual void DidPostSwapBuffers() OVERRIDE;
280 virtual void DidAbortSwapBuffers() OVERRIDE;
282 int last_started_frame() { return last_started_frame_; }
283 int last_ended_frame() { return last_ended_frame_; }
285 bool IsLocked() { return compositor_lock_ != NULL; }
287 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
288 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
290 private:
291 friend class base::RefCounted<Compositor>;
292 friend class CompositorLock;
294 // Called by CompositorLock.
295 void UnlockCompositor();
297 // Called to release any pending CompositorLock
298 void CancelCompositorLock();
300 // Notifies the compositor that compositing is complete.
301 void NotifyEnd();
303 gfx::Size size_;
305 // The root of the Layer tree drawn by this compositor.
306 Layer* root_layer_;
308 ObserverList<CompositorObserver> observer_list_;
310 gfx::AcceleratedWidget widget_;
311 scoped_refptr<cc::Layer> root_web_layer_;
312 scoped_ptr<cc::LayerTreeHost> host_;
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
318 // layers on.
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);
342 } // namespace ui
344 #endif // UI_COMPOSITOR_COMPOSITOR_H_