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 #undef LOG_TAG
18 #define LOG_TAG "SurfaceTexture"
19
20 #include <stdio.h>
21
22 #include <EGL/egl.h>
23 #include <EGL/eglext.h>
24 #include <GLES2/gl2.h>
25 #include <GLES2/gl2ext.h>
26
27 #include <gui/BufferQueue.h>
28 #include <gui/Surface.h>
29 #include <surfacetexture/SurfaceTexture.h>
30 #include <surfacetexture/surface_texture_platform.h>
31
32 #include "core_jni_helpers.h"
33
34 #include <cutils/atomic.h>
35 #include <utils/Log.h>
36 #include <utils/misc.h>
37
38 #include "jni.h"
39 #include <nativehelper/JNIHelp.h>
40 #include <nativehelper/ScopedLocalRef.h>
41
42 // ----------------------------------------------------------------------------
43
44 #define EGL_PROTECTED_CONTENT_EXT 0x32C0
45
46 namespace android {
47
48 static const char* const OutOfResourcesException =
49 "android/view/Surface$OutOfResourcesException";
50 static const char* const IllegalStateException = "java/lang/IllegalStateException";
51 const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture";
52
53 struct fields_t {
54 jfieldID surfaceTexture;
55 jfieldID producer;
56 jfieldID frameAvailableListener;
57 jmethodID postEvent;
58 };
59 static fields_t fields;
60
61 // Get an ID that's unique within this process.
createProcessUniqueId()62 static int32_t createProcessUniqueId() {
63 static volatile int32_t globalCounter = 0;
64 return android_atomic_inc(&globalCounter);
65 }
66
67 // Check whether the current EGL context is protected.
isProtectedContext()68 static bool isProtectedContext() {
69 EGLDisplay dpy = eglGetCurrentDisplay();
70 EGLContext ctx = eglGetCurrentContext();
71
72 if (dpy == EGL_NO_DISPLAY || ctx == EGL_NO_CONTEXT) {
73 return false;
74 }
75
76 EGLint isProtected = EGL_FALSE;
77 eglQueryContext(dpy, ctx, EGL_PROTECTED_CONTENT_EXT, &isProtected);
78
79 return isProtected;
80 }
81
82 // ----------------------------------------------------------------------------
83
SurfaceTexture_setSurfaceTexture(JNIEnv * env,jobject thiz,const sp<SurfaceTexture> & surfaceTexture)84 static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz,
85 const sp<SurfaceTexture>& surfaceTexture)
86 {
87 SurfaceTexture* const p =
88 (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
89 if (surfaceTexture.get()) {
90 surfaceTexture->incStrong((void*)SurfaceTexture_setSurfaceTexture);
91 }
92 if (p) {
93 p->decStrong((void*)SurfaceTexture_setSurfaceTexture);
94 }
95 env->SetLongField(thiz, fields.surfaceTexture, (jlong)surfaceTexture.get());
96 }
97
SurfaceTexture_setProducer(JNIEnv * env,jobject thiz,const sp<IGraphicBufferProducer> & producer)98 static void SurfaceTexture_setProducer(JNIEnv* env, jobject thiz,
99 const sp<IGraphicBufferProducer>& producer)
100 {
101 IGraphicBufferProducer* const p =
102 (IGraphicBufferProducer*)env->GetLongField(thiz, fields.producer);
103 if (producer.get()) {
104 producer->incStrong((void*)SurfaceTexture_setProducer);
105 }
106 if (p) {
107 p->decStrong((void*)SurfaceTexture_setProducer);
108 }
109 env->SetLongField(thiz, fields.producer, (jlong)producer.get());
110 }
111
SurfaceTexture_setFrameAvailableListener(JNIEnv * env,jobject thiz,sp<SurfaceTexture::FrameAvailableListener> listener)112 static void SurfaceTexture_setFrameAvailableListener(JNIEnv* env,
113 jobject thiz, sp<SurfaceTexture::FrameAvailableListener> listener)
114 {
115 SurfaceTexture::FrameAvailableListener* const p =
116 (SurfaceTexture::FrameAvailableListener*)
117 env->GetLongField(thiz, fields.frameAvailableListener);
118 if (listener.get()) {
119 listener->incStrong((void*)SurfaceTexture_setSurfaceTexture);
120 }
121 if (p) {
122 p->decStrong((void*)SurfaceTexture_setSurfaceTexture);
123 }
124 env->SetLongField(thiz, fields.frameAvailableListener, (jlong)listener.get());
125 }
126
SurfaceTexture_getSurfaceTexture(JNIEnv * env,jobject thiz)127 sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) {
128 return (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
129 }
130
SurfaceTexture_getProducer(JNIEnv * env,jobject thiz)131 sp<IGraphicBufferProducer> SurfaceTexture_getProducer(JNIEnv* env, jobject thiz) {
132 return (IGraphicBufferProducer*)env->GetLongField(thiz, fields.producer);
133 }
134
android_SurfaceTexture_isInstanceOf(JNIEnv * env,jobject thiz)135 bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) {
136 jclass surfaceTextureClass = env->FindClass(kSurfaceTextureClassPathName);
137 return env->IsInstanceOf(thiz, surfaceTextureClass);
138 }
139
140 // ----------------------------------------------------------------------------
141
142 class JNISurfaceTextureContext : public SurfaceTexture::FrameAvailableListener
143 {
144 public:
145 JNISurfaceTextureContext(JNIEnv* env, jobject weakThiz, jclass clazz);
146 virtual ~JNISurfaceTextureContext();
147 virtual void onFrameAvailable(const BufferItem& item);
148
149 private:
150 static JNIEnv* getJNIEnv();
151
152 jobject mWeakThiz;
153 jclass mClazz;
154 };
155
JNISurfaceTextureContext(JNIEnv * env,jobject weakThiz,jclass clazz)156 JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env,
157 jobject weakThiz, jclass clazz) :
158 mWeakThiz(env->NewGlobalRef(weakThiz)),
159 mClazz((jclass)env->NewGlobalRef(clazz))
160 {}
161
getJNIEnv()162 JNIEnv* JNISurfaceTextureContext::getJNIEnv() {
163 JNIEnv* env = AndroidRuntime::getJNIEnv();
164 if (env == NULL) {
165 JavaVMAttachArgs args = {
166 JNI_VERSION_1_4, "JNISurfaceTextureContext", NULL };
167 JavaVM* vm = AndroidRuntime::getJavaVM();
168 int result = vm->AttachCurrentThreadAsDaemon(&env, (void*)&args);
169 if (result != JNI_OK) {
170 ALOGE("thread attach failed: %#x", result);
171 return NULL;
172 }
173 }
174 return env;
175 }
176
~JNISurfaceTextureContext()177 JNISurfaceTextureContext::~JNISurfaceTextureContext()
178 {
179 JNIEnv* env = getJNIEnv();
180 if (env != NULL) {
181 env->DeleteGlobalRef(mWeakThiz);
182 env->DeleteGlobalRef(mClazz);
183 } else {
184 ALOGW("leaking JNI object references");
185 }
186 }
187
onFrameAvailable(const BufferItem &)188 void JNISurfaceTextureContext::onFrameAvailable(const BufferItem& /* item */)
189 {
190 JNIEnv* env = getJNIEnv();
191 if (env != NULL) {
192 env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz);
193 } else {
194 ALOGW("onFrameAvailable event will not posted");
195 }
196 }
197
198 // ----------------------------------------------------------------------------
199
200
201 #define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
202 #define ANDROID_GRAPHICS_PRODUCER_JNI_ID "mProducer"
203 #define ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID \
204 "mFrameAvailableListener"
205
SurfaceTexture_classInit(JNIEnv * env,jclass clazz)206 static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
207 {
208 fields.surfaceTexture = env->GetFieldID(clazz,
209 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "J");
210 if (fields.surfaceTexture == NULL) {
211 ALOGE("can't find android/graphics/SurfaceTexture.%s",
212 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID);
213 }
214 fields.producer = env->GetFieldID(clazz,
215 ANDROID_GRAPHICS_PRODUCER_JNI_ID, "J");
216 if (fields.producer == NULL) {
217 ALOGE("can't find android/graphics/SurfaceTexture.%s",
218 ANDROID_GRAPHICS_PRODUCER_JNI_ID);
219 }
220 fields.frameAvailableListener = env->GetFieldID(clazz,
221 ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID, "J");
222 if (fields.frameAvailableListener == NULL) {
223 ALOGE("can't find android/graphics/SurfaceTexture.%s",
224 ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID);
225 }
226
227 fields.postEvent = env->GetStaticMethodID(clazz, "postEventFromNative",
228 "(Ljava/lang/ref/WeakReference;)V");
229 if (fields.postEvent == NULL) {
230 ALOGE("can't find android/graphics/SurfaceTexture.postEventFromNative");
231 }
232 }
233
SurfaceTexture_init(JNIEnv * env,jobject thiz,jboolean isDetached,jint texName,jboolean singleBufferMode,jobject weakThiz)234 static void SurfaceTexture_init(JNIEnv* env, jobject thiz, jboolean isDetached,
235 jint texName, jboolean singleBufferMode, jobject weakThiz)
236 {
237 sp<IGraphicBufferProducer> producer;
238 sp<IGraphicBufferConsumer> consumer;
239 BufferQueue::createBufferQueue(&producer, &consumer);
240
241 if (singleBufferMode) {
242 consumer->setMaxBufferCount(1);
243 }
244
245 sp<SurfaceTexture> surfaceTexture;
246 if (isDetached) {
247 surfaceTexture = new SurfaceTexture(consumer, GL_TEXTURE_EXTERNAL_OES,
248 true, !singleBufferMode);
249 } else {
250 surfaceTexture = new SurfaceTexture(consumer, texName,
251 GL_TEXTURE_EXTERNAL_OES, true, !singleBufferMode);
252 }
253
254 if (surfaceTexture == 0) {
255 jniThrowException(env, OutOfResourcesException,
256 "Unable to create native SurfaceTexture");
257 return;
258 }
259 surfaceTexture->setName(String8::format("SurfaceTexture-%d-%d-%d",
260 (isDetached ? 0 : texName),
261 getpid(),
262 createProcessUniqueId()));
263
264 // If the current context is protected, inform the producer.
265 consumer->setConsumerIsProtected(isProtectedContext());
266
267 SurfaceTexture_setSurfaceTexture(env, thiz, surfaceTexture);
268 SurfaceTexture_setProducer(env, thiz, producer);
269
270 jclass clazz = env->GetObjectClass(thiz);
271 if (clazz == NULL) {
272 jniThrowRuntimeException(env,
273 "Can't find android/graphics/SurfaceTexture");
274 return;
275 }
276
277 sp<JNISurfaceTextureContext> ctx(new JNISurfaceTextureContext(env, weakThiz,
278 clazz));
279 surfaceTexture->setFrameAvailableListener(ctx);
280 SurfaceTexture_setFrameAvailableListener(env, thiz, ctx);
281 }
282
SurfaceTexture_finalize(JNIEnv * env,jobject thiz)283 static void SurfaceTexture_finalize(JNIEnv* env, jobject thiz)
284 {
285 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
286 surfaceTexture->setFrameAvailableListener(0);
287 SurfaceTexture_setFrameAvailableListener(env, thiz, 0);
288 SurfaceTexture_setSurfaceTexture(env, thiz, 0);
289 SurfaceTexture_setProducer(env, thiz, 0);
290 }
291
SurfaceTexture_setDefaultBufferSize(JNIEnv * env,jobject thiz,jint width,jint height)292 static void SurfaceTexture_setDefaultBufferSize(
293 JNIEnv* env, jobject thiz, jint width, jint height) {
294 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
295 surfaceTexture->setDefaultBufferSize(width, height);
296 }
297
SurfaceTexture_updateTexImage(JNIEnv * env,jobject thiz)298 static void SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz)
299 {
300 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
301 status_t err = surfaceTexture->updateTexImage();
302 if (err == INVALID_OPERATION) {
303 jniThrowException(env, IllegalStateException, "Unable to update texture contents (see "
304 "logcat for details)");
305 } else if (err < 0) {
306 jniThrowRuntimeException(env, "Error during updateTexImage (see logcat for details)");
307 }
308 }
309
SurfaceTexture_releaseTexImage(JNIEnv * env,jobject thiz)310 static void SurfaceTexture_releaseTexImage(JNIEnv* env, jobject thiz)
311 {
312 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
313 status_t err = surfaceTexture->releaseTexImage();
314 if (err == INVALID_OPERATION) {
315 jniThrowException(env, IllegalStateException, "Unable to release texture contents (see "
316 "logcat for details)");
317 } else if (err < 0) {
318 jniThrowRuntimeException(env, "Error during updateTexImage (see logcat for details)");
319 }
320 }
321
SurfaceTexture_detachFromGLContext(JNIEnv * env,jobject thiz)322 static jint SurfaceTexture_detachFromGLContext(JNIEnv* env, jobject thiz)
323 {
324 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
325 return surfaceTexture->detachFromContext();
326 }
327
SurfaceTexture_attachToGLContext(JNIEnv * env,jobject thiz,jint tex)328 static jint SurfaceTexture_attachToGLContext(JNIEnv* env, jobject thiz, jint tex)
329 {
330 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
331 return surfaceTexture->attachToContext((GLuint)tex);
332 }
333
SurfaceTexture_getTransformMatrix(JNIEnv * env,jobject thiz,jfloatArray jmtx)334 static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz,
335 jfloatArray jmtx)
336 {
337 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
338 float* mtx = env->GetFloatArrayElements(jmtx, NULL);
339 surfaceTexture->getTransformMatrix(mtx);
340 env->ReleaseFloatArrayElements(jmtx, mtx, 0);
341 }
342
SurfaceTexture_getTimestamp(JNIEnv * env,jobject thiz)343 static jlong SurfaceTexture_getTimestamp(JNIEnv* env, jobject thiz)
344 {
345 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
346 return surfaceTexture->getTimestamp();
347 }
348
SurfaceTexture_getDataSpace(JNIEnv * env,jobject thiz)349 static jint SurfaceTexture_getDataSpace(JNIEnv* env, jobject thiz) {
350 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
351 return surfaceTexture->getCurrentDataSpace();
352 }
353
SurfaceTexture_release(JNIEnv * env,jobject thiz)354 static void SurfaceTexture_release(JNIEnv* env, jobject thiz)
355 {
356 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
357 surfaceTexture->abandon();
358 }
359
SurfaceTexture_isReleased(JNIEnv * env,jobject thiz)360 static jboolean SurfaceTexture_isReleased(JNIEnv* env, jobject thiz)
361 {
362 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
363 return surfaceTexture->isAbandoned();
364 }
365
366 // ----------------------------------------------------------------------------
367
368 static const JNINativeMethod gSurfaceTextureMethods[] = {
369 {"nativeInit", "(ZIZLjava/lang/ref/WeakReference;)V", (void*)SurfaceTexture_init},
370 {"nativeFinalize", "()V", (void*)SurfaceTexture_finalize},
371 {"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize},
372 {"nativeUpdateTexImage", "()V", (void*)SurfaceTexture_updateTexImage},
373 {"nativeReleaseTexImage", "()V", (void*)SurfaceTexture_releaseTexImage},
374 {"nativeDetachFromGLContext", "()I", (void*)SurfaceTexture_detachFromGLContext},
375 {"nativeAttachToGLContext", "(I)I", (void*)SurfaceTexture_attachToGLContext},
376 {"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix},
377 {"nativeGetTimestamp", "()J", (void*)SurfaceTexture_getTimestamp},
378 {"nativeGetDataSpace", "()I", (void*)SurfaceTexture_getDataSpace},
379 {"nativeRelease", "()V", (void*)SurfaceTexture_release},
380 {"nativeIsReleased", "()Z", (void*)SurfaceTexture_isReleased},
381 };
382
register_android_graphics_SurfaceTexture(JNIEnv * env)383 int register_android_graphics_SurfaceTexture(JNIEnv* env)
384 {
385 // Cache some fields.
386 ScopedLocalRef<jclass> klass(env, FindClassOrDie(env, kSurfaceTextureClassPathName));
387 SurfaceTexture_classInit(env, klass.get());
388
389 return RegisterMethodsOrDie(env, kSurfaceTextureClassPathName, gSurfaceTextureMethods,
390 NELEM(gSurfaceTextureMethods));
391 }
392
393 } // namespace android
394
395 //TODO: Move this file to frameworks/base/core/jni/android_graphics_SurfaceTexture.cpp. See
396 //TODO: android_view_Surface.cpp for example.
397