| 1 | /* |
| 2 | * Copyright (C) 2019 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 "BLASTBufferQueue" |
| 18 | |
| 19 | #include <nativehelper/JNIHelp.h> |
| 20 | |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | #include <android_runtime/android_view_Surface.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/RefBase.h> |
| 25 | |
| 26 | #include <gui/BLASTBufferQueue.h> |
| 27 | #include <gui/Surface.h> |
| 28 | #include <gui/SurfaceComposerClient.h> |
| 29 | #include "core_jni_helpers.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | static struct { |
| 34 | jclass clazz; |
| 35 | jmethodID ctor; |
| 36 | } gTransactionClassInfo; |
| 37 | |
| 38 | struct { |
| 39 | jmethodID accept; |
| 40 | } gTransactionConsumer; |
| 41 | |
| 42 | static JNIEnv* getenv(JavaVM* vm) { |
| 43 | JNIEnv* env; |
| 44 | auto result = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6); |
| 45 | if (result == JNI_EDETACHED) { |
| 46 | if (vm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) { |
| 47 | LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!"); |
| 48 | } |
| 49 | } else if (result != JNI_OK) { |
| 50 | LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", vm); |
| 51 | } |
| 52 | return env; |
| 53 | } |
| 54 | |
| 55 | struct { |
| 56 | jmethodID onTransactionHang; |
| 57 | } gTransactionHangCallback; |
| 58 | |
| 59 | class TransactionHangCallbackWrapper : public LightRefBase<TransactionHangCallbackWrapper> { |
| 60 | public: |
| 61 | explicit TransactionHangCallbackWrapper(JNIEnv* env, jobject jobject) { |
| 62 | env->GetJavaVM(&mVm); |
| 63 | mTransactionHangObject = env->NewGlobalRef(jobject); |
| 64 | LOG_ALWAYS_FATAL_IF(!mTransactionHangObject, "Failed to make global ref"); |
| 65 | } |
| 66 | |
| 67 | ~TransactionHangCallbackWrapper() { |
| 68 | if (mTransactionHangObject != nullptr) { |
| 69 | getenv(mVm)->DeleteGlobalRef(mTransactionHangObject); |
| 70 | mTransactionHangObject = nullptr; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void onTransactionHang(bool isGpuHang) { |
| 75 | if (mTransactionHangObject) { |
| 76 | getenv(mVm)->CallVoidMethod(mTransactionHangObject, |
| 77 | gTransactionHangCallback.onTransactionHang, isGpuHang); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | JavaVM* mVm; |
| 83 | jobject mTransactionHangObject; |
| 84 | }; |
| 85 | |
| 86 | static jlong nativeCreate(JNIEnv* env, jclass clazz, jstring jName, |
| 87 | jboolean updateDestinationFrame) { |
| 88 | ScopedUtfChars name(env, jName); |
| 89 | sp<BLASTBufferQueue> queue = new BLASTBufferQueue(name.c_str(), updateDestinationFrame); |
| 90 | queue->incStrong((void*)nativeCreate); |
| 91 | return reinterpret_cast<jlong>(queue.get()); |
| 92 | } |
| 93 | |
| 94 | static void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) { |
| 95 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 96 | queue->decStrong((void*)nativeCreate); |
| 97 | } |
| 98 | |
| 99 | static jobject nativeGetSurface(JNIEnv* env, jclass clazz, jlong ptr, |
| 100 | jboolean includeSurfaceControlHandle) { |
| 101 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 102 | return android_view_Surface_createFromSurface(env, |
| 103 | queue->getSurface(includeSurfaceControlHandle)); |
| 104 | } |
| 105 | |
| 106 | class JGlobalRefHolder { |
| 107 | public: |
| 108 | JGlobalRefHolder(JavaVM* vm, jobject object) : mVm(vm), mObject(object) {} |
| 109 | |
| 110 | virtual ~JGlobalRefHolder() { |
| 111 | getenv(mVm)->DeleteGlobalRef(mObject); |
| 112 | mObject = nullptr; |
| 113 | } |
| 114 | |
| 115 | jobject object() { return mObject; } |
| 116 | JavaVM* vm() { return mVm; } |
| 117 | |
| 118 | private: |
| 119 | JGlobalRefHolder(const JGlobalRefHolder&) = delete; |
| 120 | void operator=(const JGlobalRefHolder&) = delete; |
| 121 | |
| 122 | JavaVM* mVm; |
| 123 | jobject mObject; |
| 124 | }; |
| 125 | |
| 126 | static void nativeSyncNextTransaction(JNIEnv* env, jclass clazz, jlong ptr, jobject callback, |
| 127 | jboolean acquireSingleBuffer) { |
| 128 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 129 | JavaVM* vm = nullptr; |
| 130 | LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Unable to get Java VM"); |
| 131 | if (!callback) { |
| 132 | queue->syncNextTransaction(nullptr, acquireSingleBuffer); |
| 133 | } else { |
| 134 | auto globalCallbackRef = |
| 135 | std::make_shared<JGlobalRefHolder>(vm, env->NewGlobalRef(callback)); |
| 136 | queue->syncNextTransaction( |
| 137 | [globalCallbackRef](SurfaceComposerClient::Transaction* t) { |
| 138 | JNIEnv* env = getenv(globalCallbackRef->vm()); |
| 139 | env->CallVoidMethod(globalCallbackRef->object(), gTransactionConsumer.accept, |
| 140 | env->NewObject(gTransactionClassInfo.clazz, |
| 141 | gTransactionClassInfo.ctor, |
| 142 | reinterpret_cast<jlong>(t))); |
| 143 | }, |
| 144 | acquireSingleBuffer); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void nativeStopContinuousSyncTransaction(JNIEnv* env, jclass clazz, jlong ptr) { |
| 149 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 150 | queue->stopContinuousSyncTransaction(); |
| 151 | } |
| 152 | |
| 153 | static void nativeUpdate(JNIEnv* env, jclass clazz, jlong ptr, jlong surfaceControl, jlong width, |
| 154 | jlong height, jint format) { |
| 155 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 156 | queue->update(reinterpret_cast<SurfaceControl*>(surfaceControl), width, height, format); |
| 157 | } |
| 158 | |
| 159 | static void nativeMergeWithNextTransaction(JNIEnv*, jclass clazz, jlong ptr, jlong transactionPtr, |
| 160 | jlong framenumber) { |
| 161 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 162 | auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr); |
| 163 | queue->mergeWithNextTransaction(transaction, CC_UNLIKELY(framenumber < 0) ? 0 : framenumber); |
| 164 | } |
| 165 | |
| 166 | static jlong nativeGetLastAcquiredFrameNum(JNIEnv* env, jclass clazz, jlong ptr) { |
| 167 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 168 | return queue->getLastAcquiredFrameNum(); |
| 169 | } |
| 170 | |
| 171 | static void nativeApplyPendingTransactions(JNIEnv* env, jclass clazz, jlong ptr, jlong frameNum) { |
| 172 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 173 | queue->applyPendingTransactions(frameNum); |
| 174 | } |
| 175 | |
| 176 | static bool nativeIsSameSurfaceControl(JNIEnv* env, jclass clazz, jlong ptr, jlong surfaceControl) { |
| 177 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 178 | return queue->isSameSurfaceControl(reinterpret_cast<SurfaceControl*>(surfaceControl)); |
| 179 | } |
| 180 | |
| 181 | static void nativeSetTransactionHangCallback(JNIEnv* env, jclass clazz, jlong ptr, |
| 182 | jobject transactionHangCallback) { |
| 183 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 184 | if (transactionHangCallback == nullptr) { |
| 185 | queue->setTransactionHangCallback(nullptr); |
| 186 | } else { |
| 187 | sp<TransactionHangCallbackWrapper> wrapper = |
| 188 | new TransactionHangCallbackWrapper{env, transactionHangCallback}; |
| 189 | queue->setTransactionHangCallback([wrapper](bool isGpuHang) { |
| 190 | wrapper->onTransactionHang(isGpuHang); |
| 191 | }); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static jobject nativeGatherPendingTransactions(JNIEnv* env, jclass clazz, jlong ptr, |
| 196 | jlong frameNum) { |
| 197 | sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr); |
| 198 | SurfaceComposerClient::Transaction* transaction = queue->gatherPendingTransactions(frameNum); |
| 199 | return env->NewObject(gTransactionClassInfo.clazz, gTransactionClassInfo.ctor, |
| 200 | reinterpret_cast<jlong>(transaction)); |
| 201 | } |
| 202 | |
| 203 | static const JNINativeMethod gMethods[] = { |
| 204 | /* name, signature, funcPtr */ |
| 205 | // clang-format off |
| 206 | {"nativeCreate", "(Ljava/lang/String;Z)J", (void*)nativeCreate}, |
| 207 | {"nativeGetSurface", "(JZ)Landroid/view/Surface;", (void*)nativeGetSurface}, |
| 208 | {"nativeDestroy", "(J)V", (void*)nativeDestroy}, |
| 209 | {"nativeSyncNextTransaction", "(JLjava/util/function/Consumer;Z)V", (void*)nativeSyncNextTransaction}, |
| 210 | {"nativeStopContinuousSyncTransaction", "(J)V", (void*)nativeStopContinuousSyncTransaction}, |
| 211 | {"nativeUpdate", "(JJJJI)V", (void*)nativeUpdate}, |
| 212 | {"nativeMergeWithNextTransaction", "(JJJ)V", (void*)nativeMergeWithNextTransaction}, |
| 213 | {"nativeGetLastAcquiredFrameNum", "(J)J", (void*)nativeGetLastAcquiredFrameNum}, |
| 214 | {"nativeApplyPendingTransactions", "(JJ)V", (void*)nativeApplyPendingTransactions}, |
| 215 | {"nativeIsSameSurfaceControl", "(JJ)Z", (void*)nativeIsSameSurfaceControl}, |
| 216 | {"nativeGatherPendingTransactions", "(JJ)Landroid/view/SurfaceControl$Transaction;", (void*)nativeGatherPendingTransactions}, |
| 217 | {"nativeSetTransactionHangCallback", |
| 218 | "(JLandroid/graphics/BLASTBufferQueue$TransactionHangCallback;)V", |
| 219 | (void*)nativeSetTransactionHangCallback}, |
| 220 | // clang-format on |
| 221 | }; |
| 222 | |
| 223 | int register_android_graphics_BLASTBufferQueue(JNIEnv* env) { |
| 224 | int res = jniRegisterNativeMethods(env, "android/graphics/BLASTBufferQueue", |
| 225 | gMethods, NELEM(gMethods)); |
| 226 | LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods."); |
| 227 | |
| 228 | jclass transactionClazz = FindClassOrDie(env, "android/view/SurfaceControl$Transaction"); |
| 229 | gTransactionClassInfo.clazz = MakeGlobalRefOrDie(env, transactionClazz); |
| 230 | gTransactionClassInfo.ctor = |
| 231 | GetMethodIDOrDie(env, gTransactionClassInfo.clazz, "<init>", "(J)V"); |
| 232 | |
| 233 | jclass consumer = FindClassOrDie(env, "java/util/function/Consumer"); |
| 234 | gTransactionConsumer.accept = |
| 235 | GetMethodIDOrDie(env, consumer, "accept", "(Ljava/lang/Object;)V"); |
| 236 | jclass transactionHangClass = |
| 237 | FindClassOrDie(env, "android/graphics/BLASTBufferQueue$TransactionHangCallback"); |
| 238 | gTransactionHangCallback.onTransactionHang = |
| 239 | GetMethodIDOrDie(env, transactionHangClass, "onTransactionHang", "(Z)V"); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | } // namespace android |