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
{
48 CompositorImpl(CompositorClient
* client
, gfx::NativeWindow root_window
);
49 ~CompositorImpl() override
;
51 static bool IsInitialized();
53 static cc::SurfaceManager
* GetSurfaceManager();
54 static scoped_ptr
<cc::SurfaceIdAllocator
> CreateSurfaceIdAllocator();
56 void PopulateGpuCapabilities(gpu::Capabilities gpu_capabilities
);
59 // Compositor implementation.
60 void SetRootLayer(scoped_refptr
<cc::Layer
> root
) override
;
61 void SetSurface(jobject surface
) override
;
62 void setDeviceScaleFactor(float factor
) override
;
63 void SetWindowBounds(const gfx::Size
& size
) override
;
64 void SetHasTransparentBackground(bool flag
) override
;
65 void SetNeedsComposite() override
;
66 ui::UIResourceProvider
& GetUIResourceProvider() override
;
67 ui::ResourceManager
& GetResourceManager() override
;
69 // LayerTreeHostClient implementation.
70 void WillBeginMainFrame() override
{}
71 void DidBeginMainFrame() override
{}
72 void BeginMainFrame(const cc::BeginFrameArgs
& args
) override
{}
73 void BeginMainFrameNotExpectedSoon() override
{}
74 void Layout() override
;
75 void ApplyViewportDeltas(const gfx::Vector2dF
& inner_delta
,
76 const gfx::Vector2dF
& outer_delta
,
77 const gfx::Vector2dF
& elastic_overscroll_delta
,
79 float top_controls_delta
) override
{}
80 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
82 float top_controls_delta
) override
{}
83 void RequestNewOutputSurface() override
;
84 void DidInitializeOutputSurface() override
;
85 void DidFailToInitializeOutputSurface() override
;
86 void WillCommit() override
{}
87 void DidCommit() override
;
88 void DidCommitAndDrawFrame() override
{}
89 void DidCompleteSwapBuffers() override
;
90 void DidCompletePageScaleAnimation() override
{}
92 // LayerTreeHostSingleThreadClient implementation.
93 void ScheduleComposite() override
;
94 void ScheduleAnimation() override
;
95 void DidPostSwapBuffers() override
;
96 void DidAbortSwapBuffers() override
;
98 // WindowAndroidCompositor implementation.
99 void AttachLayerForReadback(scoped_refptr
<cc::Layer
> layer
) override
;
100 void RequestCopyOfOutputOnRootLayer(
101 scoped_ptr
<cc::CopyOutputRequest
> request
) override
;
102 void OnVSync(base::TimeTicks frame_time
,
103 base::TimeDelta vsync_period
) override
;
104 void SetNeedsAnimate() override
;
106 void SetWindowSurface(ANativeWindow
* window
);
107 void SetVisible(bool visible
);
109 enum CompositingTrigger
{
111 COMPOSITE_IMMEDIATELY
,
112 COMPOSITE_EVENTUALLY
,
114 void PostComposite(CompositingTrigger trigger
);
115 void Composite(CompositingTrigger trigger
);
116 void CreateOutputSurface();
118 bool WillCompositeThisFrame() const {
119 return current_composite_task_
&&
120 !current_composite_task_
->callback().is_null();
122 bool DidCompositeThisFrame() const {
123 return current_composite_task_
&&
124 current_composite_task_
->callback().is_null();
126 bool WillComposite() const {
127 return WillCompositeThisFrame() ||
128 composite_on_vsync_trigger_
!= DO_NOT_COMPOSITE
;
130 void CancelComposite() {
131 DCHECK(WillComposite());
132 if (WillCompositeThisFrame())
133 current_composite_task_
->Cancel();
134 current_composite_task_
.reset();
135 composite_on_vsync_trigger_
= DO_NOT_COMPOSITE
;
136 will_composite_immediately_
= false;
138 void CreateLayerTreeHost();
140 void OnGpuChannelEstablished();
141 void OnGpuChannelTimeout();
143 // root_layer_ is the persistent internal root layer, while subroot_layer_
144 // is the one attached by the compositor client.
145 scoped_refptr
<cc::Layer
> root_layer_
;
146 scoped_refptr
<cc::Layer
> subroot_layer_
;
148 scoped_ptr
<cc::LayerTreeHost
> host_
;
149 ui::UIResourceProvider ui_resource_provider_
;
150 ui::ResourceManagerImpl resource_manager_
;
152 scoped_ptr
<cc::OnscreenDisplayClient
> display_client_
;
153 scoped_ptr
<cc::SurfaceIdAllocator
> surface_id_allocator_
;
156 bool has_transparent_background_
;
157 float device_scale_factor_
;
159 ANativeWindow
* window_
;
162 CompositorClient
* client_
;
164 gfx::NativeWindow root_window_
;
166 // Used locally to track whether a call to LTH::Composite() did result in
167 // a posted SwapBuffers().
168 bool did_post_swapbuffers_
;
170 // Used locally to inhibit ScheduleComposite() during Layout().
171 bool ignore_schedule_composite_
;
173 // Whether we need to composite in general because of any invalidation or
175 bool needs_composite_
;
177 // Whether we need to update animations on the next composite.
180 // Whether we posted a task and are about to composite.
181 bool will_composite_immediately_
;
183 // How we should schedule Composite during the next vsync.
184 CompositingTrigger composite_on_vsync_trigger_
;
186 // The Composite operation scheduled for the current vsync interval.
187 scoped_ptr
<base::CancelableClosure
> current_composite_task_
;
189 // The number of SwapBuffer calls that have not returned and ACK'd from
191 unsigned int pending_swapbuffers_
;
193 size_t num_successive_context_creation_failures_
;
195 base::TimeDelta vsync_period_
;
196 base::TimeTicks last_vsync_
;
198 base::OneShotTimer
<CompositorImpl
> establish_gpu_channel_timeout_
;
200 // Whether there is an OutputSurface request pending from the current
201 // |host_|. Becomes |true| if RequestNewOutputSurface is called, and |false|
202 // if |host_| is deleted or we succeed in creating *and* initializing an
203 // OutputSurface (which is essentially the contract with cc).
204 bool output_surface_request_pending_
;
206 base::WeakPtrFactory
<CompositorImpl
> weak_factory_
;
208 DISALLOW_COPY_AND_ASSIGN(CompositorImpl
);
211 } // namespace content
213 #endif // CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_