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_
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/output/context_provider.h"
16 #include "cc/output/overlay_candidate_validator.h"
17 #include "cc/output/software_output_device.h"
19 namespace base
{ class SingleThreadTaskRunner
; }
21 namespace ui
{ struct LatencyInfo
; }
31 class CompositorFrame
;
32 class CompositorFrameAck
;
33 struct ManagedMemoryPolicy
;
34 class OutputSurfaceClient
;
36 // Represents the output surface for a compositor. The compositor owns
37 // and manages its destruction. Its lifetime is:
38 // 1. Created on the main thread by the LayerTreeHost through its client.
39 // 2. Passed to the compositor thread and bound to a client via BindToClient.
40 // From here on, it will only be used on the compositor thread.
41 // 3. If the 3D context is lost, then the compositor will delete the output
42 // surface (on the compositor thread) and go back to step 1.
43 class CC_EXPORT OutputSurface
{
46 DEFAULT_MAX_FRAMES_PENDING
= 2
49 explicit OutputSurface(
50 const scoped_refptr
<ContextProvider
>& context_provider
);
52 explicit OutputSurface(scoped_ptr
<SoftwareOutputDevice
> software_device
);
54 OutputSurface(const scoped_refptr
<ContextProvider
>& context_provider
,
55 scoped_ptr
<SoftwareOutputDevice
> software_device
);
57 virtual ~OutputSurface();
61 : delegated_rendering(false),
62 max_frames_pending(0),
63 deferred_gl_initialization(false),
64 draw_and_swap_full_viewport_every_frame(false),
65 adjust_deadline_for_parent(true),
66 uses_default_gl_framebuffer(true) {}
67 bool delegated_rendering
;
68 int max_frames_pending
;
69 bool deferred_gl_initialization
;
70 bool draw_and_swap_full_viewport_every_frame
;
71 // This doesn't handle the <webview> case, but once BeginFrame is
72 // supported natively, we shouldn't need adjust_deadline_for_parent.
73 bool adjust_deadline_for_parent
;
74 // Whether this output surface renders to the default OpenGL zero
75 // framebuffer or to an offscreen framebuffer.
76 bool uses_default_gl_framebuffer
;
79 const Capabilities
& capabilities() const {
83 virtual bool HasExternalStencilTest() const;
85 // Obtain the 3d context or the software device associated with this output
86 // surface. Either of these may return a null pointer, but not both.
87 // In the event of a lost context, the entire output surface should be
89 ContextProvider
* context_provider() const { return context_provider_
.get(); }
90 SoftwareOutputDevice
* software_device() const {
91 return software_device_
.get();
94 // Called by the compositor on the compositor thread. This is a place where
95 // thread-specific data for the output surface can be initialized, since from
96 // this point on the output surface will only be used on the compositor
98 virtual bool BindToClient(OutputSurfaceClient
* client
);
100 // This is called by the compositor on the compositor thread inside ReleaseGL
101 // in order to release the ContextProvider. Only used with
102 // deferred_gl_initialization capability.
103 void ReleaseContextProvider();
105 virtual void EnsureBackbuffer();
106 virtual void DiscardBackbuffer();
108 virtual void Reshape(const 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
);
117 virtual void OnSwapBuffersComplete();
119 // Notifies frame-rate smoothness preference. If true, all non-critical
120 // processing should be stopped, or lowered in priority.
121 virtual void UpdateSmoothnessTakesPriority(bool prefer_smoothness
) {}
123 // Requests a BeginFrame notification from the output surface. The
124 // notification will be delivered by calling
125 // OutputSurfaceClient::BeginFrame until the callback is disabled.
126 virtual void SetNeedsBeginFrame(bool enable
) {}
128 bool HasClient() { return !!client_
; }
130 // Get the class capable of informing cc of hardware overlay capability.
131 OverlayCandidateValidator
* overlay_candidate_validator() const {
132 return overlay_candidate_validator_
.get();
136 OutputSurfaceClient
* client_
;
138 // Synchronously initialize context3d and enter hardware mode.
139 // This can only supported in threaded compositing mode.
140 bool InitializeAndSetContext3d(
141 scoped_refptr
<ContextProvider
> context_provider
);
144 void PostSwapBuffersComplete();
146 struct OutputSurface::Capabilities capabilities_
;
147 scoped_refptr
<ContextProvider
> context_provider_
;
148 scoped_ptr
<SoftwareOutputDevice
> software_device_
;
149 scoped_ptr
<OverlayCandidateValidator
> overlay_candidate_validator_
;
150 gfx::Size surface_size_
;
151 float device_scale_factor_
;
153 void CommitVSyncParameters(base::TimeTicks timebase
,
154 base::TimeDelta interval
);
156 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
157 void ReclaimResources(const CompositorFrameAck
* ack
);
158 void DidLoseOutputSurface();
159 void SetExternalStencilTest(bool enabled
);
160 void SetExternalDrawConstraints(
161 const gfx::Transform
& transform
,
162 const gfx::Rect
& viewport
,
163 const gfx::Rect
& clip
,
164 const gfx::Rect
& viewport_rect_for_tile_priority
,
165 const gfx::Transform
& transform_for_tile_priority
,
166 bool resourceless_software_draw
);
169 void SetUpContext3d();
170 void ResetContext3d();
171 void SetMemoryPolicy(const ManagedMemoryPolicy
& policy
);
173 bool external_stencil_test_enabled_
;
175 base::WeakPtrFactory
<OutputSurface
> weak_ptr_factory_
;
177 DISALLOW_COPY_AND_ASSIGN(OutputSurface
);
182 #endif // CC_OUTPUT_OUTPUT_SURFACE_H_