SurfaceTexture.cpp revision b0ba48c95ea8768a051100c5adb4c906caa1e080
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
18
19#include <stdio.h>
20
21#include <gui/SurfaceTexture.h>
22
23#include <android_runtime/AndroidRuntime.h>
24
25#include <utils/Log.h>
26#include <utils/misc.h>
27
28#include "android/graphics/GraphicsJNI.h"
29#include "jni.h"
30
31// ----------------------------------------------------------------------------
32
33namespace android {
34
35static const char* const OutOfResourcesException =
36    "android/graphics/SurfaceTexture$OutOfResourcesException";
37
38struct st_t {
39    jfieldID surfaceTexture;
40};
41static st_t st;
42
43// ----------------------------------------------------------------------------
44
45static void setSurfaceTexture(JNIEnv* env, jobject clazz,
46        const sp<SurfaceTexture>& surfaceTexture)
47{
48    SurfaceTexture* const p =
49        (SurfaceTexture*)env->GetIntField(clazz, st.surfaceTexture);
50    if (surfaceTexture.get()) {
51        surfaceTexture->incStrong(clazz);
52    }
53    if (p) {
54        p->decStrong(clazz);
55    }
56    env->SetIntField(clazz, st.surfaceTexture, (int)surfaceTexture.get());
57}
58
59sp<SurfaceTexture> getSurfaceTexture(JNIEnv* env, jobject clazz)
60{
61    sp<SurfaceTexture> surfaceTexture(
62        (SurfaceTexture*)env->GetIntField(clazz, st.surfaceTexture));
63    return surfaceTexture;
64}
65
66// ----------------------------------------------------------------------------
67
68static void SurfaceTexture_init(JNIEnv* env, jobject clazz, jint texName)
69{
70    sp<SurfaceTexture> surfaceTexture(new SurfaceTexture(texName));
71
72    if (surfaceTexture == 0) {
73        doThrow(env, OutOfResourcesException);
74        return;
75    }
76    setSurfaceTexture(env, clazz, surfaceTexture);
77}
78
79static void SurfaceTexture_updateTexImage(JNIEnv* env, jobject clazz)
80{
81    sp<SurfaceTexture> surfaceTexture(getSurfaceTexture(env, clazz));
82    surfaceTexture->updateTexImage();
83}
84
85static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject clazz,
86        jfloatArray jmtx)
87{
88    sp<SurfaceTexture> surfaceTexture(getSurfaceTexture(env, clazz));
89    float* mtx = env->GetFloatArrayElements(jmtx, NULL);
90    surfaceTexture->getTransformMatrix(mtx);
91    env->ReleaseFloatArrayElements(jmtx, mtx, 0);
92}
93
94// ----------------------------------------------------------------------------
95
96const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture";
97static void nativeClassInit(JNIEnv* env, jclass clazz);
98
99static JNINativeMethod gSurfaceTextureMethods[] = {
100    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
101    {"init",                "(I)V", (void*)SurfaceTexture_init },
102    {"updateTexImage",      "()V",  (void*)SurfaceTexture_updateTexImage },
103    {"getTransformMatrixImpl", "([F)V",  (void*)SurfaceTexture_getTransformMatrix },
104};
105
106static void nativeClassInit(JNIEnv* env, jclass clazz)
107{
108    st.surfaceTexture = env->GetFieldID(clazz,
109            ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I");
110}
111
112int register_android_graphics_SurfaceTexture(JNIEnv* env)
113{
114    int err = 0;
115    err = AndroidRuntime::registerNativeMethods(env, kSurfaceTextureClassPathName,
116            gSurfaceTextureMethods, NELEM(gSurfaceTextureMethods));
117    return err;
118}
119
120} // namespace android
121