Add Profile::IsChild and IsLegacySupervised
[chromium-blink-merge.git] / ui / compositor / compositor.h
blob57e4a8e28067ecb3804b06a5e30dcd765c73bea9
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 Layer;
38 class LayerTreeDebugState;
39 class LayerTreeHost;
40 class RendererSettings;
41 class SharedBitmapManager;
42 class SurfaceIdAllocator;
45 namespace gfx {
46 class Rect;
47 class Size;
50 namespace gpu {
51 class GpuMemoryBufferManager;
52 struct Mailbox;
55 namespace ui {
57 class Compositor;
58 class CompositorVSyncManager;
59 class Layer;
60 class Reflector;
61 class Texture;
62 struct LatencyInfo;
64 // This class abstracts the creation of the 3D context for the compositor. It is
65 // a global object.
66 class COMPOSITOR_EXPORT ContextFactory {
67 public:
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
85 // main thread.
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
93 // operations.
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
103 // compositing.
104 virtual base::MessageLoopProxy* GetCompositorMessageLoop() = 0;
106 // Creates a Surface ID allocator with a new namespace.
107 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 0;
109 // Resize the display corresponding to this compositor to a particular size.
110 virtual void ResizeDisplay(ui::Compositor* compositor,
111 const gfx::Size& size) = 0;
114 // This class represents a lock on the compositor, that can be used to prevent
115 // commits to the compositor tree while we're waiting for an asynchronous
116 // event. The typical use case is when waiting for a renderer to produce a frame
117 // at the right size. The caller keeps a reference on this object, and drops the
118 // reference once it desires to release the lock.
119 // Note however that the lock is cancelled after a short timeout to ensure
120 // responsiveness of the UI, so the compositor tree should be kept in a
121 // "reasonable" state while the lock is held.
122 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
123 class COMPOSITOR_EXPORT CompositorLock
124 : public base::RefCounted<CompositorLock>,
125 public base::SupportsWeakPtr<CompositorLock> {
126 private:
127 friend class base::RefCounted<CompositorLock>;
128 friend class Compositor;
130 explicit CompositorLock(Compositor* compositor);
131 ~CompositorLock();
133 void CancelLock();
135 Compositor* compositor_;
136 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
139 // Compositor object to take care of GPU painting.
140 // A Browser compositor object is responsible for generating the final
141 // displayable form of pixels comprising a single widget's contents. It draws an
142 // appropriately transformed texture for each transformed view in the widget's
143 // view hierarchy.
144 class COMPOSITOR_EXPORT Compositor
145 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
146 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
147 public:
148 Compositor(gfx::AcceleratedWidget widget,
149 ui::ContextFactory* context_factory,
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
151 ~Compositor() override;
153 ui::ContextFactory* context_factory() { return context_factory_; }
155 void SetOutputSurface(scoped_ptr<cc::OutputSurface> surface);
157 // Schedules a redraw of the layer tree associated with this compositor.
158 void ScheduleDraw();
160 // Sets the root of the layer tree drawn by this Compositor. The root layer
161 // must have no parent. The compositor's root layer is reset if the root layer
162 // is destroyed. NULL can be passed to reset the root layer, in which case the
163 // compositor will stop drawing anything.
164 // The Compositor does not own the root layer.
165 const Layer* root_layer() const { return root_layer_; }
166 Layer* root_layer() { return root_layer_; }
167 void SetRootLayer(Layer* root_layer);
169 // Called when we need the compositor to preserve the alpha channel in the
170 // output for situations when we want to render transparently atop something
171 // else, e.g. Aero glass.
172 void SetHostHasTransparentBackground(bool host_has_transparent_background);
174 // The scale factor of the device that this compositor is
175 // compositing layers on.
176 float device_scale_factor() const { return device_scale_factor_; }
178 // Draws the scene created by the layer tree and any visual effects.
179 void Draw();
181 // Where possible, draws are scissored to a damage region calculated from
182 // changes to layer properties. This bypasses that and indicates that
183 // the whole frame needs to be drawn.
184 void ScheduleFullRedraw();
186 // Schedule redraw and append damage_rect to the damage region calculated
187 // from changes to layer properties.
188 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
190 // Finishes all outstanding rendering and disables swapping on this surface
191 // until it is resized.
192 void DisableSwapUntilResize();
194 void SetLatencyInfo(const LatencyInfo& latency_info);
196 // Sets the compositor's device scale factor and size.
197 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
199 // Returns the size of the widget that is being drawn to in pixel coordinates.
200 const gfx::Size& size() const { return size_; }
202 // Sets the background color used for areas that aren't covered by
203 // the |root_layer|.
204 void SetBackgroundColor(SkColor color);
206 // Set the visibility of the underlying compositor.
207 void SetVisible(bool visible);
209 // Returns the widget for this compositor.
210 gfx::AcceleratedWidget widget() const { return widget_; }
212 // Returns the vsync manager for this compositor.
213 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
215 // Returns the main thread task runner this compositor uses. Users of the
216 // compositor generally shouldn't use this.
217 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
218 return task_runner_;
221 // Compositor does not own observers. It is the responsibility of the
222 // observer to remove itself when it is done observing.
223 void AddObserver(CompositorObserver* observer);
224 void RemoveObserver(CompositorObserver* observer);
225 bool HasObserver(const CompositorObserver* observer) const;
227 void AddAnimationObserver(CompositorAnimationObserver* observer);
228 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
229 bool HasAnimationObserver(const CompositorAnimationObserver* observer) const;
231 // Creates a compositor lock. Returns NULL if it is not possible to lock at
232 // this time (i.e. we're waiting to complete a previous unlock).
233 scoped_refptr<CompositorLock> GetCompositorLock();
235 // Internal functions, called back by command-buffer contexts on swap buffer
236 // events.
238 // Signals swap has been posted.
239 void OnSwapBuffersPosted();
241 // Signals swap has completed.
242 void OnSwapBuffersComplete();
244 // Signals swap has aborted (e.g. lost context).
245 void OnSwapBuffersAborted();
247 // LayerTreeHostClient implementation.
248 void WillBeginMainFrame(int frame_id) override {}
249 void DidBeginMainFrame() override {}
250 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
251 void Layout() override;
252 void ApplyViewportDeltas(const gfx::Vector2d& inner_delta,
253 const gfx::Vector2d& outer_delta,
254 float page_scale,
255 float top_controls_delta) override {}
256 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
257 float page_scale,
258 float top_controls_delta) override {}
259 void RequestNewOutputSurface(bool fallback) override;
260 void DidInitializeOutputSurface() override {}
261 void WillCommit() override {}
262 void DidCommit() override;
263 void DidCommitAndDrawFrame() override;
264 void DidCompleteSwapBuffers() override;
266 // cc::LayerTreeHostSingleThreadClient implementation.
267 void ScheduleComposite() override;
268 void ScheduleAnimation() override;
269 void DidPostSwapBuffers() override;
270 void DidAbortSwapBuffers() override;
272 int last_started_frame() { return last_started_frame_; }
273 int last_ended_frame() { return last_ended_frame_; }
275 bool IsLocked() { return compositor_lock_ != NULL; }
277 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
278 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
279 const cc::RendererSettings& GetRendererSettings() const;
281 LayerAnimatorCollection* layer_animator_collection() {
282 return &layer_animator_collection_;
285 cc::SurfaceIdAllocator* surface_id_allocator() {
286 return surface_id_allocator_.get();
289 private:
290 friend class base::RefCounted<Compositor>;
291 friend class CompositorLock;
293 // Called by CompositorLock.
294 void UnlockCompositor();
296 // Called to release any pending CompositorLock
297 void CancelCompositorLock();
299 // Notifies the compositor that compositing is complete.
300 void NotifyEnd();
302 gfx::Size size_;
304 ui::ContextFactory* context_factory_;
306 // The root of the Layer tree drawn by this compositor.
307 Layer* root_layer_;
309 ObserverList<CompositorObserver> observer_list_;
310 ObserverList<CompositorAnimationObserver> animation_observer_list_;
312 gfx::AcceleratedWidget widget_;
313 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
314 scoped_refptr<cc::Layer> root_web_layer_;
315 scoped_ptr<cc::LayerTreeHost> host_;
316 scoped_refptr<base::MessageLoopProxy> compositor_thread_loop_;
317 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
319 // The manager of vsync parameters for this compositor.
320 scoped_refptr<CompositorVSyncManager> vsync_manager_;
322 // The device scale factor of the monitor that this compositor is compositing
323 // layers on.
324 float device_scale_factor_;
326 int last_started_frame_;
327 int last_ended_frame_;
329 bool disable_schedule_composite_;
331 CompositorLock* compositor_lock_;
333 // Prevent more than one draw from being scheduled.
334 bool defer_draw_scheduling_;
336 // Used to prevent Draw()s while a composite is in progress.
337 bool waiting_on_compositing_end_;
338 bool draw_on_compositing_end_;
339 enum SwapState { SWAP_NONE, SWAP_POSTED, SWAP_COMPLETED };
340 SwapState swap_state_;
342 LayerAnimatorCollection layer_animator_collection_;
344 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
346 DISALLOW_COPY_AND_ASSIGN(Compositor);
349 } // namespace ui
351 #endif // UI_COMPOSITOR_COMPOSITOR_H_