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 struct {
34 jmethodID onTransactionComplete;
35 } gTransactionCompleteCallback;
36
37 class TransactionCompleteCallbackWrapper : public LightRefBase<TransactionCompleteCallbackWrapper> {
38 public:
TransactionCompleteCallbackWrapper(JNIEnv * env,jobject jobject)39 explicit TransactionCompleteCallbackWrapper(JNIEnv* env, jobject jobject) {
40 env->GetJavaVM(&mVm);
41 mTransactionCompleteObject = env->NewGlobalRef(jobject);
42 LOG_ALWAYS_FATAL_IF(!mTransactionCompleteObject, "Failed to make global ref");
43 }
44
~TransactionCompleteCallbackWrapper()45 ~TransactionCompleteCallbackWrapper() {
46 if (mTransactionCompleteObject) {
47 getenv()->DeleteGlobalRef(mTransactionCompleteObject);
48 mTransactionCompleteObject = nullptr;
49 }
50 }
51
onTransactionComplete(int64_t frameNr)52 void onTransactionComplete(int64_t frameNr) {
53 if (mTransactionCompleteObject) {
54 getenv()->CallVoidMethod(mTransactionCompleteObject,
55 gTransactionCompleteCallback.onTransactionComplete, frameNr);
56 }
57 }
58
59 private:
60 JavaVM* mVm;
61 jobject mTransactionCompleteObject;
62
getenv()63 JNIEnv* getenv() {
64 JNIEnv* env;
65 mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
66 return env;
67 }
68 };
69
nativeCreate(JNIEnv * env,jclass clazz,jstring jName)70 static jlong nativeCreate(JNIEnv* env, jclass clazz, jstring jName) {
71 ScopedUtfChars name(env, jName);
72 sp<BLASTBufferQueue> queue = new BLASTBufferQueue(name.c_str());
73 queue->incStrong((void*)nativeCreate);
74 return reinterpret_cast<jlong>(queue.get());
75 }
76
nativeCreateAndUpdate(JNIEnv * env,jclass clazz,jstring jName,jlong surfaceControl,jlong width,jlong height,jint format)77 static jlong nativeCreateAndUpdate(JNIEnv* env, jclass clazz, jstring jName, jlong surfaceControl,
78 jlong width, jlong height, jint format) {
79 ScopedUtfChars name(env, jName);
80 sp<BLASTBufferQueue> queue =
81 new BLASTBufferQueue(name.c_str(), reinterpret_cast<SurfaceControl*>(surfaceControl),
82 width, height, format);
83 queue->incStrong((void*)nativeCreate);
84 return reinterpret_cast<jlong>(queue.get());
85 }
86
nativeDestroy(JNIEnv * env,jclass clazz,jlong ptr)87 static void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
88 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
89 queue->decStrong((void*)nativeCreate);
90 }
91
nativeGetSurface(JNIEnv * env,jclass clazz,jlong ptr,jboolean includeSurfaceControlHandle)92 static jobject nativeGetSurface(JNIEnv* env, jclass clazz, jlong ptr,
93 jboolean includeSurfaceControlHandle) {
94 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
95 return android_view_Surface_createFromSurface(env,
96 queue->getSurface(includeSurfaceControlHandle));
97 }
98
nativeSetNextTransaction(JNIEnv * env,jclass clazz,jlong ptr,jlong transactionPtr)99 static void nativeSetNextTransaction(JNIEnv* env, jclass clazz, jlong ptr, jlong transactionPtr) {
100 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
101 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
102 queue->setNextTransaction(transaction);
103 }
104
nativeUpdate(JNIEnv * env,jclass clazz,jlong ptr,jlong surfaceControl,jlong width,jlong height,jint format,jlong transactionPtr)105 static void nativeUpdate(JNIEnv* env, jclass clazz, jlong ptr, jlong surfaceControl, jlong width,
106 jlong height, jint format, jlong transactionPtr) {
107 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
108 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
109 queue->update(reinterpret_cast<SurfaceControl*>(surfaceControl), width, height, format,
110 transaction);
111 }
112
nativeMergeWithNextTransaction(JNIEnv *,jclass clazz,jlong ptr,jlong transactionPtr,jlong framenumber)113 static void nativeMergeWithNextTransaction(JNIEnv*, jclass clazz, jlong ptr, jlong transactionPtr,
114 jlong framenumber) {
115 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
116 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
117 queue->mergeWithNextTransaction(transaction, framenumber);
118 }
119
nativeSetTransactionCompleteCallback(JNIEnv * env,jclass clazz,jlong ptr,jlong frameNumber,jobject transactionCompleteCallback)120 static void nativeSetTransactionCompleteCallback(JNIEnv* env, jclass clazz, jlong ptr,
121 jlong frameNumber,
122 jobject transactionCompleteCallback) {
123 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
124 if (transactionCompleteCallback == nullptr) {
125 queue->setTransactionCompleteCallback(frameNumber, nullptr);
126 } else {
127 sp<TransactionCompleteCallbackWrapper> wrapper =
128 new TransactionCompleteCallbackWrapper{env, transactionCompleteCallback};
129 queue->setTransactionCompleteCallback(frameNumber, [wrapper](int64_t frameNr) {
130 wrapper->onTransactionComplete(frameNr);
131 });
132 }
133 }
134
nativeGetLastAcquiredFrameNum(JNIEnv * env,jclass clazz,jlong ptr)135 static jlong nativeGetLastAcquiredFrameNum(JNIEnv* env, jclass clazz, jlong ptr) {
136 sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
137 return queue->getLastAcquiredFrameNum();
138 }
139
140 static const JNINativeMethod gMethods[] = {
141 /* name, signature, funcPtr */
142 // clang-format off
143 {"nativeCreate", "(Ljava/lang/String;)J", (void*)nativeCreate},
144 {"nativeCreateAndUpdate", "(Ljava/lang/String;JJJI)J", (void*)nativeCreateAndUpdate},
145 {"nativeGetSurface", "(JZ)Landroid/view/Surface;", (void*)nativeGetSurface},
146 {"nativeDestroy", "(J)V", (void*)nativeDestroy},
147 {"nativeSetNextTransaction", "(JJ)V", (void*)nativeSetNextTransaction},
148 {"nativeUpdate", "(JJJJIJ)V", (void*)nativeUpdate},
149 {"nativeMergeWithNextTransaction", "(JJJ)V", (void*)nativeMergeWithNextTransaction},
150 {"nativeSetTransactionCompleteCallback",
151 "(JJLandroid/graphics/BLASTBufferQueue$TransactionCompleteCallback;)V",
152 (void*)nativeSetTransactionCompleteCallback},
153 {"nativeGetLastAcquiredFrameNum", "(J)J", (void*)nativeGetLastAcquiredFrameNum},
154 // clang-format on
155 };
156
register_android_graphics_BLASTBufferQueue(JNIEnv * env)157 int register_android_graphics_BLASTBufferQueue(JNIEnv* env) {
158 int res = jniRegisterNativeMethods(env, "android/graphics/BLASTBufferQueue",
159 gMethods, NELEM(gMethods));
160 LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
161
162 jclass transactionCompleteClass =
163 FindClassOrDie(env, "android/graphics/BLASTBufferQueue$TransactionCompleteCallback");
164 gTransactionCompleteCallback.onTransactionComplete =
165 GetMethodIDOrDie(env, transactionCompleteClass, "onTransactionComplete", "(J)V");
166 return 0;
167 }
168
169 } // namespace android
170