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 flipped_output_surface(false) {}
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 // Whether this OutputSurface is flipped or not.
79 bool flipped_output_surface
;
82 const Capabilities
& capabilities() const {
86 virtual bool HasExternalStencilTest() const;
88 // Obtain the 3d context or the software device associated with this output
89 // surface. Either of these may return a null pointer, but not both.
90 // In the event of a lost context, the entire output surface should be
92 ContextProvider
* context_provider() const { return context_provider_
.get(); }
93 SoftwareOutputDevice
* software_device() const {
94 return software_device_
.get();
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
101 virtual bool BindToClient(OutputSurfaceClient
* client
);
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();
108 virtual void EnsureBackbuffer();
109 virtual void DiscardBackbuffer();
111 virtual void Reshape(const gfx::Size
& size
, float scale_factor
);
112 virtual gfx::Size
SurfaceSize() const;
114 virtual void BindFramebuffer();
116 // The implementation may destroy or steal the contents of the CompositorFrame
117 // passed in (though it will not take ownership of the CompositorFrame
118 // itself). For successful swaps, the implementation must call
119 // OutputSurfaceClient::DidSwapBuffers() and eventually
120 // DidSwapBuffersComplete().
121 virtual void SwapBuffers(CompositorFrame
* frame
) = 0;
122 virtual void OnSwapBuffersComplete();
124 // Notifies frame-rate smoothness preference. If true, all non-critical
125 // processing should be stopped, or lowered in priority.
126 virtual void UpdateSmoothnessTakesPriority(bool prefer_smoothness
) {}
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();
135 void DidLoseOutputSurface();
136 void SetMemoryPolicy(const ManagedMemoryPolicy
& policy
);
139 OutputSurfaceClient
* client_
;
141 // Synchronously initialize context3d and enter hardware mode.
142 // This can only supported in threaded compositing mode.
143 bool InitializeAndSetContext3d(
144 scoped_refptr
<ContextProvider
> context_provider
);
147 void PostSwapBuffersComplete();
149 struct OutputSurface::Capabilities capabilities_
;
150 scoped_refptr
<ContextProvider
> context_provider_
;
151 scoped_ptr
<SoftwareOutputDevice
> software_device_
;
152 scoped_ptr
<OverlayCandidateValidator
> overlay_candidate_validator_
;
153 gfx::Size surface_size_
;
154 float device_scale_factor_
;
156 void CommitVSyncParameters(base::TimeTicks timebase
,
157 base::TimeDelta interval
);
159 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
160 void ReclaimResources(const CompositorFrameAck
* ack
);
161 void SetExternalStencilTest(bool enabled
);
162 void SetExternalDrawConstraints(
163 const gfx::Transform
& transform
,
164 const gfx::Rect
& viewport
,
165 const gfx::Rect
& clip
,
166 const gfx::Rect
& viewport_rect_for_tile_priority
,
167 const gfx::Transform
& transform_for_tile_priority
,
168 bool resourceless_software_draw
);
171 void SetUpContext3d();
172 void ResetContext3d();
174 bool external_stencil_test_enabled_
;
176 base::WeakPtrFactory
<OutputSurface
> weak_ptr_factory_
;
178 DISALLOW_COPY_AND_ASSIGN(OutputSurface
);
183 #endif // CC_OUTPUT_OUTPUT_SURFACE_H_