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.
5 #ifndef CC_OUTPUT_OUTPUT_SURFACE_H_
6 #define CC_OUTPUT_OUTPUT_SURFACE_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "cc/base/cc_export.h"
13 #include "cc/output/context_provider.h"
14 #include "cc/output/software_output_device.h"
15 #include "cc/scheduler/frame_rate_controller.h"
17 namespace base
{ class SingleThreadTaskRunner
; }
19 namespace ui
{ struct LatencyInfo
; }
29 class CompositorFrame
;
30 class CompositorFrameAck
;
31 struct ManagedMemoryPolicy
;
32 class OutputSurfaceClient
;
34 // Represents the output surface for a compositor. The compositor owns
35 // and manages its destruction. Its lifetime is:
36 // 1. Created on the main thread by the LayerTreeHost through its client.
37 // 2. Passed to the compositor thread and bound to a client via BindToClient.
38 // From here on, it will only be used on the compositor thread.
39 // 3. If the 3D context is lost, then the compositor will delete the output
40 // surface (on the compositor thread) and go back to step 1.
41 class CC_EXPORT OutputSurface
: public FrameRateControllerClient
{
44 DEFAULT_MAX_FRAMES_PENDING
= 2
47 explicit OutputSurface(scoped_refptr
<ContextProvider
> context_provider
);
49 explicit OutputSurface(scoped_ptr
<cc::SoftwareOutputDevice
> software_device
);
51 OutputSurface(scoped_refptr
<ContextProvider
> context_provider
,
52 scoped_ptr
<cc::SoftwareOutputDevice
> software_device
);
54 virtual ~OutputSurface();
58 : delegated_rendering(false),
59 max_frames_pending(0),
60 deferred_gl_initialization(false),
61 draw_and_swap_full_viewport_every_frame(false),
62 adjust_deadline_for_parent(true) {}
63 bool delegated_rendering
;
64 int max_frames_pending
;
65 bool deferred_gl_initialization
;
66 bool draw_and_swap_full_viewport_every_frame
;
67 // This doesn't handle the <webview> case, but once BeginFrame is
68 // supported natively, we shouldn't need adjust_deadline_for_parent.
69 bool adjust_deadline_for_parent
;
72 const Capabilities
& capabilities() const {
76 // Obtain the 3d context or the software device associated with this output
77 // surface. Either of these may return a null pointer, but not both.
78 // In the event of a lost context, the entire output surface should be
80 scoped_refptr
<ContextProvider
> context_provider() const {
81 return context_provider_
.get();
83 SoftwareOutputDevice
* software_device() const {
84 return software_device_
.get();
87 // In the case where both the context3d and software_device are present
88 // (namely Android WebView), this is called to determine whether the software
89 // device should be used on the current frame.
90 virtual bool ForcedDrawToSoftwareDevice() const;
92 // Called by the compositor on the compositor thread. This is a place where
93 // thread-specific data for the output surface can be initialized, since from
94 // this point on the output surface will only be used on the compositor
96 virtual bool BindToClient(OutputSurfaceClient
* client
);
98 void InitializeBeginFrameEmulation(
99 base::SingleThreadTaskRunner
* task_runner
,
100 bool throttle_frame_production
,
101 base::TimeDelta interval
);
103 void SetMaxFramesPending(int max_frames_pending
);
105 virtual void EnsureBackbuffer();
106 virtual void DiscardBackbuffer();
108 virtual void Reshape(gfx::Size size
, float scale_factor
);
109 virtual gfx::Size
SurfaceSize() const;
111 virtual void BindFramebuffer();
113 // The implementation may destroy or steal the contents of the CompositorFrame
114 // passed in (though it will not take ownership of the CompositorFrame
116 virtual void SwapBuffers(CompositorFrame
* frame
);
118 // Notifies frame-rate smoothness preference. If true, all non-critical
119 // processing should be stopped, or lowered in priority.
120 virtual void UpdateSmoothnessTakesPriority(bool prefer_smoothness
) {}
122 // Requests a BeginFrame notification from the output surface. The
123 // notification will be delivered by calling
124 // OutputSurfaceClient::BeginFrame until the callback is disabled.
125 virtual void SetNeedsBeginFrame(bool enable
);
127 bool HasClient() { return !!client_
; }
130 // Synchronously initialize context3d and enter hardware mode.
131 // This can only supported in threaded compositing mode.
132 // |offscreen_context_provider| should match what is returned by
133 // LayerTreeClient::OffscreenContextProviderForCompositorThread.
134 bool InitializeAndSetContext3d(
135 scoped_refptr
<ContextProvider
> context_provider
,
136 scoped_refptr
<ContextProvider
> offscreen_context_provider
);
139 void PostSwapBuffersComplete();
141 struct cc::OutputSurface::Capabilities capabilities_
;
142 scoped_refptr
<ContextProvider
> context_provider_
;
143 scoped_ptr
<cc::SoftwareOutputDevice
> software_device_
;
144 bool has_gl_discard_backbuffer_
;
145 bool has_swap_buffers_complete_callback_
;
146 gfx::Size surface_size_
;
147 float device_scale_factor_
;
148 base::WeakPtrFactory
<OutputSurface
> weak_ptr_factory_
;
150 // The FrameRateController is deprecated.
151 // Platforms should move to native BeginFrames instead.
152 void OnVSyncParametersChanged(base::TimeTicks timebase
,
153 base::TimeDelta interval
);
154 virtual void FrameRateControllerTick(bool throttled
,
155 const BeginFrameArgs
& args
) OVERRIDE
;
156 scoped_ptr
<FrameRateController
> frame_rate_controller_
;
157 int max_frames_pending_
;
158 int pending_swap_buffers_
;
159 bool needs_begin_frame_
;
160 bool begin_frame_pending_
;
162 // Forwarded to OutputSurfaceClient but threaded through OutputSurface
163 // first so OutputSurface has a chance to update the FrameRateController
164 void SetNeedsRedrawRect(gfx::Rect damage_rect
);
165 void BeginFrame(const BeginFrameArgs
& args
);
166 void DidSwapBuffers();
167 void OnSwapBuffersComplete(const CompositorFrameAck
* ack
);
168 void DidLoseOutputSurface();
169 void SetExternalStencilTest(bool enabled
);
170 void SetExternalDrawConstraints(const gfx::Transform
& transform
,
173 // virtual for testing.
174 virtual base::TimeDelta
AlternateRetroactiveBeginFramePeriod();
175 virtual void PostCheckForRetroactiveBeginFrame();
176 void CheckForRetroactiveBeginFrame();
179 OutputSurfaceClient
* client_
;
180 friend class OutputSurfaceCallbacks
;
182 void SetUpContext3d();
183 void ResetContext3d();
184 void SetMemoryPolicy(const ManagedMemoryPolicy
& policy
,
185 bool discard_backbuffer_when_not_visible
);
187 // This stores a BeginFrame that we couldn't process immediately, but might
188 // process retroactively in the near future.
189 BeginFrameArgs skipped_begin_frame_args_
;
191 // check_for_retroactive_begin_frame_pending_ is used to avoid posting
192 // redundant checks for a retroactive BeginFrame.
193 bool check_for_retroactive_begin_frame_pending_
;
195 DISALLOW_COPY_AND_ASSIGN(OutputSurface
);
200 #endif // CC_OUTPUT_OUTPUT_SURFACE_H_