Revert "Created HardwareBufferRenderer to support rendering into..."

Revert submission 20579518-hardware_buffer_renderer

Reason for revert: The submission timing tracks with a major regression in CtsUiRenderingTestCases stability: https://screenshot.googleplex.com/3TxXCSP4xCZq7Zy.png and also some crash bugs, eg: https://b.corp.google.com/issues/264889058

Reverting to re-stabilize the tree

Reverted changes: /q/submissionid:20579518-hardware_buffer_renderer

Change-Id: I29f47da097257bdeaa963fccb9ad0dbe39ead063
diff --git a/libs/hwui/jni/Bitmap.cpp b/libs/hwui/jni/Bitmap.cpp
index 10c287d..1f5a1d3 100644
--- a/libs/hwui/jni/Bitmap.cpp
+++ b/libs/hwui/jni/Bitmap.cpp
@@ -2,42 +2,49 @@
 #define LOG_TAG "Bitmap"
 #include "Bitmap.h"
 
-#include <hwui/Bitmap.h>
-#include <hwui/Paint.h>
-
-#include "CreateJavaOutputStreamAdaptor.h"
 #include "Gainmap.h"
 #include "GraphicsJNI.h"
-#include "HardwareBufferHelpers.h"
 #include "SkBitmap.h"
 #include "SkBlendMode.h"
 #include "SkCanvas.h"
 #include "SkColor.h"
 #include "SkColorSpace.h"
 #include "SkData.h"
+#include "SkImageEncoder.h"
 #include "SkImageInfo.h"
 #include "SkPaint.h"
+#include "SkPixelRef.h"
 #include "SkPixmap.h"
 #include "SkPoint.h"
 #include "SkRefCnt.h"
 #include "SkStream.h"
 #include "SkTypes.h"
+#include "SkWebpEncoder.h"
+
+
 #include "android_nio_utils.h"
+#include "CreateJavaOutputStreamAdaptor.h"
+#include <hwui/Paint.h>
+#include <hwui/Bitmap.h>
+#include <utils/Color.h>
 
 #ifdef __ANDROID__ // Layoutlib does not support graphic buffer, parcel or render thread
 #include <android-base/unique_fd.h>
 #include <android/binder_parcel.h>
 #include <android/binder_parcel_jni.h>
 #include <android/binder_parcel_platform.h>
+#include <android/binder_parcel_utils.h>
+#include <private/android/AHardwareBufferHelpers.h>
 #include <cutils/ashmem.h>
+#include <dlfcn.h>
 #include <renderthread/RenderProxy.h>
 #include <sys/mman.h>
 #endif
 
 #include <inttypes.h>
 #include <string.h>
-
 #include <memory>
+#include <string>
 
 #define DEBUG_PARCEL 0
 
@@ -1198,11 +1205,18 @@
     return createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
 }
 
+#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
+typedef AHardwareBuffer* (*AHB_from_HB)(JNIEnv*, jobject);
+AHB_from_HB AHardwareBuffer_fromHardwareBuffer;
+
+typedef jobject (*AHB_to_HB)(JNIEnv*, AHardwareBuffer*);
+AHB_to_HB AHardwareBuffer_toHardwareBuffer;
+#endif
+
 static jobject Bitmap_wrapHardwareBufferBitmap(JNIEnv* env, jobject, jobject hardwareBuffer,
                                                jlong colorSpacePtr) {
 #ifdef __ANDROID__ // Layoutlib does not support graphic buffer
-    AHardwareBuffer* buffer = uirenderer::HardwareBufferHelpers::AHardwareBuffer_fromHardwareBuffer(
-            env, hardwareBuffer);
+    AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
     sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer,
                                               GraphicsJNI::getNativeColorSpace(colorSpacePtr));
     if (!bitmap.get()) {
@@ -1225,8 +1239,7 @@
     }
 
     Bitmap& bitmap = bitmapHandle->bitmap();
-    return uirenderer::HardwareBufferHelpers::AHardwareBuffer_toHardwareBuffer(
-            env, bitmap.hardwareBuffer());
+    return AHardwareBuffer_toHardwareBuffer(env, bitmap.hardwareBuffer());
 #else
     return nullptr;
 #endif
