| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "sky/shell/android/platform_view_android.h" | 5 #include "sky/shell/android/platform_view_android.h" |
| 6 | 6 |
| 7 #include <android/input.h> | 7 #include <android/input.h> |
| 8 #include <android/native_window_jni.h> | 8 #include <android/native_window_jni.h> |
| 9 | 9 |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "jni/PlatformViewAndroid_jni.h" | 13 #include "jni/PlatformViewAndroid_jni.h" |
| 14 #include "sky/shell/instance.h" |
| 14 #include "sky/shell/shell.h" | 15 #include "sky/shell/shell.h" |
| 15 | 16 |
| 16 namespace sky { | 17 namespace sky { |
| 17 namespace shell { | 18 namespace shell { |
| 18 | 19 |
| 19 static jlong Attach(JNIEnv* env, jclass clazz, jint viewportObserverHandle) { | 20 static jlong Attach(JNIEnv* env, jclass clazz, jint viewportObserverHandle) { |
| 20 PlatformView* view = Shell::Shared().view(); | 21 Instance* instance = new Instance(Shell::Shared()); |
| 22 auto view = static_cast<PlatformViewAndroid*>(instance->view()); |
| 23 view->SetInstance(make_scoped_ptr(instance)); |
| 21 view->ConnectToViewportObserver( | 24 view->ConnectToViewportObserver( |
| 22 mojo::MakeRequest<ViewportObserver>(mojo::ScopedMessagePipeHandle( | 25 mojo::MakeRequest<ViewportObserver>(mojo::ScopedMessagePipeHandle( |
| 23 mojo::MessagePipeHandle(viewportObserverHandle)))); | 26 mojo::MessagePipeHandle(viewportObserverHandle)))); |
| 24 return reinterpret_cast<jlong>(view); | 27 return reinterpret_cast<jlong>(instance->view()); |
| 25 } | 28 } |
| 26 | 29 |
| 27 // static | 30 // static |
| 28 bool PlatformViewAndroid::Register(JNIEnv* env) { | 31 bool PlatformViewAndroid::Register(JNIEnv* env) { |
| 29 return RegisterNativesImpl(env); | 32 return RegisterNativesImpl(env); |
| 30 } | 33 } |
| 31 | 34 |
| 35 PlatformView* PlatformView::Create(const Config& config) { |
| 36 return new PlatformViewAndroid(config); |
| 37 } |
| 38 |
| 39 PlatformViewAndroid::PlatformViewAndroid(const Config& config) |
| 40 : PlatformView(config) { |
| 41 } |
| 42 |
| 32 PlatformViewAndroid::~PlatformViewAndroid() { | 43 PlatformViewAndroid::~PlatformViewAndroid() { |
| 33 if (window_) | 44 if (window_) |
| 34 ReleaseWindow(); | 45 ReleaseWindow(); |
| 35 } | 46 } |
| 36 | 47 |
| 37 void PlatformViewAndroid::Detach(JNIEnv* env, jobject obj) { | 48 void PlatformViewAndroid::Detach(JNIEnv* env, jobject obj) { |
| 38 DCHECK(!window_); | 49 DCHECK(!window_); |
| 50 instance_.reset(); |
| 51 // Note: |this| has been destroyed at this point. |
| 39 } | 52 } |
| 40 | 53 |
| 41 void PlatformViewAndroid::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurf
ace) { | 54 void PlatformViewAndroid::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurf
ace) { |
| 42 base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface); | 55 base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface); |
| 43 // Note: This ensures that any local references used by | 56 // Note: This ensures that any local references used by |
| 44 // ANativeWindow_fromSurface are released immediately. This is needed as a | 57 // ANativeWindow_fromSurface are released immediately. This is needed as a |
| 45 // workaround for https://code.google.com/p/android/issues/detail?id=68174 | 58 // workaround for https://code.google.com/p/android/issues/detail?id=68174 |
| 46 { | 59 { |
| 47 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); | 60 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); |
| 48 window_ = ANativeWindow_fromSurface(env, jsurface); | 61 window_ = ANativeWindow_fromSurface(env, jsurface); |
| 49 } | 62 } |
| 50 SurfaceWasCreated(); | 63 SurfaceWasCreated(); |
| 51 } | 64 } |
| 52 | 65 |
| 53 void PlatformViewAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) { | 66 void PlatformViewAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) { |
| 54 DCHECK(window_); | 67 DCHECK(window_); |
| 55 SurfaceWasDestroyed(); | 68 SurfaceWasDestroyed(); |
| 56 ReleaseWindow(); | 69 ReleaseWindow(); |
| 57 } | 70 } |
| 58 | 71 |
| 72 void PlatformViewAndroid::SetInstance(scoped_ptr<Instance> instance) { |
| 73 DCHECK(!instance_); |
| 74 instance_ = instance.Pass(); |
| 75 } |
| 76 |
| 59 void PlatformViewAndroid::ReleaseWindow() { | 77 void PlatformViewAndroid::ReleaseWindow() { |
| 60 ANativeWindow_release(window_); | 78 ANativeWindow_release(window_); |
| 61 window_ = nullptr; | 79 window_ = nullptr; |
| 62 } | 80 } |
| 63 | 81 |
| 64 } // namespace shell | 82 } // namespace shell |
| 65 } // namespace sky | 83 } // namespace sky |
| OLD | NEW |