compositor.h revision 3551c9c881056c480085172ff9840cab31610854
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; 37class TestContextProvider; 38} 39 40namespace gfx { 41class GLContext; 42class GLSurface; 43class GLShareGroup; 44class Point; 45class Rect; 46class Size; 47} 48 49namespace WebKit { 50class WebGraphicsContext3D; 51} 52 53namespace webkit { 54namespace gpu { 55class ContextProviderInProcess; 56class WebGraphicsContext3DInProcessCommandBufferImpl; 57} 58} 59 60namespace ui { 61 62class Compositor; 63class CompositorObserver; 64class ContextProviderFromContextFactory; 65class Layer; 66class PostedSwapQueue; 67class Reflector; 68class Texture; 69struct LatencyInfo; 70 71// This class abstracts the creation of the 3D context for the compositor. It is 72// a global object. 73class COMPOSITOR_EXPORT ContextFactory { 74 public: 75 virtual ~ContextFactory() {} 76 77 // Gets the global instance. 78 static ContextFactory* GetInstance(); 79 80 // Sets the global instance. Caller keeps ownership. 81 // If this function isn't called (for tests), a "default" factory will be 82 // created on the first call of GetInstance. 83 static void SetInstance(ContextFactory* instance); 84 85 // Creates an output surface for the given compositor. The factory may keep 86 // per-compositor data (e.g. a shared context), that needs to be cleaned up 87 // by calling RemoveCompositor when the compositor gets destroyed. 88 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 89 Compositor* compositor) = 0; 90 91 // Creates a reflector that copies the content of the |mirrored_compositor| 92 // onto |mirroing_layer|. 93 virtual scoped_refptr<Reflector> CreateReflector( 94 Compositor* mirrored_compositor, 95 Layer* mirroring_layer) = 0; 96 // Removes the reflector, which stops the mirroring. 97 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0; 98 99 virtual scoped_refptr<cc::ContextProvider> 100 OffscreenContextProviderForMainThread() = 0; 101 virtual scoped_refptr<cc::ContextProvider> 102 OffscreenContextProviderForCompositorThread() = 0; 103 104 // Destroys per-compositor data. 105 virtual void RemoveCompositor(Compositor* compositor) = 0; 106 107 // When true, the factory uses test contexts that do not do real GL 108 // operations. 109 virtual bool DoesCreateTestContexts() = 0; 110}; 111 112// The default factory that creates in-process contexts. 113class COMPOSITOR_EXPORT DefaultContextFactory : public ContextFactory { 114 public: 115 DefaultContextFactory(); 116 virtual ~DefaultContextFactory(); 117 118 // ContextFactory implementation 119 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 120 Compositor* compositor) OVERRIDE; 121 122 virtual scoped_refptr<Reflector> CreateReflector( 123 Compositor* compositor, 124 Layer* layer) OVERRIDE; 125 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) OVERRIDE; 126 127 virtual scoped_refptr<cc::ContextProvider> 128 OffscreenContextProviderForMainThread() OVERRIDE; 129 virtual scoped_refptr<cc::ContextProvider> 130 OffscreenContextProviderForCompositorThread() OVERRIDE; 131 virtual void RemoveCompositor(Compositor* compositor) OVERRIDE; 132 virtual bool DoesCreateTestContexts() OVERRIDE; 133 134 bool Initialize(); 135 136 private: 137 scoped_refptr<webkit::gpu::ContextProviderInProcess> 138 offscreen_contexts_main_thread_; 139 scoped_refptr<webkit::gpu::ContextProviderInProcess> 140 offscreen_contexts_compositor_thread_; 141 142 DISALLOW_COPY_AND_ASSIGN(DefaultContextFactory); 143}; 144 145// The factory that creates test contexts. 146class COMPOSITOR_EXPORT TestContextFactory : public ContextFactory { 147 public: 148 TestContextFactory(); 149 virtual ~TestContextFactory(); 150 151 // ContextFactory implementation 152 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 153 Compositor* compositor) OVERRIDE; 154 155 virtual scoped_refptr<Reflector> CreateReflector( 156 Compositor* mirrored_compositor, 157 Layer* mirroring_layer) OVERRIDE; 158 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) OVERRIDE; 159 160 virtual scoped_refptr<cc::ContextProvider> 161 OffscreenContextProviderForMainThread() OVERRIDE; 162 virtual scoped_refptr<cc::ContextProvider> 163 OffscreenContextProviderForCompositorThread() OVERRIDE; 164 virtual void RemoveCompositor(Compositor* compositor) OVERRIDE; 165 virtual bool DoesCreateTestContexts() OVERRIDE; 166 167 private: 168 scoped_refptr<cc::TestContextProvider> offscreen_contexts_main_thread_; 169 scoped_refptr<cc::TestContextProvider> 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 // Set up the compositor ContextFactory for a test environment. Unit tests 294 // that do not have a full content environment need to call this before 295 // initializing the Compositor. 296 // Some tests expect pixel output, and they should pass false for 297 // |allow_test_contexts|. Most unit tests should pass true. Once this has been 298 // called, the Initialize() and Terminate() methods should be used as normal. 299 static void InitializeContextFactoryForTests(bool allow_test_contexts); 300 301 static void Initialize(); 302 static bool WasInitializedWithThread(); 303 static scoped_refptr<base::MessageLoopProxy> GetCompositorMessageLoop(); 304 static void Terminate(); 305 306 // Schedules a redraw of the layer tree associated with this compositor. 307 void ScheduleDraw(); 308 309 // Sets the root of the layer tree drawn by this Compositor. The root layer 310 // must have no parent. The compositor's root layer is reset if the root layer 311 // is destroyed. NULL can be passed to reset the root layer, in which case the 312 // compositor will stop drawing anything. 313 // The Compositor does not own the root layer. 314 const Layer* root_layer() const { return root_layer_; } 315 Layer* root_layer() { return root_layer_; } 316 void SetRootLayer(Layer* root_layer); 317 318 // Called when we need the compositor to preserve the alpha channel in the 319 // output for situations when we want to render transparently atop something 320 // else, e.g. Aero glass. 321 void SetHostHasTransparentBackground(bool host_has_transparent_background); 322 323 // The scale factor of the device that this compositor is 324 // compositing layers on. 325 float device_scale_factor() const { return device_scale_factor_; } 326 327 // Draws the scene created by the layer tree and any visual effects. 328 void Draw(); 329 330 // Where possible, draws are scissored to a damage region calculated from 331 // changes to layer properties. This bypasses that and indicates that 332 // the whole frame needs to be drawn. 333 void ScheduleFullRedraw(); 334 335 // Schedule redraw and append damage_rect to the damage region calculated 336 // from changes to layer properties. 337 void ScheduleRedrawRect(const gfx::Rect& damage_rect); 338 339 void SetLatencyInfo(const ui::LatencyInfo& latency_info); 340 341 // Reads the region |bounds_in_pixel| of the contents of the last rendered 342 // frame into the given bitmap. 343 // Returns false if the pixels could not be read. 344 bool ReadPixels(SkBitmap* bitmap, const gfx::Rect& bounds_in_pixel); 345 346 // Sets the compositor's device scale factor and size. 347 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel); 348 349 // Returns the size of the widget that is being drawn to in pixel coordinates. 350 const gfx::Size& size() const { return size_; } 351 352 // Sets the background color used for areas that aren't covered by 353 // the |root_layer|. 354 void SetBackgroundColor(SkColor color); 355 356 // Returns the widget for this compositor. 357 gfx::AcceleratedWidget widget() const { return widget_; } 358 359 // Compositor does not own observers. It is the responsibility of the 360 // observer to remove itself when it is done observing. 361 void AddObserver(CompositorObserver* observer); 362 void RemoveObserver(CompositorObserver* observer); 363 bool HasObserver(CompositorObserver* observer); 364 365 // Creates a compositor lock. Returns NULL if it is not possible to lock at 366 // this time (i.e. we're waiting to complete a previous unlock). 367 scoped_refptr<CompositorLock> GetCompositorLock(); 368 369 // Internal functions, called back by command-buffer contexts on swap buffer 370 // events. 371 372 // Signals swap has been posted. 373 void OnSwapBuffersPosted(); 374 375 // Signals swap has completed. 376 void OnSwapBuffersComplete(); 377 378 // Signals swap has aborted (e.g. lost context). 379 void OnSwapBuffersAborted(); 380 381 void OnUpdateVSyncParameters(base::TimeTicks timebase, 382 base::TimeDelta interval); 383 384 // LayerTreeHostClient implementation. 385 virtual void WillBeginFrame() OVERRIDE {} 386 virtual void DidBeginFrame() OVERRIDE {} 387 virtual void Animate(double frame_begin_time) OVERRIDE {} 388 virtual void Layout() OVERRIDE; 389 virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta, 390 float page_scale) OVERRIDE {} 391 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback) 392 OVERRIDE; 393 virtual void DidInitializeOutputSurface(bool success) OVERRIDE {} 394 virtual void WillCommit() OVERRIDE {} 395 virtual void DidCommit() OVERRIDE; 396 virtual void DidCommitAndDrawFrame() OVERRIDE; 397 virtual void DidCompleteSwapBuffers() OVERRIDE; 398 virtual void ScheduleComposite() OVERRIDE; 399 virtual scoped_refptr<cc::ContextProvider> 400 OffscreenContextProviderForMainThread() OVERRIDE; 401 virtual scoped_refptr<cc::ContextProvider> 402 OffscreenContextProviderForCompositorThread() OVERRIDE; 403 404 int last_started_frame() { return last_started_frame_; } 405 int last_ended_frame() { return last_ended_frame_; } 406 407 bool IsLocked() { return compositor_lock_ != NULL; } 408 409 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const; 410 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state); 411 412 private: 413 friend class base::RefCounted<Compositor>; 414 friend class CompositorLock; 415 416 // Called by CompositorLock. 417 void UnlockCompositor(); 418 419 // Called to release any pending CompositorLock 420 void CancelCompositorLock(); 421 422 // Notifies the compositor that compositing is complete. 423 void NotifyEnd(); 424 425 CompositorDelegate* delegate_; 426 gfx::Size size_; 427 428 // The root of the Layer tree drawn by this compositor. 429 Layer* root_layer_; 430 431 ObserverList<CompositorObserver> observer_list_; 432 433 gfx::AcceleratedWidget widget_; 434 scoped_refptr<cc::Layer> root_web_layer_; 435 scoped_ptr<cc::LayerTreeHost> host_; 436 437 // Used to verify that we have at most one draw swap in flight. 438 scoped_ptr<PostedSwapQueue> posted_swaps_; 439 440 // The device scale factor of the monitor that this compositor is compositing 441 // layers on. 442 float device_scale_factor_; 443 444 int last_started_frame_; 445 int last_ended_frame_; 446 447 bool next_draw_is_resize_; 448 449 bool disable_schedule_composite_; 450 451 CompositorLock* compositor_lock_; 452 453 DISALLOW_COPY_AND_ASSIGN(Compositor); 454}; 455 456} // namespace ui 457 458#endif // UI_COMPOSITOR_COMPOSITOR_H_ 459