Home | History | Annotate | Download | only in android
      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 
      5 #ifndef UI_GL_ANDROID_SURFACE_TEXTURE_H_
      6 #define UI_GL_ANDROID_SURFACE_TEXTURE_H_
      7 
      8 #include <jni.h>
      9 
     10 #include "base/android/scoped_java_ref.h"
     11 #include "base/callback.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "ui/gl/gl_export.h"
     14 
     15 struct ANativeWindow;
     16 
     17 namespace gfx {
     18 
     19 // This class serves as a bridge for native code to call java functions inside
     20 // android SurfaceTexture class.
     21 class GL_EXPORT SurfaceTexture
     22     : public base::RefCountedThreadSafe<SurfaceTexture>{
     23  public:
     24   static scoped_refptr<SurfaceTexture> Create(int texture_id);
     25 
     26   static scoped_refptr<SurfaceTexture> CreateSingleBuffered(int texture_id);
     27 
     28   // Set the listener callback, which will be invoked on the same thread that
     29   // is being called from here for registration.
     30   // Note: Since callbacks come in from Java objects that might outlive objects
     31   // being referenced from the callback, the only robust way here is to create
     32   // the callback from a weak pointer to your object.
     33   void SetFrameAvailableCallback(const base::Closure& callback);
     34 
     35   // Update the texture image to the most recent frame from the image stream.
     36   void UpdateTexImage();
     37 
     38   // Release the texture content. This is needed only in single buffered mode
     39   // to allow the image content producer to take ownership
     40   // of the image buffer.
     41   // This is *only* supported on SurfaceTexture instantiated via
     42   // |CreateSingleBuffered(...)|.
     43   void ReleaseTexImage();
     44 
     45   // Retrieve the 4x4 texture coordinate transform matrix associated with the
     46   // texture image set by the most recent call to updateTexImage.
     47   void GetTransformMatrix(float mtx[16]);
     48 
     49   // Set the default size of the image buffers.
     50   void SetDefaultBufferSize(int width, int height);
     51 
     52   // Attach the SurfaceTexture to the texture currently bound to
     53   // GL_TEXTURE_EXTERNAL_OES.
     54   void AttachToGLContext();
     55 
     56   // Detaches the SurfaceTexture from the context that owns its current GL
     57   // texture. Must be called with that context current on the calling thread.
     58   void DetachFromGLContext();
     59 
     60   // Creates a native render surface for this surface texture.
     61   // The caller must release the underlying reference when done with the handle
     62   // by calling ANativeWindow_release().
     63   ANativeWindow* CreateSurface();
     64 
     65   const base::android::JavaRef<jobject>& j_surface_texture() const {
     66     return j_surface_texture_;
     67   }
     68 
     69   // This should only be used to guard the SurfaceTexture instantiated via
     70   // |CreateSingleBuffered(...)|
     71   static bool IsSingleBufferModeSupported();
     72 
     73   static bool RegisterSurfaceTexture(JNIEnv* env);
     74 
     75  protected:
     76   explicit SurfaceTexture(
     77       const base::android::ScopedJavaLocalRef<jobject>& j_surface_texture);
     78 
     79  private:
     80   friend class base::RefCountedThreadSafe<SurfaceTexture>;
     81   ~SurfaceTexture();
     82 
     83   // Java SurfaceTexture instance.
     84   base::android::ScopedJavaGlobalRef<jobject> j_surface_texture_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(SurfaceTexture);
     87 };
     88 
     89 }  // namespace gfx
     90 
     91 #endif  // UI_GL_ANDROID_SURFACE_TEXTURE_H_
     92