diff options
author | reveman@chromium.org <reveman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-08 01:01:50 +0000 |
---|---|---|
committer | reveman@chromium.org <reveman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-08 01:01:50 +0000 |
commit | b3fd32d3fc22b996f0bc5dd284331cc4bab156ff (patch) | |
tree | 6884b68590ac2acd99c691903f98ac3a72340154 | |
parent | d48dc9bb8bdcff277771769c6da5b15338394245 (diff) | |
download | chromium_src-b3fd32d3fc22b996f0bc5dd284331cc4bab156ff.zip chromium_src-b3fd32d3fc22b996f0bc5dd284331cc4bab156ff.tar.gz chromium_src-b3fd32d3fc22b996f0bc5dd284331cc4bab156ff.tar.bz2 |
base: Add ScopedJavaLocalFrame class.
This class can be used to create a local reference frame. A local
reference frame ensures that some amount of local references can
be created and forces all references created in the frame to be
releases when exiting the frame.
BUG=360069
TBR=darin
Review URL: https://codereview.chromium.org/225283008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262273 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/android/scoped_java_ref.cc | 18 | ||||
-rw-r--r-- | base/android/scoped_java_ref.h | 17 | ||||
-rw-r--r-- | content/app/android/child_process_service.cc | 12 | ||||
-rw-r--r-- | content/browser/renderer_host/compositor_impl_android.cc | 7 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_android.cc | 8 | ||||
-rw-r--r-- | ui/gl/android/surface_texture.cc | 4 |
6 files changed, 62 insertions, 4 deletions
diff --git a/base/android/scoped_java_ref.cc b/base/android/scoped_java_ref.cc index 21b466e..bb6f503 100644 --- a/base/android/scoped_java_ref.cc +++ b/base/android/scoped_java_ref.cc @@ -9,6 +9,24 @@ namespace base { namespace android { +namespace { + +const int kDefaultLocalFrameCapacity = 16; + +} // namespace + +ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env) : env_(env) { + int failed = env_->PushLocalFrame(kDefaultLocalFrameCapacity); + DCHECK(!failed); +} + +ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env, int capacity) + : env_(env) { + int failed = env_->PushLocalFrame(capacity); + DCHECK(!failed); +} + +ScopedJavaLocalFrame::~ScopedJavaLocalFrame() { env_->PopLocalFrame(NULL); } JavaRef<jobject>::JavaRef() : obj_(NULL) {} diff --git a/base/android/scoped_java_ref.h b/base/android/scoped_java_ref.h index a5d71e2..7863c0be 100644 --- a/base/android/scoped_java_ref.h +++ b/base/android/scoped_java_ref.h @@ -14,6 +14,23 @@ namespace base { namespace android { +// Creates a new local reference frame, in which at least a given number of +// local references can be created. Note that local references already created +// in previous local frames are still valid in the current local frame. +class BASE_EXPORT ScopedJavaLocalFrame { + public: + explicit ScopedJavaLocalFrame(JNIEnv* env); + ScopedJavaLocalFrame(JNIEnv* env, int capacity); + ~ScopedJavaLocalFrame(); + + private: + // This class is only good for use on the thread it was created on so + // it's safe to cache the non-threadsafe JNIEnv* inside this object. + JNIEnv* env_; + + DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame); +}; + // Forward declare the generic java reference template class. template<typename T> class JavaRef; diff --git a/content/app/android/child_process_service.cc b/content/app/android/child_process_service.cc index 0a78803..62c04d6 100644 --- a/content/app/android/child_process_service.cc +++ b/content/app/android/child_process_service.cc @@ -72,8 +72,12 @@ class SurfaceTexturePeerChildImpl : public SurfaceTexturePeer, if (surface.j_surface().is_null()) return NULL; - ANativeWindow* native_window = ANativeWindow_fromSurface( - env, surface.j_surface().obj()); + // Note: This ensures that any local references used by + // ANativeWindow_fromSurface are released immediately. This is needed as a + // workaround for https://code.google.com/p/android/issues/detail?id=68174 + base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); + ANativeWindow* native_window = + ANativeWindow_fromSurface(env, surface.j_surface().obj()); return native_window; } @@ -90,6 +94,10 @@ class SurfaceTexturePeerChildImpl : public SurfaceTexturePeer, if (surface.j_surface().is_null()) return NULL; + // Note: This ensures that any local references used by + // ANativeWindow_fromSurface are released immediately. This is needed as a + // workaround for https://code.google.com/p/android/issues/detail?id=68174 + base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); ANativeWindow* native_window = ANativeWindow_fromSurface(env, surface.j_surface().obj()); diff --git a/content/browser/renderer_host/compositor_impl_android.cc b/content/browser/renderer_host/compositor_impl_android.cc index dec2ff3..3a94c3d 100644 --- a/content/browser/renderer_host/compositor_impl_android.cc +++ b/content/browser/renderer_host/compositor_impl_android.cc @@ -312,8 +312,13 @@ void CompositorImpl::SetSurface(jobject surface) { // Now, set the new surface if we have one. ANativeWindow* window = NULL; - if (surface) + if (surface) { + // Note: This ensures that any local references used by + // ANativeWindow_fromSurface are released immediately. This is needed as a + // workaround for https://code.google.com/p/android/issues/detail?id=68174 + base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); window = ANativeWindow_fromSurface(env, surface); + } if (window) { SetWindowSurface(window); ANativeWindow_release(window); diff --git a/mojo/services/native_viewport/native_viewport_android.cc b/mojo/services/native_viewport/native_viewport_android.cc index 791c27f..1763f17 100644 --- a/mojo/services/native_viewport/native_viewport_android.cc +++ b/mojo/services/native_viewport/native_viewport_android.cc @@ -60,7 +60,13 @@ void NativeViewportAndroid::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) { base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface); - window_ = ANativeWindow_fromSurface(env, jsurface); + // Note: This ensures that any local references used by + // ANativeWindow_fromSurface are released immediately. This is needed as a + // workaround for https://code.google.com/p/android/issues/detail?id=68174 + { + base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); + window_ = ANativeWindow_fromSurface(env, jsurface); + } delegate_->OnAcceleratedWidgetAvailable(window_); } diff --git a/ui/gl/android/surface_texture.cc b/ui/gl/android/surface_texture.cc index a580041..3f55cbd 100644 --- a/ui/gl/android/surface_texture.cc +++ b/ui/gl/android/surface_texture.cc @@ -123,6 +123,10 @@ void SurfaceTexture::DetachFromGLContext() { ANativeWindow* SurfaceTexture::CreateSurface() { JNIEnv* env = base::android::AttachCurrentThread(); ScopedJavaSurface surface(this); + // Note: This ensures that any local references used by + // ANativeWindow_fromSurface are released immediately. This is needed as a + // workaround for https://code.google.com/p/android/issues/detail?id=68174 + base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); ANativeWindow* native_window = ANativeWindow_fromSurface( env, surface.j_surface().obj()); return native_window; |