OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/android/surface_texture.h" | 5 #include "ui/gl/android/surface_texture.h" |
6 | 6 |
7 #include <android/native_window_jni.h> | 7 #include <android/native_window_jni.h> |
8 | 8 |
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" | 9 #include "base/android/jni_android.h" |
12 #include "base/logging.h" | 10 #include "base/logging.h" |
13 #include "jni/SurfaceTexturePlatformWrapper_jni.h" | 11 #include "jni/SurfaceTexturePlatformWrapper_jni.h" |
14 #include "ui/gl/android/scoped_java_surface.h" | 12 #include "ui/gl/android/scoped_java_surface.h" |
15 #include "ui/gl/android/surface_texture_listener.h" | 13 #include "ui/gl/android/surface_texture_listener.h" |
16 #include "ui/gl/gl_bindings.h" | 14 #include "ui/gl/gl_bindings.h" |
17 | 15 |
18 // TODO(boliu): Remove this method when Chromium stops supporting ICS. | |
19 bool GlContextMethodsAvailable() { | |
20 bool available = base::android::BuildInfo::GetInstance()->sdk_int() >= 16; | |
21 if (!available) | |
22 LOG(WARNING) << "Running on unsupported device: rendering may not work"; | |
23 return available; | |
24 } | |
25 | |
26 namespace gfx { | 16 namespace gfx { |
27 | 17 |
28 scoped_refptr<SurfaceTexture> SurfaceTexture::Create(int texture_id) { | 18 scoped_refptr<SurfaceTexture> SurfaceTexture::Create(int texture_id) { |
29 JNIEnv* env = base::android::AttachCurrentThread(); | 19 JNIEnv* env = base::android::AttachCurrentThread(); |
30 return new SurfaceTexture( | 20 return new SurfaceTexture( |
31 Java_SurfaceTexturePlatformWrapper_create(env, texture_id)); | 21 Java_SurfaceTexturePlatformWrapper_create(env, texture_id)); |
32 } | 22 } |
33 | 23 |
34 scoped_refptr<SurfaceTexture> SurfaceTexture::CreateSingleBuffered( | |
35 int texture_id) { | |
36 DCHECK(IsSingleBufferModeSupported()); | |
37 JNIEnv* env = base::android::AttachCurrentThread(); | |
38 return new SurfaceTexture( | |
39 Java_SurfaceTexturePlatformWrapper_createSingleBuffered(env, texture_id)); | |
40 } | |
41 | |
42 SurfaceTexture::SurfaceTexture( | 24 SurfaceTexture::SurfaceTexture( |
43 const base::android::ScopedJavaLocalRef<jobject>& j_surface_texture) { | 25 const base::android::ScopedJavaLocalRef<jobject>& j_surface_texture) { |
44 j_surface_texture_.Reset(j_surface_texture); | 26 j_surface_texture_.Reset(j_surface_texture); |
45 } | 27 } |
46 | 28 |
47 SurfaceTexture::~SurfaceTexture() { | 29 SurfaceTexture::~SurfaceTexture() { |
48 JNIEnv* env = base::android::AttachCurrentThread(); | 30 JNIEnv* env = base::android::AttachCurrentThread(); |
49 Java_SurfaceTexturePlatformWrapper_destroy(env, j_surface_texture_.obj()); | 31 Java_SurfaceTexturePlatformWrapper_destroy(env, j_surface_texture_.obj()); |
50 } | 32 } |
51 | 33 |
52 void SurfaceTexture::SetFrameAvailableCallback( | 34 void SurfaceTexture::SetFrameAvailableCallback( |
53 const base::Closure& callback) { | 35 const base::Closure& callback) { |
54 JNIEnv* env = base::android::AttachCurrentThread(); | 36 JNIEnv* env = base::android::AttachCurrentThread(); |
55 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback( | 37 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback( |
56 env, | 38 env, |
57 j_surface_texture_.obj(), | 39 j_surface_texture_.obj(), |
58 reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback))); | 40 reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback))); |
59 } | 41 } |
60 | 42 |
61 void SurfaceTexture::UpdateTexImage() { | 43 void SurfaceTexture::UpdateTexImage() { |
62 JNIEnv* env = base::android::AttachCurrentThread(); | 44 JNIEnv* env = base::android::AttachCurrentThread(); |
63 Java_SurfaceTexturePlatformWrapper_updateTexImage(env, | 45 Java_SurfaceTexturePlatformWrapper_updateTexImage(env, |
64 j_surface_texture_.obj()); | 46 j_surface_texture_.obj()); |
65 } | 47 } |
66 | 48 |
67 void SurfaceTexture::ReleaseTexImage() { | |
68 DCHECK(IsSingleBufferModeSupported()); | |
69 JNIEnv* env = base::android::AttachCurrentThread(); | |
70 Java_SurfaceTexturePlatformWrapper_releaseTexImage(env, | |
71 j_surface_texture_.obj()); | |
72 } | |
73 | |
74 void SurfaceTexture::GetTransformMatrix(float mtx[16]) { | 49 void SurfaceTexture::GetTransformMatrix(float mtx[16]) { |
75 JNIEnv* env = base::android::AttachCurrentThread(); | 50 JNIEnv* env = base::android::AttachCurrentThread(); |
76 | 51 |
77 base::android::ScopedJavaLocalRef<jfloatArray> jmatrix( | 52 base::android::ScopedJavaLocalRef<jfloatArray> jmatrix( |
78 env, env->NewFloatArray(16)); | 53 env, env->NewFloatArray(16)); |
79 Java_SurfaceTexturePlatformWrapper_getTransformMatrix( | 54 Java_SurfaceTexturePlatformWrapper_getTransformMatrix( |
80 env, j_surface_texture_.obj(), jmatrix.obj()); | 55 env, j_surface_texture_.obj(), jmatrix.obj()); |
81 | 56 |
82 jboolean is_copy; | 57 jboolean is_copy; |
83 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy); | 58 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy); |
84 for (int i = 0; i < 16; ++i) { | 59 for (int i = 0; i < 16; ++i) { |
85 mtx[i] = static_cast<float>(elements[i]); | 60 mtx[i] = static_cast<float>(elements[i]); |
86 } | 61 } |
87 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT); | 62 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT); |
88 } | 63 } |
89 | 64 |
90 void SurfaceTexture::AttachToGLContext() { | 65 void SurfaceTexture::AttachToGLContext() { |
91 if (GlContextMethodsAvailable()) { | 66 int texture_id; |
92 int texture_id; | 67 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
93 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 68 DCHECK(texture_id); |
94 DCHECK(texture_id); | 69 JNIEnv* env = base::android::AttachCurrentThread(); |
95 JNIEnv* env = base::android::AttachCurrentThread(); | 70 Java_SurfaceTexturePlatformWrapper_attachToGLContext( |
96 Java_SurfaceTexturePlatformWrapper_attachToGLContext( | 71 env, j_surface_texture_.obj(), texture_id); |
97 env, j_surface_texture_.obj(), texture_id); | |
98 } | |
99 } | 72 } |
100 | 73 |
101 void SurfaceTexture::DetachFromGLContext() { | 74 void SurfaceTexture::DetachFromGLContext() { |
102 if (GlContextMethodsAvailable()) { | 75 JNIEnv* env = base::android::AttachCurrentThread(); |
103 JNIEnv* env = base::android::AttachCurrentThread(); | 76 Java_SurfaceTexturePlatformWrapper_detachFromGLContext( |
104 Java_SurfaceTexturePlatformWrapper_detachFromGLContext( | 77 env, j_surface_texture_.obj()); |
105 env, j_surface_texture_.obj()); | |
106 } | |
107 } | 78 } |
108 | 79 |
109 ANativeWindow* SurfaceTexture::CreateSurface() { | 80 ANativeWindow* SurfaceTexture::CreateSurface() { |
110 JNIEnv* env = base::android::AttachCurrentThread(); | 81 JNIEnv* env = base::android::AttachCurrentThread(); |
111 ScopedJavaSurface surface(this); | 82 ScopedJavaSurface surface(this); |
112 // Note: This ensures that any local references used by | 83 // Note: This ensures that any local references used by |
113 // ANativeWindow_fromSurface are released immediately. This is needed as a | 84 // ANativeWindow_fromSurface are released immediately. This is needed as a |
114 // workaround for https://code.google.com/p/android/issues/detail?id=68174 | 85 // workaround for https://code.google.com/p/android/issues/detail?id=68174 |
115 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); | 86 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); |
116 ANativeWindow* native_window = ANativeWindow_fromSurface( | 87 ANativeWindow* native_window = ANativeWindow_fromSurface( |
117 env, surface.j_surface().obj()); | 88 env, surface.j_surface().obj()); |
118 return native_window; | 89 return native_window; |
119 } | 90 } |
120 | 91 |
121 // static | |
122 bool SurfaceTexture::IsSingleBufferModeSupported() { | |
123 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19; | |
124 } | |
125 | |
126 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv* env) { | 92 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv* env) { |
127 return RegisterNativesImpl(env); | 93 return RegisterNativesImpl(env); |
128 } | 94 } |
129 | 95 |
130 } // namespace gfx | 96 } // namespace gfx |
OLD | NEW |