Chromium Code Reviews| 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 "content/shell/android/shell_manager.h" | 5 #include "content/shell/android/shell_manager.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 12 #include "content/shell/shell.h" | 13 #include "content/shell/shell.h" |
| 13 #include "content/shell/shell_browser_context.h" | 14 #include "content/shell/shell_browser_context.h" |
| 14 #include "content/shell/shell_content_browser_client.h" | 15 #include "content/shell/shell_content_browser_client.h" |
| 16 #include "content/public/browser/android/compositor.h" | |
| 15 #include "content/public/browser/android/draw_delegate.h" | 17 #include "content/public/browser/android/draw_delegate.h" |
| 16 #include "content/public/browser/render_widget_host_view.h" | |
| 17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 18 #include "content/shell/android/draw_context.h" | |
| 19 #include "content/shell/shell.h" | 19 #include "content/shell/shell.h" |
| 20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 21 #include "jni/ShellManager_jni.h" | 21 #include "jni/ShellManager_jni.h" |
| 22 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" | |
| 22 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
| 23 | 24 |
| 24 #include <android/native_window_jni.h> | 25 #include <android/native_window_jni.h> |
| 25 | 26 |
| 26 using base::android::ScopedJavaLocalRef; | 27 using base::android::ScopedJavaLocalRef; |
| 27 using content::DrawContext; | 28 using content::Compositor; |
| 28 using content::DrawDelegate; | 29 using content::DrawDelegate; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 struct GlobalState { | 33 struct GlobalState { |
| 33 GlobalState() | |
| 34 : context(NULL) { | |
| 35 } | |
| 36 base::android::ScopedJavaGlobalRef<jobject> j_obj; | 34 base::android::ScopedJavaGlobalRef<jobject> j_obj; |
| 37 DrawContext* context; | 35 scoped_ptr<content::Compositor> compositor; |
| 36 scoped_ptr<WebKit::WebLayer> root_layer; | |
| 38 }; | 37 }; |
| 39 | 38 |
| 40 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; | 39 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; |
| 41 | 40 |
| 41 content::Compositor* GetCompositor() { | |
| 42 return g_global_state.Get().compositor.get(); | |
| 43 } | |
| 44 | |
| 45 static void SurfacePresented( | |
| 46 const DrawDelegate::SurfacePresentedCallback& callback, | |
| 47 uint32 sync_point) { | |
| 48 callback.Run(sync_point); | |
| 49 } | |
| 50 | |
| 42 static void SurfaceUpdated( | 51 static void SurfaceUpdated( |
| 43 uint64 texture, | 52 uint64 texture, |
| 44 content::RenderWidgetHostView* view, | 53 content::RenderWidgetHostView* view, |
| 45 const DrawDelegate::SurfacePresentedCallback& callback) { | 54 const DrawDelegate::SurfacePresentedCallback& callback) { |
| 46 uint32 sync_point = g_global_state.Get().context->Draw(texture); | 55 GetCompositor()->OnSurfaceUpdated(base::Bind( |
| 47 callback.Run(sync_point); | 56 &SurfacePresented, callback)); |
| 48 } | 57 } |
| 49 | 58 |
| 50 } // anonymous namespace | 59 } // anonymous namespace |
| 51 | 60 |
| 52 namespace content { | 61 namespace content { |
| 53 | 62 |
| 54 jobject CreateShellView() { | 63 jobject CreateShellView() { |
| 55 JNIEnv* env = base::android::AttachCurrentThread(); | 64 JNIEnv* env = base::android::AttachCurrentThread(); |
| 56 return Java_ShellManager_createShell( | 65 return Java_ShellManager_createShell( |
| 57 env, g_global_state.Get().j_obj.obj()).Release(); | 66 env, g_global_state.Get().j_obj.obj()).Release(); |
| 58 } | 67 } |
| 59 | 68 |
| 60 // Register native methods | 69 // Register native methods |
| 61 bool RegisterShellManager(JNIEnv* env) { | 70 bool RegisterShellManager(JNIEnv* env) { |
| 62 return RegisterNativesImpl(env); | 71 return RegisterNativesImpl(env); |
| 63 } | 72 } |
| 64 | 73 |
| 65 static void Init(JNIEnv* env, jclass clazz, jobject obj) { | 74 static void Init(JNIEnv* env, jclass clazz, jobject obj) { |
| 66 g_global_state.Get().j_obj.Reset( | 75 g_global_state.Get().j_obj.Reset( |
| 67 base::android::ScopedJavaLocalRef<jobject>(env, obj)); | 76 base::android::ScopedJavaLocalRef<jobject>(env, obj)); |
| 68 DrawDelegate::SurfaceUpdatedCallback cb = base::Bind( | 77 DrawDelegate::SurfaceUpdatedCallback cb = base::Bind( |
| 69 &SurfaceUpdated); | 78 &SurfaceUpdated); |
| 70 DrawDelegate::GetInstance()->SetUpdateCallback(cb); | 79 DrawDelegate::GetInstance()->SetUpdateCallback(cb); |
| 71 } | 80 } |
| 72 | 81 |
| 73 static void SurfaceCreated( | 82 static void SurfaceCreated( |
| 74 JNIEnv* env, jclass clazz, jobject jsurface) { | 83 JNIEnv* env, jclass clazz, jobject jsurface) { |
| 75 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); | 84 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); |
| 76 if (native_window) { | 85 if (native_window) { |
| 77 if (g_global_state.Get().context) | 86 if (!GetCompositor()) { |
| 78 delete g_global_state.Get().context; | 87 Compositor::Initialize(); |
|
klobag.chromium
2012/08/29 04:29:43
I like the old way (in #3) where you create compos
no sievers
2012/08/29 16:17:52
But I cannot call Initialize() in Init() because i
klobag.chromium
2012/08/29 22:55:05
How about in CreateShellView()?
On 2012/08/29 16:
no sievers
2012/08/29 23:09:22
That works. Done.
| |
| 79 | 88 g_global_state.Get().compositor.reset(Compositor::Create()); |
| 80 g_global_state.Get().context = new DrawContext(native_window); | 89 } |
| 90 GetCompositor()->SetWindowSurface(native_window); | |
| 81 ANativeWindow_release(native_window); | 91 ANativeWindow_release(native_window); |
| 92 if (!g_global_state.Get().root_layer.get()) | |
|
klobag.chromium
2012/08/29 04:29:43
If compositor is created once, do we want to set r
no sievers
2012/08/29 16:17:52
Yes, I'll change that.
| |
| 93 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); | |
| 94 GetCompositor()->SetRootLayer(g_global_state.Get().root_layer.get()); | |
| 82 } | 95 } |
| 83 } | 96 } |
| 84 | 97 |
| 85 static void SurfaceDestroyed(JNIEnv* env, jclass clazz) { | 98 static void SurfaceDestroyed(JNIEnv* env, jclass clazz) { |
| 86 if (g_global_state.Get().context) | 99 GetCompositor()->SetWindowSurface(NULL); |
| 87 delete g_global_state.Get().context; | |
| 88 } | 100 } |
| 89 | 101 |
| 90 static void SurfaceSetSize( | 102 static void SurfaceSetSize( |
| 91 JNIEnv* env, jclass clazz, jint width, jint height) { | 103 JNIEnv* env, jclass clazz, jint width, jint height) { |
| 92 gfx::Size size = gfx::Size(width, height); | 104 gfx::Size size = gfx::Size(width, height); |
| 93 DrawDelegate::GetInstance()->SetBounds(size); | 105 DrawDelegate::GetInstance()->SetBounds(size); |
| 94 DCHECK(g_global_state.Get().context); | 106 GetCompositor()->SetWindowBounds(size); |
| 95 g_global_state.Get().context->Reshape(width, height); | |
| 96 } | 107 } |
| 97 | 108 |
| 98 void LaunchShell(JNIEnv* env, jclass clazz, jstring jurl) { | 109 void LaunchShell(JNIEnv* env, jclass clazz, jstring jurl) { |
| 99 ShellBrowserContext* browserContext = | 110 ShellBrowserContext* browserContext = |
| 100 static_cast<ShellContentBrowserClient*>( | 111 static_cast<ShellContentBrowserClient*>( |
| 101 GetContentClient()->browser())->browser_context(); | 112 GetContentClient()->browser())->browser_context(); |
| 102 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); | 113 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); |
| 103 Shell::CreateNewWindow(browserContext, | 114 Shell::CreateNewWindow(browserContext, |
| 104 url, | 115 url, |
| 105 NULL, | 116 NULL, |
| 106 MSG_ROUTING_NONE, | 117 MSG_ROUTING_NONE, |
| 107 NULL); | 118 NULL); |
| 108 } | 119 } |
| 109 | 120 |
| 121 void ShellAttachLayer(WebKit::WebLayer* layer) { | |
| 122 g_global_state.Get().root_layer->addChild(layer); | |
| 123 } | |
| 124 | |
| 125 | |
| 126 void ShellRemoveLayer(WebKit::WebLayer* layer) { | |
| 127 layer->removeFromParent(); | |
| 128 } | |
| 129 | |
| 110 } // namespace content | 130 } // namespace content |
| OLD | NEW |