@@ -1334,7 +1347,18 @@
     gBitmap_nativePtr = GetFieldIDOrDie(env, gBitmap_class, "mNativePtr", "J");
     gBitmap_constructorMethodID = GetMethodIDOrDie(env, gBitmap_class, "<init>", "(JIIIZ[BLandroid/graphics/NinePatch$InsetStruct;Z)V");
     gBitmap_reinitMethodID = GetMethodIDOrDie(env, gBitmap_class, "reinit", "(IIZ)V");
-    uirenderer::HardwareBufferHelpers::init();
+
+#ifdef __ANDROID__ // Layoutlib does not support graphic buffer or parcel
+    void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
+    AHardwareBuffer_fromHardwareBuffer =
+            (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer");
+    LOG_ALWAYS_FATAL_IF(AHardwareBuffer_fromHardwareBuffer == nullptr,
+                        "Failed to find required symbol AHardwareBuffer_fromHardwareBuffer!");
+
+    AHardwareBuffer_toHardwareBuffer = (AHB_to_HB)dlsym(handle_, "AHardwareBuffer_toHardwareBuffer");
+    LOG_ALWAYS_FATAL_IF(AHardwareBuffer_toHardwareBuffer == nullptr,
+                        " Failed to find required symbol AHardwareBuffer_toHardwareBuffer!");
+#endif
     return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
                                          NELEM(gBitmapMethods));
 }
diff --git a/libs/hwui/jni/GraphicsJNI.h b/libs/hwui/jni/GraphicsJNI.h
index 6b983c1..2c556af 100644
--- a/libs/hwui/jni/GraphicsJNI.h
+++ b/libs/hwui/jni/GraphicsJNI.h
@@ -2,18 +2,19 @@
 #define _ANDROID_GRAPHICS_GRAPHICS_JNI_H_
 
 #include <cutils/compiler.h>
-#include <hwui/Bitmap.h>
-#include <hwui/Canvas.h>
 
-#include "BRDAllocator.h"
 #include "Bitmap.h"
+#include "BRDAllocator.h"
 #include "SkBitmap.h"
 #include "SkCodec.h"
-#include "SkColorSpace.h"
-#include "SkMallocPixelRef.h"
 #include "SkPixelRef.h"
+#include "SkMallocPixelRef.h"
 #include "SkPoint.h"
 #include "SkRect.h"
+#include "SkColorSpace.h"
+#include <hwui/Canvas.h>
+#include <hwui/Bitmap.h>
+
 #include "graphics_jni_helpers.h"
 
 class SkCanvas;
@@ -332,26 +333,6 @@
     int         fLen;
 };
 
-class JGlobalRefHolder {
-public:
-    JGlobalRefHolder(JavaVM* vm, jobject object) : mVm(vm), mObject(object) {}
-
-    virtual ~JGlobalRefHolder() {
-        GraphicsJNI::getJNIEnv()->DeleteGlobalRef(mObject);
-        mObject = nullptr;
-    }
-
-    jobject object() { return mObject; }
-    JavaVM* vm() { return mVm; }
-
-private:
-    JGlobalRefHolder(const JGlobalRefHolder&) = delete;
-    void operator=(const JGlobalRefHolder&) = delete;
-
-    JavaVM* mVm;
-    jobject mObject;
-};
-
 void doThrowNPE(JNIEnv* env);
 void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
 void doThrowIAE(JNIEnv* env, const char* msg = NULL);   // Illegal Argument
