Skip to content

Commit

Permalink
Use cpp in Cython code to avoid c trampoline, cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
misl6 committed Feb 7, 2024
1 parent 4ad4c07 commit 5bfcbb6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 41 deletions.
21 changes: 9 additions & 12 deletions kivy/graphics/egl_backend/egl_angle_metal.pyx
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
cdef extern from "egl_angle_metal_implem.h":
ctypedef void *metalangle_context_t
ctypedef void *metalangle_nativelayer_t
metalangle_context_t metalangle_create_context(void * nativeMetalLayer)
void metalangle_swap_buffers(metalangle_context_t context)
cdef extern from "egl_angle_metal_implem.mm":
cppclass MetalANGLEGraphicsContext:
MetalANGLEGraphicsContext(void* nativeMetalLayer)
void swapBuffersEGL()


cdef class EGLMetalANGLE:
cdef metalangle_context_t ctx
cdef metalangle_nativelayer_t native_layer
cdef MetalANGLEGraphicsContext* ctx
cdef void* native_layer

def __cinit__(self):
self.native_layer = NULL
self.ctx = NULL

cdef void set_native_layer(self, void * native_layer) except *:
self.native_layer = <metalangle_nativelayer_t>native_layer
self.native_layer = native_layer

def swap_buffers(self):
metalangle_swap_buffers(self.ctx)
self.ctx.swapBuffersEGL()

def create_context(self):
self.ctx = metalangle_create_context(self.native_layer)
self.ctx = new MetalANGLEGraphicsContext(self.native_layer)

def destroy_context(self):
pass


20 changes: 14 additions & 6 deletions kivy/graphics/egl_backend/egl_angle_metal_implem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
#include <EGL/egl.h>
#include <EGL/eglext.h>

class MetalANGLEGraphicsContext
{
public:
MetalANGLEGraphicsContext(void *nativeMetalLayer);
~MetalANGLEGraphicsContext();
void swapBuffersEGL();
void initialiseEGLDisplay();
void initialiseEGLContext();

typedef void *metalangle_context_t;
typedef CAMetalLayer *metalangle_nativelayer_t;

// Methods to easily interact with Cython (The objc++ code is not directly callable from Cython)
metalangle_context_t metalangle_create_context(void * nativeMetalLayer);
void metalangle_swap_buffers(metalangle_context_t context);
private:
void *m_nativeMetalLayer;
EGLContext m_contextObj;
EGLDisplay m_displayObj;
EGLSurface m_surfaceObj;
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
#include "egl_angle_metal_implem.h"


class MetalANGLEGraphicsContext {
public:
MetalANGLEGraphicsContext(void * nativeMetalLayer);
~MetalANGLEGraphicsContext();
void swapBuffersEGL();
void initialiseEGLDisplay();
void initialiseEGLContext();
private:
void * m_nativeMetalLayer;
EGLContext m_contextObj;
EGLDisplay m_displayObj;
EGLSurface m_surfaceObj;
};

MetalANGLEGraphicsContext::MetalANGLEGraphicsContext(void * nativeMetalLayer) {
m_nativeMetalLayer = nativeMetalLayer;
// Initialize the EGL display
Expand Down Expand Up @@ -136,12 +122,4 @@

// Set the context
m_contextObj = context;
}

metalangle_context_t metalangle_create_context(void * nativeMetalLayer) {
return new MetalANGLEGraphicsContext(nativeMetalLayer);
}

void metalangle_swap_buffers(metalangle_context_t context) {
((MetalANGLEGraphicsContext *)context)->swapBuffersEGL();
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ def determine_sdl2():
gl_flags,
{
"extra_compile_args": ["-ObjC++"],
"c_depends": ["graphics/egl_backend/egl_angle_metal_implem.m"],
'language': 'c++',
},
)

Expand Down

0 comments on commit 5bfcbb6

Please sign in to comment.