blob: fc7fb5416ac46f060cda9228db0ae3d6de9313a1 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// 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
5#ifndef UI_GL_ANDROID_SURFACE_TEXTURE_BRIDGE_H_
6#define UI_GL_ANDROID_SURFACE_TEXTURE_BRIDGE_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
15struct ANativeWindow;
16
17namespace gfx {
18
19// This class serves as a bridge for native code to call java functions inside
20// android SurfaceTexture class.
21class GL_EXPORT SurfaceTextureBridge
22 : public base::RefCountedThreadSafe<SurfaceTextureBridge>{
23 public:
24 explicit SurfaceTextureBridge(int texture_id);
25
26 // Set the listener callback, which will be invoked on the same thread that
27 // is being called from here for registration.
28 // Note: Since callbacks come in from Java objects that might outlive objects
29 // being referenced from the callback, the only robust way here is to create
30 // the callback from a weak pointer to your object.
31 void SetFrameAvailableCallback(const base::Closure& callback);
32
33 // Update the texture image to the most recent frame from the image stream.
34 void UpdateTexImage();
35
36 // Retrieve the 4x4 texture coordinate transform matrix associated with the
37 // texture image set by the most recent call to updateTexImage.
38 void GetTransformMatrix(float mtx[16]);
39
40 // Set the default size of the image buffers.
41 void SetDefaultBufferSize(int width, int height);
42
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010043 // Attach the SurfaceTexture to the texture currently bound to
44 // GL_TEXTURE_EXTERNAL_OES.
45 void AttachToGLContext();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010046
47 // Detaches the SurfaceTexture from the context that owns its current GL
48 // texture. Must be called with that context current on the calling thread.
49 void DetachFromGLContext();
50
51 // Creates a native render surface for this surface texture.
52 // The caller must release the underlying reference when done with the handle
53 // by calling ANativeWindow_release().
54 ANativeWindow* CreateSurface();
55
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010056 const base::android::JavaRef<jobject>& j_surface_texture() const {
57 return j_surface_texture_;
58 }
59
60 private:
61 friend class base::RefCountedThreadSafe<SurfaceTextureBridge>;
62 ~SurfaceTextureBridge();
63
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010064 // Java SurfaceTexture instance.
65 base::android::ScopedJavaGlobalRef<jobject> j_surface_texture_;
66
67 DISALLOW_COPY_AND_ASSIGN(SurfaceTextureBridge);
68};
69
70} // namespace gfx
71
72#endif // UI_GL_ANDROID_SURFACE_TEXTURE_BRIDGE_H_