James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ui/gl/android/surface_texture.h" |
| 6 | |
| 7 | #include <android/native_window_jni.h> |
| 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" |
| 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" |
| 17 | |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 18 | // TODO(boliu): Remove this method when Chromium stops supporting ICS. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 { |
| 27 | |
| 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)); |
| 32 | } |
| 33 | |
| 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( |
| 43 | const base::android::ScopedJavaLocalRef<jobject>& j_surface_texture) { |
| 44 | j_surface_texture_.Reset(j_surface_texture); |
| 45 | } |
| 46 | |
| 47 | SurfaceTexture::~SurfaceTexture() { |
| 48 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 49 | Java_SurfaceTexturePlatformWrapper_destroy(env, j_surface_texture_.obj()); |
| 50 | } |
| 51 | |
| 52 | void SurfaceTexture::SetFrameAvailableCallback( |
| 53 | const base::Closure& callback) { |
| 54 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 55 | Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback( |
| 56 | env, |
| 57 | j_surface_texture_.obj(), |
| 58 | reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback))); |
| 59 | } |
| 60 | |
| 61 | void SurfaceTexture::UpdateTexImage() { |
| 62 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 63 | Java_SurfaceTexturePlatformWrapper_updateTexImage(env, |
| 64 | j_surface_texture_.obj()); |
| 65 | } |
| 66 | |
| 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]) { |
| 75 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 76 | |
| 77 | base::android::ScopedJavaLocalRef<jfloatArray> jmatrix( |
| 78 | env, env->NewFloatArray(16)); |
| 79 | Java_SurfaceTexturePlatformWrapper_getTransformMatrix( |
| 80 | env, j_surface_texture_.obj(), jmatrix.obj()); |
| 81 | |
| 82 | jboolean is_copy; |
| 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]); |
| 86 | } |
| 87 | env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT); |
| 88 | } |
| 89 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 90 | void SurfaceTexture::AttachToGLContext() { |
| 91 | if (GlContextMethodsAvailable()) { |
| 92 | int texture_id; |
| 93 | glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 94 | DCHECK(texture_id); |
| 95 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 96 | Java_SurfaceTexturePlatformWrapper_attachToGLContext( |
| 97 | env, j_surface_texture_.obj(), texture_id); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void SurfaceTexture::DetachFromGLContext() { |
| 102 | if (GlContextMethodsAvailable()) { |
| 103 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 104 | Java_SurfaceTexturePlatformWrapper_detachFromGLContext( |
| 105 | env, j_surface_texture_.obj()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ANativeWindow* SurfaceTexture::CreateSurface() { |
| 110 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 111 | ScopedJavaSurface surface(this); |
| 112 | // Note: This ensures that any local references used by |
| 113 | // ANativeWindow_fromSurface are released immediately. This is needed as a |
| 114 | // workaround for https://code.google.com/p/android/issues/detail?id=68174 |
| 115 | base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); |
| 116 | ANativeWindow* native_window = ANativeWindow_fromSurface( |
| 117 | env, surface.j_surface().obj()); |
| 118 | return native_window; |
| 119 | } |
| 120 | |
| 121 | // static |
| 122 | bool SurfaceTexture::IsSingleBufferModeSupported() { |
| 123 | return base::android::BuildInfo::GetInstance()->sdk_int() >= 19; |
| 124 | } |
| 125 | |
| 126 | bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv* env) { |
| 127 | return RegisterNativesImpl(env); |
| 128 | } |
| 129 | |
| 130 | } // namespace gfx |