surface_texture.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 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_SURFACE_TEXTURE_H_
  5. #define UI_GL_ANDROID_SURFACE_TEXTURE_H_
  6. #include <jni.h>
  7. #include "base/android/scoped_java_ref.h"
  8. #include "base/callback.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "ui/gl/gl_export.h"
  11. struct ANativeWindow;
  12. namespace gl {
  13. // This class serves as a bridge for native code to call java functions inside
  14. // android SurfaceTexture class.
  15. class GL_EXPORT SurfaceTexture
  16. : public base::RefCountedThreadSafe<SurfaceTexture> {
  17. public:
  18. static scoped_refptr<SurfaceTexture> Create(int texture_id);
  19. SurfaceTexture(const SurfaceTexture&) = delete;
  20. SurfaceTexture& operator=(const SurfaceTexture&) = delete;
  21. // Set the listener callback, which will be invoked on the same thread that
  22. // is being called from here for registration.
  23. // Note: Since callbacks come in from Java objects that might outlive objects
  24. // being referenced from the callback, the only robust way here is to create
  25. // the callback from a weak pointer to your object.
  26. void SetFrameAvailableCallback(base::RepeatingClosure callback);
  27. // Set the listener callback, but allow it to be invoked on any thread. The
  28. // same caveats apply as SetFrameAvailableCallback, plus whatever other issues
  29. // show up due to multithreading (e.g., don't bind the Closure to a method
  30. // via a weak ref).
  31. void SetFrameAvailableCallbackOnAnyThread(base::RepeatingClosure callback);
  32. // Update the texture image to the most recent frame from the image stream.
  33. void UpdateTexImage();
  34. // Retrieve the 4x4 texture coordinate transform matrix associated with the
  35. // texture image set by the most recent call to updateTexImage.
  36. void GetTransformMatrix(float mtx[16]);
  37. // Attach the SurfaceTexture to the texture currently bound to
  38. // GL_TEXTURE_EXTERNAL_OES.
  39. void AttachToGLContext();
  40. // Detaches the SurfaceTexture from the context that owns its current GL
  41. // texture. Must be called with that context current on the calling thread.
  42. void DetachFromGLContext();
  43. // Creates a native render surface for this surface texture.
  44. // The caller must release the underlying reference when done with the handle
  45. // by calling ANativeWindow_release().
  46. ANativeWindow* CreateSurface();
  47. // Release the SurfaceTexture back buffers. The SurfaceTexture is no longer
  48. // usable after calling this but the front buffer is still valid. Note that
  49. // this is not called 'Release', like the Android API, because scoped_refptr
  50. // calls that quite a bit.
  51. void ReleaseBackBuffers();
  52. // Set the default buffer size for the surface texture.
  53. void SetDefaultBufferSize(int width, int height);
  54. const base::android::JavaRef<jobject>& j_surface_texture() const {
  55. return j_surface_texture_;
  56. }
  57. protected:
  58. explicit SurfaceTexture(
  59. const base::android::ScopedJavaLocalRef<jobject>& j_surface_texture);
  60. private:
  61. friend class base::RefCountedThreadSafe<SurfaceTexture>;
  62. virtual ~SurfaceTexture();
  63. // Java SurfaceTexture instance.
  64. base::android::ScopedJavaGlobalRef<jobject> j_surface_texture_;
  65. };
  66. } // namespace gl
  67. #endif // UI_GL_ANDROID_SURFACE_TEXTURE_H_