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 RequestNewOutputSurface() override
;
81 void DidInitializeOutputSurface() override
;
82 void DidFailToInitializeOutputSurface() override
;
83 void WillCommit() override
{}
84 void DidCommit() override
;
85 void DidCommitAndDrawFrame() override
{}
86 void DidCompleteSwapBuffers() override
;
87 void DidCompletePageScaleAnimation() override
{}
88 void RecordFrameTimingEvents(
89 scoped_ptr
<cc::FrameTimingTracker::CompositeTimingSet
> composite_events
,
90 scoped_ptr
<cc::FrameTimingTracker::MainFrameTimingSet
> main_frame_events
)
93 // LayerTreeHostSingleThreadClient implementation.
94 void ScheduleComposite() override
;
95 void ScheduleAnimation() override
;
96 void DidPostSwapBuffers() override
;
97 void DidAbortSwapBuffers() override
;
99 // WindowAndroidCompositor implementation.
100 void AttachLayerForReadback(scoped_refptr
<cc::Layer
> layer
) override
;
101 void RequestCopyOfOutputOnRootLayer(
102 scoped_ptr
<cc::CopyOutputRequest
> request
) override
;
103 void OnVSync(base::TimeTicks frame_time
,
104 base::TimeDelta vsync_period
) override
;
105 void SetNeedsAnimate() override
;
107 void SetWindowSurface(ANativeWindow
* window
);
108 void SetVisible(bool visible
);
110 enum CompositingTrigger
{
112 COMPOSITE_IMMEDIATELY
,
113 COMPOSITE_EVENTUALLY
,
115 void PostComposite(CompositingTrigger trigger
);
116 void Composite(CompositingTrigger trigger
);
117 void CreateOutputSurface();
119 bool WillCompositeThisFrame() const {
120 return current_composite_task_
&&
121 !current_composite_task_
->callback().is_null();
123 bool DidCompositeThisFrame() const {
124 return current_composite_task_
&&
125 current_composite_task_
->callback().is_null();
127 bool WillComposite() const {
128 return WillCompositeThisFrame() ||
129 composite_on_vsync_trigger_
!= DO_NOT_COMPOSITE
;
131 void CancelComposite() {
132 DCHECK(WillComposite());
133 if (WillCompositeThisFrame())
134 current_composite_task_
->Cancel();
135 current_composite_task_
.reset();
136 composite_on_vsync_trigger_
= DO_NOT_COMPOSITE
;
137 will_composite_immediately_
= false;
139 void CreateLayerTreeHost();
141 void OnGpuChannelEstablished();
142 void OnGpuChannelTimeout();
144 // root_layer_ is the persistent internal root layer, while subroot_layer_
145 // is the one attached by the compositor client.
146 scoped_refptr
<cc::Layer
> root_layer_
;
147 scoped_refptr
<cc::Layer
> subroot_layer_
;
149 scoped_ptr
<cc::LayerTreeHost
> host_
;
150 ui::UIResourceProvider ui_resource_provider_
;
151 ui::ResourceManagerImpl resource_manager_
;
153 scoped_ptr
<cc::OnscreenDisplayClient
> display_client_
;
154 scoped_ptr
<cc::SurfaceIdAllocator
> surface_id_allocator_
;
157 bool has_transparent_background_
;
158 float device_scale_factor_
;
160 ANativeWindow
* window_
;
163 CompositorClient
* client_
;
165 gfx::NativeWindow root_window_
;
167 // Used locally to track whether a call to LTH::Composite() did result in
168 // a posted SwapBuffers().
169 bool did_post_swapbuffers_
;
171 // Used locally to inhibit ScheduleComposite() during Layout().
172 bool ignore_schedule_composite_
;
174 // Whether we need to composite in general because of any invalidation or
176 bool needs_composite_
;
178 // Whether we need to update animations on the next composite.
181 // Whether we posted a task and are about to composite.
182 bool will_composite_immediately_
;
184 // How we should schedule Composite during the next vsync.
185 CompositingTrigger composite_on_vsync_trigger_
;
187 // The Composite operation scheduled for the current vsync interval.
188 scoped_ptr
<base::CancelableClosure
> current_composite_task_
;
190 // The number of SwapBuffer calls that have not returned and ACK'd from
192 unsigned int pending_swapbuffers_
;
194 size_t num_successive_context_creation_failures_
;
196 base::TimeDelta vsync_period_
;
197 base::TimeTicks last_vsync_
;
199 base::OneShotTimer
<CompositorImpl
> establish_gpu_channel_timeout_
;
201 // Whether there is an OutputSurface request pending from the current
202 // |host_|. Becomes |true| if RequestNewOutputSurface is called, and |false|
203 // if |host_| is deleted or we succeed in creating *and* initializing an
204 // OutputSurface (which is essentially the contract with cc).
205 bool output_surface_request_pending_
;
207 base::WeakPtrFactory
<CompositorImpl
> weak_factory_
;
209 DISALLOW_COPY_AND_ASSIGN(CompositorImpl
);
212 } // namespace content
214 #endif // CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_