diff --git a/libs/hwui/jni/HardwareBufferHelpers.cpp b/libs/hwui/jni/HardwareBufferHelpers.cpp
deleted file mode 100644
index 7e3f771..0000000
--- a/libs/hwui/jni/HardwareBufferHelpers.cpp
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "HardwareBufferHelpers.h"
-
-#include <dlfcn.h>
-#include <log/log.h>
-
-#ifdef __ANDROID__
-typedef AHardwareBuffer* (*AHB_from_HB)(JNIEnv*, jobject);
-typedef jobject (*AHB_to_HB)(JNIEnv*, AHardwareBuffer*);
-static AHB_from_HB fromHardwareBuffer = nullptr;
-static AHB_to_HB toHardwareBuffer = nullptr;
-#endif
-
-void android::uirenderer::HardwareBufferHelpers::init() {
-#ifdef __ANDROID__  // Layoutlib does not support graphic buffer or parcel
-    void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
-    fromHardwareBuffer = (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer");
-    LOG_ALWAYS_FATAL_IF(fromHardwareBuffer == nullptr,
-                        "Failed to find required symbol AHardwareBuffer_fromHardwareBuffer!");
-
-    toHardwareBuffer = (AHB_to_HB)dlsym(handle_, "AHardwareBuffer_toHardwareBuffer");
-    LOG_ALWAYS_FATAL_IF(toHardwareBuffer == nullptr,
-                        " Failed to find required symbol AHardwareBuffer_toHardwareBuffer!");
-#endif
-}
-
-AHardwareBuffer* android::uirenderer::HardwareBufferHelpers::AHardwareBuffer_fromHardwareBuffer(
-        JNIEnv* env, jobject hardwarebuffer) {
-#ifdef __ANDROID__
-    LOG_ALWAYS_FATAL_IF(fromHardwareBuffer == nullptr,
-                        "Failed to find symbol AHardwareBuffer_fromHardwareBuffer, did you forget "
-                        "to call HardwareBufferHelpers::init?");
-    return fromHardwareBuffer(env, hardwarebuffer);
-#else
-    ALOGE("ERROR attempting to invoke AHardwareBuffer_fromHardwareBuffer on non Android "
-          "configuration");
-    return nullptr;
-#endif
-}
-
-jobject android::uirenderer::HardwareBufferHelpers::AHardwareBuffer_toHardwareBuffer(
-        JNIEnv* env, AHardwareBuffer* ahardwarebuffer) {
-#ifdef __ANDROID__
-    LOG_ALWAYS_FATAL_IF(toHardwareBuffer == nullptr,
-                        "Failed to find symbol AHardwareBuffer_toHardwareBuffer, did you forget to "
-                        "call HardwareBufferHelpers::init?");
-    return toHardwareBuffer(env, ahardwarebuffer);
-#else
-    ALOGE("ERROR attempting to invoke AHardwareBuffer_toHardwareBuffer on non Android "
-          "configuration");
-    return nullptr;
-#endif
-}
\ No newline at end of file
diff --git a/libs/hwui/jni/HardwareBufferHelpers.h b/libs/hwui/jni/HardwareBufferHelpers.h
deleted file mode 100644
index 326babf..0000000
--- a/libs/hwui/jni/HardwareBufferHelpers.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#ifndef HARDWAREBUFFER_JNI_HELPERS_H
-#define HARDWAREBUFFER_JNI_HELPERS_H
-
-#include <android/bitmap.h>
-#include <jni.h>
-
-namespace android {
-namespace uirenderer {
-
-class HardwareBufferHelpers {
-public:
-    static void init();
-    static AHardwareBuffer* AHardwareBuffer_fromHardwareBuffer(JNIEnv*, jobject);
-    static jobject AHardwareBuffer_toHardwareBuffer(JNIEnv*, AHardwareBuffer*);
-
-private:
-    HardwareBufferHelpers() = default;  // not to be instantiated
-};
-
-}  // namespace uirenderer
-}  // namespace android
-
-#endif  // HARDWAREBUFFER_JNI_HELPERS_H
\ No newline at end of file
diff --git a/libs/hwui/jni/JvmErrorReporter.h b/libs/hwui/jni/JvmErrorReporter.h
deleted file mode 100644
index 5e10b9d..0000000
--- a/libs/hwui/jni/JvmErrorReporter.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#ifndef JVMERRORREPORTER_H
-#define JVMERRORREPORTER_H
-
-#include <TreeInfo.h>
-#include <jni.h>
-#include <nativehelper/JNIHelp.h>
-
-#include "GraphicsJNI.h"
-
-namespace android {
-namespace uirenderer {
-
-class JvmErrorReporter : public android::uirenderer::ErrorHandler {
-public:
-    JvmErrorReporter(JNIEnv* env) { env->GetJavaVM(&mVm); }
-
-    virtual void onError(const std::string& message) override {
-        JNIEnv* env = GraphicsJNI::getJNIEnv();
-        jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
-    }
-
-private:
-    JavaVM* mVm;
-};
-
-}  // namespace uirenderer
-}  // namespace android
-
-#endif  // JVMERRORREPORTER_H
diff --git a/libs/hwui/jni/android_graphics_HardwareBufferRenderer.cpp b/libs/hwui/jni/android_graphics_HardwareBufferRenderer.cpp
deleted file mode 100644
index 4886fdd..0000000
--- a/libs/hwui/jni/android_graphics_HardwareBufferRenderer.cpp
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#undef LOG_TAG
-#define LOG_TAG "HardwareBufferRenderer"
-#define ATRACE_TAG ATRACE_TAG_VIEW
-
-#include <GraphicsJNI.h>
-#include <RootRenderNode.h>
-#include <TreeInfo.h>
-#include <android-base/unique_fd.h>
-#include <android/native_window.h>
-#include <nativehelper/JNIPlatformHelp.h>
-#include <renderthread/CanvasContext.h>
-#include <renderthread/RenderProxy.h>
-#include <renderthread/RenderThread.h>
-
-#include "HardwareBufferHelpers.h"
-#include "JvmErrorReporter.h"
-
-namespace android {
-
-using namespace android::uirenderer;
-using namespace android::uirenderer::renderthread;
-
-struct {
-    jclass clazz;
-    jmethodID invokeRenderCallback;
-} gHardwareBufferRendererClassInfo;
-
-static RenderCallback createRenderCallback(JNIEnv* env, jobject releaseCallback) {
-    if (releaseCallback == nullptr) return nullptr;
-
-    JavaVM* vm = nullptr;
-    LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Unable to get Java VM");
-    auto globalCallbackRef =
-            std::make_shared<JGlobalRefHolder>(vm, env->NewGlobalRef(releaseCallback));
-    return [globalCallbackRef](android::base::unique_fd&& fd, int status) {
-        GraphicsJNI::getJNIEnv()->CallStaticVoidMethod(
-                gHardwareBufferRendererClassInfo.clazz,
-                gHardwareBufferRendererClassInfo.invokeRenderCallback, globalCallbackRef->object(),
-                reinterpret_cast<jint>(fd.release()), reinterpret_cast<jint>(status));
-    };
-}
-
-static long android_graphics_HardwareBufferRenderer_createRootNode(JNIEnv* env, jobject) {
-    auto* node = new RootRenderNode(std::make_unique<JvmErrorReporter>(env));
-    node->incStrong(nullptr);
-    node->setName("RootRenderNode");
-    return reinterpret_cast<jlong>(node);
-}
-
-static void android_graphics_hardwareBufferRenderer_destroyRootNode(JNIEnv*, jobject,
-                                                                    jlong renderNodePtr) {
-    auto* node = reinterpret_cast<RootRenderNode*>(renderNodePtr);
-    node->destroy();
-}
-
-static long android_graphics_HardwareBufferRenderer_create(JNIEnv* env, jobject, jobject buffer,
-                                                           jlong renderNodePtr) {
-    auto* hardwareBuffer = HardwareBufferHelpers::AHardwareBuffer_fromHardwareBuffer(env, buffer);
-    auto* rootRenderNode = reinterpret_cast<RootRenderNode*>(renderNodePtr);
-    ContextFactoryImpl factory(rootRenderNode);
-    auto* proxy = new RenderProxy(true, rootRenderNode, &factory);
-    proxy->setHardwareBuffer(hardwareBuffer);
-    return (jlong)proxy;
-}
-
-static void HardwareBufferRenderer_destroy(jobject renderProxy) {
-    auto* proxy = reinterpret_cast<RenderProxy*>(renderProxy);
-    delete proxy;
-}
-
-static SkMatrix createMatrixFromBufferTransform(SkScalar width, SkScalar height, int transform) {
-    auto matrix = SkMatrix();
-    switch (transform) {
-        case ANATIVEWINDOW_TRANSFORM_ROTATE_90:
-            matrix.setRotate(90);
-            matrix.postTranslate(width, 0);
-            break;
-        case ANATIVEWINDOW_TRANSFORM_ROTATE_180:
-            matrix.setRotate(180);
-            matrix.postTranslate(width, height);
-            break;
-        case ANATIVEWINDOW_TRANSFORM_ROTATE_270:
-            matrix.setRotate(270);
-            matrix.postTranslate(0, width);
-            break;
-        default:
-            ALOGE("Invalid transform provided. Transform should be validated from"
-                  "the java side. Leveraging identity transform as a fallback");
-            [[fallthrough]];
-        case ANATIVEWINDOW_TRANSFORM_IDENTITY:
-            break;
-    }
-    return matrix;
-}
-
-static int android_graphics_HardwareBufferRenderer_render(JNIEnv* env, jobject, jobject renderProxy,
-                                                          jint transform, jint width, jint height,
-                                                          jlong colorspacePtr, jobject consumer) {
-    auto* proxy = reinterpret_cast<RenderProxy*>(renderProxy);
-    auto skWidth = static_cast<SkScalar>(width);
-    auto skHeight = static_cast<SkScalar>(height);
-    auto matrix = createMatrixFromBufferTransform(skWidth, skHeight, transform);
-    auto colorSpace = GraphicsJNI::getNativeColorSpace(colorspacePtr);
-    proxy->setHardwareBufferRenderParams(
-            HardwareBufferRenderParams(matrix, colorSpace, createRenderCallback(env, consumer)));
-    return proxy->syncAndDrawFrame();
-}
-
-static void android_graphics_HardwareBufferRenderer_setLightGeometry(JNIEnv*, jobject,
-                                                                     jobject renderProxyPtr,
-                                                                     jfloat lightX, jfloat lightY,
-                                                                     jfloat lightZ,
-                                                                     jfloat lightRadius) {
-    auto* proxy = reinterpret_cast<RenderProxy*>(renderProxyPtr);
-    proxy->setLightGeometry((Vector3){lightX, lightY, lightZ}, lightRadius);
-}
-
-static void android_graphics_HardwareBufferRenderer_setLightAlpha(JNIEnv* env, jobject,
-                                                                  jobject renderProxyPtr,
-                                                                  jfloat ambientShadowAlpha,
-                                                                  jfloat spotShadowAlpha) {
-    auto* proxy = reinterpret_cast<RenderProxy*>(renderProxyPtr);
-    proxy->setLightAlpha((uint8_t)(255 * ambientShadowAlpha), (uint8_t)(255 * spotShadowAlpha));
-}
-
-static jlong android_graphics_HardwareBufferRenderer_getFinalizer() {
-    return static_cast<jlong>(reinterpret_cast<uintptr_t>(&HardwareBufferRenderer_destroy));
-}
-
-// ----------------------------------------------------------------------------
-// JNI Glue
-// ----------------------------------------------------------------------------
-
-const char* const kClassPathName = "android/graphics/HardwareBufferRenderer";
-
-static const JNINativeMethod gMethods[] = {
-        {"nCreateHardwareBufferRenderer", "(Landroid/hardware/HardwareBuffer;J)J",
-         (void*)android_graphics_HardwareBufferRenderer_create},
-        {"nRender", "(JIIIJLjava/util/function/Consumer;)I",
-         (void*)android_graphics_HardwareBufferRenderer_render},
-        {"nCreateRootRenderNode", "()J",
-         (void*)android_graphics_HardwareBufferRenderer_createRootNode},
-        {"nSetLightGeometry", "(JFFFF)V",
-         (void*)android_graphics_HardwareBufferRenderer_setLightGeometry},
-        {"nSetLightAlpha", "(JFF)V", (void*)android_graphics_HardwareBufferRenderer_setLightAlpha},
-        {"nGetFinalizer", "()J", (void*)android_graphics_HardwareBufferRenderer_getFinalizer},
-        {"nDestroyRootRenderNode", "(J)V",
-         (void*)android_graphics_hardwareBufferRenderer_destroyRootNode}};
-
-int register_android_graphics_HardwareBufferRenderer(JNIEnv* env) {
-    jclass hardwareBufferRendererClazz =
-            FindClassOrDie(env, "android/graphics/HardwareBufferRenderer");
-    gHardwareBufferRendererClassInfo.clazz = hardwareBufferRendererClazz;
-    gHardwareBufferRendererClassInfo.invokeRenderCallback =
-            GetStaticMethodIDOrDie(env, hardwareBufferRendererClazz, "invokeRenderCallback",
-                                   "(Ljava/util/function/Consumer;II)V");
-    HardwareBufferHelpers::init();
-    return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
-}
-
-}  // namespace android
\ No newline at end of file
diff --git a/libs/hwui/jni/android_graphics_HardwareRenderer.cpp b/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
index d6aad7d..a30fc49 100644
--- a/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
+++ b/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
@@ -56,7 +56,6 @@
 #include <atomic>
 #include <vector>
 
-#include "JvmErrorReporter.h"
 #include "android_graphics_HardwareRendererObserver.h"
 
 namespace android {
@@ -94,12 +93,35 @@
     jmethodID getDestinationBitmap;
 } gCopyRequest;
 
+static JNIEnv* getenv(JavaVM* vm) {
+    JNIEnv* env;
+    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
+        LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", vm);
+    }
+    return env;
+}
+
 typedef ANativeWindow* (*ANW_fromSurface)(JNIEnv* env, jobject surface);
 ANW_fromSurface fromSurface;
 
