1 /*
2  * Copyright (C) 2022 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 #ifndef JVMERRORREPORTER_H
17 #define JVMERRORREPORTER_H
18 
19 #include <TreeInfo.h>
20 #include <jni.h>
21 #include <nativehelper/JNIHelp.h>
22 
23 #include "GraphicsJNI.h"
24 
25 namespace android {
26 namespace uirenderer {
27 
28 class JvmErrorReporter : public android::uirenderer::ErrorHandler {
29 public:
JvmErrorReporter(JNIEnv * env)30     JvmErrorReporter(JNIEnv* env) { env->GetJavaVM(&mVm); }
31 
onError(const std::string & message)32     virtual void onError(const std::string& message) override {
33         JNIEnv* env;
34         if (mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
35             LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", mVm);
36         }
37         jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
38     }
39 
40 private:
41     JavaVM* mVm;
42 };
43 
44 }  // namespace uirenderer
45 }  // namespace android
46 
47 #endif  // JVMERRORREPORTER_H
48