@@ -12,6 +12,9 @@ typedef struct {
1212 EGLDisplay egl_display;
1313 EGLSurface egl_surface;
1414 EGLContext egl_context;
15+
16+ EGLSurface resource_surface;
17+ EGLContext resource_context;
1518} FlRendererPrivate;
1619
1720G_DEFINE_TYPE_WITH_PRIVATE (FlRenderer, fl_renderer, G_TYPE_OBJECT)
@@ -55,18 +58,38 @@ static const gchar* get_egl_error() {
5558 }
5659}
5760
61+ // Creates a resource surface.
62+ static void create_resource_surface (FlRenderer* self, EGLConfig config) {
63+ FlRendererPrivate* priv =
64+ static_cast <FlRendererPrivate*>(fl_renderer_get_instance_private (self));
65+
66+ EGLint context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2 , EGL_NONE};
67+ const EGLint resource_context_attribs[] = {EGL_WIDTH, 1 , EGL_HEIGHT, 1 ,
68+ EGL_NONE};
69+ priv->resource_surface = eglCreatePbufferSurface (priv->egl_display , config,
70+ resource_context_attribs);
71+ if (priv->resource_surface != nullptr ) {
72+ g_warning (" Failed to create EGL resource surface: %s" , get_egl_error ());
73+ return ;
74+ }
75+
76+ priv->resource_context = eglCreateContext (
77+ priv->egl_display , config, priv->egl_context , context_attributes);
78+ if (priv->resource_context == nullptr )
79+ g_warning (" Failed to create EGL resource context: %s" , get_egl_error ());
80+ }
81+
5882// Default implementation for the start virtual method.
5983// Provided so subclasses can chain up to here.
6084static gboolean fl_renderer_real_start (FlRenderer* self, GError** error) {
6185 FlRendererPrivate* priv =
6286 static_cast <FlRendererPrivate*>(fl_renderer_get_instance_private (self));
6387
64- // Note the use of EGL_DEFAULT_DISPLAY rather than sharing an existing display
65- // connection (e.g. an X11 connection from GTK). This is because this EGL
66- // display is going to be accessed by a thread from Flutter. In the case
67- // of GTK/X11 the display connection is not thread safe and this would cause
68- // a crash.
69- //
88+ // Note the use of EGL_DEFAULT_DISPLAY rather than sharing an existing
89+ // display connection (e.g. an X11 connection from GTK). This is because
90+ // this EGL display is going to be accessed by a thread from Flutter. In the
91+ // case of GTK/X11 the display connection is not thread safe and this would
92+ // cause a crash.
7093 priv->egl_display = eglGetDisplay (EGL_DEFAULT_DISPLAY);
7194
7295 if (!eglInitialize (priv->egl_display , nullptr , nullptr )) {
@@ -120,6 +143,9 @@ static gboolean fl_renderer_real_start(FlRenderer* self, GError** error) {
120143 " Failed to create EGL context: %s" , get_egl_error ());
121144 return FALSE ;
122145 }
146+
147+ create_resource_surface (self, egl_config);
148+
123149 EGLint value;
124150 eglQueryContext (priv->egl_display , priv->egl_context ,
125151 EGL_CONTEXT_CLIENT_VERSION, &value);
@@ -155,6 +181,23 @@ gboolean fl_renderer_make_current(FlRenderer* self, GError** error) {
155181 return TRUE ;
156182}
157183
184+ gboolean fl_renderer_make_resource_current (FlRenderer* self, GError** error) {
185+ FlRendererPrivate* priv =
186+ static_cast <FlRendererPrivate*>(fl_renderer_get_instance_private (self));
187+
188+ if (priv->resource_surface == nullptr || priv->resource_context == nullptr )
189+ return FALSE ;
190+
191+ if (!eglMakeCurrent (priv->egl_display , priv->resource_surface ,
192+ priv->resource_surface , priv->resource_context )) {
193+ g_set_error (error, fl_renderer_error_quark (), FL_RENDERER_ERROR_FAILED,
194+ " Failed to make EGL context current: %s" , get_egl_error ());
195+ return FALSE ;
196+ }
197+
198+ return TRUE ;
199+ }
200+
158201gboolean fl_renderer_clear_current (FlRenderer* self, GError** error) {
159202 FlRendererPrivate* priv =
160203 static_cast <FlRendererPrivate*>(fl_renderer_get_instance_private (self));
0 commit comments