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