1 // Copyright 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 CC_OUTPUT_OUTPUT_SURFACE_H_ 6 #define CC_OUTPUT_OUTPUT_SURFACE_H_ 7 8 #include <deque> 9 10 #include "base/basictypes.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "cc/base/cc_export.h" 15 #include "cc/base/rolling_time_delta_history.h" 16 #include "cc/output/context_provider.h" 17 #include "cc/output/overlay_candidate_validator.h" 18 #include "cc/output/software_output_device.h" 19 20 namespace base { class SingleThreadTaskRunner; } 21 22 namespace ui { struct LatencyInfo; } 23 24 namespace gfx { 25 class Rect; 26 class Size; 27 class Transform; 28 } 29 30 namespace cc { 31 32 class CompositorFrame; 33 class CompositorFrameAck; 34 struct ManagedMemoryPolicy; 35 class OutputSurfaceClient; 36 37 // Represents the output surface for a compositor. The compositor owns 38 // and manages its destruction. Its lifetime is: 39 // 1. Created on the main thread by the LayerTreeHost through its client. 40 // 2. Passed to the compositor thread and bound to a client via BindToClient. 41 // From here on, it will only be used on the compositor thread. 42 // 3. If the 3D context is lost, then the compositor will delete the output 43 // surface (on the compositor thread) and go back to step 1. 44 class CC_EXPORT OutputSurface { 45 public: 46 enum { 47 DEFAULT_MAX_FRAMES_PENDING = 2 48 }; 49 50 explicit OutputSurface( 51 const scoped_refptr<ContextProvider>& context_provider); 52 53 explicit OutputSurface(scoped_ptr<SoftwareOutputDevice> software_device); 54 55 OutputSurface(const scoped_refptr<ContextProvider>& context_provider, 56 scoped_ptr<SoftwareOutputDevice> software_device); 57 58 virtual ~OutputSurface(); 59 60 struct Capabilities { CapabilitiesCapabilities61 Capabilities() 62 : delegated_rendering(false), 63 max_frames_pending(0), 64 deferred_gl_initialization(false), 65 draw_and_swap_full_viewport_every_frame(false), 66 adjust_deadline_for_parent(true), 67 uses_default_gl_framebuffer(true) {} 68 bool delegated_rendering; 69 int max_frames_pending; 70 bool deferred_gl_initialization; 71 bool draw_and_swap_full_viewport_every_frame; 72 // This doesn't handle the <webview> case, but once BeginFrame is 73 // supported natively, we shouldn't need adjust_deadline_for_parent. 74 bool adjust_deadline_for_parent; 75 // Whether this output surface renders to the default OpenGL zero 76 // framebuffer or to an offscreen framebuffer. 77 bool uses_default_gl_framebuffer; 78 }; 79 capabilities()80 const Capabilities& capabilities() const { 81 return capabilities_; 82 } 83 84 virtual bool HasExternalStencilTest() const; 85 86 // Obtain the 3d context or the software device associated with this output 87 // surface. Either of these may return a null pointer, but not both. 88 // In the event of a lost context, the entire output surface should be 89 // recreated. context_provider()90 ContextProvider* context_provider() const { return context_provider_.get(); } software_device()91 SoftwareOutputDevice* software_device() const { 92 return software_device_.get(); 93 } 94 95 // Called by the compositor on the compositor thread. This is a place where 96 // thread-specific data for the output surface can be initialized, since from 97 // this point on the output surface will only be used on the compositor 98 // thread. 99 virtual bool BindToClient(OutputSurfaceClient* client); 100 101 // This is called by the compositor on the compositor thread inside ReleaseGL 102 // in order to release the ContextProvider. Only used with 103 // deferred_gl_initialization capability. 104 void ReleaseContextProvider(); 105 106 virtual void EnsureBackbuffer(); 107 virtual void DiscardBackbuffer(); 108 109 virtual void Reshape(const gfx::Size& size, float scale_factor); 110 virtual gfx::Size SurfaceSize() const; 111 112 virtual void BindFramebuffer(); 113 114 // The implementation may destroy or steal the contents of the CompositorFrame 115 // passed in (though it will not take ownership of the CompositorFrame 116 // itself). 117 virtual void SwapBuffers(CompositorFrame* frame); 118 virtual void OnSwapBuffersComplete(); 119 120 // Notifies frame-rate smoothness preference. If true, all non-critical 121 // processing should be stopped, or lowered in priority. UpdateSmoothnessTakesPriority(bool prefer_smoothness)122 virtual void UpdateSmoothnessTakesPriority(bool prefer_smoothness) {} 123 124 // Requests a BeginFrame notification from the output surface. The 125 // notification will be delivered by calling 126 // OutputSurfaceClient::BeginFrame until the callback is disabled. SetNeedsBeginFrame(bool enable)127 virtual void SetNeedsBeginFrame(bool enable) {} 128 HasClient()129 bool HasClient() { return !!client_; } 130 131 // Returns an estimate of the current GPU latency. When only a software 132 // device is present, returns 0. 133 base::TimeDelta GpuLatencyEstimate(); 134 135 // Get the class capable of informing cc of hardware overlay capability. overlay_candidate_validator()136 OverlayCandidateValidator* overlay_candidate_validator() const { 137 return overlay_candidate_validator_.get(); 138 } 139 140 protected: 141 OutputSurfaceClient* client_; 142 143 // Synchronously initialize context3d and enter hardware mode. 144 // This can only supported in threaded compositing mode. 145 bool InitializeAndSetContext3d( 146 scoped_refptr<ContextProvider> context_provider); 147 void ReleaseGL(); 148 149 void PostSwapBuffersComplete(); 150 151 struct OutputSurface::Capabilities capabilities_; 152 scoped_refptr<ContextProvider> context_provider_; 153 scoped_ptr<SoftwareOutputDevice> software_device_; 154 scoped_ptr<OverlayCandidateValidator> overlay_candidate_validator_; 155 gfx::Size surface_size_; 156 float device_scale_factor_; 157 158 void CommitVSyncParameters(base::TimeTicks timebase, 159 base::TimeDelta interval); 160 161 void SetNeedsRedrawRect(const gfx::Rect& damage_rect); 162 void ReclaimResources(const CompositorFrameAck* ack); 163 void DidLoseOutputSurface(); 164 void SetExternalStencilTest(bool enabled); 165 void SetExternalDrawConstraints( 166 const gfx::Transform& transform, 167 const gfx::Rect& viewport, 168 const gfx::Rect& clip, 169 const gfx::Rect& viewport_rect_for_tile_priority, 170 const gfx::Transform& transform_for_tile_priority, 171 bool resourceless_software_draw); 172 173 private: 174 void SetUpContext3d(); 175 void ResetContext3d(); 176 void SetMemoryPolicy(const ManagedMemoryPolicy& policy); 177 void UpdateAndMeasureGpuLatency(); 178 179 bool external_stencil_test_enabled_; 180 181 std::deque<unsigned> available_gpu_latency_query_ids_; 182 std::deque<unsigned> pending_gpu_latency_query_ids_; 183 RollingTimeDeltaHistory gpu_latency_history_; 184 185 base::WeakPtrFactory<OutputSurface> weak_ptr_factory_; 186 187 DISALLOW_COPY_AND_ASSIGN(OutputSurface); 188 }; 189 190 } // namespace cc 191 192 #endif // CC_OUTPUT_OUTPUT_SURFACE_H_ 193