• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/begin_frame_args.h"
17 #include "cc/output/context_provider.h"
18 #include "cc/output/overlay_candidate_validator.h"
19 #include "cc/output/software_output_device.h"
20 
21 namespace base { class SingleThreadTaskRunner; }
22 
23 namespace ui { struct LatencyInfo; }
24 
25 namespace gfx {
26 class Rect;
27 class Size;
28 class Transform;
29 }
30 
31 namespace cc {
32 
33 class CompositorFrame;
34 class CompositorFrameAck;
35 struct ManagedMemoryPolicy;
36 class OutputSurfaceClient;
37 
38 // Represents the output surface for a compositor. The compositor owns
39 // and manages its destruction. Its lifetime is:
40 //   1. Created on the main thread by the LayerTreeHost through its client.
41 //   2. Passed to the compositor thread and bound to a client via BindToClient.
42 //      From here on, it will only be used on the compositor thread.
43 //   3. If the 3D context is lost, then the compositor will delete the output
44 //      surface (on the compositor thread) and go back to step 1.
45 class CC_EXPORT OutputSurface {
46  public:
47   enum {
48     DEFAULT_MAX_FRAMES_PENDING = 2
49   };
50 
51   explicit OutputSurface(scoped_refptr<ContextProvider> context_provider);
52 
53   explicit OutputSurface(scoped_ptr<SoftwareOutputDevice> software_device);
54 
55   OutputSurface(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   scoped_refptr<ContextProvider> context_provider() const {
91     return context_provider_.get();
92   }
software_device()93   SoftwareOutputDevice* software_device() const {
94     return software_device_.get();
95   }
96 
97   // Called by the compositor on the compositor thread. This is a place where
98   // thread-specific data for the output surface can be initialized, since from
99   // this point on the output surface will only be used on the compositor
100   // thread.
101   virtual bool BindToClient(OutputSurfaceClient* client);
102 
103   // This is called by the compositor on the compositor thread inside ReleaseGL
104   // in order to release the ContextProvider. Only used with
105   // deferred_gl_initialization capability.
106   void ReleaseContextProvider();
107 
108   // Enable or disable vsync.
109   void SetThrottleFrameProduction(bool enable);
110 
111   virtual void EnsureBackbuffer();
112   virtual void DiscardBackbuffer();
113 
114   virtual void Reshape(const gfx::Size& size, float scale_factor);
115   virtual gfx::Size SurfaceSize() const;
116 
117   virtual void BindFramebuffer();
118 
119   // The implementation may destroy or steal the contents of the CompositorFrame
120   // passed in (though it will not take ownership of the CompositorFrame
121   // itself).
122   virtual void SwapBuffers(CompositorFrame* frame);
123   virtual void OnSwapBuffersComplete();
124 
125   // Notifies frame-rate smoothness preference. If true, all non-critical
126   // processing should be stopped, or lowered in priority.
UpdateSmoothnessTakesPriority(bool prefer_smoothness)127   virtual void UpdateSmoothnessTakesPriority(bool prefer_smoothness) {}
128 
129   // Requests a BeginFrame notification from the output surface. The
130   // notification will be delivered by calling
131   // OutputSurfaceClient::BeginFrame until the callback is disabled.
SetNeedsBeginFrame(bool enable)132   virtual void SetNeedsBeginFrame(bool enable) {}
133 
HasClient()134   bool HasClient() { return !!client_; }
135 
136   // Returns an estimate of the current GPU latency. When only a software
137   // device is present, returns 0.
138   base::TimeDelta GpuLatencyEstimate();
139 
140   // Get the class capable of informing cc of hardware overlay capability.
overlay_candidate_validator()141   OverlayCandidateValidator* overlay_candidate_validator() const {
142     return overlay_candidate_validator_.get();
143   }
144 
145  protected:
146   OutputSurfaceClient* client_;
147 
148   // Synchronously initialize context3d and enter hardware mode.
149   // This can only supported in threaded compositing mode.
150   bool InitializeAndSetContext3d(
151       scoped_refptr<ContextProvider> context_provider);
152   void ReleaseGL();
153 
154   void PostSwapBuffersComplete();
155 
156   struct OutputSurface::Capabilities capabilities_;
157   scoped_refptr<ContextProvider> context_provider_;
158   scoped_ptr<SoftwareOutputDevice> software_device_;
159   scoped_ptr<OverlayCandidateValidator> overlay_candidate_validator_;
160   gfx::Size surface_size_;
161   float device_scale_factor_;
162 
163   void CommitVSyncParameters(base::TimeTicks timebase,
164                              base::TimeDelta interval);
165 
166   void SetNeedsRedrawRect(const gfx::Rect& damage_rect);
167   void ReclaimResources(const CompositorFrameAck* ack);
168   void DidLoseOutputSurface();
169   void SetExternalStencilTest(bool enabled);
170   void SetExternalDrawConstraints(
171       const gfx::Transform& transform,
172       const gfx::Rect& viewport,
173       const gfx::Rect& clip,
174       const gfx::Rect& viewport_rect_for_tile_priority,
175       const gfx::Transform& transform_for_tile_priority,
176       bool resourceless_software_draw);
177 
178  private:
179   void SetUpContext3d();
180   void ResetContext3d();
181   void SetMemoryPolicy(const ManagedMemoryPolicy& policy);
182   void UpdateAndMeasureGpuLatency();
183 
184   bool external_stencil_test_enabled_;
185 
186   base::WeakPtrFactory<OutputSurface> weak_ptr_factory_;
187 
188   std::deque<unsigned> available_gpu_latency_query_ids_;
189   std::deque<unsigned> pending_gpu_latency_query_ids_;
190   RollingTimeDeltaHistory gpu_latency_history_;
191 
192   DISALLOW_COPY_AND_ASSIGN(OutputSurface);
193 };
194 
195 }  // namespace cc
196 
197 #endif  // CC_OUTPUT_OUTPUT_SURFACE_H_
198