compositor.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "base/hash_tables.h" 9#include "base/memory/ref_counted.h" 10#include "base/memory/scoped_ptr.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/WebLayerTreeViewClient.h" 15#include "ui/compositor/compositor_export.h" 16#include "ui/gfx/native_widget_types.h" 17#include "ui/gfx/size.h" 18#include "ui/gfx/transform.h" 19#include "ui/gl/gl_share_group.h" 20 21class SkBitmap; 22 23namespace gfx { 24class GLContext; 25class GLSurface; 26class GLShareGroup; 27class Point; 28class Rect; 29} 30 31namespace ui { 32 33class Compositor; 34class CompositorObserver; 35class Layer; 36class PostedSwapQueue; 37 38// This class abstracts the creation of the 3D context for the compositor. It is 39// a global object. 40class COMPOSITOR_EXPORT ContextFactory { 41 public: 42 virtual ~ContextFactory() {} 43 44 // Gets the global instance. 45 static ContextFactory* GetInstance(); 46 47 // Sets the global instance. Caller keeps ownership. 48 // If this function isn't called (for tests), a "default" factory will be 49 // created on the first call of GetInstance. 50 static void SetInstance(ContextFactory* instance); 51 52 // Creates an output surface for the given compositor. The factory may keep 53 // per-compositor data (e.g. a shared context), that needs to be cleaned up 54 // by calling RemoveCompositor when the compositor gets destroyed. 55 virtual WebKit::WebCompositorOutputSurface* CreateOutputSurface( 56 Compositor* compositor) = 0; 57 58 // Creates a context used for offscreen rendering. This context can be shared 59 // with all compositors. 60 virtual WebKit::WebGraphicsContext3D* CreateOffscreenContext() = 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. 67class COMPOSITOR_EXPORT DefaultContextFactory : public ContextFactory { 68 public: 69 DefaultContextFactory(); 70 virtual ~DefaultContextFactory(); 71 72 // ContextFactory implementation 73 virtual WebKit::WebCompositorOutputSurface* CreateOutputSurface( 74 Compositor* compositor) OVERRIDE; 75 virtual WebKit::WebGraphicsContext3D* CreateOffscreenContext() OVERRIDE; 76 virtual void RemoveCompositor(Compositor* compositor) OVERRIDE; 77 78 bool Initialize(); 79 80 void set_share_group(gfx::GLShareGroup* share_group) { 81 share_group_ = share_group; 82 } 83 84 private: 85 WebKit::WebGraphicsContext3D* CreateContextCommon( 86 Compositor* compositor, 87 bool offscreen); 88 89 scoped_refptr<gfx::GLShareGroup> share_group_; 90 91 DISALLOW_COPY_AND_ASSIGN(DefaultContextFactory); 92}; 93 94// Texture provide an abstraction over the external texture that can be passed 95// to a layer. 96class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> { 97 public: 98 Texture(bool flipped, const gfx::Size& size, float device_scale_factor); 99 100 bool flipped() const { return flipped_; } 101 gfx::Size size() const { return size_; } 102 float device_scale_factor() const { return device_scale_factor_; } 103 104 virtual unsigned int PrepareTexture() = 0; 105 virtual WebKit::WebGraphicsContext3D* HostContext3D() = 0; 106 107 protected: 108 virtual ~Texture(); 109 110 private: 111 friend class base::RefCounted<Texture>; 112 113 bool flipped_; 114 gfx::Size size_; // in pixel 115 float device_scale_factor_; 116 117 DISALLOW_COPY_AND_ASSIGN(Texture); 118}; 119 120// An interface to allow the compositor to communicate with its owner. 121class COMPOSITOR_EXPORT CompositorDelegate { 122 public: 123 // Requests the owner to schedule a redraw of the layer tree. 124 virtual void ScheduleDraw() = 0; 125 126 protected: 127 virtual ~CompositorDelegate() {} 128}; 129 130// This class represents a lock on the compositor, that can be used to prevent 131// commits to the compositor tree while we're waiting for an asynchronous 132// event. The typical use case is when waiting for a renderer to produce a frame 133// at the right size. The caller keeps a reference on this object, and drops the 134// reference once it desires to release the lock. 135// Note however that the lock is cancelled after a short timeout to ensure 136// responsiveness of the UI, so the compositor tree should be kept in a 137// "reasonable" state while the lock is held. 138// Don't instantiate this class directly, use Compositor::GetCompositorLock. 139class COMPOSITOR_EXPORT CompositorLock 140 : public base::RefCounted<CompositorLock>, 141 public base::SupportsWeakPtr<CompositorLock> { 142 private: 143 friend class base::RefCounted<CompositorLock>; 144 friend class Compositor; 145 146 explicit CompositorLock(Compositor* compositor); 147 ~CompositorLock(); 148 149 void CancelLock(); 150 151 Compositor* compositor_; 152 DISALLOW_COPY_AND_ASSIGN(CompositorLock); 153}; 154 155 156// Compositor object to take care of GPU painting. 157// A Browser compositor object is responsible for generating the final 158// displayable form of pixels comprising a single widget's contents. It draws an 159// appropriately transformed texture for each transformed view in the widget's 160// view hierarchy. 161class COMPOSITOR_EXPORT Compositor 162 : NON_EXPORTED_BASE(public WebKit::WebLayerTreeViewClient) { 163 public: 164 Compositor(CompositorDelegate* delegate, 165 gfx::AcceleratedWidget widget); 166 virtual ~Compositor(); 167 168 static void Initialize(bool useThread); 169 static void Terminate(); 170 171 // Schedules a redraw of the layer tree associated with this compositor. 172 void ScheduleDraw(); 173 174 // Sets the root of the layer tree drawn by this Compositor. The root layer 175 // must have no parent. The compositor's root layer is reset if the root layer 176 // is destroyed. NULL can be passed to reset the root layer, in which case the 177 // compositor will stop drawing anything. 178 // The Compositor does not own the root layer. 179 const Layer* root_layer() const { return root_layer_; } 180 Layer* root_layer() { return root_layer_; } 181 void SetRootLayer(Layer* root_layer); 182 183 // Called when we need the compositor to preserve the alpha channel in the 184 // output for situations when we want to render transparently atop something 185 // else, e.g. Aero glass. 186 void SetHostHasTransparentBackground(bool host_has_transparent_background); 187 188 // The scale factor of the device that this compositor is 189 // compositing layers on. 190 float device_scale_factor() const { return device_scale_factor_; } 191 192 // Draws the scene created by the layer tree and any visual effects. If 193 // |force_clear| is true, this will cause the compositor to clear before 194 // compositing. 195 void Draw(bool force_clear); 196 197 // Where possible, draws are scissored to a damage region calculated from 198 // changes to layer properties. This bypasses that and indicates that 199 // the whole frame needs to be drawn. 200 void ScheduleFullDraw(); 201 202 // Reads the region |bounds_in_pixel| of the contents of the last rendered 203 // frame into the given bitmap. 204 // Returns false if the pixels could not be read. 205 bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds_in_pixel); 206 207 // Sets the compositor's device scale factor and size. 208 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel); 209 210 // Returns the size of the widget that is being drawn to in pixel coordinates. 211 const gfx::Size& size() const { return size_; } 212 213 // Returns the widget for this compositor. 214 gfx::AcceleratedWidget widget() const { return widget_; } 215 216 // Compositor does not own observers. It is the responsibility of the 217 // observer to remove itself when it is done observing. 218 void AddObserver(CompositorObserver* observer); 219 void RemoveObserver(CompositorObserver* observer); 220 bool HasObserver(CompositorObserver* observer); 221 222 // Creates a compositor lock. Returns NULL if it is not possible to lock at 223 // this time (i.e. we're waiting to complete a previous unlock). 224 scoped_refptr<CompositorLock> GetCompositorLock(); 225 226 // Internal functions, called back by command-buffer contexts on swap buffer 227 // events. 228 229 // Signals swap has been posted. 230 void OnSwapBuffersPosted(); 231 232 // Signals swap has completed. 233 void OnSwapBuffersComplete(); 234 235 // Signals swap has aborted (e.g. lost context). 236 void OnSwapBuffersAborted(); 237 238 // WebLayerTreeViewClient implementation. 239 virtual void updateAnimations(double frameBeginTime); 240 virtual void layout(); 241 virtual void applyScrollAndScale(const WebKit::WebSize& scrollDelta, 242 float scaleFactor); 243 virtual WebKit::WebCompositorOutputSurface* createOutputSurface(); 244 virtual void didRecreateOutputSurface(bool success); 245 virtual void didCommit(); 246 virtual void didCommitAndDrawFrame(); 247 virtual void didCompleteSwapBuffers(); 248 virtual void scheduleComposite(); 249 250 int last_started_frame() { return last_started_frame_; } 251 int last_ended_frame() { return last_ended_frame_; } 252 253 bool IsLocked() { return compositor_lock_ != NULL; } 254 255 private: 256 friend class base::RefCounted<Compositor>; 257 friend class CompositorLock; 258 259 // Called by CompositorLock. 260 void UnlockCompositor(); 261 262 // Called to release any pending CompositorLock 263 void CancelCompositorLock(); 264 265 // Notifies the compositor that compositing is complete. 266 void NotifyEnd(); 267 268 CompositorDelegate* delegate_; 269 gfx::Size size_; 270 271 // The root of the Layer tree drawn by this compositor. 272 Layer* root_layer_; 273 274 ObserverList<CompositorObserver> observer_list_; 275 276 gfx::AcceleratedWidget widget_; 277 scoped_ptr<WebKit::WebLayer> root_web_layer_; 278 scoped_ptr<WebKit::WebLayerTreeView> host_; 279 280 // Used to verify that we have at most one draw swap in flight. 281 scoped_ptr<PostedSwapQueue> posted_swaps_; 282 283 // The device scale factor of the monitor that this compositor is compositing 284 // layers on. 285 float device_scale_factor_; 286 287 int last_started_frame_; 288 int last_ended_frame_; 289 290 bool disable_schedule_composite_; 291 292 CompositorLock* compositor_lock_; 293 294 DISALLOW_COPY_AND_ASSIGN(Compositor); 295}; 296 297} // namespace ui 298 299#endif // UI_COMPOSITOR_COMPOSITOR_H_ 300