• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
7 
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 "cc/trees/layer_tree_host_client.h"
14 #include "cc/trees/layer_tree_host_single_thread_client.h"
15 #include "content/browser/android/ui_resource_provider_impl.h"
16 #include "content/browser/renderer_host/image_transport_factory_android.h"
17 #include "content/common/content_export.h"
18 #include "content/common/gpu/client/context_provider_command_buffer.h"
19 #include "content/public/browser/android/compositor.h"
20 #include "gpu/command_buffer/common/capabilities.h"
21 #include "third_party/khronos/GLES2/gl2.h"
22 #include "ui/base/android/system_ui_resource_manager.h"
23 #include "ui/base/android/window_android_compositor.h"
24 
25 class SkBitmap;
26 struct ANativeWindow;
27 
28 namespace cc {
29 class Layer;
30 class LayerTreeHost;
31 }
32 
33 namespace content {
34 class CompositorClient;
35 class UIResourceProvider;
36 
37 // -----------------------------------------------------------------------------
38 // Browser-side compositor that manages a tree of content and UI layers.
39 // -----------------------------------------------------------------------------
40 class CONTENT_EXPORT CompositorImpl
41     : public Compositor,
42       public cc::LayerTreeHostClient,
43       public cc::LayerTreeHostSingleThreadClient,
44       public ImageTransportFactoryAndroidObserver,
45       public ui::WindowAndroidCompositor {
46  public:
47   CompositorImpl(CompositorClient* client, gfx::NativeWindow root_window);
48   virtual ~CompositorImpl();
49 
50   static bool IsInitialized();
51 
52   // Creates a surface texture and returns a surface texture id. Returns -1 on
53   // failure.
54   static int CreateSurfaceTexture(int child_process_id);
55 
56   // Destroy all surface textures associated with |child_process_id|.
57   static void DestroyAllSurfaceTextures(int child_process_id);
58 
59   void PopulateGpuCapabilities(gpu::Capabilities gpu_capabilities);
60 
61  private:
62   // Compositor implementation.
63   virtual void SetRootLayer(scoped_refptr<cc::Layer> root) OVERRIDE;
64   virtual void SetSurface(jobject surface) OVERRIDE;
65   virtual void SetVisible(bool visible) OVERRIDE;
66   virtual void setDeviceScaleFactor(float factor) OVERRIDE;
67   virtual void SetWindowBounds(const gfx::Size& size) OVERRIDE;
68   virtual void SetHasTransparentBackground(bool flag) OVERRIDE;
69   virtual void SetNeedsComposite() OVERRIDE;
70   virtual UIResourceProvider& GetUIResourceProvider() OVERRIDE;
71 
72   // LayerTreeHostClient implementation.
WillBeginMainFrame(int frame_id)73   virtual void WillBeginMainFrame(int frame_id) OVERRIDE {}
DidBeginMainFrame()74   virtual void DidBeginMainFrame() OVERRIDE {}
BeginMainFrame(const cc::BeginFrameArgs & args)75   virtual void BeginMainFrame(const cc::BeginFrameArgs& args) OVERRIDE {}
76   virtual void Layout() OVERRIDE;
ApplyViewportDeltas(const gfx::Vector2d & scroll_delta,float page_scale,float top_controls_delta)77   virtual void ApplyViewportDeltas(
78       const gfx::Vector2d& scroll_delta,
79       float page_scale,
80       float top_controls_delta) OVERRIDE {}
81   virtual void RequestNewOutputSurface(bool fallback) OVERRIDE;
DidInitializeOutputSurface()82   virtual void DidInitializeOutputSurface() OVERRIDE {}
WillCommit()83   virtual void WillCommit() OVERRIDE {}
84   virtual void DidCommit() OVERRIDE;
DidCommitAndDrawFrame()85   virtual void DidCommitAndDrawFrame() OVERRIDE {}
86   virtual void DidCompleteSwapBuffers() OVERRIDE;
87 
88   // LayerTreeHostSingleThreadClient implementation.
89   virtual void ScheduleComposite() OVERRIDE;
90   virtual void ScheduleAnimation() OVERRIDE;
91   virtual void DidPostSwapBuffers() OVERRIDE;
92   virtual void DidAbortSwapBuffers() OVERRIDE;
93 
94   // ImageTransportFactoryAndroidObserver implementation.
95   virtual void OnLostResources() OVERRIDE;
96 
97   // WindowAndroidCompositor implementation.
98   virtual void AttachLayerForReadback(scoped_refptr<cc::Layer> layer) OVERRIDE;
99   virtual void RequestCopyOfOutputOnRootLayer(
100       scoped_ptr<cc::CopyOutputRequest> request) OVERRIDE;
101   virtual void OnVSync(base::TimeTicks frame_time,
102                        base::TimeDelta vsync_period) OVERRIDE;
103   virtual void SetNeedsAnimate() OVERRIDE;
104   virtual ui::SystemUIResourceManager& GetSystemUIResourceManager() OVERRIDE;
105 
106   void SetWindowSurface(ANativeWindow* window);
107 
108   enum CompositingTrigger {
109     DO_NOT_COMPOSITE,
110     COMPOSITE_IMMEDIATELY,
111     COMPOSITE_EVENTUALLY,
112   };
113   void PostComposite(CompositingTrigger trigger);
114   void Composite(CompositingTrigger trigger);
115   void CreateOutputSurface(bool fallback);
116 
WillCompositeThisFrame()117   bool WillCompositeThisFrame() const {
118     return current_composite_task_ &&
119            !current_composite_task_->callback().is_null();
120   }
DidCompositeThisFrame()121   bool DidCompositeThisFrame() const {
122     return current_composite_task_ &&
123            current_composite_task_->callback().is_null();
124   }
WillComposite()125   bool WillComposite() const {
126     return WillCompositeThisFrame() ||
127            composite_on_vsync_trigger_ != DO_NOT_COMPOSITE;
128   }
CancelComposite()129   void CancelComposite() {
130     DCHECK(WillComposite());
131     if (WillCompositeThisFrame())
132       current_composite_task_->Cancel();
133     current_composite_task_.reset();
134     composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
135     will_composite_immediately_ = false;
136   }
137   void OnGpuChannelEstablished();
138 
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_;
143 
144   scoped_ptr<cc::LayerTreeHost> host_;
145   content::UIResourceProviderImpl ui_resource_provider_;
146 
147   gfx::Size size_;
148   bool has_transparent_background_;
149   float device_scale_factor_;
150 
151   ANativeWindow* window_;
152   int surface_id_;
153 
154   CompositorClient* client_;
155 
156   gfx::NativeWindow root_window_;
157 
158   // Used locally to track whether a call to LTH::Composite() did result in
159   // a posted SwapBuffers().
160   bool did_post_swapbuffers_;
161 
162   // Used locally to inhibit ScheduleComposite() during Layout().
163   bool ignore_schedule_composite_;
164 
165   // Whether we need to composite in general because of any invalidation or
166   // explicit request.
167   bool needs_composite_;
168 
169   // Whether we need to update animations on the next composite.
170   bool needs_animate_;
171 
172   // Whether we posted a task and are about to composite.
173   bool will_composite_immediately_;
174 
175   // How we should schedule Composite during the next vsync.
176   CompositingTrigger composite_on_vsync_trigger_;
177 
178   // The Composite operation scheduled for the current vsync interval.
179   scoped_ptr<base::CancelableClosure> current_composite_task_;
180 
181   // The number of SwapBuffer calls that have not returned and ACK'd from
182   // the GPU thread.
183   unsigned int pending_swapbuffers_;
184 
185   base::TimeDelta vsync_period_;
186   base::TimeTicks last_vsync_;
187 
188   base::WeakPtrFactory<CompositorImpl> weak_factory_;
189 
190   DISALLOW_COPY_AND_ASSIGN(CompositorImpl);
191 };
192 
193 } // namespace content
194 
195 #endif  // CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
196