Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / output / output_surface.h
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/output/context_provider.h"
16 #include "cc/output/overlay_candidate_validator.h"
17 #include "cc/output/software_output_device.h"
18
19 namespace base { class SingleThreadTaskRunner; }
20
21 namespace ui { struct LatencyInfo; }
22
23 namespace gfx {
24 class Rect;
25 class Size;
26 class Transform;
27 }
28
29 namespace cc {
30
31 class CompositorFrame;
32 class CompositorFrameAck;
33 struct ManagedMemoryPolicy;
34 class OutputSurfaceClient;
35
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 {
44  public:
45   enum {
46     DEFAULT_MAX_FRAMES_PENDING = 2
47   };
48
49   explicit OutputSurface(
50       const scoped_refptr<ContextProvider>& context_provider);
51
52   explicit OutputSurface(scoped_ptr<SoftwareOutputDevice> software_device);
53
54   OutputSurface(const scoped_refptr<ContextProvider>& context_provider,
55                 scoped_ptr<SoftwareOutputDevice> software_device);
56
57   virtual ~OutputSurface();
58
59   struct Capabilities {
60     Capabilities()
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;
80   };
81
82   const Capabilities& capabilities() const {
83     return capabilities_;
84   }
85
86   virtual bool HasExternalStencilTest() const;
87
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
91   // recreated.
92   ContextProvider* context_provider() const { return context_provider_.get(); }
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   virtual void EnsureBackbuffer();
109   virtual void DiscardBackbuffer();
110
111   virtual void Reshape(const gfx::Size& size, float scale_factor);
112   virtual gfx::Size SurfaceSize() const;
113
114   virtual void BindFramebuffer();
115
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();
123
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) {}
127
128   // Requests a BeginFrame notification from the output surface. The
129   // notification will be delivered by calling
130   // OutputSurfaceClient::BeginFrame until the callback is disabled.
131   virtual void SetNeedsBeginFrame(bool enable) {}
132
133   bool HasClient() { return !!client_; }
134
135   // Get the class capable of informing cc of hardware overlay capability.
136   OverlayCandidateValidator* overlay_candidate_validator() const {
137     return overlay_candidate_validator_.get();
138   }
139
140   void DidLoseOutputSurface();
141   void SetMemoryPolicy(const ManagedMemoryPolicy& policy);
142
143  protected:
144   OutputSurfaceClient* client_;
145
146   // Synchronously initialize context3d and enter hardware mode.
147   // This can only supported in threaded compositing mode.
148   bool InitializeAndSetContext3d(
149       scoped_refptr<ContextProvider> context_provider);
150   void ReleaseGL();
151
152   void PostSwapBuffersComplete();
153
154   struct OutputSurface::Capabilities capabilities_;
155   scoped_refptr<ContextProvider> context_provider_;
156   scoped_ptr<SoftwareOutputDevice> software_device_;
157   scoped_ptr<OverlayCandidateValidator> overlay_candidate_validator_;
158   gfx::Size surface_size_;
159   float device_scale_factor_;
160
161   void CommitVSyncParameters(base::TimeTicks timebase,
162                              base::TimeDelta interval);
163
164   void SetNeedsRedrawRect(const gfx::Rect& damage_rect);
165   void ReclaimResources(const CompositorFrameAck* ack);
166   void SetExternalStencilTest(bool enabled);
167   void SetExternalDrawConstraints(
168       const gfx::Transform& transform,
169       const gfx::Rect& viewport,
170       const gfx::Rect& clip,
171       const gfx::Rect& viewport_rect_for_tile_priority,
172       const gfx::Transform& transform_for_tile_priority,
173       bool resourceless_software_draw);
174
175  private:
176   void SetUpContext3d();
177   void ResetContext3d();
178
179   bool external_stencil_test_enabled_;
180
181   base::WeakPtrFactory<OutputSurface> weak_ptr_factory_;
182
183   DISALLOW_COPY_AND_ASSIGN(OutputSurface);
184 };
185
186 }  // namespace cc
187
188 #endif  // CC_OUTPUT_OUTPUT_SURFACE_H_