Fix compilation error triggered with -Werror=sign-compare
[chromium-blink-merge.git] / ui / compositor / compositor.h
blob79bf691c41520057728284ab3f1d4ba9f165fc77
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/compositor/layer_animator_collection.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gfx/size.h"
23 #include "ui/gfx/vector2d.h"
25 class SkBitmap;
27 namespace base {
28 class MessageLoopProxy;
29 class RunLoop;
32 namespace cc {
33 class ContextProvider;
34 class Layer;
35 class LayerTreeDebugState;
36 class LayerTreeHost;
37 class SharedBitmapManager;
40 namespace gfx {
41 class Rect;
42 class Size;
45 namespace gpu {
46 struct Mailbox;
49 namespace ui {
51 class Compositor;
52 class CompositorVSyncManager;
53 class Layer;
54 class Reflector;
55 class Texture;
56 struct LatencyInfo;
58 // This class abstracts the creation of the 3D context for the compositor. It is
59 // a global object.
60 class COMPOSITOR_EXPORT ContextFactory {
61 public:
62 virtual ~ContextFactory() {}
64 // Creates an output surface for the given compositor. The factory may keep
65 // per-compositor data (e.g. a shared context), that needs to be cleaned up
66 // by calling RemoveCompositor when the compositor gets destroyed.
67 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(
68 Compositor* compositor, bool software_fallback) = 0;
70 // Creates a reflector that copies the content of the |mirrored_compositor|
71 // onto |mirroing_layer|.
72 virtual scoped_refptr<Reflector> CreateReflector(
73 Compositor* mirrored_compositor,
74 Layer* mirroring_layer) = 0;
75 // Removes the reflector, which stops the mirroring.
76 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0;
78 // Return a reference to a shared offscreen context provider usable from the
79 // main thread.
80 virtual scoped_refptr<cc::ContextProvider>
81 SharedMainThreadContextProvider() = 0;
83 // Destroys per-compositor data.
84 virtual void RemoveCompositor(Compositor* compositor) = 0;
86 // When true, the factory uses test contexts that do not do real GL
87 // operations.
88 virtual bool DoesCreateTestContexts() = 0;
90 // Gets the shared bitmap manager for software mode.
91 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0;
93 // Gets the compositor message loop, or NULL if not using threaded
94 // compositing.
95 virtual base::MessageLoopProxy* GetCompositorMessageLoop() = 0;
98 // This class represents a lock on the compositor, that can be used to prevent
99 // commits to the compositor tree while we're waiting for an asynchronous
100 // event. The typical use case is when waiting for a renderer to produce a frame
101 // at the right size. The caller keeps a reference on this object, and drops the
102 // reference once it desires to release the lock.
103 // Note however that the lock is cancelled after a short timeout to ensure
104 // responsiveness of the UI, so the compositor tree should be kept in a
105 // "reasonable" state while the lock is held.
106 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
107 class COMPOSITOR_EXPORT CompositorLock
108 : public base::RefCounted<CompositorLock>,
109 public base::SupportsWeakPtr<CompositorLock> {
110 private:
111 friend class base::RefCounted<CompositorLock>;
112 friend class Compositor;
114 explicit CompositorLock(Compositor* compositor);
115 ~CompositorLock();
117 void CancelLock();
119 Compositor* compositor_;
120 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
123 // Compositor object to take care of GPU painting.
124 // A Browser compositor object is responsible for generating the final
125 // displayable form of pixels comprising a single widget's contents. It draws an
126 // appropriately transformed texture for each transformed view in the widget's
127 // view hierarchy.
128 class COMPOSITOR_EXPORT Compositor
129 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
130 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient),
131 NON_EXPORTED_BASE(public LayerAnimatorCollectionDelegate) {
132 public:
133 Compositor(gfx::AcceleratedWidget widget,
134 ui::ContextFactory* context_factory);
135 virtual ~Compositor();
137 ui::ContextFactory* context_factory() { return context_factory_; }
139 // Schedules a redraw of the layer tree associated with this compositor.
140 void ScheduleDraw();
142 // Sets the root of the layer tree drawn by this Compositor. The root layer
143 // must have no parent. The compositor's root layer is reset if the root layer
144 // is destroyed. NULL can be passed to reset the root layer, in which case the
145 // compositor will stop drawing anything.
146 // The Compositor does not own the root layer.
147 const Layer* root_layer() const { return root_layer_; }
148 Layer* root_layer() { return root_layer_; }
149 void SetRootLayer(Layer* root_layer);
151 // Called when we need the compositor to preserve the alpha channel in the
152 // output for situations when we want to render transparently atop something
153 // else, e.g. Aero glass.
154 void SetHostHasTransparentBackground(bool host_has_transparent_background);
156 // The scale factor of the device that this compositor is
157 // compositing layers on.
158 float device_scale_factor() const { return device_scale_factor_; }
160 // Draws the scene created by the layer tree and any visual effects.
161 void Draw();
163 // Where possible, draws are scissored to a damage region calculated from
164 // changes to layer properties. This bypasses that and indicates that
165 // the whole frame needs to be drawn.
166 void ScheduleFullRedraw();
168 // Schedule redraw and append damage_rect to the damage region calculated
169 // from changes to layer properties.
170 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
172 // Finishes all outstanding rendering on the GPU.
173 void FinishAllRendering();
175 void SetLatencyInfo(const LatencyInfo& latency_info);
177 // Sets the compositor's device scale factor and size.
178 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
180 // Returns the size of the widget that is being drawn to in pixel coordinates.
181 const gfx::Size& size() const { return size_; }
183 // Sets the background color used for areas that aren't covered by
184 // the |root_layer|.
185 void SetBackgroundColor(SkColor color);
187 // Returns the widget for this compositor.
188 gfx::AcceleratedWidget widget() const { return widget_; }
190 // Returns the vsync manager for this compositor.
191 scoped_refptr<CompositorVSyncManager> vsync_manager() const;
193 // Compositor does not own observers. It is the responsibility of the
194 // observer to remove itself when it is done observing.
195 void AddObserver(CompositorObserver* observer);
196 void RemoveObserver(CompositorObserver* observer);
197 bool HasObserver(CompositorObserver* observer);
199 // Creates a compositor lock. Returns NULL if it is not possible to lock at
200 // this time (i.e. we're waiting to complete a previous unlock).
201 scoped_refptr<CompositorLock> GetCompositorLock();
203 // Internal functions, called back by command-buffer contexts on swap buffer
204 // events.
206 // Signals swap has been posted.
207 void OnSwapBuffersPosted();
209 // Signals swap has completed.
210 void OnSwapBuffersComplete();
212 // Signals swap has aborted (e.g. lost context).
213 void OnSwapBuffersAborted();
215 // LayerTreeHostClient implementation.
216 virtual void WillBeginMainFrame(int frame_id) OVERRIDE {}
217 virtual void DidBeginMainFrame() OVERRIDE {}
218 virtual void Animate(base::TimeTicks frame_begin_time) OVERRIDE;
219 virtual void Layout() OVERRIDE;
220 virtual void ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
221 float page_scale) OVERRIDE {}
222 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback)
223 OVERRIDE;
224 virtual void DidInitializeOutputSurface() OVERRIDE {}
225 virtual void WillCommit() OVERRIDE {}
226 virtual void DidCommit() OVERRIDE;
227 virtual void DidCommitAndDrawFrame() OVERRIDE;
228 virtual void DidCompleteSwapBuffers() OVERRIDE;
230 // cc::LayerTreeHostSingleThreadClient implementation.
231 virtual void ScheduleComposite() OVERRIDE;
232 virtual void ScheduleAnimation() OVERRIDE;
233 virtual void DidPostSwapBuffers() OVERRIDE;
234 virtual void DidAbortSwapBuffers() OVERRIDE;
236 // LayerAnimatorCollectionDelegate implementation.
237 virtual void ScheduleAnimationForLayerCollection() OVERRIDE;
239 int last_started_frame() { return last_started_frame_; }
240 int last_ended_frame() { return last_ended_frame_; }
242 bool IsLocked() { return compositor_lock_ != NULL; }
244 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
245 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
247 LayerAnimatorCollection* layer_animator_collection() {
248 return &layer_animator_collection_;
251 private:
252 friend class base::RefCounted<Compositor>;
253 friend class CompositorLock;
255 // Called by CompositorLock.
256 void UnlockCompositor();
258 // Called to release any pending CompositorLock
259 void CancelCompositorLock();
261 // Notifies the compositor that compositing is complete.
262 void NotifyEnd();
264 gfx::Size size_;
266 ui::ContextFactory* context_factory_;
268 // The root of the Layer tree drawn by this compositor.
269 Layer* root_layer_;
271 ObserverList<CompositorObserver> observer_list_;
273 gfx::AcceleratedWidget widget_;
274 scoped_refptr<cc::Layer> root_web_layer_;
275 scoped_ptr<cc::LayerTreeHost> host_;
276 scoped_refptr<base::MessageLoopProxy> compositor_thread_loop_;
278 // The manager of vsync parameters for this compositor.
279 scoped_refptr<CompositorVSyncManager> vsync_manager_;
281 // The device scale factor of the monitor that this compositor is compositing
282 // layers on.
283 float device_scale_factor_;
285 int last_started_frame_;
286 int last_ended_frame_;
288 bool disable_schedule_composite_;
290 CompositorLock* compositor_lock_;
292 // Prevent more than one draw from being scheduled.
293 bool defer_draw_scheduling_;
295 // Used to prevent Draw()s while a composite is in progress.
296 bool waiting_on_compositing_end_;
297 bool draw_on_compositing_end_;
298 enum SwapState { SWAP_NONE, SWAP_POSTED, SWAP_COMPLETED };
299 SwapState swap_state_;
301 LayerAnimatorCollection layer_animator_collection_;
303 base::WeakPtrFactory<Compositor> schedule_draw_factory_;
305 DISALLOW_COPY_AND_ASSIGN(Compositor);
308 } // namespace ui
310 #endif // UI_COMPOSITOR_COMPOSITOR_H_