scoped_java_surface.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef UI_GL_ANDROID_SCOPED_JAVA_SURFACE_H_
  5. #define UI_GL_ANDROID_SCOPED_JAVA_SURFACE_H_
  6. #include <jni.h>
  7. #include "base/android/scoped_java_ref.h"
  8. #include "ui/gl/gl_export.h"
  9. namespace gl {
  10. class SurfaceTexture;
  11. // A helper class for holding a scoped reference to a Java Surface instance.
  12. // When going out of scope, Surface.release() is called on the Java object to
  13. // make sure server-side references (esp. wrt graphics memory) are released.
  14. class GL_EXPORT ScopedJavaSurface {
  15. public:
  16. ScopedJavaSurface();
  17. // Wraps an existing Java Surface object in a ScopedJavaSurface.
  18. explicit ScopedJavaSurface(const base::android::JavaRef<jobject>& surface);
  19. // Creates a Java Surface from a SurfaceTexture and wraps it in a
  20. // ScopedJavaSurface.
  21. explicit ScopedJavaSurface(const SurfaceTexture* surface_texture);
  22. // Move constructor. Take the surface from another ScopedJavaSurface object,
  23. // the latter no longer owns the surface afterwards.
  24. ScopedJavaSurface(ScopedJavaSurface&& rvalue);
  25. ScopedJavaSurface& operator=(ScopedJavaSurface&& rhs);
  26. // Creates a ScopedJavaSurface that is owned externally, i.e.,
  27. // someone else is responsible to call Surface.release().
  28. static ScopedJavaSurface AcquireExternalSurface(
  29. const base::android::JavaRef<jobject>& surface);
  30. ScopedJavaSurface(const ScopedJavaSurface&) = delete;
  31. ScopedJavaSurface& operator=(const ScopedJavaSurface&) = delete;
  32. ~ScopedJavaSurface();
  33. // Checks whether the surface is an empty one.
  34. bool IsEmpty() const;
  35. // Checks whether this object references a valid surface.
  36. bool IsValid() const;
  37. // Checks whether the surface is hardware protected so that no readback is
  38. // possible.
  39. bool is_protected() const { return is_protected_; }
  40. const base::android::JavaRef<jobject>& j_surface() const {
  41. return j_surface_;
  42. }
  43. private:
  44. // Performs destructive move from |other| to this.
  45. void MoveFrom(ScopedJavaSurface& other);
  46. void ReleaseSurfaceIfNeeded();
  47. bool auto_release_ = true;
  48. bool is_protected_ = false;
  49. base::android::ScopedJavaGlobalRef<jobject> j_surface_;
  50. };
  51. } // namespace gl
  52. #endif // UI_GL_ANDROID_SCOPED_JAVA_SURFACE_H_