• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,jlong surfaceControl,jlong width,jlong height,jint format)70 static jlong nativeCreate(JNIEnv* env, jclass clazz, jstring jName, jlong surfaceControl,
71                           jlong width, jlong height, jint format) {
72     String8 str8;
73     if (jName) {
74         const jchar* str16 = env->GetStringCritical(jName, nullptr);
75         if (str16) {
76             str8 = String8(reinterpret_cast<const char16_t*>(str16), env->GetStringLength(jName));
77             env->ReleaseStringCritical(jName, str16);
78             str16 = nullptr;
79         }
80     }
81     std::string name = str8.string();
82     sp<BLASTBufferQueue> queue =
83             new BLASTBufferQueue(name, reinterpret_cast<SurfaceControl*>(surfaceControl), width,
84                                  height, format);
85     queue->incStrong((void*)nativeCreate);
86     return reinterpret_cast<jlong>(queue.get());
87 }
88 
nativeDestroy(JNIEnv * env,jclass clazz,jlong ptr)89 static void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
90     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
91     queue->decStrong((void*)nativeCreate);
92 }
93 
nativeGetSurface(JNIEnv * env,jclass clazz,jlong ptr,jboolean includeSurfaceControlHandle)94 static jobject nativeGetSurface(JNIEnv* env, jclass clazz, jlong ptr,
95                                 jboolean includeSurfaceControlHandle) {
96     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
97     return android_view_Surface_createFromSurface(env,
98                                                   queue->getSurface(includeSurfaceControlHandle));
99 }
100 
nativeSetNextTransaction(JNIEnv * env,jclass clazz,jlong ptr,jlong transactionPtr)101 static void nativeSetNextTransaction(JNIEnv* env, jclass clazz, jlong ptr, jlong transactionPtr) {
102     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
103     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
104     queue->setNextTransaction(transaction);
105 }
106 
nativeUpdate(JNIEnv * env,jclass clazz,jlong ptr,jlong surfaceControl,jlong width,jlong height,jint format,jlong transactionPtr)107 static void nativeUpdate(JNIEnv* env, jclass clazz, jlong ptr, jlong surfaceControl, jlong width,
108                          jlong height, jint format, jlong transactionPtr) {
109     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
110     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
111     queue->update(reinterpret_cast<SurfaceControl*>(surfaceControl), width, height, format,
112                   transaction);
113 }
114 
nativeFlushShadowQueue(JNIEnv * env,jclass clazz,jlong ptr)115 static void nativeFlushShadowQueue(JNIEnv* env, jclass clazz, jlong ptr) {
116     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
117     queue->flushShadowQueue();
118 }
119 
nativeMergeWithNextTransaction(JNIEnv *,jclass clazz,jlong ptr,jlong transactionPtr,jlong framenumber)120 static void nativeMergeWithNextTransaction(JNIEnv*, jclass clazz, jlong ptr, jlong transactionPtr,
121                                            jlong framenumber) {
122     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
123     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
124     queue->mergeWithNextTransaction(transaction, framenumber);
125 }
126 
nativeSetTransactionCompleteCallback(JNIEnv * env,jclass clazz,jlong ptr,jlong frameNumber,jobject transactionCompleteCallback)127 static void nativeSetTransactionCompleteCallback(JNIEnv* env, jclass clazz, jlong ptr,
128                                                  jlong frameNumber,
129                                                  jobject transactionCompleteCallback) {
130     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
131     if (transactionCompleteCallback == nullptr) {
132         queue->setTransactionCompleteCallback(frameNumber, nullptr);
133     } else {
134         sp<TransactionCompleteCallbackWrapper> wrapper =
135                 new TransactionCompleteCallbackWrapper{env, transactionCompleteCallback};
136         queue->setTransactionCompleteCallback(frameNumber, [wrapper](int64_t frameNr) {
137             wrapper->onTransactionComplete(frameNr);
138         });
139     }
140 }
141 
142 static const JNINativeMethod gMethods[] = {
143         /* name, signature, funcPtr */
144         // clang-format off
145         {"nativeCreate", "(Ljava/lang/String;JJJI)J", (void*)nativeCreate},
146         {"nativeGetSurface", "(JZ)Landroid/view/Surface;", (void*)nativeGetSurface},
147         {"nativeDestroy", "(J)V", (void*)nativeDestroy},
148         {"nativeSetNextTransaction", "(JJ)V", (void*)nativeSetNextTransaction},
149         {"nativeUpdate", "(JJJJIJ)V", (void*)nativeUpdate},
150         {"nativeFlushShadowQueue", "(J)V", (void*)nativeFlushShadowQueue},
151         {"nativeMergeWithNextTransaction", "(JJJ)V", (void*)nativeMergeWithNextTransaction},
152         {"nativeSetTransactionCompleteCallback",
153                 "(JJLandroid/graphics/BLASTBufferQueue$TransactionCompleteCallback;)V",
154                 (void*)nativeSetTransactionCompleteCallback}
155         // clang-format on
156 };
157 
register_android_graphics_BLASTBufferQueue(JNIEnv * env)158 int register_android_graphics_BLASTBufferQueue(JNIEnv* env) {
159     int res = jniRegisterNativeMethods(env, "android/graphics/BLASTBufferQueue",
160             gMethods, NELEM(gMethods));
161     LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
162 
163     jclass transactionCompleteClass =
164             FindClassOrDie(env, "android/graphics/BLASTBufferQueue$TransactionCompleteCallback");
165     gTransactionCompleteCallback.onTransactionComplete =
166             GetMethodIDOrDie(env, transactionCompleteClass, "onTransactionComplete", "(J)V");
167     return 0;
168 }
169 
170 } // namespace android
171