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