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 // TODO(boliu): Remove this include when we move off ICS.
10 #include "base/android/build_info.h"
11 #include "base/android/jni_android.h"
12 #include "base/logging.h"
13 #include "jni/SurfaceTexturePlatformWrapper_jni.h"
14 #include "ui/gl/android/scoped_java_surface.h"
15 #include "ui/gl/android/surface_texture_listener.h"
16 #include "ui/gl/gl_bindings.h"
18 // TODO(boliu): Remove this method when when we move off ICS. See
19 // http://crbug.com/161864.
20 bool GlContextMethodsAvailable() {
21 bool available
= base::android::BuildInfo::GetInstance()->sdk_int() >= 16;
23 LOG(WARNING
) << "Running on unsupported device: rendering may not work";
29 scoped_refptr
<SurfaceTexture
> SurfaceTexture::Create(int texture_id
) {
30 JNIEnv
* env
= base::android::AttachCurrentThread();
31 return new SurfaceTexture(
32 Java_SurfaceTexturePlatformWrapper_create(env
, texture_id
));
35 scoped_refptr
<SurfaceTexture
> SurfaceTexture::CreateSingleBuffered(
37 DCHECK(IsSingleBufferModeSupported());
38 JNIEnv
* env
= base::android::AttachCurrentThread();
39 return new SurfaceTexture(
40 Java_SurfaceTexturePlatformWrapper_createSingleBuffered(env
, texture_id
));
43 SurfaceTexture::SurfaceTexture(
44 const base::android::ScopedJavaLocalRef
<jobject
>& j_surface_texture
) {
45 j_surface_texture_
.Reset(j_surface_texture
);
48 SurfaceTexture::~SurfaceTexture() {
49 JNIEnv
* env
= base::android::AttachCurrentThread();
50 Java_SurfaceTexturePlatformWrapper_destroy(env
, j_surface_texture_
.obj());
53 void SurfaceTexture::SetFrameAvailableCallback(
54 const base::Closure
& callback
) {
55 JNIEnv
* env
= base::android::AttachCurrentThread();
56 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback(
58 j_surface_texture_
.obj(),
59 reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback
)));
62 void SurfaceTexture::UpdateTexImage() {
63 JNIEnv
* env
= base::android::AttachCurrentThread();
64 Java_SurfaceTexturePlatformWrapper_updateTexImage(env
,
65 j_surface_texture_
.obj());
68 void SurfaceTexture::ReleaseTexImage() {
69 DCHECK(IsSingleBufferModeSupported());
70 JNIEnv
* env
= base::android::AttachCurrentThread();
71 Java_SurfaceTexturePlatformWrapper_releaseTexImage(env
,
72 j_surface_texture_
.obj());
75 void SurfaceTexture::GetTransformMatrix(float mtx
[16]) {
76 JNIEnv
* env
= base::android::AttachCurrentThread();
78 base::android::ScopedJavaLocalRef
<jfloatArray
> jmatrix(
79 env
, env
->NewFloatArray(16));
80 Java_SurfaceTexturePlatformWrapper_getTransformMatrix(
81 env
, j_surface_texture_
.obj(), jmatrix
.obj());
84 jfloat
* elements
= env
->GetFloatArrayElements(jmatrix
.obj(), &is_copy
);
85 for (int i
= 0; i
< 16; ++i
) {
86 mtx
[i
] = static_cast<float>(elements
[i
]);
88 env
->ReleaseFloatArrayElements(jmatrix
.obj(), elements
, JNI_ABORT
);
91 void SurfaceTexture::SetDefaultBufferSize(int width
, int height
) {
92 JNIEnv
* env
= base::android::AttachCurrentThread();
94 if (width
> 0 && height
> 0) {
95 Java_SurfaceTexturePlatformWrapper_setDefaultBufferSize(
96 env
, j_surface_texture_
.obj(), static_cast<jint
>(width
),
97 static_cast<jint
>(height
));
99 LOG(WARNING
) << "Not setting surface texture buffer size - "
100 "width or height is 0";
104 void SurfaceTexture::AttachToGLContext() {
105 if (GlContextMethodsAvailable()) {
107 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES
, &texture_id
);
109 JNIEnv
* env
= base::android::AttachCurrentThread();
110 Java_SurfaceTexturePlatformWrapper_attachToGLContext(
111 env
, j_surface_texture_
.obj(), texture_id
);
115 void SurfaceTexture::DetachFromGLContext() {
116 if (GlContextMethodsAvailable()) {
117 JNIEnv
* env
= base::android::AttachCurrentThread();
118 Java_SurfaceTexturePlatformWrapper_detachFromGLContext(
119 env
, j_surface_texture_
.obj());
123 ANativeWindow
* SurfaceTexture::CreateSurface() {
124 JNIEnv
* env
= base::android::AttachCurrentThread();
125 ScopedJavaSurface
surface(this);
126 // Note: This ensures that any local references used by
127 // ANativeWindow_fromSurface are released immediately. This is needed as a
128 // workaround for https://code.google.com/p/android/issues/detail?id=68174
129 base::android::ScopedJavaLocalFrame
scoped_local_reference_frame(env
);
130 ANativeWindow
* native_window
= ANativeWindow_fromSurface(
131 env
, surface
.j_surface().obj());
132 return native_window
;
136 bool SurfaceTexture::IsSingleBufferModeSupported() {
137 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
140 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv
* env
) {
141 return RegisterNativesImpl(env
);