+class JvmErrorReporter : public ErrorHandler {
+public:
+    JvmErrorReporter(JNIEnv* env) {
+        env->GetJavaVM(&mVm);
+    }
+
+    virtual void onError(const std::string& message) override {
+        JNIEnv* env = getenv(mVm);
+        jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
+    }
+private:
+    JavaVM* mVm;
+};
+
 class FrameCommitWrapper : public LightRefBase<FrameCommitWrapper> {
 public:
     explicit FrameCommitWrapper(JNIEnv* env, jobject jobject) {
+        env->GetJavaVM(&mVm);
         mObject = env->NewGlobalRef(jobject);
         LOG_ALWAYS_FATAL_IF(!mObject, "Failed to make global ref");
     }
@@ -109,18 +131,19 @@
     void onFrameCommit(bool didProduceBuffer) {
         if (mObject) {
             ATRACE_FORMAT("frameCommit success=%d", didProduceBuffer);
-            GraphicsJNI::getJNIEnv()->CallVoidMethod(mObject, gFrameCommitCallback.onFrameCommit,
-                                                     didProduceBuffer);
+            getenv(mVm)->CallVoidMethod(mObject, gFrameCommitCallback.onFrameCommit,
+                                        didProduceBuffer);
             releaseObject();
         }
     }
 
 private:
+    JavaVM* mVm;
     jobject mObject;
 
     void releaseObject() {
         if (mObject) {
-            GraphicsJNI::getJNIEnv()->DeleteGlobalRef(mObject);
+            getenv(mVm)->DeleteGlobalRef(mObject);
             mObject = nullptr;
         }
     }
@@ -426,6 +449,26 @@
     proxy->forceDrawNextFrame();
 }
 
