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