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
; }
33 class CompositorFrame
;
34 class CompositorFrameAck
;
35 struct ManagedMemoryPolicy
;
36 class OutputSurfaceClient
;
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
{
48 DEFAULT_MAX_FRAMES_PENDING
= 2
51 OutputSurface(const scoped_refptr
<ContextProvider
>& context_provider
,
52 const scoped_refptr
<ContextProvider
>& worker_context_provider
,
53 scoped_ptr
<SoftwareOutputDevice
> software_device
);
54 OutputSurface(const scoped_refptr
<ContextProvider
>& context_provider
,
55 const scoped_refptr
<ContextProvider
>& worker_context_provider
);
56 explicit OutputSurface(
57 const scoped_refptr
<ContextProvider
>& context_provider
);
59 explicit OutputSurface(scoped_ptr
<SoftwareOutputDevice
> software_device
);
61 OutputSurface(const scoped_refptr
<ContextProvider
>& context_provider
,
62 scoped_ptr
<SoftwareOutputDevice
> software_device
);
64 virtual ~OutputSurface();
68 : delegated_rendering(false),
69 max_frames_pending(0),
70 draw_and_swap_full_viewport_every_frame(false),
71 adjust_deadline_for_parent(true),
72 uses_default_gl_framebuffer(true),
73 flipped_output_surface(false),
74 can_force_reclaim_resources(false),
75 delegated_sync_points_required(true) {}
76 bool delegated_rendering
;
77 int max_frames_pending
;
78 bool draw_and_swap_full_viewport_every_frame
;
79 // This doesn't handle the <webview> case, but once BeginFrame is
80 // supported natively, we shouldn't need adjust_deadline_for_parent.
81 bool adjust_deadline_for_parent
;
82 // Whether this output surface renders to the default OpenGL zero
83 // framebuffer or to an offscreen framebuffer.
84 bool uses_default_gl_framebuffer
;
85 // Whether this OutputSurface is flipped or not.
86 bool flipped_output_surface
;
87 // Whether ForceReclaimResources can be called to reclaim all resources
88 // from the OutputSurface.
89 bool can_force_reclaim_resources
;
90 // True if sync points for resources are needed when swapping delegated
92 bool delegated_sync_points_required
;
95 const Capabilities
& capabilities() const {
99 virtual bool HasExternalStencilTest() const;
101 // Obtain the 3d context or the software device associated with this output
102 // surface. Either of these may return a null pointer, but not both.
103 // In the event of a lost context, the entire output surface should be
105 ContextProvider
* context_provider() const { return context_provider_
.get(); }
106 ContextProvider
* worker_context_provider() const {
107 return worker_context_provider_
.get();
109 SoftwareOutputDevice
* software_device() const {
110 return software_device_
.get();
113 // Called by the compositor on the compositor thread. This is a place where
114 // thread-specific data for the output surface can be initialized, since from
115 // this point on the output surface will only be used on the compositor
117 virtual bool BindToClient(OutputSurfaceClient
* client
);
119 virtual void EnsureBackbuffer();
120 virtual void DiscardBackbuffer();
122 virtual void Reshape(const gfx::Size
& size
, float scale_factor
);
123 gfx::Size
SurfaceSize() const { return surface_size_
; }
124 float device_scale_factor() const { return device_scale_factor_
; }
126 // If supported, this causes a ReclaimResources for all resources that are
128 virtual void ForceReclaimResources() {}
130 virtual void BindFramebuffer();
132 // The implementation may destroy or steal the contents of the CompositorFrame
133 // passed in (though it will not take ownership of the CompositorFrame
134 // itself). For successful swaps, the implementation must call
135 // OutputSurfaceClient::DidSwapBuffers() and eventually
136 // DidSwapBuffersComplete().
137 virtual void SwapBuffers(CompositorFrame
* frame
) = 0;
138 virtual void OnSwapBuffersComplete();
140 // Notifies frame-rate smoothness preference. If true, all non-critical
141 // processing should be stopped, or lowered in priority.
142 virtual void UpdateSmoothnessTakesPriority(bool prefer_smoothness
) {}
144 bool HasClient() { return !!client_
; }
146 // Get the class capable of informing cc of hardware overlay capability.
147 virtual OverlayCandidateValidator
* GetOverlayCandidateValidator() const;
149 // Get the texture for the main image's overlay.
150 virtual unsigned GetOverlayTextureId() const;
152 void DidLoseOutputSurface();
153 void SetMemoryPolicy(const ManagedMemoryPolicy
& policy
);
155 // Support for a pull-model where draws are requested by the output surface.
157 // OutputSurface::Invalidate is called by the compositor to notify that
158 // there's new content.
159 virtual void Invalidate() {}
161 // Updates the worker context provider's visibility, freeing GPU resources if
163 virtual void SetWorkerContextShouldAggressivelyFreeResources(bool is_visible
);
165 // If this returns true, then the surface will not attempt to draw.
166 virtual bool SurfaceIsSuspendForRecycle() const;
169 OutputSurfaceClient
* client_
;
171 void PostSwapBuffersComplete();
173 struct OutputSurface::Capabilities capabilities_
;
174 scoped_refptr
<ContextProvider
> context_provider_
;
175 scoped_refptr
<ContextProvider
> worker_context_provider_
;
176 scoped_ptr
<SoftwareOutputDevice
> software_device_
;
177 gfx::Size surface_size_
;
178 float device_scale_factor_
;
180 void CommitVSyncParameters(base::TimeTicks timebase
,
181 base::TimeDelta interval
);
183 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
184 void ReclaimResources(const CompositorFrameAck
* ack
);
185 void SetExternalStencilTest(bool enabled
);
186 void SetExternalDrawConstraints(
187 const gfx::Transform
& transform
,
188 const gfx::Rect
& viewport
,
189 const gfx::Rect
& clip
,
190 const gfx::Rect
& viewport_rect_for_tile_priority
,
191 const gfx::Transform
& transform_for_tile_priority
,
192 bool resourceless_software_draw
);
195 bool external_stencil_test_enabled_
;
197 base::WeakPtrFactory
<OutputSurface
> weak_ptr_factory_
;
199 DISALLOW_COPY_AND_ASSIGN(OutputSurface
);
204 #endif // CC_OUTPUT_OUTPUT_SURFACE_H_