1 // Copyright (c) 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 CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
8 #include "base/basictypes.h"
9 #include "base/cancelable_callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/timer/timer.h"
14 #include "cc/trees/layer_tree_host_client.h"
15 #include "cc/trees/layer_tree_host_single_thread_client.h"
16 #include "content/common/content_export.h"
17 #include "content/common/gpu/client/context_provider_command_buffer.h"
18 #include "content/public/browser/android/compositor.h"
19 #include "gpu/command_buffer/common/capabilities.h"
20 #include "third_party/khronos/GLES2/gl2.h"
21 #include "ui/android/resources/resource_manager_impl.h"
22 #include "ui/android/resources/ui_resource_provider.h"
23 #include "ui/android/window_android_compositor.h"
31 class OnscreenDisplayClient
;
32 class SurfaceIdAllocator
;
37 class CompositorClient
;
39 // -----------------------------------------------------------------------------
40 // Browser-side compositor that manages a tree of content and UI layers.
41 // -----------------------------------------------------------------------------
42 class CONTENT_EXPORT CompositorImpl
44 public cc::LayerTreeHostClient
,
45 public cc::LayerTreeHostSingleThreadClient
,
46 public ui::WindowAndroidCompositor
{
50 virtual void OnUpdateVSyncParameters(base::TimeTicks timebase
,
51 base::TimeDelta interval
) = 0;
54 CompositorImpl(CompositorClient
* client
, gfx::NativeWindow root_window
);
55 ~CompositorImpl() override
;
57 static bool IsInitialized();
59 static cc::SurfaceManager
* GetSurfaceManager();
60 static scoped_ptr
<cc::SurfaceIdAllocator
> CreateSurfaceIdAllocator();
62 void PopulateGpuCapabilities(gpu::Capabilities gpu_capabilities
);
64 void AddObserver(VSyncObserver
* observer
);
65 void RemoveObserver(VSyncObserver
* observer
);
68 // Compositor implementation.
69 void SetRootLayer(scoped_refptr
<cc::Layer
> root
) override
;
70 void SetSurface(jobject surface
) override
;
71 void setDeviceScaleFactor(float factor
) override
;
72 void SetWindowBounds(const gfx::Size
& size
) override
;
73 void SetHasTransparentBackground(bool flag
) override
;
74 void SetNeedsComposite() override
;
75 ui::UIResourceProvider
& GetUIResourceProvider() override
;
76 ui::ResourceManager
& GetResourceManager() override
;
78 // LayerTreeHostClient implementation.
79 void WillBeginMainFrame() override
{}
80 void DidBeginMainFrame() override
{}
81 void BeginMainFrame(const cc::BeginFrameArgs
& args
) override
{}
82 void BeginMainFrameNotExpectedSoon() override
{}
83 void Layout() override
;
84 void ApplyViewportDeltas(const gfx::Vector2dF
& inner_delta
,
85 const gfx::Vector2dF
& outer_delta
,
86 const gfx::Vector2dF
& elastic_overscroll_delta
,
88 float top_controls_delta
) override
{}
89 void RequestNewOutputSurface() override
;
90 void DidInitializeOutputSurface() override
;
91 void DidFailToInitializeOutputSurface() override
;
92 void WillCommit() override
{}
93 void DidCommit() override
;
94 void DidCommitAndDrawFrame() override
{}
95 void DidCompleteSwapBuffers() override
;
96 void DidCompletePageScaleAnimation() override
{}
97 void RecordFrameTimingEvents(
98 scoped_ptr
<cc::FrameTimingTracker::CompositeTimingSet
> composite_events
,
99 scoped_ptr
<cc::FrameTimingTracker::MainFrameTimingSet
> main_frame_events
)
102 // LayerTreeHostSingleThreadClient implementation.
103 void ScheduleComposite() override
;
104 void ScheduleAnimation() override
;
105 void DidPostSwapBuffers() override
;
106 void DidAbortSwapBuffers() override
;
108 // WindowAndroidCompositor implementation.
109 void AttachLayerForReadback(scoped_refptr
<cc::Layer
> layer
) override
;
110 void RequestCopyOfOutputOnRootLayer(
111 scoped_ptr
<cc::CopyOutputRequest
> request
) override
;
112 void OnVSync(base::TimeTicks frame_time
,
113 base::TimeDelta vsync_period
) override
;
114 void SetNeedsAnimate() override
;
116 void SetWindowSurface(ANativeWindow
* window
);
117 void SetVisible(bool visible
);
119 enum CompositingTrigger
{
121 COMPOSITE_IMMEDIATELY
,
122 COMPOSITE_EVENTUALLY
,
124 void PostComposite(CompositingTrigger trigger
);
125 void Composite(CompositingTrigger trigger
);
126 void CreateOutputSurface();
128 bool WillCompositeThisFrame() const {
129 return current_composite_task_
&&
130 !current_composite_task_
->callback().is_null();
132 bool DidCompositeThisFrame() const {
133 return current_composite_task_
&&
134 current_composite_task_
->callback().is_null();
136 bool WillComposite() const {
137 return WillCompositeThisFrame() ||
138 composite_on_vsync_trigger_
!= DO_NOT_COMPOSITE
;
140 void CancelComposite() {
141 DCHECK(WillComposite());
142 if (WillCompositeThisFrame())
143 current_composite_task_
->Cancel();
144 current_composite_task_
.reset();
145 composite_on_vsync_trigger_
= DO_NOT_COMPOSITE
;
146 will_composite_immediately_
= false;
148 void CreateLayerTreeHost();
150 void OnGpuChannelEstablished();
151 void OnGpuChannelTimeout();
153 // root_layer_ is the persistent internal root layer, while subroot_layer_
154 // is the one attached by the compositor client.
155 scoped_refptr
<cc::Layer
> root_layer_
;
156 scoped_refptr
<cc::Layer
> subroot_layer_
;
158 scoped_ptr
<cc::LayerTreeHost
> host_
;
159 ui::UIResourceProvider ui_resource_provider_
;
160 ui::ResourceManagerImpl resource_manager_
;
162 scoped_ptr
<cc::OnscreenDisplayClient
> display_client_
;
163 scoped_ptr
<cc::SurfaceIdAllocator
> surface_id_allocator_
;
166 bool has_transparent_background_
;
167 float device_scale_factor_
;
169 ANativeWindow
* window_
;
172 CompositorClient
* client_
;
174 gfx::NativeWindow root_window_
;
176 // Used locally to track whether a call to LTH::Composite() did result in
177 // a posted SwapBuffers().
178 bool did_post_swapbuffers_
;
180 // Used locally to inhibit ScheduleComposite() during Layout().
181 bool ignore_schedule_composite_
;
183 // Whether we need to composite in general because of any invalidation or
185 bool needs_composite_
;
187 // Whether we need to update animations on the next composite.
190 // Whether we posted a task and are about to composite.
191 bool will_composite_immediately_
;
193 // How we should schedule Composite during the next vsync.
194 CompositingTrigger composite_on_vsync_trigger_
;
196 // The Composite operation scheduled for the current vsync interval.
197 scoped_ptr
<base::CancelableClosure
> current_composite_task_
;
199 // The number of SwapBuffer calls that have not returned and ACK'd from
201 unsigned int pending_swapbuffers_
;
203 size_t num_successive_context_creation_failures_
;
205 base::TimeDelta vsync_period_
;
206 base::TimeTicks last_vsync_
;
208 base::OneShotTimer
<CompositorImpl
> establish_gpu_channel_timeout_
;
210 // Whether there is an OutputSurface request pending from the current
211 // |host_|. Becomes |true| if RequestNewOutputSurface is called, and |false|
212 // if |host_| is deleted or we succeed in creating *and* initializing an
213 // OutputSurface (which is essentially the contract with cc).
214 bool output_surface_request_pending_
;
216 base::ObserverList
<VSyncObserver
, true> observer_list_
;
218 base::WeakPtrFactory
<CompositorImpl
> weak_factory_
;
220 DISALLOW_COPY_AND_ASSIGN(CompositorImpl
);
223 } // namespace content
225 #endif // CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_