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.
5 #include "ui/gl/android/surface_texture.h"
7 #include <android/native_window_jni.h>
9 #include "base/android/jni_android.h"
10 #include "base/logging.h"
11 #include "jni/SurfaceTexturePlatformWrapper_jni.h"
12 #include "ui/gl/android/scoped_java_surface.h"
13 #include "ui/gl/android/surface_texture_listener.h"
14 #include "ui/gl/gl_bindings.h"
18 scoped_refptr
<SurfaceTexture
> SurfaceTexture::Create(int texture_id
) {
19 JNIEnv
* env
= base::android::AttachCurrentThread();
20 return new SurfaceTexture(
21 Java_SurfaceTexturePlatformWrapper_create(env
, texture_id
));
24 SurfaceTexture::SurfaceTexture(
25 const base::android::ScopedJavaLocalRef
<jobject
>& j_surface_texture
) {
26 j_surface_texture_
.Reset(j_surface_texture
);
29 SurfaceTexture::~SurfaceTexture() {
30 JNIEnv
* env
= base::android::AttachCurrentThread();
31 Java_SurfaceTexturePlatformWrapper_destroy(env
, j_surface_texture_
.obj());
34 void SurfaceTexture::SetFrameAvailableCallback(
35 const base::Closure
& callback
) {
36 JNIEnv
* env
= base::android::AttachCurrentThread();
37 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback(
39 j_surface_texture_
.obj(),
40 reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback
)));
43 void SurfaceTexture::UpdateTexImage() {
44 JNIEnv
* env
= base::android::AttachCurrentThread();
45 Java_SurfaceTexturePlatformWrapper_updateTexImage(env
,
46 j_surface_texture_
.obj());
49 void SurfaceTexture::GetTransformMatrix(float mtx
[16]) {
50 JNIEnv
* env
= base::android::AttachCurrentThread();
52 base::android::ScopedJavaLocalRef
<jfloatArray
> jmatrix(
53 env
, env
->NewFloatArray(16));
54 Java_SurfaceTexturePlatformWrapper_getTransformMatrix(
55 env
, j_surface_texture_
.obj(), jmatrix
.obj());
58 jfloat
* elements
= env
->GetFloatArrayElements(jmatrix
.obj(), &is_copy
);
59 for (int i
= 0; i
< 16; ++i
) {
60 mtx
[i
] = static_cast<float>(elements
[i
]);
62 env
->ReleaseFloatArrayElements(jmatrix
.obj(), elements
, JNI_ABORT
);
65 void SurfaceTexture::AttachToGLContext() {
67 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES
, &texture_id
);
69 JNIEnv
* env
= base::android::AttachCurrentThread();
70 Java_SurfaceTexturePlatformWrapper_attachToGLContext(
71 env
, j_surface_texture_
.obj(), texture_id
);
74 void SurfaceTexture::DetachFromGLContext() {
75 JNIEnv
* env
= base::android::AttachCurrentThread();
76 Java_SurfaceTexturePlatformWrapper_detachFromGLContext(
77 env
, j_surface_texture_
.obj());
80 ANativeWindow
* SurfaceTexture::CreateSurface() {
81 JNIEnv
* env
= base::android::AttachCurrentThread();
82 ScopedJavaSurface
surface(this);
83 // Note: This ensures that any local references used by
84 // ANativeWindow_fromSurface are released immediately. This is needed as a
85 // workaround for https://code.google.com/p/android/issues/detail?id=68174
86 base::android::ScopedJavaLocalFrame
scoped_local_reference_frame(env
);
87 ANativeWindow
* native_window
= ANativeWindow_fromSurface(
88 env
, surface
.j_surface().obj());
92 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv
* env
) {
93 return RegisterNativesImpl(env
);