f91a29423434800cfb509892333a4fe69d496f13
[platform/framework/web/crosswalk.git] / src / ui / compositor / compositor.h
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.
4
5 #ifndef UI_COMPOSITOR_COMPOSITOR_H_
6 #define UI_COMPOSITOR_COMPOSITOR_H_
7
8 #include <string>
9
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/gfx/native_widget_types.h"
21 #include "ui/gfx/size.h"
22 #include "ui/gfx/vector2d.h"
23
24 class SkBitmap;
25
26 namespace base {
27 class MessageLoopProxy;
28 class RunLoop;
29 }
30
31 namespace cc {
32 class ContextProvider;
33 class Layer;
34 class LayerTreeDebugState;
35 class LayerTreeHost;
36 class SharedBitmapManager;
37 }
38
39 namespace gfx {
40 class Rect;
41 class Size;
42 }
43
44 namespace gpu {
45 struct Mailbox;
46 }
47
48 namespace ui {
49
50 class Compositor;
51 class CompositorVSyncManager;
52 class Layer;
53 class PostedSwapQueue;
54 class Reflector;
55 class Texture;
56 struct LatencyInfo;
57
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() {}
63
64   // Gets the global instance.
65   static ContextFactory* GetInstance();
66
67   // Sets the global instance. Caller keeps ownership.
68   // If this function isn't called (for tests), a "default" factory will be
69   // created on the first call of GetInstance.
70   static void SetInstance(ContextFactory* instance);
71
72   // Creates an output surface for the given compositor. The factory may keep
73   // per-compositor data (e.g. a shared context), that needs to be cleaned up
74   // by calling RemoveCompositor when the compositor gets destroyed.
75   virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(
76       Compositor* compositor, bool software_fallback) = 0;
77
78   // Creates a reflector that copies the content of the |mirrored_compositor|
79   // onto |mirroing_layer|.
80   virtual scoped_refptr<Reflector> CreateReflector(
81       Compositor* mirrored_compositor,
82       Layer* mirroring_layer) = 0;
83   // Removes the reflector, which stops the mirroring.
84   virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0;
85
86   // Returns a reference to the offscreen context provider used by the
87   // compositor. This provider is bound and used on whichever thread the
88   // compositor is rendering from.
89   virtual scoped_refptr<cc::ContextProvider>
90       OffscreenCompositorContextProvider() = 0;
91
92   // Return a reference to a shared offscreen context provider usable from the
93   // main thread. This may be the same as OffscreenCompositorContextProvider()
94   // depending on the compositor's threading configuration. This provider will
95   // be bound to the main thread.
96   virtual scoped_refptr<cc::ContextProvider>
97       SharedMainThreadContextProvider() = 0;
98
99   // Destroys per-compositor data.
100   virtual void RemoveCompositor(Compositor* compositor) = 0;
101
102   // When true, the factory uses test contexts that do not do real GL
103   // operations.
104   virtual bool DoesCreateTestContexts() = 0;
105 };
106
107 // Texture provide an abstraction over the external texture that can be passed
108 // to a layer.
109 class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> {
110  public:
111   Texture(bool flipped, const gfx::Size& size, float device_scale_factor);
112
113   bool flipped() const { return flipped_; }
114   gfx::Size size() const { return size_; }
115   float device_scale_factor() const { return device_scale_factor_; }
116
117   virtual unsigned int PrepareTexture() = 0;
118
119   // Replaces the texture with the texture from the specified mailbox.
120   virtual void Consume(const gpu::Mailbox& mailbox,
121                        const gfx::Size& new_size) {}
122
123   // Moves the texture into the mailbox and returns the mailbox name.
124   // The texture must have been previously consumed from a mailbox.
125   virtual gpu::Mailbox Produce();
126
127  protected:
128   virtual ~Texture();
129   gfx::Size size_;  // in pixel
130
131  private:
132   friend class base::RefCounted<Texture>;
133
134   bool flipped_;
135   float device_scale_factor_;
136
137   DISALLOW_COPY_AND_ASSIGN(Texture);
138 };
139
140 // This class represents a lock on the compositor, that can be used to prevent
141 // commits to the compositor tree while we're waiting for an asynchronous
142 // event. The typical use case is when waiting for a renderer to produce a frame
143 // at the right size. The caller keeps a reference on this object, and drops the
144 // reference once it desires to release the lock.
145 // Note however that the lock is cancelled after a short timeout to ensure
146 // responsiveness of the UI, so the compositor tree should be kept in a
147 // "reasonable" state while the lock is held.
148 // Don't instantiate this class directly, use Compositor::GetCompositorLock.
149 class COMPOSITOR_EXPORT CompositorLock
150     : public base::RefCounted<CompositorLock>,
151       public base::SupportsWeakPtr<CompositorLock> {
152  private:
153   friend class base::RefCounted<CompositorLock>;
154   friend class Compositor;
155
156   explicit CompositorLock(Compositor* compositor);
157   ~CompositorLock();
158
159   void CancelLock();
160
161   Compositor* compositor_;
162   DISALLOW_COPY_AND_ASSIGN(CompositorLock);
163 };
164
165 // Compositor object to take care of GPU painting.
166 // A Browser compositor object is responsible for generating the final
167 // displayable form of pixels comprising a single widget's contents. It draws an
168 // appropriately transformed texture for each transformed view in the widget's
169 // view hierarchy.
170 class COMPOSITOR_EXPORT Compositor
171     : NON_EXPORTED_BASE(public cc::LayerTreeHostClient),
172       NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) {
173  public:
174   explicit Compositor(gfx::AcceleratedWidget widget);
175   virtual ~Compositor();
176
177   static void Initialize();
178   static bool WasInitializedWithThread();
179   static scoped_refptr<base::MessageLoopProxy> GetCompositorMessageLoop();
180   static void Terminate();
181   static void SetSharedBitmapManager(cc::SharedBitmapManager* manager);
182
183   // Schedules a redraw of the layer tree associated with this compositor.
184   void ScheduleDraw();
185
186   // Sets the root of the layer tree drawn by this Compositor. The root layer
187   // must have no parent. The compositor's root layer is reset if the root layer
188   // is destroyed. NULL can be passed to reset the root layer, in which case the
189   // compositor will stop drawing anything.
190   // The Compositor does not own the root layer.
191   const Layer* root_layer() const { return root_layer_; }
192   Layer* root_layer() { return root_layer_; }
193   void SetRootLayer(Layer* root_layer);
194
195   // Called when we need the compositor to preserve the alpha channel in the
196   // output for situations when we want to render transparently atop something
197   // else, e.g. Aero glass.
198   void SetHostHasTransparentBackground(bool host_has_transparent_background);
199
200   // The scale factor of the device that this compositor is
201   // compositing layers on.
202   float device_scale_factor() const { return device_scale_factor_; }
203
204   // Draws the scene created by the layer tree and any visual effects.
205   void Draw();
206
207   // Where possible, draws are scissored to a damage region calculated from
208   // changes to layer properties.  This bypasses that and indicates that
209   // the whole frame needs to be drawn.
210   void ScheduleFullRedraw();
211
212   // Schedule redraw and append damage_rect to the damage region calculated
213   // from changes to layer properties.
214   void ScheduleRedrawRect(const gfx::Rect& damage_rect);
215
216   void SetLatencyInfo(const LatencyInfo& latency_info);
217
218   // Sets the compositor's device scale factor and size.
219   void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel);
220
221   // Returns the size of the widget that is being drawn to in pixel coordinates.
222   const gfx::Size& size() const { return size_; }
223
224   // Sets the background color used for areas that aren't covered by
225   // the |root_layer|.
226   void SetBackgroundColor(SkColor color);
227
228   // Returns the widget for this compositor.
229   gfx::AcceleratedWidget widget() const { return widget_; }
230
231   // Returns the vsync manager for this compositor.
232   scoped_refptr<CompositorVSyncManager> vsync_manager() const;
233
234   // Compositor does not own observers. It is the responsibility of the
235   // observer to remove itself when it is done observing.
236   void AddObserver(CompositorObserver* observer);
237   void RemoveObserver(CompositorObserver* observer);
238   bool HasObserver(CompositorObserver* observer);
239
240   // Creates a compositor lock. Returns NULL if it is not possible to lock at
241   // this time (i.e. we're waiting to complete a previous unlock).
242   scoped_refptr<CompositorLock> GetCompositorLock();
243
244   // Internal functions, called back by command-buffer contexts on swap buffer
245   // events.
246
247   // Signals swap has been posted.
248   void OnSwapBuffersPosted();
249
250   // Signals swap has completed.
251   void OnSwapBuffersComplete();
252
253   // Signals swap has aborted (e.g. lost context).
254   void OnSwapBuffersAborted();
255
256   // LayerTreeHostClient implementation.
257   virtual void WillBeginMainFrame(int frame_id) OVERRIDE {}
258   virtual void DidBeginMainFrame() OVERRIDE {}
259   virtual void Animate(base::TimeTicks frame_begin_time) OVERRIDE {}
260   virtual void Layout() OVERRIDE;
261   virtual void ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
262                                    float page_scale) OVERRIDE {}
263   virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback)
264       OVERRIDE;
265   virtual void DidInitializeOutputSurface(bool success) OVERRIDE {}
266   virtual void WillCommit() OVERRIDE {}
267   virtual void DidCommit() OVERRIDE;
268   virtual void DidCommitAndDrawFrame() OVERRIDE;
269   virtual void DidCompleteSwapBuffers() OVERRIDE;
270   virtual scoped_refptr<cc::ContextProvider>
271       OffscreenContextProvider() OVERRIDE;
272
273   // cc::LayerTreeHostSingleThreadClient implementation.
274   virtual void ScheduleComposite() OVERRIDE;
275   virtual void ScheduleAnimation() OVERRIDE;
276   virtual void DidPostSwapBuffers() OVERRIDE;
277   virtual void DidAbortSwapBuffers() OVERRIDE;
278
279   int last_started_frame() { return last_started_frame_; }
280   int last_ended_frame() { return last_ended_frame_; }
281
282   bool IsLocked() { return compositor_lock_ != NULL; }
283
284   const cc::LayerTreeDebugState& GetLayerTreeDebugState() const;
285   void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state);
286
287  private:
288   friend class base::RefCounted<Compositor>;
289   friend class CompositorLock;
290
291   // Called by CompositorLock.
292   void UnlockCompositor();
293
294   // Called to release any pending CompositorLock
295   void CancelCompositorLock();
296
297   // Notifies the compositor that compositing is complete.
298   void NotifyEnd();
299
300   gfx::Size size_;
301
302   // The root of the Layer tree drawn by this compositor.
303   Layer* root_layer_;
304
305   ObserverList<CompositorObserver> observer_list_;
306
307   gfx::AcceleratedWidget widget_;
308   scoped_refptr<cc::Layer> root_web_layer_;
309   scoped_ptr<cc::LayerTreeHost> host_;
310
311   // The manager of vsync parameters for this compositor.
312   scoped_refptr<CompositorVSyncManager> vsync_manager_;
313
314   // Used to verify that we have at most one draw swap in flight.
315   scoped_ptr<PostedSwapQueue> posted_swaps_;
316
317   // The device scale factor of the monitor that this compositor is compositing
318   // layers on.
319   float device_scale_factor_;
320
321   int last_started_frame_;
322   int last_ended_frame_;
323
324   bool next_draw_is_resize_;
325
326   bool disable_schedule_composite_;
327
328   CompositorLock* compositor_lock_;
329
330   // Prevent more than one draw from being scheduled.
331   bool defer_draw_scheduling_;
332
333   // Used to prevent Draw()s while a composite is in progress.
334   bool waiting_on_compositing_end_;
335   bool draw_on_compositing_end_;
336
337   base::WeakPtrFactory<Compositor> schedule_draw_factory_;
338
339   DISALLOW_COPY_AND_ASSIGN(Compositor);
340 };
341
342 }  // namespace ui
343
344 #endif  // UI_COMPOSITOR_COMPOSITOR_H_