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 
30 namespace android {
31 
nativeCreate(JNIEnv * env,jclass clazz,jlong surfaceControl,jlong width,jlong height,jboolean enableTripleBuffering)32 static jlong nativeCreate(JNIEnv* env, jclass clazz, jlong surfaceControl, jlong width, jlong height,
33                           jboolean enableTripleBuffering) {
34     sp<BLASTBufferQueue> queue = new BLASTBufferQueue(
35             reinterpret_cast<SurfaceControl*>(surfaceControl), width, height, enableTripleBuffering);
36     queue->incStrong((void*)nativeCreate);
37     return reinterpret_cast<jlong>(queue.get());
38 }
39 
nativeDestroy(JNIEnv * env,jclass clazz,jlong ptr)40 static void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
41     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
42     queue->decStrong((void*)nativeCreate);
43 }
44 
nativeGetSurface(JNIEnv * env,jclass clazz,jlong ptr)45 static jobject nativeGetSurface(JNIEnv* env, jclass clazz, jlong ptr) {
46     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
47     return android_view_Surface_createFromIGraphicBufferProducer(env, queue->getIGraphicBufferProducer());
48 }
49 
nativeSetNextTransaction(JNIEnv * env,jclass clazz,jlong ptr,jlong transactionPtr)50 static void nativeSetNextTransaction(JNIEnv* env, jclass clazz, jlong ptr, jlong transactionPtr) {
51     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
52     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionPtr);
53     queue->setNextTransaction(transaction);
54 }
55 
nativeUpdate(JNIEnv * env,jclass clazz,jlong ptr,jlong surfaceControl,jlong width,jlong height)56 static void nativeUpdate(JNIEnv*env, jclass clazz, jlong ptr, jlong surfaceControl, jlong width, jlong height) {
57     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(ptr);
58     queue->update(reinterpret_cast<SurfaceControl*>(surfaceControl), width, height);
59 }
60 
61 static const JNINativeMethod gMethods[] = {
62     /* name, signature, funcPtr */
63     { "nativeCreate", "(JJJZ)J",
64             (void*)nativeCreate },
65     {  "nativeGetSurface", "(J)Landroid/view/Surface;",
66        (void*)nativeGetSurface },
67     { "nativeDestroy", "(J)V",
68             (void*)nativeDestroy },
69     { "nativeSetNextTransaction", "(JJ)V",
70       (void*)nativeSetNextTransaction },
71     { "nativeUpdate", "(JJJJ)V",
72       (void*)nativeUpdate }
73 };
74 
register_android_graphics_BLASTBufferQueue(JNIEnv * env)75 int register_android_graphics_BLASTBufferQueue(JNIEnv* env) {
76     int res = jniRegisterNativeMethods(env, "android/graphics/BLASTBufferQueue",
77             gMethods, NELEM(gMethods));
78     LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
79 
80     return 0;
81 }
82 
83 } // namespace android
84