• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include "src/java_sdk/main/cpp/com_google_perfetto_sdk_PerfettoExampleWrapper.h"
18 
19 #include <jni.h>
20 
21 #include <string>
22 
23 #include "src/java_sdk/main/cpp/example.h"
24 
Java_com_google_perfetto_sdk_PerfettoExampleWrapper_runPerfettoMain(JNIEnv * env,jobject,jstring outputFilePath)25 static jint Java_com_google_perfetto_sdk_PerfettoExampleWrapper_runPerfettoMain(
26     JNIEnv* env,
27     jobject /*thiz*/,
28     jstring outputFilePath) {
29   const char* cstr = env->GetStringUTFChars(outputFilePath, NULL);
30   std::string file_path = std::string(cstr);
31   env->ReleaseStringUTFChars(outputFilePath, cstr);
32   return run_main(file_path);
33 }
34 
35 static jint
Java_com_google_perfetto_sdk_PerfettoExampleWrapper_incrementIntCritical(jint value)36 Java_com_google_perfetto_sdk_PerfettoExampleWrapper_incrementIntCritical(
37     jint value) {
38   return value + 1;
39 }
40 
41 static const JNINativeMethod myMethods[] = {
runPerfettoMain(Ljava/lang/String;)42     {"runPerfettoMain", "(Ljava/lang/String;)I",
43      reinterpret_cast<void*>(
44          Java_com_google_perfetto_sdk_PerfettoExampleWrapper_runPerfettoMain)},
incrementIntCritical(I)45     {"incrementIntCritical", "(I)I",
46      reinterpret_cast<void*>(
47          Java_com_google_perfetto_sdk_PerfettoExampleWrapper_incrementIntCritical)}};
48 
JNI_OnLoad(JavaVM * vm,void *)49 JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
50   JNIEnv* env;
51   if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
52     return JNI_ERR;
53   }
54 
55   jclass myCls =
56       env->FindClass("com/google/perfetto/sdk/PerfettoExampleWrapper");
57   if (myCls == nullptr) {
58     return JNI_ERR;
59   }
60 
61   int rc = env->RegisterNatives(myCls, myMethods,
62                                 sizeof(myMethods) / sizeof(JNINativeMethod));
63   if (rc != JNI_OK) {
64     return JNI_ERR;
65   }
66 
67   return JNI_VERSION_1_6;
68 }
69