OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_COMPOSITOR_COMPOSITOR_H_ | 5 #ifndef UI_GFX_COMPOSITOR_COMPOSITOR_H_ |
6 #define UI_GFX_COMPOSITOR_COMPOSITOR_H_ | 6 #define UI_GFX_COMPOSITOR_COMPOSITOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/singleton.h" |
10 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebLayer.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebLayerTree
View.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebLayerTree
ViewClient.h" |
11 #include "ui/gfx/compositor/compositor_export.h" | 15 #include "ui/gfx/compositor/compositor_export.h" |
12 #include "ui/gfx/transform.h" | 16 #include "ui/gfx/transform.h" |
13 #include "ui/gfx/native_widget_types.h" | 17 #include "ui/gfx/native_widget_types.h" |
14 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
15 | 19 |
| 20 |
16 class SkBitmap; | 21 class SkBitmap; |
17 class SkCanvas; | 22 class SkCanvas; |
18 namespace gfx { | 23 namespace gfx { |
| 24 class GLContext; |
| 25 class GLSurface; |
| 26 class GLShareGroup; |
19 class Point; | 27 class Point; |
20 class Rect; | 28 class Rect; |
21 class ScopedMakeCurrent; | 29 class ScopedMakeCurrent; |
22 } | 30 } |
23 | 31 |
24 namespace ui { | 32 namespace ui { |
25 | 33 |
26 class CompositorObserver; | 34 class CompositorObserver; |
27 class Layer; | 35 class Layer; |
28 | 36 |
29 class SharedResources { | 37 class COMPOSITOR_EXPORT SharedResources { |
30 public: | 38 public: |
31 virtual ~SharedResources() {} | 39 static SharedResources* GetInstance(); |
32 | 40 |
33 // Creates an instance of ScopedMakeCurrent. | 41 // Creates an instance of ScopedMakeCurrent. |
34 // Note: Caller is responsible for managing lifetime of returned pointer. | 42 // Note: Caller is responsible for managing lifetime of returned pointer. |
35 virtual gfx::ScopedMakeCurrent* GetScopedMakeCurrent() = 0; | 43 gfx::ScopedMakeCurrent* GetScopedMakeCurrent(); |
36 | 44 |
37 virtual void* GetDisplay() = 0; | 45 void* GetDisplay(); |
| 46 gfx::GLShareGroup* GetShareGroup(); |
| 47 |
| 48 private: |
| 49 friend struct DefaultSingletonTraits<SharedResources>; |
| 50 |
| 51 SharedResources(); |
| 52 ~SharedResources(); |
| 53 |
| 54 bool Initialize(); |
| 55 void Destroy(); |
| 56 |
| 57 bool initialized_; |
| 58 |
| 59 scoped_refptr<gfx::GLContext> context_; |
| 60 scoped_refptr<gfx::GLSurface> surface_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(SharedResources); |
38 }; | 63 }; |
39 | 64 |
40 struct TextureDrawParams { | 65 // Texture provide an abstraction over the external texture that can be passed |
41 TextureDrawParams(); | 66 // to a layer. |
42 | |
43 // The transform to be applied to the texture. | |
44 ui::Transform transform; | |
45 | |
46 // If this is true, then the texture is blended with the pixels behind it. | |
47 // Otherwise, the drawn pixels clobber the old pixels. | |
48 bool blend; | |
49 | |
50 // If this is false, the alpha values for this texture should not be trusted. | |
51 bool has_valid_alpha_channel; | |
52 | |
53 // This multiplier is applied to all pixels before blending. The intent is to | |
54 // allow alpha to be animated (for effects such as cross fades). | |
55 float opacity; | |
56 | |
57 // Sometimes the texture is vertically flipped. In this case we have to | |
58 // draw the texture differently. | |
59 bool vertically_flipped; | |
60 | |
61 // The size of the surface that the texture is drawn to. | |
62 gfx::Size compositor_size; | |
63 | |
64 // Copy and assignment are allowed. | |
65 }; | |
66 | |
67 // Textures are created by a Compositor for managing an accelerated view. | |
68 // Any time a View with a texture needs to redraw itself it invokes SetCanvas(). | |
69 // When the view is ready to be drawn Draw() is invoked. | |
70 // | |
71 // Texture is really a proxy to the gpu. Texture does not itself keep a copy of | |
72 // the bitmap. | |
73 // | |
74 // Views own the Texture. | |
75 class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> { | 67 class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> { |
76 public: | 68 public: |
77 // Sets the canvas of this texture. The origin is at |origin|. | 69 Texture(); |
78 // |overall_size| gives the total size of texture. | 70 virtual ~Texture(); |
79 virtual void SetCanvas(const SkCanvas& canvas, | |
80 const gfx::Point& origin, | |
81 const gfx::Size& overall_size) = 0; | |
82 | 71 |
83 // Draws the portion of the texture contained within clip_bounds | 72 unsigned int texture_id() const { return texture_id_; } |
84 virtual void Draw(const ui::TextureDrawParams& params, | 73 bool flipped() const { return flipped_; } |
85 const gfx::Rect& clip_bounds_in_texture) = 0; | 74 gfx::Size size() const { return size_; } |
86 | 75 |
87 protected: | 76 protected: |
88 virtual ~Texture() {} | 77 unsigned int texture_id_; |
| 78 bool flipped_; |
| 79 gfx::Size size_; |
89 | 80 |
90 private: | 81 private: |
91 friend class base::RefCounted<Texture>; | 82 DISALLOW_COPY_AND_ASSIGN(Texture); |
92 }; | 83 }; |
93 | 84 |
94 // An interface to allow the compositor to communicate with its owner. | 85 // An interface to allow the compositor to communicate with its owner. |
95 class COMPOSITOR_EXPORT CompositorDelegate { | 86 class COMPOSITOR_EXPORT CompositorDelegate { |
96 public: | 87 public: |
97 // Requests the owner to schedule a redraw of the layer tree. | 88 // Requests the owner to schedule a redraw of the layer tree. |
98 virtual void ScheduleDraw() = 0; | 89 virtual void ScheduleDraw() = 0; |
99 | 90 |
100 protected: | 91 protected: |
101 virtual ~CompositorDelegate() {} | 92 virtual ~CompositorDelegate() {} |
102 }; | 93 }; |
103 | 94 |
104 // Compositor object to take care of GPU painting. | 95 // Compositor object to take care of GPU painting. |
105 // A Browser compositor object is responsible for generating the final | 96 // A Browser compositor object is responsible for generating the final |
106 // displayable form of pixels comprising a single widget's contents. It draws an | 97 // displayable form of pixels comprising a single widget's contents. It draws an |
107 // appropriately transformed texture for each transformed view in the widget's | 98 // appropriately transformed texture for each transformed view in the widget's |
108 // view hierarchy. | 99 // view hierarchy. |
109 class COMPOSITOR_EXPORT Compositor : public base::RefCounted<Compositor> { | 100 class COMPOSITOR_EXPORT Compositor |
| 101 : public base::RefCounted<Compositor>, |
| 102 NON_EXPORTED_BASE(public WebKit::WebLayerTreeViewClient) { |
110 public: | 103 public: |
111 // Create a compositor from the provided handle. | 104 Compositor(CompositorDelegate* delegate, |
112 static Compositor* Create(CompositorDelegate* delegate, | 105 gfx::AcceleratedWidget widget, |
113 gfx::AcceleratedWidget widget, | 106 const gfx::Size& size); |
114 const gfx::Size& size); | 107 virtual ~Compositor(); |
115 | 108 |
116 // Creates a new texture. The caller owns the returned object. | 109 static void Initialize(bool useThread); |
117 virtual Texture* CreateTexture() = 0; | 110 static void Terminate(); |
118 | |
119 // Blurs the specific region in the compositor. | |
120 virtual void Blur(const gfx::Rect& bounds) = 0; | |
121 | 111 |
122 // Schedules a redraw of the layer tree associated with this compositor. | 112 // Schedules a redraw of the layer tree associated with this compositor. |
123 virtual void ScheduleDraw(); | 113 void ScheduleDraw(); |
124 | 114 |
125 // Sets the root of the layer tree drawn by this Compositor. The root layer | 115 // Sets the root of the layer tree drawn by this Compositor. The root layer |
126 // must have no parent. The compositor's root layer is reset if the root layer | 116 // must have no parent. The compositor's root layer is reset if the root layer |
127 // is destroyed. NULL can be passed to reset the root layer, in which case the | 117 // is destroyed. NULL can be passed to reset the root layer, in which case the |
128 // compositor will stop drawing anything. | 118 // compositor will stop drawing anything. |
129 // The Compositor does not own the root layer. | 119 // The Compositor does not own the root layer. |
130 const Layer* root_layer() const { return root_layer_; } | 120 const Layer* root_layer() const { return root_layer_; } |
131 Layer* root_layer() { return root_layer_; } | 121 Layer* root_layer() { return root_layer_; } |
132 void SetRootLayer(Layer* root_layer); | 122 void SetRootLayer(Layer* root_layer); |
133 | 123 |
134 // Draws the scene created by the layer tree and any visual effects. If | 124 // Draws the scene created by the layer tree and any visual effects. If |
135 // |force_clear| is true, this will cause the compositor to clear before | 125 // |force_clear| is true, this will cause the compositor to clear before |
136 // compositing. | 126 // compositing. |
137 void Draw(bool force_clear); | 127 void Draw(bool force_clear); |
138 | 128 |
139 // Reads the region |bounds| of the contents of the last rendered frame | 129 // Reads the region |bounds| of the contents of the last rendered frame |
140 // into the given bitmap. | 130 // into the given bitmap. |
141 // Returns false if the pixels could not be read. | 131 // Returns false if the pixels could not be read. |
142 virtual bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds) = 0; | 132 bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds); |
143 | 133 |
144 // Notifies the compositor that the size of the widget that it is | 134 // Notifies the compositor that the size of the widget that it is |
145 // drawing to has changed. | 135 // drawing to has changed. |
146 void WidgetSizeChanged(const gfx::Size& size) { | 136 void WidgetSizeChanged(const gfx::Size& size); |
147 if (size.IsEmpty()) | |
148 return; | |
149 size_ = size; | |
150 OnWidgetSizeChanged(); | |
151 } | |
152 | 137 |
153 // Returns the size of the widget that is being drawn to. | 138 // Returns the size of the widget that is being drawn to. |
154 const gfx::Size& size() { return size_; } | 139 const gfx::Size& size() { return size_; } |
155 | 140 |
156 // Compositor does not own observers. It is the responsibility of the | 141 // Compositor does not own observers. It is the responsibility of the |
157 // observer to remove itself when it is done observing. | 142 // observer to remove itself when it is done observing. |
158 void AddObserver(CompositorObserver* observer); | 143 void AddObserver(CompositorObserver* observer); |
159 void RemoveObserver(CompositorObserver* observer); | 144 void RemoveObserver(CompositorObserver* observer); |
160 bool HasObserver(CompositorObserver* observer); | 145 bool HasObserver(CompositorObserver* observer); |
161 | 146 |
162 protected: | 147 // WebLayerTreeViewClient implementation. |
163 Compositor(CompositorDelegate* delegate, const gfx::Size& size); | 148 virtual void updateAnimations(double frameBeginTime); |
164 virtual ~Compositor(); | 149 virtual void layout(); |
| 150 virtual void applyScrollAndScale(const WebKit::WebSize& scrollDelta, |
| 151 float scaleFactor); |
| 152 virtual WebKit::WebGraphicsContext3D* createContext3D(); |
| 153 virtual void didCompleteSwapBuffers(); |
| 154 virtual void didRebindGraphicsContext(bool success); |
| 155 virtual void scheduleComposite(); |
165 | 156 |
166 // Notifies the compositor that compositing is about to start. | 157 private: |
167 virtual void OnNotifyStart(bool clear) = 0; | |
168 | |
169 // Notifies the compositor that compositing is complete. | |
170 virtual void OnNotifyEnd() = 0; | |
171 | |
172 virtual void OnWidgetSizeChanged() = 0; | |
173 virtual void OnRootLayerChanged(); | |
174 virtual void DrawTree(); | |
175 virtual bool CompositesAsynchronously(); | |
176 | |
177 CompositorDelegate* delegate() { return delegate_; } | |
178 | |
179 // When reading back pixel data we often get RGBA rather than BGRA pixels and | 158 // When reading back pixel data we often get RGBA rather than BGRA pixels and |
180 // and the image often needs to be flipped vertically. | 159 // and the image often needs to be flipped vertically. |
181 static void SwizzleRGBAToBGRAAndFlip(unsigned char* pixels, | 160 static void SwizzleRGBAToBGRAAndFlip(unsigned char* pixels, |
182 const gfx::Size& image_size); | 161 const gfx::Size& image_size); |
183 | 162 |
184 // Notifies the compositor that compositing is complete. | 163 // Notifies the compositor that compositing is complete. |
185 void NotifyEnd(); | 164 void NotifyEnd(); |
186 | 165 |
187 private: | |
188 // Notifies the compositor that compositing is about to start. See Draw() for | |
189 // notes about |force_clear|. | |
190 void NotifyStart(bool force_clear); | |
191 | |
192 CompositorDelegate* delegate_; | 166 CompositorDelegate* delegate_; |
193 gfx::Size size_; | 167 gfx::Size size_; |
194 | 168 |
195 // The root of the Layer tree drawn by this compositor. | 169 // The root of the Layer tree drawn by this compositor. |
196 Layer* root_layer_; | 170 Layer* root_layer_; |
197 | 171 |
198 ObserverList<CompositorObserver> observer_list_; | 172 ObserverList<CompositorObserver> observer_list_; |
199 | 173 |
| 174 gfx::AcceleratedWidget widget_; |
| 175 WebKit::WebLayer root_web_layer_; |
| 176 WebKit::WebLayerTreeView host_; |
| 177 |
200 friend class base::RefCounted<Compositor>; | 178 friend class base::RefCounted<Compositor>; |
201 }; | 179 }; |
202 | 180 |
203 } // namespace ui | 181 } // namespace ui |
204 | 182 |
205 #endif // UI_GFX_COMPOSITOR_COMPOSITOR_H_ | 183 #endif // UI_GFX_COMPOSITOR_COMPOSITOR_H_ |
OLD | NEW |