SurfaceTexture.cpp revision 6714efc5e0c52953b65e774de0003e22377e7d39
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
85// ----------------------------------------------------------------------------
86
87const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture";
88static void nativeClassInit(JNIEnv* env, jclass clazz);
89
90static JNINativeMethod gSurfaceTextureMethods[] = {
91    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
92    {"init",                "(I)V", (void*)SurfaceTexture_init },
93    {"updateTexImage",      "()V",  (void*)SurfaceTexture_updateTexImage },
94};
95
96static void nativeClassInit(JNIEnv* env, jclass clazz)
97{
98    st.surfaceTexture = env->GetFieldID(clazz,
99            ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I");
100}
101
102int register_android_graphics_SurfaceTexture(JNIEnv* env)
103{
104    int err = 0;
105    err = AndroidRuntime::registerNativeMethods(env, kSurfaceTextureClassPathName,
106            gSurfaceTextureMethods, NELEM(gSurfaceTextureMethods));
107    return err;
108}
109
110} // namespace android
111