Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include <android/surface_texture.h> |
| 18 | #include <android/surface_texture_jni.h> |
| 19 | |
| 20 | #define LOG_TAG "ASurfaceTexture" |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <gui/Surface.h> |
| 25 | |
| 26 | #include <surfacetexture/surface_texture_platform.h> |
| 27 | #include <surfacetexture/SurfaceTexture.h> |
| 28 | |
| 29 | #include <mutex> |
| 30 | |
| 31 | #include <jni.h> |
Orion Hodson | e53587b | 2021-02-02 15:33:33 +0000 | [diff] [blame] | 32 | #include <nativehelper/scoped_local_ref.h> |
Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 33 | |
| 34 | struct ASurfaceTexture { |
| 35 | android::sp<android::SurfaceTexture> consumer; |
| 36 | android::sp<android::IGraphicBufferProducer> producer; |
| 37 | }; |
| 38 | |
| 39 | using namespace android; |
| 40 | |
| 41 | const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture"; |
| 42 | |
| 43 | struct fields_t { |
| 44 | jfieldID surfaceTexture; |
| 45 | jfieldID producer; |
| 46 | }; |
| 47 | static fields_t fields; |
| 48 | static std::once_flag sInitFieldsOnce; |
| 49 | |
| 50 | #define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture" |
| 51 | #define ANDROID_GRAPHICS_PRODUCER_JNI_ID "mProducer" |
| 52 | |
| 53 | static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz) |
| 54 | { |
| 55 | fields.surfaceTexture = env->GetFieldID(clazz, |
| 56 | ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "J"); |
| 57 | if (fields.surfaceTexture == NULL) { |
| 58 | ALOGE("can't find android/graphics/SurfaceTexture.%s", |
| 59 | ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID); |
| 60 | } |
| 61 | fields.producer = env->GetFieldID(clazz, |
| 62 | ANDROID_GRAPHICS_PRODUCER_JNI_ID, "J"); |
| 63 | if (fields.producer == NULL) { |
| 64 | ALOGE("can't find android/graphics/SurfaceTexture.%s", |
| 65 | ANDROID_GRAPHICS_PRODUCER_JNI_ID); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) { |
| 70 | jclass clazz = env->FindClass(class_name); |
| 71 | LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name); |
| 72 | return clazz; |
| 73 | } |
| 74 | |
| 75 | static void register_android_graphics_SurfaceTexture(JNIEnv* env) |
| 76 | { |
| 77 | // Cache some fields. |
| 78 | ScopedLocalRef<jclass> klass(env, FindClassOrDie(env, kSurfaceTextureClassPathName)); |
| 79 | SurfaceTexture_classInit(env, klass.get()); |
| 80 | } |
| 81 | |
| 82 | static bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) { |
| 83 | std::call_once(sInitFieldsOnce, [=]() { |
| 84 | register_android_graphics_SurfaceTexture(env); |
| 85 | }); |
| 86 | |
| 87 | jclass surfaceTextureClass = env->FindClass(kSurfaceTextureClassPathName); |
| 88 | return env->IsInstanceOf(thiz, surfaceTextureClass); |
| 89 | } |
| 90 | |
| 91 | static sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) { |
| 92 | std::call_once(sInitFieldsOnce, [=]() { |
| 93 | register_android_graphics_SurfaceTexture(env); |
| 94 | }); |
| 95 | |
| 96 | return (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture); |
| 97 | } |
| 98 | |
| 99 | static sp<IGraphicBufferProducer> SurfaceTexture_getProducer(JNIEnv* env, jobject thiz) { |
| 100 | std::call_once(sInitFieldsOnce, [=]() { |
| 101 | register_android_graphics_SurfaceTexture(env); |
| 102 | }); |
| 103 | |
| 104 | return (IGraphicBufferProducer*)env->GetLongField(thiz, fields.producer); |
| 105 | } |
| 106 | |
| 107 | // The following functions implement NDK API. |
| 108 | ASurfaceTexture* ASurfaceTexture_fromSurfaceTexture(JNIEnv* env, jobject surfacetexture) { |
| 109 | if (!surfacetexture || !android_SurfaceTexture_isInstanceOf(env, surfacetexture)) { |
| 110 | return nullptr; |
| 111 | } |
| 112 | ASurfaceTexture* ast = new ASurfaceTexture; |
| 113 | ast->consumer = SurfaceTexture_getSurfaceTexture(env, surfacetexture); |
| 114 | ast->producer = SurfaceTexture_getProducer(env, surfacetexture); |
| 115 | return ast; |
| 116 | } |
| 117 | |
| 118 | ANativeWindow* ASurfaceTexture_acquireANativeWindow(ASurfaceTexture* st) { |
| 119 | sp<Surface> surface = new Surface(st->producer); |
| 120 | ANativeWindow* win(surface.get()); |
| 121 | ANativeWindow_acquire(win); |
| 122 | return win; |
| 123 | } |
| 124 | |
| 125 | void ASurfaceTexture_release(ASurfaceTexture* st) { |
| 126 | delete st; |
| 127 | } |
| 128 | |
| 129 | int ASurfaceTexture_attachToGLContext(ASurfaceTexture* st, uint32_t tex) { |
| 130 | return st->consumer->attachToContext(tex); |
| 131 | } |
| 132 | |
| 133 | int ASurfaceTexture_detachFromGLContext(ASurfaceTexture* st) { |
| 134 | return st->consumer->detachFromContext(); |
| 135 | } |
| 136 | |
| 137 | int ASurfaceTexture_updateTexImage(ASurfaceTexture* st) { |
| 138 | return st->consumer->updateTexImage(); |
| 139 | } |
| 140 | |
| 141 | void ASurfaceTexture_getTransformMatrix(ASurfaceTexture* st, float mtx[16]) { |
| 142 | st->consumer->getTransformMatrix(mtx); |
| 143 | } |
| 144 | |
| 145 | int64_t ASurfaceTexture_getTimestamp(ASurfaceTexture* st) { |
| 146 | return st->consumer->getTimestamp(); |
| 147 | } |
| 148 | |
| 149 | // The following functions are private/unstable API. |
| 150 | namespace android { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 151 | ANativeWindow* ASurfaceTexture_routeAcquireANativeWindow(ASurfaceTexture* st) { |
| 152 | return ASurfaceTexture_acquireANativeWindow(st); |
| 153 | } |
| 154 | |
| 155 | int ASurfaceTexture_routeAttachToGLContext(ASurfaceTexture* st, uint32_t texName) { |
| 156 | return ASurfaceTexture_attachToGLContext(st, texName); |
| 157 | } |
| 158 | |
| 159 | void ASurfaceTexture_routeRelease(ASurfaceTexture* st) { |
| 160 | return ASurfaceTexture_release(st); |
| 161 | } |
| 162 | |
| 163 | int ASurfaceTexture_routeDetachFromGLContext(ASurfaceTexture* st) { |
| 164 | return ASurfaceTexture_detachFromGLContext(st); |
| 165 | } |
| 166 | |
| 167 | int ASurfaceTexture_routeUpdateTexImage(ASurfaceTexture* st) { |
| 168 | return ASurfaceTexture_updateTexImage(st); |
| 169 | } |
| 170 | |
| 171 | void ASurfaceTexture_routeGetTransformMatrix(ASurfaceTexture* st, float mtx[16]) { |
| 172 | return ASurfaceTexture_getTransformMatrix(st, mtx); |
| 173 | } |
| 174 | |
| 175 | int64_t ASurfaceTexture_routeGetTimestamp(ASurfaceTexture* st) { |
| 176 | return ASurfaceTexture_getTimestamp(st); |
| 177 | } |
| 178 | |
| 179 | ASurfaceTexture* ASurfaceTexture_routeFromSurfaceTexture(JNIEnv* env, jobject surfacetexture) { |
| 180 | return ASurfaceTexture_fromSurfaceTexture(env, surfacetexture); |
| 181 | } |
Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 182 | |
| 183 | unsigned int ASurfaceTexture_getCurrentTextureTarget(ASurfaceTexture* st) { |
| 184 | return st->consumer->getCurrentTextureTarget(); |
| 185 | } |
| 186 | |
| 187 | void ASurfaceTexture_takeConsumerOwnership(ASurfaceTexture* texture) { |
| 188 | texture->consumer->takeConsumerOwnership(); |
| 189 | } |
| 190 | |
| 191 | void ASurfaceTexture_releaseConsumerOwnership(ASurfaceTexture* texture) { |
| 192 | texture->consumer->releaseConsumerOwnership(); |
| 193 | } |
| 194 | |
| 195 | AHardwareBuffer* ASurfaceTexture_dequeueBuffer(ASurfaceTexture* st, int* outSlotid, |
bsears | 619eea3 | 2021-08-06 15:18:26 +0000 | [diff] [blame^] | 196 | android_dataspace* outDataspace, |
| 197 | float* outTransformMatrix, bool* outNewContent, |
Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 198 | ASurfaceTexture_createReleaseFence createFence, |
bsears | 619eea3 | 2021-08-06 15:18:26 +0000 | [diff] [blame^] | 199 | ASurfaceTexture_fenceWait fenceWait, void* handle) { |
Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 200 | sp<GraphicBuffer> buffer; |
| 201 | *outNewContent = false; |
| 202 | bool queueEmpty; |
| 203 | do { |
bsears | 619eea3 | 2021-08-06 15:18:26 +0000 | [diff] [blame^] | 204 | buffer = st->consumer->dequeueBuffer(outSlotid, outDataspace, outTransformMatrix, |
| 205 | &queueEmpty, createFence, fenceWait, handle); |
Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 206 | if (!queueEmpty) { |
| 207 | *outNewContent = true; |
| 208 | } |
| 209 | } while (buffer.get() && (!queueEmpty)); |
Stan Iliev | 0702f1d | 2020-07-16 17:09:57 -0400 | [diff] [blame] | 210 | AHardwareBuffer* result = nullptr; |
| 211 | if (buffer.get()) { |
| 212 | result = buffer->toAHardwareBuffer(); |
| 213 | // add a reference to keep the hardware buffer alive, even if |
| 214 | // BufferQueueProducer is disconnected. This is needed, because |
| 215 | // sp reference is destroyed at the end of this function. |
| 216 | AHardwareBuffer_acquire(result); |
| 217 | } |
| 218 | return result; |
Stan Iliev | 5d6bdb0 | 2020-01-13 11:19:44 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | } // namespace android |