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 "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" | |
| 13 #include "content/shell/shell.h" | 14 #include "content/shell/shell.h" |
| 14 #include "content/shell/shell_browser_context.h" | 15 #include "content/shell/shell_browser_context.h" |
| 15 #include "content/shell/shell_content_browser_client.h" | 16 #include "content/shell/shell_content_browser_client.h" |
| 16 #include "content/public/browser/android/compositor.h" | 17 #include "content/public/browser/android/compositor.h" |
| 17 #include "content/public/browser/android/draw_delegate.h" | 18 #include "content/public/browser/android/draw_delegate.h" |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 19 #include "content/shell/shell.h" | 20 #include "content/shell/shell.h" |
| 20 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 21 #include "jni/ShellManager_jni.h" | 22 #include "jni/ShellManager_jni.h" |
| 22 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" | 23 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" |
| 23 #include "ui/gfx/size.h" | 24 #include "ui/gfx/size.h" |
| 24 | 25 |
| 25 #include <android/native_window_jni.h> | 26 #include <android/native_window_jni.h> |
| 26 | 27 |
| 27 using base::android::ScopedJavaLocalRef; | 28 using base::android::ScopedJavaLocalRef; |
| 28 using content::Compositor; | 29 using content::Compositor; |
| 29 using content::DrawDelegate; | 30 using content::DrawDelegate; |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 34 class CompositorClient : public Compositor::Client { | |
| 35 public: | |
| 36 virtual void ScheduleComposite() OVERRIDE; | |
| 37 }; | |
| 38 | |
| 33 struct GlobalState { | 39 struct GlobalState { |
| 34 base::android::ScopedJavaGlobalRef<jobject> j_obj; | 40 base::android::ScopedJavaGlobalRef<jobject> j_obj; |
| 41 CompositorClient client; | |
| 35 scoped_ptr<content::Compositor> compositor; | 42 scoped_ptr<content::Compositor> compositor; |
| 36 scoped_ptr<WebKit::WebLayer> root_layer; | 43 scoped_ptr<WebKit::WebLayer> root_layer; |
| 37 }; | 44 }; |
| 38 | 45 |
| 39 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; | 46 base::LazyInstance<GlobalState> g_global_state = LAZY_INSTANCE_INITIALIZER; |
| 40 | 47 |
| 41 content::Compositor* GetCompositor() { | 48 content::Compositor* GetCompositor() { |
| 42 return g_global_state.Get().compositor.get(); | 49 return g_global_state.Get().compositor.get(); |
| 43 } | 50 } |
| 44 | 51 |
| 45 static void SurfacePresented( | 52 bool g_scheduled_composite = false; |
|
piman
2012/10/13 18:04:21
nit: should this be in the global state?
| |
| 46 const DrawDelegate::SurfacePresentedCallback& callback, | 53 void Composite() { |
| 47 uint32 sync_point) { | 54 g_scheduled_composite = false; |
| 48 callback.Run(sync_point); | 55 if (GetCompositor()) { |
| 56 GetCompositor()->Composite(); | |
| 57 } | |
| 49 } | 58 } |
| 50 | 59 |
| 51 static void SurfaceUpdated( | 60 void CompositorClient::ScheduleComposite() { |
| 52 uint64 texture, | 61 if (!g_scheduled_composite) { |
| 53 content::RenderWidgetHostView* view, | 62 g_scheduled_composite = true; |
| 54 const DrawDelegate::SurfacePresentedCallback& callback) { | 63 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&Composite)); |
| 55 GetCompositor()->OnSurfaceUpdated(base::Bind( | 64 } |
| 56 &SurfacePresented, callback)); | |
| 57 } | 65 } |
| 58 | 66 |
| 59 } // anonymous namespace | 67 } // anonymous namespace |
| 60 | 68 |
| 61 namespace content { | 69 namespace content { |
| 62 | 70 |
| 63 jobject CreateShellView() { | 71 jobject CreateShellView() { |
| 64 JNIEnv* env = base::android::AttachCurrentThread(); | 72 JNIEnv* env = base::android::AttachCurrentThread(); |
| 65 if (!GetCompositor()) { | 73 if (!GetCompositor()) { |
| 66 Compositor::Initialize(); | 74 Compositor::Initialize(); |
| 67 g_global_state.Get().compositor.reset(Compositor::Create()); | 75 g_global_state.Get().compositor.reset(Compositor::Create( |
| 76 &g_global_state.Get().client)); | |
| 68 DCHECK(!g_global_state.Get().root_layer.get()); | 77 DCHECK(!g_global_state.Get().root_layer.get()); |
| 69 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); | 78 g_global_state.Get().root_layer.reset(WebKit::WebLayer::create()); |
| 70 } | 79 } |
| 71 return Java_ShellManager_createShell( | 80 return Java_ShellManager_createShell( |
| 72 env, g_global_state.Get().j_obj.obj()).Release(); | 81 env, g_global_state.Get().j_obj.obj()).Release(); |
| 73 } | 82 } |
| 74 | 83 |
| 75 // Register native methods | 84 // Register native methods |
| 76 bool RegisterShellManager(JNIEnv* env) { | 85 bool RegisterShellManager(JNIEnv* env) { |
| 77 return RegisterNativesImpl(env); | 86 return RegisterNativesImpl(env); |
| 78 } | 87 } |
| 79 | 88 |
| 80 static void Init(JNIEnv* env, jclass clazz, jobject obj) { | 89 static void Init(JNIEnv* env, jclass clazz, jobject obj) { |
| 81 g_global_state.Get().j_obj.Reset( | 90 g_global_state.Get().j_obj.Reset( |
| 82 base::android::ScopedJavaLocalRef<jobject>(env, obj)); | 91 base::android::ScopedJavaLocalRef<jobject>(env, obj)); |
| 83 DrawDelegate::SurfaceUpdatedCallback cb = base::Bind( | |
| 84 &SurfaceUpdated); | |
| 85 DrawDelegate::GetInstance()->SetUpdateCallback(cb); | |
| 86 } | 92 } |
| 87 | 93 |
| 88 static void SurfaceCreated( | 94 static void SurfaceCreated( |
| 89 JNIEnv* env, jclass clazz, jobject jsurface) { | 95 JNIEnv* env, jclass clazz, jobject jsurface) { |
| 90 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); | 96 ANativeWindow* native_window = ANativeWindow_fromSurface(env, jsurface); |
| 91 if (native_window) { | 97 if (native_window) { |
| 92 GetCompositor()->SetWindowSurface(native_window); | 98 GetCompositor()->SetWindowSurface(native_window); |
| 93 ANativeWindow_release(native_window); | 99 ANativeWindow_release(native_window); |
| 94 GetCompositor()->SetRootLayer(g_global_state.Get().root_layer.get()); | 100 GetCompositor()->SetRootLayer(g_global_state.Get().root_layer.get()); |
| 95 } | 101 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 121 void ShellAttachLayer(WebKit::WebLayer* layer) { | 127 void ShellAttachLayer(WebKit::WebLayer* layer) { |
| 122 g_global_state.Get().root_layer->addChild(layer); | 128 g_global_state.Get().root_layer->addChild(layer); |
| 123 } | 129 } |
| 124 | 130 |
| 125 | 131 |
| 126 void ShellRemoveLayer(WebKit::WebLayer* layer) { | 132 void ShellRemoveLayer(WebKit::WebLayer* layer) { |
| 127 layer->removeFromParent(); | 133 layer->removeFromParent(); |
| 128 } | 134 } |
| 129 | 135 |
| 130 } // namespace content | 136 } // namespace content |
| OLD | NEW |