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 Chromium stops supporting ICS.
19 bool GlContextMethodsAvailable() {
20 bool available
= base::android::BuildInfo::GetInstance()->sdk_int() >= 16;
22 LOG(WARNING
) << "Running on unsupported device: rendering may not work";
28 scoped_refptr
<SurfaceTexture
> SurfaceTexture::Create(int texture_id
) {
29 JNIEnv
* env
= base::android::AttachCurrentThread();
30 return new SurfaceTexture(
31 Java_SurfaceTexturePlatformWrapper_create(env
, texture_id
));
34 scoped_refptr
<SurfaceTexture
> SurfaceTexture::CreateSingleBuffered(
36 DCHECK(IsSingleBufferModeSupported());
37 JNIEnv
* env
= base::android::AttachCurrentThread();
38 return new SurfaceTexture(
39 Java_SurfaceTexturePlatformWrapper_createSingleBuffered(env
, texture_id
));
42 SurfaceTexture::SurfaceTexture(
43 const base::android::ScopedJavaLocalRef
<jobject
>& j_surface_texture
) {
44 j_surface_texture_
.Reset(j_surface_texture
);
47 SurfaceTexture::~SurfaceTexture() {
48 JNIEnv
* env
= base::android::AttachCurrentThread();
49 Java_SurfaceTexturePlatformWrapper_destroy(env
, j_surface_texture_
.obj());
52 void SurfaceTexture::SetFrameAvailableCallback(
53 const base::Closure
& callback
) {
54 JNIEnv
* env
= base::android::AttachCurrentThread();
55 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback(
57 j_surface_texture_
.obj(),
58 reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback
)));
61 void SurfaceTexture::UpdateTexImage() {
62 JNIEnv
* env
= base::android::AttachCurrentThread();
63 Java_SurfaceTexturePlatformWrapper_updateTexImage(env
,
64 j_surface_texture_
.obj());
67 void SurfaceTexture::ReleaseTexImage() {
68 DCHECK(IsSingleBufferModeSupported());
69 JNIEnv
* env
= base::android::AttachCurrentThread();
70 Java_SurfaceTexturePlatformWrapper_releaseTexImage(env
,
71 j_surface_texture_
.obj());
74 void SurfaceTexture::GetTransformMatrix(float mtx
[16]) {
75 JNIEnv
* env
= base::android::AttachCurrentThread();
77 base::android::ScopedJavaLocalRef
<jfloatArray
> jmatrix(
78 env
, env
->NewFloatArray(16));
79 Java_SurfaceTexturePlatformWrapper_getTransformMatrix(
80 env
, j_surface_texture_
.obj(), jmatrix
.obj());
83 jfloat
* elements
= env
->GetFloatArrayElements(jmatrix
.obj(), &is_copy
);
84 for (int i
= 0; i
< 16; ++i
) {
85 mtx
[i
] = static_cast<float>(elements
[i
]);
87 env
->ReleaseFloatArrayElements(jmatrix
.obj(), elements
, JNI_ABORT
);
90 void SurfaceTexture::SetDefaultBufferSize(int width
, int height
) {
91 JNIEnv
* env
= base::android::AttachCurrentThread();
93 if (width
> 0 && height
> 0) {
94 Java_SurfaceTexturePlatformWrapper_setDefaultBufferSize(
95 env
, j_surface_texture_
.obj(), static_cast<jint
>(width
),
96 static_cast<jint
>(height
));
98 LOG(WARNING
) << "Not setting surface texture buffer size - "
99 "width or height is 0";
103 void SurfaceTexture::AttachToGLContext() {
104 if (GlContextMethodsAvailable()) {
106 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES
, &texture_id
);
108 JNIEnv
* env
= base::android::AttachCurrentThread();
109 Java_SurfaceTexturePlatformWrapper_attachToGLContext(
110 env
, j_surface_texture_
.obj(), texture_id
);
114 void SurfaceTexture::DetachFromGLContext() {
115 if (GlContextMethodsAvailable()) {
116 JNIEnv
* env
= base::android::AttachCurrentThread();
117 Java_SurfaceTexturePlatformWrapper_detachFromGLContext(
118 env
, j_surface_texture_
.obj());
122 ANativeWindow
* SurfaceTexture::CreateSurface() {
123 JNIEnv
* env
= base::android::AttachCurrentThread();
124 ScopedJavaSurface
surface(this);
125 // Note: This ensures that any local references used by
126 // ANativeWindow_fromSurface are released immediately. This is needed as a
127 // workaround for https://code.google.com/p/android/issues/detail?id=68174
128 base::android::ScopedJavaLocalFrame
scoped_local_reference_frame(env
);
129 ANativeWindow
* native_window
= ANativeWindow_fromSurface(
130 env
, surface
.j_surface().obj());
131 return native_window
;
135 bool SurfaceTexture::IsSingleBufferModeSupported() {
136 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
139 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv
* env
) {
140 return RegisterNativesImpl(env
);