compositor.h revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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 "third_party/skia/include/core/SkColor.h" 17#include "ui/compositor/compositor_export.h" 18#include "ui/compositor/compositor_observer.h" 19#include "ui/gfx/native_widget_types.h" 20#include "ui/gfx/size.h" 21#include "ui/gfx/transform.h" 22#include "ui/gfx/vector2d.h" 23#include "ui/gl/gl_share_group.h" 24 25class SkBitmap; 26 27namespace base { 28class MessageLoopProxy; 29class RunLoop; 30} 31 32namespace cc { 33class ContextProvider; 34class Layer; 35class LayerTreeDebugState; 36class LayerTreeHost; 37} 38 39namespace gfx { 40class GLContext; 41class GLSurface; 42class GLShareGroup; 43class Point; 44class Rect; 45class Size; 46} 47 48namespace WebKit { 49class WebGraphicsContext3D; 50} 51 52namespace ui { 53 54class Compositor; 55class CompositorObserver; 56class ContextProviderFromContextFactory; 57class Layer; 58class PostedSwapQueue; 59class Reflector; 60class Texture; 61struct LatencyInfo; 62 63// This class abstracts the creation of the 3D context for the compositor. It is 64// a global object. 65class COMPOSITOR_EXPORT ContextFactory { 66 public: 67 virtual ~ContextFactory() {} 68 69 // Gets the global instance. 70 static ContextFactory* GetInstance(); 71 72 // Sets the global instance. Caller keeps ownership. 73 // If this function isn't called (for tests), a "default" factory will be 74 // created on the first call of GetInstance. 75 static void SetInstance(ContextFactory* instance); 76 77 // Creates an output surface for the given compositor. The factory may keep 78 // per-compositor data (e.g. a shared context), that needs to be cleaned up 79 // by calling RemoveCompositor when the compositor gets destroyed. 80 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 81 Compositor* compositor) = 0; 82 83 // Creates a context used for offscreen rendering. This context can be shared 84 // with all compositors. 85 virtual scoped_ptr<WebKit::WebGraphicsContext3D> CreateOffscreenContext() = 0; 86 87 // Creates a reflector that copies the content of the |mirrored_compositor| 88 // onto |mirroing_layer|. 89 virtual scoped_refptr<Reflector> CreateReflector( 90 Compositor* mirrored_compositor, 91 Layer* mirroring_layer) = 0; 92 // Removes the reflector, which stops the mirroring. 93 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0; 94 95 virtual scoped_refptr<cc::ContextProvider> 96 OffscreenContextProviderForMainThread() = 0; 97 virtual scoped_refptr<cc::ContextProvider> 98 OffscreenContextProviderForCompositorThread() = 0; 99 100 // Destroys per-compositor data. 101 virtual void RemoveCompositor(Compositor* compositor) = 0; 102}; 103 104// The default factory that creates in-process contexts. 105class COMPOSITOR_EXPORT DefaultContextFactory : public ContextFactory { 106 public: 107 DefaultContextFactory(); 108 virtual ~DefaultContextFactory(); 109 110 // ContextFactory implementation 111 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 112 Compositor* compositor) OVERRIDE; 113 virtual scoped_ptr<WebKit::WebGraphicsContext3D> CreateOffscreenContext() 114 OVERRIDE; 115 116 virtual scoped_refptr<Reflector> CreateReflector( 117 Compositor* compositor, 118 Layer* layer) OVERRIDE; 119 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) OVERRIDE; 120 121 virtual scoped_refptr<cc::ContextProvider> 122 OffscreenContextProviderForMainThread() OVERRIDE; 123 virtual scoped_refptr<cc::ContextProvider> 124 OffscreenContextProviderForCompositorThread() OVERRIDE; 125 virtual void RemoveCompositor(Compositor* compositor) OVERRIDE; 126 127 bool Initialize(); 128 129 private: 130 scoped_ptr<WebKit::WebGraphicsContext3D> CreateContextCommon( 131 Compositor* compositor, 132 bool offscreen); 133 134 scoped_refptr<ContextProviderFromContextFactory> 135 offscreen_contexts_main_thread_; 136 scoped_refptr<ContextProviderFromContextFactory> 137 offscreen_contexts_compositor_thread_; 138 139 DISALLOW_COPY_AND_ASSIGN(DefaultContextFactory); 140}; 141 142// The factory that creates test contexts. 143class COMPOSITOR_EXPORT TestContextFactory : public ContextFactory { 144 public: 145 TestContextFactory(); 146 virtual ~TestContextFactory(); 147 148 // ContextFactory implementation 149 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 150 Compositor* compositor) OVERRIDE; 151 virtual scoped_ptr<WebKit::WebGraphicsContext3D> CreateOffscreenContext() 152 OVERRIDE; 153 154 virtual scoped_refptr<Reflector> CreateReflector( 155 Compositor* mirrored_compositor, 156 Layer* mirroring_layer) OVERRIDE; 157 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) OVERRIDE; 158 159 virtual scoped_refptr<cc::ContextProvider> 160 OffscreenContextProviderForMainThread() OVERRIDE; 161 virtual scoped_refptr<cc::ContextProvider> 162 OffscreenContextProviderForCompositorThread() OVERRIDE; 163 virtual void RemoveCompositor(Compositor* compositor) OVERRIDE; 164 165 private: 166 scoped_refptr<ContextProviderFromContextFactory> 167 offscreen_contexts_main_thread_; 168 scoped_refptr<ContextProviderFromContextFactory> 169 offscreen_contexts_compositor_thread_; 170 171 DISALLOW_COPY_AND_ASSIGN(TestContextFactory); 172}; 173 174// Texture provide an abstraction over the external texture that can be passed 175// to a layer. 176class COMPOSITOR_EXPORT Texture : public base::RefCounted<Texture> { 177 public: 178 Texture(bool flipped, const gfx::Size& size, float device_scale_factor); 179 180 bool flipped() const { return flipped_; } 181 gfx::Size size() const { return size_; } 182 float device_scale_factor() const { return device_scale_factor_; } 183 184 virtual unsigned int PrepareTexture() = 0; 185 virtual WebKit::WebGraphicsContext3D* HostContext3D() = 0; 186 187 // Replaces the texture with the texture from the specified mailbox. 188 virtual void Consume(const std::string& mailbox_name, 189 const gfx::Size& new_size) {} 190 191 // Moves the texture into the mailbox and returns the mailbox name. 192 // The texture must have been previously consumed from a mailbox. 193 virtual std::string Produce(); 194 195 protected: 196 virtual ~Texture(); 197 gfx::Size size_; // in pixel 198 199 private: 200 friend class base::RefCounted<Texture>; 201 202 bool flipped_; 203 float device_scale_factor_; 204 205 DISALLOW_COPY_AND_ASSIGN(Texture); 206}; 207 208// An interface to allow the compositor to communicate with its owner. 209class COMPOSITOR_EXPORT CompositorDelegate { 210 public: 211 // Requests the owner to schedule a redraw of the layer tree. 212 virtual void ScheduleDraw() = 0; 213 214 protected: 215 virtual ~CompositorDelegate() {} 216}; 217 218// This class represents a lock on the compositor, that can be used to prevent 219// commits to the compositor tree while we're waiting for an asynchronous 220// event. The typical use case is when waiting for a renderer to produce a frame 221// at the right size. The caller keeps a reference on this object, and drops the 222// reference once it desires to release the lock. 223// Note however that the lock is cancelled after a short timeout to ensure 224// responsiveness of the UI, so the compositor tree should be kept in a 225// "reasonable" state while the lock is held. 226// Don't instantiate this class directly, use Compositor::GetCompositorLock. 227class COMPOSITOR_EXPORT CompositorLock 228 : public base::RefCounted<CompositorLock>, 229 public base::SupportsWeakPtr<CompositorLock> { 230 private: 231 friend class base::RefCounted<CompositorLock>; 232 friend class Compositor; 233 234 explicit CompositorLock(Compositor* compositor); 235 ~CompositorLock(); 236 237 void CancelLock(); 238 239 Compositor* compositor_; 240 DISALLOW_COPY_AND_ASSIGN(CompositorLock); 241}; 242 243// This is only to be used for test. It allows execution of other tasks on 244// the current message loop before the current task finishs (there is a 245// potential for re-entrancy). 246class COMPOSITOR_EXPORT DrawWaiterForTest : public ui::CompositorObserver { 247 public: 248 // Waits for a draw to be issued by the compositor. If the test times out 249 // here, there may be a logic error in the compositor code causing it 250 // not to draw. 251 static void Wait(Compositor* compositor); 252 253 // Waits for a commit instead of a draw. 254 static void WaitForCommit(Compositor* compositor); 255 256 private: 257 DrawWaiterForTest(); 258 virtual ~DrawWaiterForTest(); 259 260 void WaitImpl(Compositor* compositor); 261 262 // CompositorObserver implementation. 263 virtual void OnCompositingDidCommit(Compositor* compositor) OVERRIDE; 264 virtual void OnCompositingStarted(Compositor* compositor, 265 base::TimeTicks start_time) OVERRIDE; 266 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE; 267 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE; 268 virtual void OnCompositingLockStateChanged(Compositor* compositor) OVERRIDE; 269 virtual void OnUpdateVSyncParameters(Compositor* compositor, 270 base::TimeTicks timebase, 271 base::TimeDelta interval) OVERRIDE; 272 273 scoped_ptr<base::RunLoop> wait_run_loop_; 274 275 bool wait_for_commit_; 276 277 DISALLOW_COPY_AND_ASSIGN(DrawWaiterForTest); 278}; 279 280// Compositor object to take care of GPU painting. 281// A Browser compositor object is responsible for generating the final 282// displayable form of pixels comprising a single widget's contents. It draws an 283// appropriately transformed texture for each transformed view in the widget's 284// view hierarchy. 285class COMPOSITOR_EXPORT Compositor 286 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient), 287 public base::SupportsWeakPtr<Compositor> { 288 public: 289 Compositor(CompositorDelegate* delegate, 290 gfx::AcceleratedWidget widget); 291 virtual ~Compositor(); 292 293 static void Initialize(); 294 static bool WasInitializedWithThread(); 295 static scoped_refptr<base::MessageLoopProxy> GetCompositorMessageLoop(); 296 static void Terminate(); 297 298 // Schedules a redraw of the layer tree associated with this compositor. 299 void ScheduleDraw(); 300 301 // Sets the root of the layer tree drawn by this Compositor. The root layer 302 // must have no parent. The compositor's root layer is reset if the root layer 303 // is destroyed. NULL can be passed to reset the root layer, in which case the 304 // compositor will stop drawing anything. 305 // The Compositor does not own the root layer. 306 const Layer* root_layer() const { return root_layer_; } 307 Layer* root_layer() { return root_layer_; } 308 void SetRootLayer(Layer* root_layer); 309 310 // Called when we need the compositor to preserve the alpha channel in the 311 // output for situations when we want to render transparently atop something 312 // else, e.g. Aero glass. 313 void SetHostHasTransparentBackground(bool host_has_transparent_background); 314 315 // The scale factor of the device that this compositor is 316 // compositing layers on. 317 float device_scale_factor() const { return device_scale_factor_; } 318 319 // Draws the scene created by the layer tree and any visual effects. 320 void Draw(); 321 322 // Where possible, draws are scissored to a damage region calculated from 323 // changes to layer properties. This bypasses that and indicates that 324 // the whole frame needs to be drawn. 325 void ScheduleFullRedraw(); 326 327 // Schedule redraw and append damage_rect to the damage region calculated 328 // from changes to layer properties. 329 void ScheduleRedrawRect(const gfx::Rect& damage_rect); 330 331 void SetLatencyInfo(const ui::LatencyInfo& latency_info); 332 333 // Reads the region |bounds_in_pixel| of the contents of the last rendered 334 // frame into the given bitmap. 335 // Returns false if the pixels could not be read. 336 bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds_in_pixel); 337 338 // Sets the compositor's device scale factor and size. 339 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel); 340 341 // Returns the size of the widget that is being drawn to in pixel coordinates. 342 const gfx::Size& size() const { return size_; } 343 344 // Sets the background color used for areas that aren't covered by 345 // the |root_layer|. 346 void SetBackgroundColor(SkColor color); 347 348 // Returns the widget for this compositor. 349 gfx::AcceleratedWidget widget() const { return widget_; } 350 351 // Compositor does not own observers. It is the responsibility of the 352 // observer to remove itself when it is done observing. 353 void AddObserver(CompositorObserver* observer); 354 void RemoveObserver(CompositorObserver* observer); 355 bool HasObserver(CompositorObserver* observer); 356 357 // Creates a compositor lock. Returns NULL if it is not possible to lock at 358 // this time (i.e. we're waiting to complete a previous unlock). 359 scoped_refptr<CompositorLock> GetCompositorLock(); 360 361 // Internal functions, called back by command-buffer contexts on swap buffer 362 // events. 363 364 // Signals swap has been posted. 365 void OnSwapBuffersPosted(); 366 367 // Signals swap has completed. 368 void OnSwapBuffersComplete(); 369 370 // Signals swap has aborted (e.g. lost context). 371 void OnSwapBuffersAborted(); 372 373 void OnUpdateVSyncParameters(base::TimeTicks timebase, 374 base::TimeDelta interval); 375 376 // LayerTreeHostClient implementation. 377 virtual void WillBeginFrame() OVERRIDE {} 378 virtual void DidBeginFrame() OVERRIDE {} 379 virtual void Animate(double frame_begin_time) OVERRIDE {} 380 virtual void Layout() OVERRIDE; 381 virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta, 382 float page_scale) OVERRIDE {} 383 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface() OVERRIDE; 384 virtual void DidInitializeOutputSurface(bool success) OVERRIDE {} 385 virtual void WillCommit() OVERRIDE {} 386 virtual void DidCommit() OVERRIDE; 387 virtual void DidCommitAndDrawFrame() OVERRIDE; 388 virtual void DidCompleteSwapBuffers() OVERRIDE; 389 virtual void ScheduleComposite() OVERRIDE; 390 virtual scoped_refptr<cc::ContextProvider> 391 OffscreenContextProviderForMainThread() OVERRIDE; 392 virtual scoped_refptr<cc::ContextProvider> 393 OffscreenContextProviderForCompositorThread() OVERRIDE; 394 395 int last_started_frame() { return last_started_frame_; } 396 int last_ended_frame() { return last_ended_frame_; } 397 398 bool IsLocked() { return compositor_lock_ != NULL; } 399 400 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const; 401 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state); 402 403 private: 404 friend class base::RefCounted<Compositor>; 405 friend class CompositorLock; 406 407 // Called by CompositorLock. 408 void UnlockCompositor(); 409 410 // Called to release any pending CompositorLock 411 void CancelCompositorLock(); 412 413 // Notifies the compositor that compositing is complete. 414 void NotifyEnd(); 415 416 CompositorDelegate* delegate_; 417 gfx::Size size_; 418 419 // The root of the Layer tree drawn by this compositor. 420 Layer* root_layer_; 421 422 ObserverList<CompositorObserver> observer_list_; 423 424 gfx::AcceleratedWidget widget_; 425 scoped_refptr<cc::Layer> root_web_layer_; 426 scoped_ptr<cc::LayerTreeHost> host_; 427 428 // Used to verify that we have at most one draw swap in flight. 429 scoped_ptr<PostedSwapQueue> posted_swaps_; 430 431 // The device scale factor of the monitor that this compositor is compositing 432 // layers on. 433 float device_scale_factor_; 434 435 int last_started_frame_; 436 int last_ended_frame_; 437 438 bool next_draw_is_resize_; 439 440 bool disable_schedule_composite_; 441 442 CompositorLock* compositor_lock_; 443 444 DISALLOW_COPY_AND_ASSIGN(Compositor); 445}; 446 447} // namespace ui 448 449#endif // UI_COMPOSITOR_COMPOSITOR_H_ 450