Skip to content

Commit c6d9930

Browse files
author
Jamie Gennis
committed
SurfaceTexture: add GL context attach & detach
This change adds Java API support for detaching a SurfaceTexture from one GLES context and then attaching it to a different one. Change-Id: I8eed4b0d0e339c11598cb0408d9f4f2d99b3aa06
1 parent 9828830 commit c6d9930

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

core/jni/android/graphics/SurfaceTexture.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ static jint SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz)
218218
return surfaceTexture->updateTexImage();
219219
}
220220

221+
static jint SurfaceTexture_detachFromGLContext(JNIEnv* env, jobject thiz)
222+
{
223+
sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
224+
return surfaceTexture->detachFromContext();
225+
}
226+
227+
static jint SurfaceTexture_attachToGLContext(JNIEnv* env, jobject thiz, jint tex)
228+
{
229+
sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
230+
return surfaceTexture->attachToContext((GLuint)tex);
231+
}
232+
221233
static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz,
222234
jfloatArray jmtx)
223235
{
@@ -242,14 +254,16 @@ static void SurfaceTexture_release(JNIEnv* env, jobject thiz)
242254
// ----------------------------------------------------------------------------
243255

244256
static JNINativeMethod gSurfaceTextureMethods[] = {
245-
{"nativeClassInit", "()V", (void*)SurfaceTexture_classInit },
246-
{"nativeInit", "(ILjava/lang/Object;Z)V", (void*)SurfaceTexture_init },
247-
{"nativeFinalize", "()V", (void*)SurfaceTexture_finalize },
257+
{"nativeClassInit", "()V", (void*)SurfaceTexture_classInit },
258+
{"nativeInit", "(ILjava/lang/Object;Z)V", (void*)SurfaceTexture_init },
259+
{"nativeFinalize", "()V", (void*)SurfaceTexture_finalize },
248260
{"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize },
249-
{"nativeUpdateTexImage", "()I", (void*)SurfaceTexture_updateTexImage },
250-
{"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix },
251-
{"nativeGetTimestamp", "()J", (void*)SurfaceTexture_getTimestamp },
252-
{"nativeRelease", "()V", (void*)SurfaceTexture_release },
261+
{"nativeUpdateTexImage", "()I", (void*)SurfaceTexture_updateTexImage },
262+
{"nativeDetachFromGLContext", "()I", (void*)SurfaceTexture_detachFromGLContext },
263+
{"nativeAttachToGLContext", "(I)I", (void*)SurfaceTexture_attachToGLContext },
264+
{"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix },
265+
{"nativeGetTimestamp", "()J", (void*)SurfaceTexture_getTimestamp },
266+
{"nativeRelease", "()V", (void*)SurfaceTexture_release },
253267
};
254268

255269
int register_android_graphics_SurfaceTexture(JNIEnv* env)

graphics/java/android/graphics/SurfaceTexture.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,31 @@ public void setDefaultBufferSize(int width, int height) {
161161
public void updateTexImage() {
162162
int err = nativeUpdateTexImage();
163163
if (err != 0) {
164-
throw new RuntimeException("Error during updateTexImage (see logs)");
164+
throw new RuntimeException("Error during updateTexImage (see logcat for details)");
165+
}
166+
}
167+
168+
/**
169+
* Detach the SurfaceTexture from the OpenGL ES context with which it is currently associated.
170+
* This can be used to change from one OpenGL ES context to another.
171+
*
172+
* @hide
173+
*/
174+
public void detachFromGLContext() {
175+
int err = nativeDetachFromGLContext();
176+
if (err != 0) {
177+
throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
178+
}
179+
}
180+
181+
/**
182+
*
183+
* @hide
184+
*/
185+
public void attachToGLContext(int texName) {
186+
int err = nativeAttachToGLContext(texName);
187+
if (err != 0) {
188+
throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
165189
}
166190
}
167191

@@ -269,6 +293,8 @@ private static void postEventFromNative(Object selfRef) {
269293
private native long nativeGetTimestamp();
270294
private native void nativeSetDefaultBufferSize(int width, int height);
271295
private native int nativeUpdateTexImage();
296+
private native int nativeDetachFromGLContext();
297+
private native int nativeAttachToGLContext(int texName);
272298
private native int nativeGetQueuedCount();
273299
private native void nativeRelease();
274300

0 commit comments

Comments
 (0)