scoped_java_surface.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gl/android/scoped_java_surface.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "ui/gl/android/surface_texture.h"
  8. #include "ui/gl/surface_jni_headers/Surface_jni.h"
  9. using base::android::ScopedJavaLocalRef;
  10. namespace gl {
  11. ScopedJavaSurface::ScopedJavaSurface() {
  12. }
  13. ScopedJavaSurface::ScopedJavaSurface(
  14. const base::android::JavaRef<jobject>& surface) {
  15. JNIEnv* env = base::android::AttachCurrentThread();
  16. DCHECK(env->IsInstanceOf(surface.obj(), android_view_Surface_clazz(env)));
  17. j_surface_.Reset(surface);
  18. }
  19. ScopedJavaSurface::ScopedJavaSurface(const SurfaceTexture* surface_texture) {
  20. JNIEnv* env = base::android::AttachCurrentThread();
  21. ScopedJavaLocalRef<jobject> tmp(JNI_Surface::Java_Surface_ConstructorAVS_AGST(
  22. env, surface_texture->j_surface_texture()));
  23. DCHECK(!tmp.is_null());
  24. j_surface_.Reset(tmp);
  25. }
  26. ScopedJavaSurface::ScopedJavaSurface(ScopedJavaSurface&& rvalue) {
  27. MoveFrom(rvalue);
  28. }
  29. ScopedJavaSurface& ScopedJavaSurface::operator=(ScopedJavaSurface&& rhs) {
  30. MoveFrom(rhs);
  31. return *this;
  32. }
  33. ScopedJavaSurface::~ScopedJavaSurface() {
  34. ReleaseSurfaceIfNeeded();
  35. }
  36. void ScopedJavaSurface::ReleaseSurfaceIfNeeded() {
  37. if (auto_release_ && !j_surface_.is_null()) {
  38. JNIEnv* env = base::android::AttachCurrentThread();
  39. JNI_Surface::Java_Surface_releaseV(env, j_surface_);
  40. }
  41. }
  42. void ScopedJavaSurface::MoveFrom(ScopedJavaSurface& other) {
  43. ReleaseSurfaceIfNeeded();
  44. j_surface_ = std::move(other.j_surface_);
  45. auto_release_ = other.auto_release_;
  46. is_protected_ = other.is_protected_;
  47. }
  48. bool ScopedJavaSurface::IsEmpty() const {
  49. return j_surface_.is_null();
  50. }
  51. bool ScopedJavaSurface::IsValid() const {
  52. JNIEnv* env = base::android::AttachCurrentThread();
  53. return !IsEmpty() && JNI_Surface::Java_Surface_isValidZ(env, j_surface_);
  54. }
  55. // static
  56. ScopedJavaSurface ScopedJavaSurface::AcquireExternalSurface(
  57. const base::android::JavaRef<jobject>& surface) {
  58. ScopedJavaSurface scoped_surface(surface);
  59. scoped_surface.auto_release_ = false;
  60. scoped_surface.is_protected_ = true;
  61. return scoped_surface;
  62. }
  63. } // namespace gl