Skip to content

Commit

Permalink
🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Nov 5, 2024
1 parent ca3c3c3 commit e548dfb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 1 addition & 2 deletions packages/skia/android/cpp/rnskia-android/OpenGLContext.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#pragma once

#include "SkiaOpenGLSurfaceFactory.h"
#include "WindowContext.h"

Expand All @@ -11,6 +9,7 @@ class OpenGLContext {
public:
OpenGLContext(const OpenGLContext &) = delete;
OpenGLContext &operator=(const OpenGLContext &) = delete;
// TODO: ANativeWindow_release(_window);

static OpenGLContext &getInstance() {
static thread_local OpenGLContext instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,25 @@ float RNSkOpenGLCanvasProvider::getScaledHeight() {

bool RNSkOpenGLCanvasProvider::renderToCanvas(
const std::function<void(SkCanvas *)> &cb) {

JNIEnv *env = facebook::jni::Environment::current();
if (_surfaceHolder != nullptr && cb != nullptr) {
// Get the surface
auto surface = _surfaceHolder->getSurface();
env->CallVoidMethod(_jSurfaceTexture, _updateTexImageMethod);

// Check for exceptions
if (env->ExceptionCheck()) {
RNSkLogger::logToConsole("updateAndRelease() failed. The exception above "
"can safely be ignored");
env->ExceptionClear();
}
if (surface) {
// Draw into canvas using callback
cb(surface->getCanvas());

// Swap buffers and show on screen
_surfaceHolder->present();

return true;
} else {
// the render context did not provide a surface
Expand All @@ -61,9 +70,8 @@ void RNSkOpenGLCanvasProvider::surfaceAvailable(jobject jSurfaceTexture,
int width, int height) {
// Create renderer!
JNIEnv *env = facebook::jni::Environment::current();
// TODO: clean global Ref
// env->DeleteGlobalRef(_jSurfaceTexture);
auto _jSurfaceTexture = env->NewGlobalRef(jSurfaceTexture);

_jSurfaceTexture = env->NewGlobalRef(jSurfaceTexture);
jclass surfaceClass = env->FindClass("android/view/Surface");
jmethodID surfaceConstructor = env->GetMethodID(
surfaceClass, "<init>", "(Landroid/graphics/SurfaceTexture;)V");
Expand All @@ -72,9 +80,8 @@ void RNSkOpenGLCanvasProvider::surfaceAvailable(jobject jSurfaceTexture,
env->NewObject(surfaceClass, surfaceConstructor, jSurfaceTexture);

jclass surfaceTextureClass = env->GetObjectClass(_jSurfaceTexture);
// TODO: use in present()
// auto _updateTexImageMethod =
// env->GetMethodID(surfaceTextureClass, "updateTexImage", "()V");
_updateTexImageMethod =
env->GetMethodID(surfaceTextureClass, "updateTexImage", "()V");

// Acquire the native window from the Surface
auto window = ANativeWindow_fromSurface(env, jSurface);
Expand All @@ -92,6 +99,11 @@ void RNSkOpenGLCanvasProvider::surfaceDestroyed() {
// destroy the renderer (a unique pointer so the dtor will be called
// immediately.)
_surfaceHolder = nullptr;
if (_jSurfaceTexture) {
JNIEnv *env = facebook::jni::Environment::current();
env->DeleteGlobalRef(_jSurfaceTexture);
_jSurfaceTexture = nullptr;
}
}

void RNSkOpenGLCanvasProvider::surfaceSizeChanged(int width, int height) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ class RNSkOpenGLCanvasProvider
private:
std::unique_ptr<WindowContext> _surfaceHolder = nullptr;
std::shared_ptr<RNSkPlatformContext> _platformContext;
jobject _jSurfaceTexture = nullptr;
jmethodID _updateTexImageMethod = nullptr;
};
} // namespace RNSkia

0 comments on commit e548dfb

Please sign in to comment.