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
;
36 class CompositorClient
;
38 // -----------------------------------------------------------------------------
39 // Browser-side compositor that manages a tree of content and UI layers.
40 // -----------------------------------------------------------------------------
41 class CONTENT_EXPORT CompositorImpl
43 public cc::LayerTreeHostClient
,
44 public cc::LayerTreeHostSingleThreadClient
,
45 public ui::WindowAndroidCompositor
{
47 CompositorImpl(CompositorClient
* client
, gfx::NativeWindow root_window
);
48 ~CompositorImpl() override
;
50 static bool IsInitialized();
52 void PopulateGpuCapabilities(gpu::Capabilities gpu_capabilities
);
55 // Compositor implementation.
56 void SetRootLayer(scoped_refptr
<cc::Layer
> root
) override
;
57 void SetSurface(jobject surface
) override
;
58 void setDeviceScaleFactor(float factor
) override
;
59 void SetWindowBounds(const gfx::Size
& size
) override
;
60 void SetHasTransparentBackground(bool flag
) override
;
61 void SetNeedsComposite() override
;
62 ui::UIResourceProvider
& GetUIResourceProvider() override
;
63 ui::ResourceManager
& GetResourceManager() override
;
65 // LayerTreeHostClient implementation.
66 void WillBeginMainFrame() override
{}
67 void DidBeginMainFrame() override
{}
68 void BeginMainFrame(const cc::BeginFrameArgs
& args
) override
{}
69 void BeginMainFrameNotExpectedSoon() override
{}
70 void Layout() override
;
71 void ApplyViewportDeltas(const gfx::Vector2dF
& inner_delta
,
72 const gfx::Vector2dF
& outer_delta
,
73 const gfx::Vector2dF
& elastic_overscroll_delta
,
75 float top_controls_delta
) override
{}
76 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
78 float top_controls_delta
) override
{}
79 void RequestNewOutputSurface() override
;
80 void DidInitializeOutputSurface() override
;
81 void DidFailToInitializeOutputSurface() override
;
82 void WillCommit() override
{}
83 void DidCommit() override
;
84 void DidCommitAndDrawFrame() override
{}
85 void DidCompleteSwapBuffers() override
;
86 void DidCompletePageScaleAnimation() override
{}
88 // LayerTreeHostSingleThreadClient implementation.
89 void ScheduleComposite() override
;
90 void ScheduleAnimation() override
;
91 void DidPostSwapBuffers() override
;
92 void DidAbortSwapBuffers() override
;
94 // WindowAndroidCompositor implementation.
95 void AttachLayerForReadback(scoped_refptr
<cc::Layer
> layer
) override
;
96 void RequestCopyOfOutputOnRootLayer(
97 scoped_ptr
<cc::CopyOutputRequest
> request
) override
;
98 void OnVSync(base::TimeTicks frame_time
,
99 base::TimeDelta vsync_period
) override
;
100 void SetNeedsAnimate() override
;
102 void SetWindowSurface(ANativeWindow
* window
);
103 void SetVisible(bool visible
);
105 enum CompositingTrigger
{
107 COMPOSITE_IMMEDIATELY
,
108 COMPOSITE_EVENTUALLY
,
110 void PostComposite(CompositingTrigger trigger
);
111 void Composite(CompositingTrigger trigger
);
112 void CreateOutputSurface();
114 bool WillCompositeThisFrame() const {
115 return current_composite_task_
&&
116 !current_composite_task_
->callback().is_null();
118 bool DidCompositeThisFrame() const {
119 return current_composite_task_
&&
120 current_composite_task_
->callback().is_null();
122 bool WillComposite() const {
123 return WillCompositeThisFrame() ||
124 composite_on_vsync_trigger_
!= DO_NOT_COMPOSITE
;
126 void CancelComposite() {
127 DCHECK(WillComposite());
128 if (WillCompositeThisFrame())
129 current_composite_task_
->Cancel();
130 current_composite_task_
.reset();
131 composite_on_vsync_trigger_
= DO_NOT_COMPOSITE
;
132 will_composite_immediately_
= false;
134 void CreateLayerTreeHost();
136 void OnGpuChannelEstablished();
137 void OnGpuChannelTimeout();
139 // root_layer_ is the persistent internal root layer, while subroot_layer_
140 // is the one attached by the compositor client.
141 scoped_refptr
<cc::Layer
> root_layer_
;
142 scoped_refptr
<cc::Layer
> subroot_layer_
;
144 scoped_ptr
<cc::LayerTreeHost
> host_
;
145 ui::UIResourceProvider ui_resource_provider_
;
146 ui::ResourceManagerImpl resource_manager_
;
148 scoped_ptr
<cc::OnscreenDisplayClient
> display_client_
;
149 scoped_ptr
<cc::SurfaceIdAllocator
> surface_id_allocator_
;
152 bool has_transparent_background_
;
153 float device_scale_factor_
;
155 ANativeWindow
* window_
;
158 CompositorClient
* client_
;
160 gfx::NativeWindow root_window_
;
162 // Used locally to track whether a call to LTH::Composite() did result in
163 // a posted SwapBuffers().
164 bool did_post_swapbuffers_
;
166 // Used locally to inhibit ScheduleComposite() during Layout().
167 bool ignore_schedule_composite_
;
169 // Whether we need to composite in general because of any invalidation or
171 bool needs_composite_
;
173 // Whether we need to update animations on the next composite.
176 // Whether we posted a task and are about to composite.
177 bool will_composite_immediately_
;
179 // How we should schedule Composite during the next vsync.
180 CompositingTrigger composite_on_vsync_trigger_
;
182 // The Composite operation scheduled for the current vsync interval.
183 scoped_ptr
<base::CancelableClosure
> current_composite_task_
;
185 // The number of SwapBuffer calls that have not returned and ACK'd from
187 unsigned int pending_swapbuffers_
;
189 size_t num_successive_context_creation_failures_
;
191 base::TimeDelta vsync_period_
;
192 base::TimeTicks last_vsync_
;
194 base::OneShotTimer
<CompositorImpl
> establish_gpu_channel_timeout_
;
196 // Whether there is an OutputSurface request pending from the current
197 // |host_|. Becomes |true| if RequestNewOutputSurface is called, and |false|
198 // if |host_| is deleted or we succeed in creating *and* initializing an
199 // OutputSurface (which is essentially the contract with cc).
200 bool output_surface_request_pending_
;
202 base::WeakPtrFactory
<CompositorImpl
> weak_factory_
;
204 DISALLOW_COPY_AND_ASSIGN(CompositorImpl
);
207 } // namespace content
209 #endif // CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_