1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <jni.h>
16 #include <string>
17 
18 #include <gui/BufferQueue.h>
19 
log(JNIEnv * env,std::string l)20 void log(JNIEnv* env, std::string l) {
21     jclass clazz = env->FindClass("com/android/graphics/bufferstreamsdemoapp/LogOutput");
22     jmethodID getInstance = env->GetStaticMethodID(clazz, "getInstance",
23         "()Lcom/android/graphics/bufferstreamsdemoapp/LogOutput;");
24     jmethodID addLog = env->GetMethodID(clazz, "addLog", "(Ljava/lang/String;)V");
25     jobject dmg = env->CallStaticObjectMethod(clazz, getInstance);
26 
27     jstring jlog = env->NewStringUTF(l.c_str());
28     env->CallVoidMethod(dmg, addLog, jlog);
29 }
30 
31 extern "C" {
32 
33 JNIEXPORT jstring JNICALL
Java_com_android_graphics_bufferstreamsdemoapp_BufferStreamJNI_stringFromJNI(JNIEnv * env,jobject)34 Java_com_android_graphics_bufferstreamsdemoapp_BufferStreamJNI_stringFromJNI(JNIEnv* env,
35                                                                              jobject /* this */) {
36     const char* hello = "Hello from C++";
37     return env->NewStringUTF(hello);
38 }
39 
40 JNIEXPORT void JNICALL
Java_com_android_graphics_bufferstreamsdemoapp_BufferStreamJNI_testBufferQueueCreation(JNIEnv * env,jobject)41 Java_com_android_graphics_bufferstreamsdemoapp_BufferStreamJNI_testBufferQueueCreation(
42         JNIEnv* env, jobject /* thiz */) {
43 
44     log(env, "Calling testBufferQueueCreation.");
45     android::sp<android::IGraphicBufferProducer> producer;
46     log(env, "Created producer.");
47     android::sp<android::IGraphicBufferConsumer> consumer;
48     log(env, "Created consumer.");
49     android::BufferQueue::createBufferQueue(&producer, &consumer);
50     log(env, "Created BufferQueue successfully.");
51     log(env, "Done!");
52 }
53 }