Update V8 to version 4.6.55.
[chromium-blink-merge.git] / ui / compositor / compositor.h
blob7c4166f8654b0f57300bc096481a301f4fe6def0
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/gpu_memory_buffer.h"
29 #include "ui/gfx/native_widget_types.h"
31 namespace base {
32 class RunLoop;
33 class SingleThreadTaskRunner;
36 namespace cc {
37 class ContextProvider;
38 class Layer;
39 class LayerTreeDebugState;
40 class LayerTreeHost;
41 class RendererSettings;
42 class SharedBitmapManager;
43 class SurfaceIdAllocator;
44 class TaskGraphRunner;
47 namespace gfx {
48 class Rect;
49 class Size;
52 namespace gpu {
53 class GpuMemoryBufferManager;
54 struct Mailbox;
57 namespace ui {
59 class Compositor;
60 class CompositorVSyncManager;
61 class LatencyInfo;
62 class Layer;
63 class Reflector;
64 class Texture;
66 const int kCompositorLockTimeoutMs = 67;
68 // This class abstracts the creation of the 3D context for the compositor. It is
69 // a global object.
70 class COMPOSITOR_EXPORT ContextFactory {
71 public:
72 virtual ~ContextFactory() {}
74 // Creates an output surface for the given compositor. The factory may keep
75 // per-compositor data (e.g. a shared context), that needs to be cleaned up
76 // by calling RemoveCompositor when the compositor gets destroyed.
77 virtual void CreateOutputSurface(base::WeakPtr<Compositor> compositor) = 0;
79 // Creates a reflector that copies the content of the |mirrored_compositor|
80 // onto |mirroring_layer|.
81 virtual scoped_ptr<Reflector> CreateReflector(Compositor* mirrored_compositor,
82 Layer* mirroring_layer) = 0;
83 // Removes the reflector, which stops the mirroring.
84 virtual void RemoveReflector(Reflector* reflector) = 0;
86 // Return a reference to a shared offscreen context provider usable from the
87 // main thread.
88 virtual scoped_refptr<cc::ContextProvider>
89 SharedMainThreadContextProvider() = 0;
91 // Destroys per-compositor data.
92 virtual void RemoveCompositor(Compositor* compositor) = 0;
94 // When true, the factory uses test contexts that do not do real GL
95 // operations.
96 virtual bool DoesCreateTestContexts() = 0;
98 // Returns the OpenGL target to use for image textures.
99 virtual uint32 GetImageTextureTarget(gfx::BufferFormat format,
100 gfx::BufferUsage usage) = 0;
102 // Gets the shared bitmap manager for software mode.
103 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
105 // Gets the GPU memory buffer manager.
106 virtual gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() = 0;
108 // Gets the task graph runner.
109 virtual cc::TaskGraphRunner* GetTaskGraphRunner() = 0;
111 // Creates a Surface ID allocator with a new namespace.
112 virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator() = 0;
114 // Resize the display corresponding to this compositor to a particular size.
115 virtual void ResizeDisplay(ui::Compositor* compositor,
116 const gfx::Size& size) = 0;
119 // This class represents a lock on the compositor, that can be used to prevent
120 // commits to the compositor tree while we're waiting for an asynchronous
121 // event. The typical use case is when waiting for a renderer to produce a frame
122 // at the right size. The caller keeps a reference on this object, and drops the
123 // reference once it desires to release the lock.
124 // By default, the lock will be cancelled after a short timeout to ensure
125 // responsiveness of the UI, so the compositor tree should be kept in a
126 // "reasonable" state while the lock is held. If the compositor sets
127 // locks to not time out, then the lock will remain in effect until destroyed.
128 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
129 class COMPOSITOR_EXPORT CompositorLock
130 : public base::RefCounted<CompositorLock>,
131 public base::SupportsWeakPtr<CompositorLock> {
132 private:
133 friend class base::RefCounted<CompositorLock>;
134 friend class Compositor;
136 explicit CompositorLock(Compositor* compositor);
137 ~CompositorLock();
139 void CancelLock();
141 Compositor* compositor_;
142 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
145 // This class observes BeginFrame notification from LayerTreeHost.
146 class COMPOSITOR_EXPORT CompositorBeginFrameObserver {
147 public:
148 virtual ~CompositorBeginFrameObserver() {}
149 virtual void OnSendBeginFrame(const cc::BeginFrameArgs& args) = 0;
152 // Compositor object to take care of GPU painting.
153 // A Browser compositor object is responsible for generating the final
154 // displayable form of pixels comprising a single widget's contents. It draws an
155 // appropriately transformed texture for each transformed view in the widget's
156 // view hierarchy.
157 class COMPOSITOR_EXPORT Compositor
158 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
159 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
160 public:
161 Compositor(gfx::AcceleratedWidget widget,
162 ui::ContextFactory* context_factory,
163 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
164 ~Compositor() override;
166 ui::ContextFactory* context_factory() { return context_factory_; }
168 void SetOutputSurface(scoped_ptr<cc::OutputSurface> surface);
170 // Schedules a redraw of the layer tree associated with this compositor.
171 void ScheduleDraw();
173 // Sets the root of the layer tree drawn by this Compositor. The root layer
174 // must have no parent. The compositor's root layer is reset if the root layer
175 // is destroyed. NULL can be passed to reset the root layer, in which case the
176 // compositor will stop drawing anything.
177 // The Compositor does not own the root layer.
178 const Layer* root_layer() const { return root_layer_; }
179 Layer* root_layer() { return root_layer_; }
180 void SetRootLayer(Layer* root_layer);
182 // Called when we need the compositor to preserve the alpha channel in the
183 // output for situations when we want to render transparently atop something
184 // else, e.g. Aero glass.
185 void SetHostHasTransparentBackground(bool host_has_transparent_background);
187 // The scale factor of the device that this compositor is
188 // compositing layers on.
189 float device_scale_factor() const { return device_scale_factor_; }
191 // Where possible, draws are scissored to a damage region calculated from
192 // changes to layer properties. This bypasses that and indicates that
193 // the whole frame needs to be drawn.
194 void ScheduleFullRedraw();
196 // Schedule redraw and append damage_rect to the damage region calculated
197 // from changes to layer properties.
198 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
200 // Finishes all outstanding rendering and disables swapping on this surface.
201 void FinishAllRendering();
203 // Finishes all outstanding rendering and disables swapping on this surface
204 // until it is resized.
205 void DisableSwapUntilResize();
207 void SetLatencyInfo(const LatencyInfo& latency_info);
209 // Sets the compositor's device scale factor and size.
210 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
212 // Returns the size of the widget that is being drawn to in pixel coordinates.
213 const gfx::Size& size() const { return size_; }
215 // Sets the background color used for areas that aren't covered by
216 // the |root_layer|.
217 void SetBackgroundColor(SkColor color);
219 // Sets the visibility of the underlying compositor.
220 void SetVisible(bool visible);
222 // Gets the visibility of the underlying compositor.
223 bool IsVisible();
225 // The "authoritative" vsync interval, if provided, will override interval
226 // reported from 3D context. This is typically the value reported by a more
227 // reliable source, e.g, the platform display configuration.
228 // In the particular case of ChromeOS -- this is the value queried through
229 // XRandR, which is more reliable than the value queried through the 3D
230 // context.
231 void SetAuthoritativeVSyncInterval(const base::TimeDelta& interval);
233 // Returns the widget for this compositor.
234 gfx::AcceleratedWidget widget() const { return widget_; }
236 // Returns the vsync manager for this compositor.
237 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
239 // Returns the main thread task runner this compositor uses. Users of the
240 // compositor generally shouldn't use this.
241 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
242 return task_runner_;
245 // Compositor does not own observers. It is the responsibility of the
246 // observer to remove itself when it is done observing.
247 void AddObserver(CompositorObserver* observer);
248 void RemoveObserver(CompositorObserver* observer);
249 bool HasObserver(const CompositorObserver* observer) const;
251 void AddAnimationObserver(CompositorAnimationObserver* observer);
252 void RemoveAnimationObserver(CompositorAnimationObserver* observer);
253 bool HasAnimationObserver(const CompositorAnimationObserver* observer) const;
255 void AddBeginFrameObserver(CompositorBeginFrameObserver* observer);
256 void RemoveBeginFrameObserver(CompositorBeginFrameObserver* observer);
258 // Change the timeout behavior for all future locks that are created. Locks
259 // should time out if there is an expectation that the compositor will be
260 // responsive.
261 void SetLocksWillTimeOut(bool locks_will_time_out) {
262 locks_will_time_out_ = locks_will_time_out;
265 // Creates a compositor lock. Returns NULL if it is not possible to lock at
266 // this time (i.e. we're waiting to complete a previous unlock).
267 scoped_refptr<CompositorLock> GetCompositorLock();
269 // Internal functions, called back by command-buffer contexts on swap buffer
270 // events.
272 // Signals swap has been posted.
273 void OnSwapBuffersPosted();
275 // Signals swap has completed.
276 void OnSwapBuffersComplete();
278 // Signals swap has aborted (e.g. lost context).
279 void OnSwapBuffersAborted();
281 // LayerTreeHostClient implementation.
282 void WillBeginMainFrame() override {}
283 void DidBeginMainFrame() override {}
284 void BeginMainFrame(const cc::BeginFrameArgs& args) override;
285 void BeginMainFrameNotExpectedSoon() override;
286 void Layout() override;
287 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
288 const gfx::Vector2dF& outer_delta,
289 const gfx::Vector2dF& elastic_overscroll_delta,
290 float page_scale,
291 float top_controls_delta) override {}
292 void RequestNewOutputSurface() override;
293 void DidInitializeOutputSurface() override;
294 void DidFailToInitializeOutputSurface() override;
295 void WillCommit() override {}
296 void DidCommit() override;
297 void DidCommitAndDrawFrame() override;
298 void DidCompleteSwapBuffers() override;
299 void DidCompletePageScaleAnimation() override {}
300 void SendBeginFramesToChildren(const cc::BeginFrameArgs& args) override;
301 void RecordFrameTimingEvents(
302 scoped_ptr<cc::FrameTimingTracker::CompositeTimingSet> composite_events,
303 scoped_ptr<cc::FrameTimingTracker::MainFrameTimingSet> main_frame_events)
304 override {}
306 // cc::LayerTreeHostSingleThreadClient implementation.
307 void DidPostSwapBuffers() override;
308 void DidAbortSwapBuffers() override;
310 bool IsLocked() { return compositor_lock_ != NULL; }
312 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
313 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
314 const cc::RendererSettings& GetRendererSettings() const;
316 LayerAnimatorCollection* layer_animator_collection() {
317 return &layer_animator_collection_;
320 cc::SurfaceIdAllocator* surface_id_allocator() {
321 return surface_id_allocator_.get();
324 private:
325 friend class base::RefCounted<Compositor>;
326 friend class CompositorLock;
328 // Called by CompositorLock.
329 void UnlockCompositor();
331 // Called to release any pending CompositorLock
332 void CancelCompositorLock();
334 gfx::Size size_;
336 ui::ContextFactory* context_factory_;
338 // The root of the Layer tree drawn by this compositor.
339 Layer* root_layer_;
341 base::ObserverList<CompositorObserver, true> observer_list_;
342 base::ObserverList<CompositorAnimationObserver> animation_observer_list_;
343 std::list<CompositorBeginFrameObserver*> begin_frame_observer_list_;
345 gfx::AcceleratedWidget widget_;
346 scoped_ptr<cc::SurfaceIdAllocator> surface_id_allocator_;
347 scoped_refptr<cc::Layer> root_web_layer_;
348 scoped_ptr<cc::LayerTreeHost> host_;
349 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
351 // The manager of vsync parameters for this compositor.
352 scoped_refptr<CompositorVSyncManager> vsync_manager_;
354 // The device scale factor of the monitor that this compositor is compositing
355 // layers on.
356 float device_scale_factor_;
358 int last_started_frame_;
359 int last_ended_frame_;
361 bool locks_will_time_out_;
362 CompositorLock* compositor_lock_;
364 LayerAnimatorCollection layer_animator_collection_;
366 // Used to send to any new CompositorBeginFrameObserver immediately.
367 cc::BeginFrameArgs missed_begin_frame_args_;
369 base::WeakPtrFactory<Compositor> weak_ptr_factory_;
371 DISALLOW_COPY_AND_ASSIGN(Compositor);
374 } // namespace ui
376 #endif // UI_COMPOSITOR_COMPOSITOR_H_