Added llvm into exceptions as we can't add README.chromium into 3rd party repository
[chromium-blink-merge.git] / ui / compositor / compositor.h
blob3c99a1640acadd7216a42382219fd1e316751cd5
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/geometry/size.h"
25 #include "ui/gfx/geometry/vector2d.h"
26 #include "ui/gfx/native_widget_types.h"
28 namespace base {
29 class MessageLoopProxy;
30 class RunLoop;
33 namespace cc {
34 class ContextProvider;
35 class Layer;
36 class LayerTreeDebugState;
37 class LayerTreeHost;
38 class RendererSettings;
39 class SharedBitmapManager;
40 class SurfaceIdAllocator;
43 namespace gfx {
44 class Rect;
45 class Size;
48 namespace gpu {
49 class GpuMemoryBufferManager;
50 struct Mailbox;
53 namespace ui {
55 class Compositor;
56 class CompositorVSyncManager;
57 class Layer;
58 class Reflector;
59 class Texture;
60 struct LatencyInfo;
62 // This class abstracts the creation of the 3D context for the compositor. It is
63 // a global object.
64 class COMPOSITOR_EXPORT ContextFactory {
65 public:
66 virtual ~ContextFactory() {}
68 // Creates an output surface for the given compositor. The factory may keep
69 // per-compositor data (e.g. a shared context), that needs to be cleaned up
70 // by calling RemoveCompositor when the compositor gets destroyed.
71 virtual void CreateOutputSurface(base::WeakPtr<Compositor> compositor,
72 bool software_fallback) = 0;
74 // Creates a reflector that copies the content of the |mirrored_compositor|
75 // onto |mirroring_layer|.
76 virtual scoped_refptr<Reflector> CreateReflector(
77 Compositor* mirrored_compositor,
78 Layer* mirroring_layer) = 0;
79 // Removes the reflector, which stops the mirroring.
80 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0;
82 // Return a reference to a shared offscreen context provider usable from the
83 // main thread.
84 virtual scoped_refptr<cc::ContextProvider>
85 SharedMainThreadContextProvider() = 0;
87 // Destroys per-compositor data.
88 virtual void RemoveCompositor(Compositor* compositor) = 0;
90 // When true, the factory uses test contexts that do not do real GL
91 // operations.
92 virtual bool DoesCreateTestContexts() = 0;
94 // Gets the shared bitmap manager for software mode.
95 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
97 // Gets the GPU memory buffer manager.
98 virtual gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0;
100 // Gets the compositor message loop, or NULL if not using threaded
101 // compositing.
102 virtual base::MessageLoopProxy* GetCompositorMessageLoop() = 0;
104 // Creates a Surface ID allocator with a new namespace.
105 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 0;
107 // Resize the display corresponding to this compositor to a particular size.
108 virtual void ResizeDisplay(ui::Compositor* compositor,
109 const gfx::Size& size) = 0;
112 // This class represents a lock on the compositor, that can be used to prevent
113 // commits to the compositor tree while we're waiting for an asynchronous
114 // event. The typical use case is when waiting for a renderer to produce a frame
115 // at the right size. The caller keeps a reference on this object, and drops the
116 // reference once it desires to release the lock.
117 // Note however that the lock is cancelled after a short timeout to ensure
118 // responsiveness of the UI, so the compositor tree should be kept in a
119 // "reasonable" state while the lock is held.
120 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
121 class COMPOSITOR_EXPORT CompositorLock
122 : public base::RefCounted<CompositorLock>,
123 public base::SupportsWeakPtr<CompositorLock> {
124 private:
125 friend class base::RefCounted<CompositorLock>;
126 friend class Compositor;
128 explicit CompositorLock(Compositor* compositor);
129 ~CompositorLock();
131 void CancelLock();
133 Compositor* compositor_;
134 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
137 // Compositor object to take care of GPU painting.
138 // A Browser compositor object is responsible for generating the final
139 // displayable form of pixels comprising a single widget's contents. It draws an
140 // appropriately transformed texture for each transformed view in the widget's
141 // view hierarchy.
142 class COMPOSITOR_EXPORT Compositor
143 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
144 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
145 public:
146 Compositor(gfx::AcceleratedWidget widget,
147 ui::ContextFactory* context_factory,
148 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
149 ~Compositor() override;
151 ui::ContextFactory* context_factory() { return context_factory_; }
153 void SetOutputSurface(scoped_ptr<cc::OutputSurface> surface);
155 // Schedules a redraw of the layer tree associated with this compositor.
156 void ScheduleDraw();
158 // Sets the root of the layer tree drawn by this Compositor. The root layer
159 // must have no parent. The compositor's root layer is reset if the root layer
160 // is destroyed. NULL can be passed to reset the root layer, in which case the
161 // compositor will stop drawing anything.
162 // The Compositor does not own the root layer.
163 const Layer* root_layer() const { return root_layer_; }
164 Layer* root_layer() { return root_layer_; }
165 void SetRootLayer(Layer* root_layer);
167 // Called when we need the compositor to preserve the alpha channel in the
168 // output for situations when we want to render transparently atop something
169 // else, e.g. Aero glass.
170 void SetHostHasTransparentBackground(bool host_has_transparent_background);
172 // The scale factor of the device that this compositor is
173 // compositing layers on.
174 float device_scale_factor() const { return device_scale_factor_; }
176 // Where possible, draws are scissored to a damage region calculated from
177 // changes to layer properties. This bypasses that and indicates that
178 // the whole frame needs to be drawn.
179 void ScheduleFullRedraw();
181 // Schedule redraw and append damage_rect to the damage region calculated
182 // from changes to layer properties.
183 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
185 // Finishes all outstanding rendering and disables swapping on this surface.
186 void FinishAllRendering();
188 // Finishes all outstanding rendering and disables swapping on this surface
189 // until it is resized.
190 void DisableSwapUntilResize();
192 void SetLatencyInfo(const LatencyInfo& latency_info);
194 // Sets the compositor's device scale factor and size.
195 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
197 // Returns the size of the widget that is being drawn to in pixel coordinates.
198 const gfx::Size& size() const { return size_; }
200 // Sets the background color used for areas that aren't covered by
201 // the |root_layer|.
202 void SetBackgroundColor(SkColor color);
204 // Sets the visibility of the underlying compositor.
205 void SetVisible(bool visible);
207 // Gets the visibility of the underlying compositor.
208 bool IsVisible();
210 // Returns the widget for this compositor.
211 gfx::AcceleratedWidget widget() const { return widget_; }
213 // Returns the vsync manager for this compositor.
214 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
216 // Returns the main thread task runner this compositor uses. Users of the
217 // compositor generally shouldn't use this.
218 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
219 return task_runner_;
222 // Compositor does not own observers. It is the responsibility of the
223 // observer to remove itself when it is done observing.
224 void AddObserver(CompositorObserver* observer);
225 void RemoveObserver(CompositorObserver* observer);
226 bool HasObserver(const CompositorObserver* observer) const;
228 void AddAnimationObserver(CompositorAnimationObserver* observer);
229 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
230 bool HasAnimationObserver(const CompositorAnimationObserver* observer) const;
232 // Creates a compositor lock. Returns NULL if it is not possible to lock at
233 // this time (i.e. we're waiting to complete a previous unlock).
234 scoped_refptr<CompositorLock> GetCompositorLock();
236 // Internal functions, called back by command-buffer contexts on swap buffer
237 // events.
239 // Signals swap has been posted.
240 void OnSwapBuffersPosted();
242 // Signals swap has completed.
243 void OnSwapBuffersComplete();
245 // Signals swap has aborted (e.g. lost context).
246 void OnSwapBuffersAborted();
248 // LayerTreeHostClient implementation.
249 void WillBeginMainFrame() override {}
250 void DidBeginMainFrame() override {}
251 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
252 void BeginMainFrameNotExpectedSoon() override;
253 void Layout() override;
254 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
255 const gfx::Vector2dF& outer_delta,
256 const gfx::Vector2dF& elastic_overscroll_delta,
257 float page_scale,
258 float top_controls_delta) override {}
259 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
260 float page_scale,
261 float top_controls_delta) override {}
262 void RequestNewOutputSurface() override;
263 void DidInitializeOutputSurface() override;
264 void DidFailToInitializeOutputSurface() override;
265 void WillCommit() override {}
266 void DidCommit() override;
267 void DidCommitAndDrawFrame() override;
268 void DidCompleteSwapBuffers() override;
269 void DidCompletePageScaleAnimation() override {}
271 // cc::LayerTreeHostSingleThreadClient implementation.
272 void DidPostSwapBuffers() override;
273 void DidAbortSwapBuffers() override;
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 enum {
294 OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK = 4,
295 MAX_OUTPUT_SURFACE_RETRIES = 5,
298 // Called by CompositorLock.
299 void UnlockCompositor();
301 // Called to release any pending CompositorLock
302 void CancelCompositorLock();
304 gfx::Size size_;
306 ui::ContextFactory* context_factory_;
308 // The root of the Layer tree drawn by this compositor.
309 Layer* root_layer_;
311 ObserverList<CompositorObserver, true> observer_list_;
312 ObserverList<CompositorAnimationObserver> animation_observer_list_;
314 gfx::AcceleratedWidget widget_;
315 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
316 scoped_refptr<cc::Layer> root_web_layer_;
317 scoped_ptr<cc::LayerTreeHost> host_;
318 scoped_refptr<base::MessageLoopProxy> compositor_thread_loop_;
319 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
321 // The manager of vsync parameters for this compositor.
322 scoped_refptr<CompositorVSyncManager> vsync_manager_;
324 // The device scale factor of the monitor that this compositor is compositing
325 // layers on.
326 float device_scale_factor_;
328 int last_started_frame_;
329 int last_ended_frame_;
331 int num_failed_recreate_attempts_;
333 bool disable_schedule_composite_;
335 CompositorLock* compositor_lock_;
337 LayerAnimatorCollection layer_animator_collection_;
339 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
341 DISALLOW_COPY_AND_ASSIGN(Compositor);
344 } // namespace ui
346 #endif // UI_COMPOSITOR_COMPOSITOR_H_