compositor.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 "cc/trees/layer_tree_host_single_thread_client.h" 17#include "third_party/skia/include/core/SkColor.h" 18#include "ui/compositor/compositor_export.h" 19#include "ui/compositor/compositor_observer.h" 20#include "ui/gfx/native_widget_types.h" 21#include "ui/gfx/size.h" 22#include "ui/gfx/vector2d.h" 23 24class SkBitmap; 25 26namespace base { 27class MessageLoopProxy; 28class RunLoop; 29} 30 31namespace cc { 32class ContextProvider; 33class Layer; 34class LayerTreeDebugState; 35class LayerTreeHost; 36class SharedBitmapManager; 37} 38 39namespace gfx { 40class Rect; 41class Size; 42} 43 44namespace gpu { 45struct Mailbox; 46} 47 48namespace ui { 49 50class Compositor; 51class CompositorVSyncManager; 52class Layer; 53class Reflector; 54class Texture; 55struct LatencyInfo; 56 57// This class abstracts the creation of the 3D context for the compositor. It is 58// a global object. 59class COMPOSITOR_EXPORT ContextFactory { 60 public: 61 virtual ~ContextFactory() {} 62 63 // Gets the global instance. 64 static ContextFactory* GetInstance(); 65 66 // Sets the global instance. Caller keeps ownership. 67 // If this function isn't called (for tests), a "default" factory will be 68 // created on the first call of GetInstance. 69 static void SetInstance(ContextFactory* instance); 70 71 // Creates an output surface for the given compositor. The factory may keep 72 // per-compositor data (e.g. a shared context), that needs to be cleaned up 73 // by calling RemoveCompositor when the compositor gets destroyed. 74 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( 75 Compositor* compositor, bool software_fallback) = 0; 76 77 // Creates a reflector that copies the content of the |mirrored_compositor| 78 // onto |mirroing_layer|. 79 virtual scoped_refptr<Reflector> CreateReflector( 80 Compositor* mirrored_compositor, 81 Layer* mirroring_layer) = 0; 82 // Removes the reflector, which stops the mirroring. 83 virtual void RemoveReflector(scoped_refptr<Reflector> reflector) = 0; 84 85 // Return a reference to a shared offscreen context provider usable from the 86 // main thread. 87 virtual scoped_refptr<cc::ContextProvider> 88 SharedMainThreadContextProvider() = 0; 89 90 // Destroys per-compositor data. 91 virtual void RemoveCompositor(Compositor* compositor) = 0; 92 93 // When true, the factory uses test contexts that do not do real GL 94 // operations. 95 virtual bool DoesCreateTestContexts() = 0; 96 97 // Gets the shared bitmap manager for software mode. 98 virtual cc::SharedBitmapManager* GetSharedBitmapManager() = 0; 99 100 // Gets the compositor message loop, or NULL if not using threaded 101 // compositing. 102 virtual base::MessageLoopProxy* GetCompositorMessageLoop() = 0; 103}; 104 105// This class represents a lock on the compositor, that can be used to prevent 106// commits to the compositor tree while we're waiting for an asynchronous 107// event. The typical use case is when waiting for a renderer to produce a frame 108// at the right size. The caller keeps a reference on this object, and drops the 109// reference once it desires to release the lock. 110// Note however that the lock is cancelled after a short timeout to ensure 111// responsiveness of the UI, so the compositor tree should be kept in a 112// "reasonable" state while the lock is held. 113// Don't instantiate this class directly, use Compositor::GetCompositorLock. 114class COMPOSITOR_EXPORT CompositorLock 115 : public base::RefCounted<CompositorLock>, 116 public base::SupportsWeakPtr<CompositorLock> { 117 private: 118 friend class base::RefCounted<CompositorLock>; 119 friend class Compositor; 120 121 explicit CompositorLock(Compositor* compositor); 122 ~CompositorLock(); 123 124 void CancelLock(); 125 126 Compositor* compositor_; 127 DISALLOW_COPY_AND_ASSIGN(CompositorLock); 128}; 129 130// Compositor object to take care of GPU painting. 131// A Browser compositor object is responsible for generating the final 132// displayable form of pixels comprising a single widget's contents. It draws an 133// appropriately transformed texture for each transformed view in the widget's 134// view hierarchy. 135class COMPOSITOR_EXPORT Compositor 136 : NON_EXPORTED_BASE(public cc::LayerTreeHostClient), 137 NON_EXPORTED_BASE(public cc::LayerTreeHostSingleThreadClient) { 138 public: 139 // This is deprecated, and will be removed shortly. 140 // TODO(sky): remove this. 141 explicit Compositor(gfx::AcceleratedWidget widget); 142 Compositor(gfx::AcceleratedWidget widget, 143 ui::ContextFactory* context_factory); 144 virtual ~Compositor(); 145 146 ui::ContextFactory* context_factory() { return context_factory_; } 147 148 // Schedules a redraw of the layer tree associated with this compositor. 149 void ScheduleDraw(); 150 151 // Sets the root of the layer tree drawn by this Compositor. The root layer 152 // must have no parent. The compositor's root layer is reset if the root layer 153 // is destroyed. NULL can be passed to reset the root layer, in which case the 154 // compositor will stop drawing anything. 155 // The Compositor does not own the root layer. 156 const Layer* root_layer() const { return root_layer_; } 157 Layer* root_layer() { return root_layer_; } 158 void SetRootLayer(Layer* root_layer); 159 160 // Called when we need the compositor to preserve the alpha channel in the 161 // output for situations when we want to render transparently atop something 162 // else, e.g. Aero glass. 163 void SetHostHasTransparentBackground(bool host_has_transparent_background); 164 165 // The scale factor of the device that this compositor is 166 // compositing layers on. 167 float device_scale_factor() const { return device_scale_factor_; } 168 169 // Draws the scene created by the layer tree and any visual effects. 170 void Draw(); 171 172 // Where possible, draws are scissored to a damage region calculated from 173 // changes to layer properties. This bypasses that and indicates that 174 // the whole frame needs to be drawn. 175 void ScheduleFullRedraw(); 176 177 // Schedule redraw and append damage_rect to the damage region calculated 178 // from changes to layer properties. 179 void ScheduleRedrawRect(const gfx::Rect& damage_rect); 180 181 // Finishes all outstanding rendering on the GPU. 182 void FinishAllRendering(); 183 184 void SetLatencyInfo(const LatencyInfo& latency_info); 185 186 // Sets the compositor's device scale factor and size. 187 void SetScaleAndSize(float scale, const gfx::Size& size_in_pixel); 188 189 // Returns the size of the widget that is being drawn to in pixel coordinates. 190 const gfx::Size& size() const { return size_; } 191 192 // Sets the background color used for areas that aren't covered by 193 // the |root_layer|. 194 void SetBackgroundColor(SkColor color); 195 196 // Returns the widget for this compositor. 197 gfx::AcceleratedWidget widget() const { return widget_; } 198 199 // Returns the vsync manager for this compositor. 200 scoped_refptr<CompositorVSyncManager> vsync_manager() const; 201 202 // Compositor does not own observers. It is the responsibility of the 203 // observer to remove itself when it is done observing. 204 void AddObserver(CompositorObserver* observer); 205 void RemoveObserver(CompositorObserver* observer); 206 bool HasObserver(CompositorObserver* observer); 207 208 // Creates a compositor lock. Returns NULL if it is not possible to lock at 209 // this time (i.e. we're waiting to complete a previous unlock). 210 scoped_refptr<CompositorLock> GetCompositorLock(); 211 212 // Internal functions, called back by command-buffer contexts on swap buffer 213 // events. 214 215 // Signals swap has been posted. 216 void OnSwapBuffersPosted(); 217 218 // Signals swap has completed. 219 void OnSwapBuffersComplete(); 220 221 // Signals swap has aborted (e.g. lost context). 222 void OnSwapBuffersAborted(); 223 224 // LayerTreeHostClient implementation. 225 virtual void WillBeginMainFrame(int frame_id) OVERRIDE {} 226 virtual void DidBeginMainFrame() OVERRIDE {} 227 virtual void Animate(base::TimeTicks frame_begin_time) OVERRIDE {} 228 virtual void Layout() OVERRIDE; 229 virtual void ApplyScrollAndScale(const gfx::Vector2d& scroll_delta, 230 float page_scale) OVERRIDE {} 231 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback) 232 OVERRIDE; 233 virtual void DidInitializeOutputSurface() OVERRIDE {} 234 virtual void WillCommit() OVERRIDE {} 235 virtual void DidCommit() OVERRIDE; 236 virtual void DidCommitAndDrawFrame() OVERRIDE; 237 virtual void DidCompleteSwapBuffers() OVERRIDE; 238 239 // cc::LayerTreeHostSingleThreadClient implementation. 240 virtual void ScheduleComposite() OVERRIDE; 241 virtual void ScheduleAnimation() OVERRIDE; 242 virtual void DidPostSwapBuffers() OVERRIDE; 243 virtual void DidAbortSwapBuffers() OVERRIDE; 244 245 int last_started_frame() { return last_started_frame_; } 246 int last_ended_frame() { return last_ended_frame_; } 247 248 bool IsLocked() { return compositor_lock_ != NULL; } 249 250 const cc::LayerTreeDebugState& GetLayerTreeDebugState() const; 251 void SetLayerTreeDebugState(const cc::LayerTreeDebugState& debug_state); 252 253 private: 254 friend class base::RefCounted<Compositor>; 255 friend class CompositorLock; 256 257 // Called from both constructors. It's temporary while we have both 258 // constructors. 259 // TODO(sky): nuke this. 260 void Init(); 261 262 // Called by CompositorLock. 263 void UnlockCompositor(); 264 265 // Called to release any pending CompositorLock 266 void CancelCompositorLock(); 267 268 // Notifies the compositor that compositing is complete. 269 void NotifyEnd(); 270 271 gfx::Size size_; 272 273 ui::ContextFactory* context_factory_; 274 275 // The root of the Layer tree drawn by this compositor. 276 Layer* root_layer_; 277 278 ObserverList<CompositorObserver> observer_list_; 279 280 gfx::AcceleratedWidget widget_; 281 scoped_refptr<cc::Layer> root_web_layer_; 282 scoped_ptr<cc::LayerTreeHost> host_; 283 scoped_refptr<base::MessageLoopProxy> compositor_thread_loop_; 284 285 // The manager of vsync parameters for this compositor. 286 scoped_refptr<CompositorVSyncManager> vsync_manager_; 287 288 // The device scale factor of the monitor that this compositor is compositing 289 // layers on. 290 float device_scale_factor_; 291 292 int last_started_frame_; 293 int last_ended_frame_; 294 295 bool disable_schedule_composite_; 296 297 CompositorLock* compositor_lock_; 298 299 // Prevent more than one draw from being scheduled. 300 bool defer_draw_scheduling_; 301 302 // Used to prevent Draw()s while a composite is in progress. 303 bool waiting_on_compositing_end_; 304 bool draw_on_compositing_end_; 305 enum SwapState { SWAP_NONE, SWAP_POSTED, SWAP_COMPLETED }; 306 SwapState swap_state_; 307 308 base::WeakPtrFactory<Compositor> schedule_draw_factory_; 309 310 DISALLOW_COPY_AND_ASSIGN(Compositor); 311}; 312 313} // namespace ui 314 315#endif // UI_COMPOSITOR_COMPOSITOR_H_ 316