2
2
3
3
#include < memory>
4
4
5
+ #include " OpenGLContext.h"
6
+
5
7
#pragma clang diagnostic push
6
8
#pragma clang diagnostic ignored "-Wdocumentation"
7
9
@@ -20,33 +22,41 @@ RNSkOpenGLCanvasProvider::RNSkOpenGLCanvasProvider(
20
22
RNSkOpenGLCanvasProvider::~RNSkOpenGLCanvasProvider () {}
21
23
22
24
float RNSkOpenGLCanvasProvider::getScaledWidth () {
23
- return _surfaceHolder ? _surfaceHolder->getWidth () : 0 ;
25
+ if (_surfaceHolder) {
26
+ return static_cast <float >(_surfaceHolder->getWidth ());
27
+ }
28
+ return 0 ;
24
29
}
25
30
26
31
float RNSkOpenGLCanvasProvider::getScaledHeight () {
27
- return _surfaceHolder ? _surfaceHolder->getHeight () : 0 ;
32
+ if (_surfaceHolder) {
33
+ return static_cast <float >(_surfaceHolder->getHeight ());
34
+ }
35
+ return 0 ;
28
36
}
29
37
30
38
bool RNSkOpenGLCanvasProvider::renderToCanvas (
31
39
const std::function<void (SkCanvas *)> &cb) {
32
-
40
+ JNIEnv *env = facebook::jni::Environment::current ();
33
41
if (_surfaceHolder != nullptr && cb != nullptr ) {
34
42
// Get the surface
35
43
auto surface = _surfaceHolder->getSurface ();
36
- if (surface) {
37
-
38
- // Ensure we are ready to render
39
- if (!_surfaceHolder->makeCurrent ()) {
40
- return false ;
41
- }
42
- _surfaceHolder->updateTexImage ();
44
+ env->CallVoidMethod (_jSurfaceTexture, _updateTexImageMethod);
43
45
46
+ // Check for exceptions
47
+ if (env->ExceptionCheck ()) {
48
+ RNSkLogger::logToConsole (" updateAndRelease() failed. The exception above "
49
+ " can safely be ignored" );
50
+ env->ExceptionClear ();
51
+ }
52
+ if (surface) {
44
53
// Draw into canvas using callback
45
54
cb (surface->getCanvas ());
46
55
47
56
// Swap buffers and show on screen
48
- return _surfaceHolder->present ();
57
+ _surfaceHolder->present ();
49
58
59
+ return true ;
50
60
} else {
51
61
// the render context did not provide a surface
52
62
return false ;
@@ -56,11 +66,31 @@ bool RNSkOpenGLCanvasProvider::renderToCanvas(
56
66
return false ;
57
67
}
58
68
59
- void RNSkOpenGLCanvasProvider::surfaceAvailable (jobject surface, int width ,
60
- int height) {
69
+ void RNSkOpenGLCanvasProvider::surfaceAvailable (jobject jSurfaceTexture ,
70
+ int width, int height) {
61
71
// Create renderer!
72
+ JNIEnv *env = facebook::jni::Environment::current ();
73
+
74
+ _jSurfaceTexture = env->NewGlobalRef (jSurfaceTexture);
75
+ jclass surfaceClass = env->FindClass (" android/view/Surface" );
76
+ jmethodID surfaceConstructor = env->GetMethodID (
77
+ surfaceClass, " <init>" , " (Landroid/graphics/SurfaceTexture;)V" );
78
+ // Create a new Surface instance
79
+ jobject jSurface =
80
+ env->NewObject (surfaceClass, surfaceConstructor, jSurfaceTexture);
81
+
82
+ jclass surfaceTextureClass = env->GetObjectClass (_jSurfaceTexture);
83
+ _updateTexImageMethod =
84
+ env->GetMethodID (surfaceTextureClass, " updateTexImage" , " ()V" );
85
+
86
+ // Acquire the native window from the Surface
87
+ auto window = ANativeWindow_fromSurface (env, jSurface);
88
+ // Clean up local references
89
+ env->DeleteLocalRef (jSurface);
90
+ env->DeleteLocalRef (surfaceClass);
91
+ env->DeleteLocalRef (surfaceTextureClass);
62
92
_surfaceHolder =
63
- SkiaOpenGLSurfaceFactory::makeWindowedSurface (surface , width, height);
93
+ OpenGLContext::getInstance (). MakeWindow (window , width, height);
64
94
65
95
// Post redraw request to ensure we paint in the next draw cycle.
66
96
_requestRedraw ();
@@ -69,6 +99,11 @@ void RNSkOpenGLCanvasProvider::surfaceDestroyed() {
69
99
// destroy the renderer (a unique pointer so the dtor will be called
70
100
// immediately.)
71
101
_surfaceHolder = nullptr ;
102
+ if (_jSurfaceTexture) {
103
+ JNIEnv *env = facebook::jni::Environment::current ();
104
+ env->DeleteGlobalRef (_jSurfaceTexture);
105
+ _jSurfaceTexture = nullptr ;
106
+ }
72
107
}
73
108
74
109
void RNSkOpenGLCanvasProvider::surfaceSizeChanged (int width, int height) {
0 commit comments