+class JGlobalRefHolder {
+public:
+    JGlobalRefHolder(JavaVM* vm, jobject object) : mVm(vm), mObject(object) {}
+
+    virtual ~JGlobalRefHolder() {
+        getenv(mVm)->DeleteGlobalRef(mObject);
+        mObject = nullptr;
+    }
+
+    jobject object() { return mObject; }
+    JavaVM* vm() { return mVm; }
+
+private:
+    JGlobalRefHolder(const JGlobalRefHolder&) = delete;
+    void operator=(const JGlobalRefHolder&) = delete;
+
+    JavaVM* mVm;
+    jobject mObject;
+};
+
 using TextureMap = std::unordered_map<uint32_t, sk_sp<SkImage>>;
 
 struct PictureCaptureState {
@@ -541,7 +584,7 @@
         auto pictureState = std::make_shared<PictureCaptureState>();
         proxy->setPictureCapturedCallback([globalCallbackRef,
                                            pictureState](sk_sp<SkPicture>&& picture) {
-            JNIEnv* env = GraphicsJNI::getJNIEnv();
+            JNIEnv* env = getenv(globalCallbackRef->vm());
             Picture* wrapper = new PictureWrapper{std::move(picture), pictureState};
             env->CallStaticVoidMethod(gHardwareRenderer.clazz,
                     gHardwareRenderer.invokePictureCapturedCallback,
@@ -563,7 +606,7 @@
                 vm, env->NewGlobalRef(aSurfaceTransactionCallback));
         proxy->setASurfaceTransactionCallback(
                 [globalCallbackRef](int64_t transObj, int64_t scObj, int64_t frameNr) -> bool {
-                    JNIEnv* env = GraphicsJNI::getJNIEnv();
+                    JNIEnv* env = getenv(globalCallbackRef->vm());
                     jboolean ret = env->CallBooleanMethod(
                             globalCallbackRef->object(),
                             gASurfaceTransactionCallback.onMergeTransaction,
@@ -585,7 +628,7 @@
         auto globalCallbackRef =
                 std::make_shared<JGlobalRefHolder>(vm, env->NewGlobalRef(callback));
         proxy->setPrepareSurfaceControlForWebviewCallback([globalCallbackRef]() {
-            JNIEnv* env = GraphicsJNI::getJNIEnv();
+            JNIEnv* env = getenv(globalCallbackRef->vm());
             env->CallVoidMethod(globalCallbackRef->object(),
                                 gPrepareSurfaceControlForWebviewCallback.prepare);
         });
@@ -604,7 +647,7 @@
                 env->NewGlobalRef(frameCallback));
         proxy->setFrameCallback([globalCallbackRef](int32_t syncResult,
                                                     int64_t frameNr) -> std::function<void(bool)> {
-            JNIEnv* env = GraphicsJNI::getJNIEnv();
+            JNIEnv* env = getenv(globalCallbackRef->vm());
             ScopedLocalRef<jobject> frameCommitCallback(
                     env, env->CallObjectMethod(
                                  globalCallbackRef->object(), gFrameDrawingCallback.onFrameDraw,
@@ -643,7 +686,7 @@
         auto globalCallbackRef =
                 std::make_shared<JGlobalRefHolder>(vm, env->NewGlobalRef(callback));
         proxy->setFrameCompleteCallback([globalCallbackRef]() {
-            JNIEnv* env = GraphicsJNI::getJNIEnv();
+            JNIEnv* env = getenv(globalCallbackRef->vm());
             env->CallVoidMethod(globalCallbackRef->object(),
                                 gFrameCompleteCallback.onFrameComplete);
         });
@@ -656,7 +699,7 @@
             : CopyRequest(srcRect), mRefHolder(vm, jCopyRequest) {}
 
     virtual SkBitmap getDestinationBitmap(int srcWidth, int srcHeight) override {
-        JNIEnv* env = GraphicsJNI::getJNIEnv();
+        JNIEnv* env = getenv(mRefHolder.vm());
         jlong bitmapPtr = env->CallLongMethod(
                 mRefHolder.object(), gCopyRequest.getDestinationBitmap, srcWidth, srcHeight);
         SkBitmap bitmap;
@@ -665,7 +708,7 @@
     }
 
     virtual void onCopyFinished(CopyResult result) override {
-        JNIEnv* env = GraphicsJNI::getJNIEnv();
+        JNIEnv* env = getenv(mRefHolder.vm());
         env->CallVoidMethod(mRefHolder.object(), gCopyRequest.onCopyFinished,
                             static_cast<jint>(result));
     }