OLD | NEW |
| (Empty) |
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_GFX_COMPOSITOR_COMPOSITOR_H_ | |
6 #define UI_GFX_COMPOSITOR_COMPOSITOR_H_ | |
7 #pragma once | |
8 | |
9 #include "base/hash_tables.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/observer_list.h" | |
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" | |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayerTreeView.h" | |
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayerTreeViewCli
ent.h" | |
15 #include "ui/gfx/compositor/compositor_export.h" | |
16 #include "ui/gfx/gl/gl_share_group.h" | |
17 #include "ui/gfx/native_widget_types.h" | |
18 #include "ui/gfx/size.h" | |
19 #include "ui/gfx/transform.h" | |
20 | |
21 | |
22 class SkBitmap; | |
23 namespace gfx { | |
24 class GLContext; | |
25 class GLSurface; | |
26 class GLShareGroup; | |
27 class Point; | |
28 class Rect; | |
29 } | |
30 | |
31 namespace ui { | |
32 | |
33 class Compositor; | |
34 class CompositorObserver; | |
35 class Layer; | |
36 | |
37 // This class abstracts the creation of the 3D context for the compositor. It is | |
38 // a global object. | |
39 class COMPOSITOR_EXPORT ContextFactory { | |
40 public: | |
41 virtual ~ContextFactory() {} | |
42 | |
43 // Gets the global instance. | |
44 static ContextFactory* GetInstance(); | |
45 | |
46 // Sets the global instance. Caller keeps ownership. | |
47 // If this function isn't called (for tests), a "default" factory will be | |
48 // created on the first call of GetInstance. | |
49 static void SetInstance(ContextFactory* instance); | |
50 | |
51 // Creates a context for given compositor. The factory may keep per-compositor | |
52 // data (e.g. a shared context), that needs to be cleaned up by calling | |
53 // RemoveCompositor when the compositor gets destroyed. | |
54 virtual WebKit::WebGraphicsContext3D* CreateContext( | |
55 Compositor* compositor) = 0; | |
56 | |
57 // Creates a context for given compositor used for offscreen rendering. See | |
58 // the comments of CreateContext to know how per-compositor data is handled. | |
59 virtual WebKit::WebGraphicsContext3D* CreateOffscreenContext( | |
60 Compositor* compositor) = 0; | |
61 | |
62 // Destroys per-compositor data. | |
63 virtual void RemoveCompositor(Compositor* compositor) = 0; | |
64 }; | |
65 | |
66 // The default factory that creates in-process contexts. | |
67 class COMPOSITOR_EXPORT DefaultContextFactory : public ContextFactory { | |
68 public: | |
69 DefaultContextFactory(); | |
70 virtual ~DefaultContextFactory(); | |
71 | |
72 // ContextFactory implementation | |
73 virtual WebKit::WebGraphicsContext3D* CreateContext( | |
74 Compositor* compositor) OVERRIDE; | |
75 virtual WebKit::WebGraphicsContext3D* CreateOffscreenContext( | |
76 Compositor* compositor) OVERRIDE; | |
77 virtual void RemoveCompositor(Compositor* compositor) OVERRIDE; | |
78 | |
79 bool Initialize(); | |
80 | |
81 void set_share_group(gfx::GLShareGroup* share_group) { | |
82 share_group_ = share_group; | |
83 } | |
84 | |
85 private: | |
86 WebKit::WebGraphicsContext3D* CreateContextCommon( | |
87 Compositor* compositor, | |
88 bool offscreen); | |
89 | |
90 scoped_refptr<gfx::GLShareGroup> share_group_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(DefaultContextFactory); | |
93 }; | |
94 | |
95 // Texture provide an abstraction over the external texture that can be passed | |
96 // to a layer. | |
97 class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> { | |
98 public: | |
99 Texture(bool flipped, const gfx::Size& size); | |
100 | |
101 unsigned int texture_id() const { return texture_id_; } | |
102 void set_texture_id(unsigned int id) { texture_id_ = id; } | |
103 bool flipped() const { return flipped_; } | |
104 gfx::Size size() const { return size_; } | |
105 | |
106 protected: | |
107 virtual ~Texture(); | |
108 | |
109 private: | |
110 friend class base::RefCounted<Texture>; | |
111 | |
112 unsigned int texture_id_; | |
113 bool flipped_; | |
114 gfx::Size size_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(Texture); | |
117 }; | |
118 | |
119 // An interface to allow the compositor to communicate with its owner. | |
120 class COMPOSITOR_EXPORT CompositorDelegate { | |
121 public: | |
122 // Requests the owner to schedule a redraw of the layer tree. | |
123 virtual void ScheduleDraw() = 0; | |
124 | |
125 protected: | |
126 virtual ~CompositorDelegate() {} | |
127 }; | |
128 | |
129 // Compositor object to take care of GPU painting. | |
130 // A Browser compositor object is responsible for generating the final | |
131 // displayable form of pixels comprising a single widget's contents. It draws an | |
132 // appropriately transformed texture for each transformed view in the widget's | |
133 // view hierarchy. | |
134 class COMPOSITOR_EXPORT Compositor | |
135 : NON_EXPORTED_BASE(public WebKit::WebLayerTreeViewClient) { | |
136 public: | |
137 Compositor(CompositorDelegate* delegate, | |
138 gfx::AcceleratedWidget widget, | |
139 const gfx::Size& size); | |
140 virtual ~Compositor(); | |
141 | |
142 static void Initialize(bool useThread); | |
143 static void Terminate(); | |
144 | |
145 // Schedules a redraw of the layer tree associated with this compositor. | |
146 void ScheduleDraw(); | |
147 | |
148 // Sets the root of the layer tree drawn by this Compositor. The root layer | |
149 // must have no parent. The compositor's root layer is reset if the root layer | |
150 // is destroyed. NULL can be passed to reset the root layer, in which case the | |
151 // compositor will stop drawing anything. | |
152 // The Compositor does not own the root layer. | |
153 const Layer* root_layer() const { return root_layer_; } | |
154 Layer* root_layer() { return root_layer_; } | |
155 void SetRootLayer(Layer* root_layer); | |
156 | |
157 // Draws the scene created by the layer tree and any visual effects. If | |
158 // |force_clear| is true, this will cause the compositor to clear before | |
159 // compositing. | |
160 void Draw(bool force_clear); | |
161 | |
162 // Where possible, draws are scissored to a damage region calculated from | |
163 // changes to layer properties. This bypasses that and indicates that | |
164 // the whole frame needs to be drawn. | |
165 void ScheduleFullDraw(); | |
166 | |
167 // Reads the region |bounds| of the contents of the last rendered frame | |
168 // into the given bitmap. | |
169 // Returns false if the pixels could not be read. | |
170 bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds); | |
171 | |
172 // Notifies the compositor that the size of the widget that it is | |
173 // drawing to has changed. | |
174 void WidgetSizeChanged(const gfx::Size& size); | |
175 | |
176 // Returns the size of the widget that is being drawn to. | |
177 const gfx::Size& size() const { return size_; } | |
178 | |
179 // Returns the widget for this compositor. | |
180 gfx::AcceleratedWidget widget() const { return widget_; } | |
181 | |
182 // Compositor does not own observers. It is the responsibility of the | |
183 // observer to remove itself when it is done observing. | |
184 void AddObserver(CompositorObserver* observer); | |
185 void RemoveObserver(CompositorObserver* observer); | |
186 bool HasObserver(CompositorObserver* observer); | |
187 | |
188 // Returns whether a draw is pending, that is, if we're between the Draw call | |
189 // and the OnCompositingEnded. | |
190 bool DrawPending() const { return swap_posted_; } | |
191 | |
192 // Internal functions, called back by command-buffer contexts on swap buffer | |
193 // events. | |
194 | |
195 // Signals swap has been posted. | |
196 void OnSwapBuffersPosted(); | |
197 | |
198 // Signals swap has completed. | |
199 void OnSwapBuffersComplete(); | |
200 | |
201 // Signals swap has aborted (e.g. lost context). | |
202 void OnSwapBuffersAborted(); | |
203 | |
204 // WebLayerTreeViewClient implementation. | |
205 virtual void updateAnimations(double frameBeginTime); | |
206 virtual void layout(); | |
207 virtual void applyScrollAndScale(const WebKit::WebSize& scrollDelta, | |
208 float scaleFactor); | |
209 virtual WebKit::WebGraphicsContext3D* createContext3D(); | |
210 virtual void didRebindGraphicsContext(bool success); | |
211 virtual void didCommitAndDrawFrame(); | |
212 virtual void didCompleteSwapBuffers(); | |
213 virtual void scheduleComposite(); | |
214 | |
215 private: | |
216 // When reading back pixel data we often get RGBA rather than BGRA pixels and | |
217 // and the image often needs to be flipped vertically. | |
218 static void SwizzleRGBAToBGRAAndFlip(unsigned char* pixels, | |
219 const gfx::Size& image_size); | |
220 | |
221 // Notifies the compositor that compositing is complete. | |
222 void NotifyEnd(); | |
223 | |
224 CompositorDelegate* delegate_; | |
225 gfx::Size size_; | |
226 | |
227 // The root of the Layer tree drawn by this compositor. | |
228 Layer* root_layer_; | |
229 | |
230 ObserverList<CompositorObserver> observer_list_; | |
231 | |
232 gfx::AcceleratedWidget widget_; | |
233 WebKit::WebLayer root_web_layer_; | |
234 WebKit::WebLayerTreeView host_; | |
235 | |
236 // This is set to true when the swap buffers has been posted and we're waiting | |
237 // for completion. | |
238 bool swap_posted_; | |
239 | |
240 friend class base::RefCounted<Compositor>; | |
241 }; | |
242 | |
243 } // namespace ui | |
244 | |
245 #endif // UI_GFX_COMPOSITOR_COMPOSITOR_H_ | |
OLD | NEW |