OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/android/testshell/tab_manager.h" | 5 #include "chrome/android/testshell/tab_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/message_loop.h" |
13 #include "chrome/browser/android/tab_base_android_impl.h" | 14 #include "chrome/browser/android/tab_base_android_impl.h" |
14 #include "content/public/browser/android/compositor.h" | 15 #include "content/public/browser/android/compositor.h" |
15 #include "content/public/browser/android/draw_delegate.h" | 16 #include "content/public/browser/android/draw_delegate.h" |
16 #include "jni/TabManager_jni.h" | 17 #include "jni/TabManager_jni.h" |
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" | 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" |
18 | 19 |
19 #include <android/native_window_jni.h> | 20 #include <android/native_window_jni.h> |
20 | 21 |
21 using base::android::ScopedJavaLocalRef; | 22 using base::android::ScopedJavaLocalRef; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
| 26 class CompositorClient : public content::Compositor::Client { |
| 27 public: |
| 28 virtual void ScheduleComposite() OVERRIDE; |
| 29 }; |
| 30 |
25 struct GlobalState { | 31 struct GlobalState { |
| 32 GlobalState() |
| 33 : g_scheduled_composite(false) {} |
| 34 CompositorClient client; |
26 scoped_ptr<content::Compositor> compositor; | 35 scoped_ptr<content::Compositor> compositor; |
27 scoped_ptr<WebKit::WebLayer> root_layer; | 36 scoped_ptr<WebKit::WebLayer> root_layer; |
| 37 bool g_scheduled_composite; |
28 }; | 38 }; |
29 | 39 |
30 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; | 40 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; |
31 | 41 |
32 content::Compositor* GetCompositor() { | 42 content::Compositor* GetCompositor() { |
33 return g_global_state.Get().compositor.get(); | 43 return g_global_state.Get().compositor.get(); |
34 } | 44 } |
35 | 45 |
36 static void SurfacePresented( | 46 void Composite() { |
37 const content::DrawDelegate::SurfacePresentedCallback& callback, | 47 g_global_state.Get().g_scheduled_composite = false; |
38 uint32 sync_point) { | 48 if (GetCompositor()) { |
39 callback.Run(sync_point); | 49 GetCompositor()->Composite(); |
| 50 } |
40 } | 51 } |
41 | 52 |
42 void DummyCallback(uint32) { } | 53 void CompositorClient::ScheduleComposite() { |
43 | 54 if (!g_global_state.Get().g_scheduled_composite) { |
44 static void SurfaceUpdated( | 55 g_global_state.Get().g_scheduled_composite = true; |
45 uint64 texture, | 56 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&Composite)); |
46 content::RenderWidgetHostView* view, | 57 } |
47 const content::DrawDelegate::SurfacePresentedCallback& callback) { | |
48 GetCompositor()->OnSurfaceUpdated(base::Bind( | |
49 &SurfacePresented, callback)); | |
50 } | 58 } |
51 | 59 |
52 } // anonymous namespace | 60 } // anonymous namespace |
53 | 61 |
54 namespace chrome { | 62 namespace chrome { |
55 | 63 |
56 // Register native methods | 64 // Register native methods |
57 bool RegisterTabManager(JNIEnv* env) { | 65 bool RegisterTabManager(JNIEnv* env) { |
58 return RegisterNativesImpl(env); | 66 return RegisterNativesImpl(env); |
59 } | 67 } |
60 | 68 |
61 static void Init(JNIEnv* env, jclass clazz, jobject obj) { | 69 static void Init(JNIEnv* env, jclass clazz, jobject obj) { |
62 content::DrawDelegate::SurfaceUpdatedCallback cb = base::Bind( | |
63 &SurfaceUpdated); | |
64 content::DrawDelegate::GetInstance()->SetUpdateCallback(cb); | |
65 if (!GetCompositor()) { | 70 if (!GetCompositor()) { |
66 content::Compositor::Initialize(); | 71 content::Compositor::Initialize(); |
67 g_global_state.Get().compositor.reset(content::Compositor::Create()); | 72 g_global_state.Get().compositor.reset(content::Compositor::Create( |
| 73 &g_global_state.Get().client)); |
68 DCHECK(!g_global_state.Get().root_layer.get()); | 74 DCHECK(!g_global_state.Get().root_layer.get()); |
69 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); | 75 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); |
70 } | 76 } |
71 } | 77 } |
72 | 78 |
73 static void SurfaceCreated(JNIEnv* env, jclass clazz, jobject jsurface) { | 79 static void SurfaceCreated(JNIEnv* env, jclass clazz, jobject jsurface) { |
74 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); | 80 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); |
75 if (native_window) { | 81 if (native_window) { |
76 GetCompositor()->SetWindowSurface(native_window); | 82 GetCompositor()->SetWindowSurface(native_window); |
77 ANativeWindow_release(native_window); | 83 ANativeWindow_release(native_window); |
(...skipping 10 matching lines...) Expand all Loading... |
88 gfx::Size size = gfx::Size(width, height); | 94 gfx::Size size = gfx::Size(width, height); |
89 content::DrawDelegate::GetInstance()->SetBounds(size); | 95 content::DrawDelegate::GetInstance()->SetBounds(size); |
90 GetCompositor()->SetWindowBounds(size); | 96 GetCompositor()->SetWindowBounds(size); |
91 } | 97 } |
92 | 98 |
93 static void ShowTab(JNIEnv* env, jclass clazz, jint jtab) { | 99 static void ShowTab(JNIEnv* env, jclass clazz, jint jtab) { |
94 if (!GetCompositor()) | 100 if (!GetCompositor()) |
95 return; | 101 return; |
96 TabBaseAndroidImpl* tab = reinterpret_cast<TabBaseAndroidImpl*>(jtab); | 102 TabBaseAndroidImpl* tab = reinterpret_cast<TabBaseAndroidImpl*>(jtab); |
97 g_global_state.Get().root_layer->addChild(tab->tab_layer()); | 103 g_global_state.Get().root_layer->addChild(tab->tab_layer()); |
98 GetCompositor()->OnSurfaceUpdated(base::Bind(&DummyCallback)); | |
99 } | 104 } |
100 | 105 |
101 static void HideTab(JNIEnv* env, jclass clazz, jint jtab) { | 106 static void HideTab(JNIEnv* env, jclass clazz, jint jtab) { |
102 if (!GetCompositor()) | 107 if (!GetCompositor()) |
103 return; | 108 return; |
104 TabBaseAndroidImpl* tab = reinterpret_cast<TabBaseAndroidImpl*>(jtab); | 109 TabBaseAndroidImpl* tab = reinterpret_cast<TabBaseAndroidImpl*>(jtab); |
105 tab->tab_layer()->removeFromParent(); | 110 tab->tab_layer()->removeFromParent(); |
106 } | 111 } |
107 | 112 |
108 } // namespace chrome | 113 } // namespace chrome |
OLD | NEW |