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