- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gl / android / surface_texture.cc
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
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;
22   if (!available)
23     LOG(WARNING) << "Running on unsupported device: rendering may not work";
24   return available;
25 }
26
27 namespace gfx {
28
29 SurfaceTexture::SurfaceTexture(int texture_id) {
30   JNIEnv* env = base::android::AttachCurrentThread();
31   j_surface_texture_.Reset(
32       Java_SurfaceTexturePlatformWrapper_create(env, texture_id));
33 }
34
35 SurfaceTexture::~SurfaceTexture() {
36   JNIEnv* env = base::android::AttachCurrentThread();
37   Java_SurfaceTexturePlatformWrapper_destroy(env, j_surface_texture_.obj());
38 }
39
40 void SurfaceTexture::SetFrameAvailableCallback(
41     const base::Closure& callback) {
42   JNIEnv* env = base::android::AttachCurrentThread();
43   Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback(
44       env,
45       j_surface_texture_.obj(),
46       reinterpret_cast<int>(new SurfaceTextureListener(callback)));
47 }
48
49 void SurfaceTexture::UpdateTexImage() {
50   JNIEnv* env = base::android::AttachCurrentThread();
51   Java_SurfaceTexturePlatformWrapper_updateTexImage(env,
52                                                     j_surface_texture_.obj());
53 }
54
55 void SurfaceTexture::GetTransformMatrix(float mtx[16]) {
56   JNIEnv* env = base::android::AttachCurrentThread();
57
58   base::android::ScopedJavaLocalRef<jfloatArray> jmatrix(
59       env, env->NewFloatArray(16));
60   Java_SurfaceTexturePlatformWrapper_getTransformMatrix(
61       env, j_surface_texture_.obj(), jmatrix.obj());
62
63   jboolean is_copy;
64   jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy);
65   for (int i = 0; i < 16; ++i) {
66     mtx[i] = static_cast<float>(elements[i]);
67   }
68   env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT);
69 }
70
71 void SurfaceTexture::SetDefaultBufferSize(int width, int height) {
72   JNIEnv* env = base::android::AttachCurrentThread();
73
74   if (width > 0 && height > 0) {
75     Java_SurfaceTexturePlatformWrapper_setDefaultBufferSize(
76         env, j_surface_texture_.obj(), static_cast<jint>(width),
77         static_cast<jint>(height));
78   } else {
79     LOG(WARNING) << "Not setting surface texture buffer size - "
80                     "width or height is 0";
81   }
82 }
83
84 void SurfaceTexture::AttachToGLContext() {
85   if (GlContextMethodsAvailable()) {
86     int texture_id;
87     glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
88     DCHECK(texture_id);
89     JNIEnv* env = base::android::AttachCurrentThread();
90     Java_SurfaceTexturePlatformWrapper_attachToGLContext(
91         env, j_surface_texture_.obj(), texture_id);
92   }
93 }
94
95 void SurfaceTexture::DetachFromGLContext() {
96   if (GlContextMethodsAvailable()) {
97     JNIEnv* env = base::android::AttachCurrentThread();
98     Java_SurfaceTexturePlatformWrapper_detachFromGLContext(
99         env, j_surface_texture_.obj());
100   }
101 }
102
103 ANativeWindow* SurfaceTexture::CreateSurface() {
104   JNIEnv* env = base::android::AttachCurrentThread();
105   ScopedJavaSurface surface(this);
106   ANativeWindow* native_window = ANativeWindow_fromSurface(
107       env, surface.j_surface().obj());
108   return native_window;
109 }
110
111 // static
112 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv* env) {
113   return RegisterNativesImpl(env);
114 }
115
116 }  // namespace gfx