Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / compositor / compositor.h
blobcf115b0238f0646ec43532acbe032ad775f89e8c
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 <list>
9 #include <string>
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/time/time.h"
17 #include "cc/output/begin_frame_args.h"
18 #include "cc/surfaces/surface_sequence.h"
19 #include "cc/trees/layer_tree_host_client.h"
20 #include "cc/trees/layer_tree_host_single_thread_client.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/compositor/compositor_animation_observer.h"
23 #include "ui/compositor/compositor_export.h"
24 #include "ui/compositor/compositor_observer.h"
25 #include "ui/compositor/layer_animator_collection.h"
26 #include "ui/gfx/geometry/size.h"
27 #include "ui/gfx/geometry/vector2d.h"
28 #include "ui/gfx/native_widget_types.h"
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;
43 class TaskGraphRunner;
46 namespace gfx {
47 class Rect;
48 class Size;
51 namespace gpu {
52 class GpuMemoryBufferManager;
53 struct Mailbox;
56 namespace ui {
58 class Compositor;
59 class CompositorVSyncManager;
60 class Layer;
61 class Reflector;
62 class Texture;
63 struct LatencyInfo;
65 const int kCompositorLockTimeoutMs = 67;
67 // This class abstracts the creation of the 3D context for the compositor. It is
68 // a global object.
69 class COMPOSITOR_EXPORT ContextFactory {
70 public:
71 virtual ~ContextFactory() {}
73 // Creates an output surface for the given compositor. The factory may keep
74 // per-compositor data (e.g. a shared context), that needs to be cleaned up
75 // by calling RemoveCompositor when the compositor gets destroyed.
76 virtual void CreateOutputSurface(base::WeakPtr<Compositor> compositor) = 0;
78 // Creates a reflector that copies the content of the |mirrored_compositor|
79 // onto |mirroring_layer|.
80 virtual scoped_ptr<Reflector> CreateReflector(Compositor* mirrored_compositor,
81 Layer* mirroring_layer) = 0;
82 // Removes the reflector, which stops the mirroring.
83 virtual void RemoveReflector(Reflector* reflector) = 0;
85 // Return a reference to a shared offscreen context provider usable from the
86 // main thread.
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
94 // operations.
95 virtual bool DoesCreateTestContexts() = 0;
97 // Returns the OpenGL target to use for image textures.
98 virtual uint32 GetImageTextureTarget() = 0;
100 // Gets the shared bitmap manager for software mode.
101 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
103 // Gets the GPU memory buffer manager.
104 virtual gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0;
106 // Gets the task graph runner.
107 virtual cc::TaskGraphRunner* GetTaskGraphRunner() = 0;
109 // Creates a Surface ID allocator with a new namespace.
110 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 0;
112 // Resize the display corresponding to this compositor to a particular size.
113 virtual void ResizeDisplay(ui::Compositor* compositor,
114 const gfx::Size& size) = 0;
117 // This class represents a lock on the compositor, that can be used to prevent
118 // commits to the compositor tree while we're waiting for an asynchronous
119 // event. The typical use case is when waiting for a renderer to produce a frame
120 // at the right size. The caller keeps a reference on this object, and drops the
121 // reference once it desires to release the lock.
122 // By default, the lock will be cancelled after a short timeout to ensure
123 // responsiveness of the UI, so the compositor tree should be kept in a
124 // "reasonable" state while the lock is held. If the compositor sets
125 // locks to not time out, then the lock will remain in effect until destroyed.
126 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
127 class COMPOSITOR_EXPORT CompositorLock
128 : public base::RefCounted<CompositorLock>,
129 public base::SupportsWeakPtr<CompositorLock> {
130 private:
131 friend class base::RefCounted<CompositorLock>;
132 friend class Compositor;
134 explicit CompositorLock(Compositor* compositor);
135 ~CompositorLock();
137 void CancelLock();
139 Compositor* compositor_;
140 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
143 // This class observes BeginFrame notification from LayerTreeHost.
144 class COMPOSITOR_EXPORT CompositorBeginFrameObserver {
145 public:
146 virtual ~CompositorBeginFrameObserver() {}
147 virtual void OnSendBeginFrame(const cc::BeginFrameArgs& args) = 0;
150 // Compositor object to take care of GPU painting.
151 // A Browser compositor object is responsible for generating the final
152 // displayable form of pixels comprising a single widget's contents. It draws an
153 // appropriately transformed texture for each transformed view in the widget's
154 // view hierarchy.
155 class COMPOSITOR_EXPORT Compositor
156 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
157 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
158 public:
159 Compositor(gfx::AcceleratedWidget widget,
160 ui::ContextFactory* context_factory,
161 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
162 ~Compositor() override;
164 ui::ContextFactory* context_factory() { return context_factory_; }
166 void SetOutputSurface(scoped_ptr<cc::OutputSurface> surface);
168 // Schedules a redraw of the layer tree associated with this compositor.
169 void ScheduleDraw();
171 // Sets the root of the layer tree drawn by this Compositor. The root layer
172 // must have no parent. The compositor's root layer is reset if the root layer
173 // is destroyed. NULL can be passed to reset the root layer, in which case the
174 // compositor will stop drawing anything.
175 // The Compositor does not own the root layer.
176 const Layer* root_layer() const { return root_layer_; }
177 Layer* root_layer() { return root_layer_; }
178 void SetRootLayer(Layer* root_layer);
180 // Called when we need the compositor to preserve the alpha channel in the
181 // output for situations when we want to render transparently atop something
182 // else, e.g. Aero glass.
183 void SetHostHasTransparentBackground(bool host_has_transparent_background);
185 // The scale factor of the device that this compositor is
186 // compositing layers on.
187 float device_scale_factor() const { return device_scale_factor_; }
189 // Where possible, draws are scissored to a damage region calculated from
190 // changes to layer properties. This bypasses that and indicates that
191 // the whole frame needs to be drawn.
192 void ScheduleFullRedraw();
194 // Schedule redraw and append damage_rect to the damage region calculated
195 // from changes to layer properties.
196 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
198 // Finishes all outstanding rendering and disables swapping on this surface.
199 void FinishAllRendering();
201 // Finishes all outstanding rendering and disables swapping on this surface
202 // until it is resized.
203 void DisableSwapUntilResize();
205 void SetLatencyInfo(const LatencyInfo& latency_info);
207 // Sets the compositor's device scale factor and size.
208 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
210 // Returns the size of the widget that is being drawn to in pixel coordinates.
211 const gfx::Size& size() const { return size_; }
213 // Sets the background color used for areas that aren't covered by
214 // the |root_layer|.
215 void SetBackgroundColor(SkColor color);
217 // Sets the visibility of the underlying compositor.
218 void SetVisible(bool visible);
220 // Gets the visibility of the underlying compositor.
221 bool IsVisible();
223 // Returns the widget for this compositor.
224 gfx::AcceleratedWidget widget() const { return widget_; }
226 // Returns the vsync manager for this compositor.
227 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
229 // Returns the main thread task runner this compositor uses. Users of the
230 // compositor generally shouldn't use this.
231 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
232 return task_runner_;
235 // Compositor does not own observers. It is the responsibility of the
236 // observer to remove itself when it is done observing.
237 void AddObserver(CompositorObserver* observer);
238 void RemoveObserver(CompositorObserver* observer);
239 bool HasObserver(const CompositorObserver* observer) const;
241 void AddAnimationObserver(CompositorAnimationObserver* observer);
242 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
243 bool HasAnimationObserver(const CompositorAnimationObserver* observer) const;
245 void AddBeginFrameObserver(CompositorBeginFrameObserver* observer);
246 void RemoveBeginFrameObserver(CompositorBeginFrameObserver* observer);
248 // Change the timeout behavior for all future locks that are created. Locks
249 // should time out if there is an expectation that the compositor will be
250 // responsive.
251 void SetLocksWillTimeOut(bool locks_will_time_out) {
252 locks_will_time_out_ = locks_will_time_out;
255 // Creates a compositor lock. Returns NULL if it is not possible to lock at
256 // this time (i.e. we're waiting to complete a previous unlock).
257 scoped_refptr<CompositorLock> GetCompositorLock();
259 // Internal functions, called back by command-buffer contexts on swap buffer
260 // events.
262 // Signals swap has been posted.
263 void OnSwapBuffersPosted();
265 // Signals swap has completed.
266 void OnSwapBuffersComplete();
268 // Signals swap has aborted (e.g. lost context).
269 void OnSwapBuffersAborted();
271 // LayerTreeHostClient implementation.
272 void WillBeginMainFrame() override {}
273 void DidBeginMainFrame() override {}
274 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
275 void BeginMainFrameNotExpectedSoon() override;
276 void Layout() override;
277 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
278 const gfx::Vector2dF& outer_delta,
279 const gfx::Vector2dF& elastic_overscroll_delta,
280 float page_scale,
281 float top_controls_delta) override {}
282 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
283 float page_scale,
284 float top_controls_delta) override {}
285 void RequestNewOutputSurface() override;
286 void DidInitializeOutputSurface() override;
287 void DidFailToInitializeOutputSurface() override;
288 void WillCommit() override {}
289 void DidCommit() override;
290 void DidCommitAndDrawFrame() override;
291 void DidCompleteSwapBuffers() override;
292 void DidCompletePageScaleAnimation() override {}
293 void SendBeginFramesToChildren(const cc::BeginFrameArgs& args) override;
295 // cc::LayerTreeHostSingleThreadClient implementation.
296 void DidPostSwapBuffers() override;
297 void DidAbortSwapBuffers() override;
299 bool IsLocked() { return compositor_lock_ != NULL; }
301 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
302 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
303 const cc::RendererSettings& GetRendererSettings() const;
305 LayerAnimatorCollection* layer_animator_collection() {
306 return &layer_animator_collection_;
309 cc::SurfaceIdAllocator* surface_id_allocator() {
310 return surface_id_allocator_.get();
313 private:
314 friend class base::RefCounted<Compositor>;
315 friend class CompositorLock;
317 // Called by CompositorLock.
318 void UnlockCompositor();
320 // Called to release any pending CompositorLock
321 void CancelCompositorLock();
323 gfx::Size size_;
325 ui::ContextFactory* context_factory_;
327 // The root of the Layer tree drawn by this compositor.
328 Layer* root_layer_;
330 ObserverList<CompositorObserver, true> observer_list_;
331 ObserverList<CompositorAnimationObserver> animation_observer_list_;
332 std::list<CompositorBeginFrameObserver*> begin_frame_observer_list_;
334 gfx::AcceleratedWidget widget_;
335 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
336 scoped_refptr<cc::Layer> root_web_layer_;
337 scoped_ptr<cc::LayerTreeHost> host_;
338 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
340 // The manager of vsync parameters for this compositor.
341 scoped_refptr<CompositorVSyncManager> vsync_manager_;
343 // The device scale factor of the monitor that this compositor is compositing
344 // layers on.
345 float device_scale_factor_;
347 int last_started_frame_;
348 int last_ended_frame_;
350 bool locks_will_time_out_;
351 CompositorLock* compositor_lock_;
353 LayerAnimatorCollection layer_animator_collection_;
355 // Used to send to any new CompositorBeginFrameObserver immediately.
356 cc::BeginFrameArgs missed_begin_frame_args_;
358 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
360 DISALLOW_COPY_AND_ASSIGN(Compositor);
363 } // namespace ui
365 #endif // UI_COMPOSITOR_COMPOSITOR_H_