Flutter Linux Embedder
fl_egl_image.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "fl_egl_image.h"
6 
7 #include <epoxy/egl.h>
8 #include <epoxy/gl.h>
9 
10 struct _FlEGLImage {
11  GObject parent_instance;
12 
13  EGLImage image;
14 };
15 
16 static EGLImage create_egl_image(GLuint texture_id) {
17  EGLDisplay egl_display = eglGetCurrentDisplay();
18  if (egl_display == EGL_NO_DISPLAY) {
19  g_warning("Failed to create EGL image: Failed to get current EGL display");
20  return EGL_NO_IMAGE_KHR;
21  }
22 
23  EGLContext egl_context = eglGetCurrentContext();
24  if (egl_context == EGL_NO_CONTEXT) {
25  g_warning("Failed to create EGL image: Failed to get current EGL context");
26  return EGL_NO_IMAGE_KHR;
27  }
28 
29  return eglCreateImageKHR(
30  egl_display, egl_context, EGL_GL_TEXTURE_2D,
31  reinterpret_cast<EGLClientBuffer>(static_cast<intptr_t>(texture_id)),
32  nullptr);
33 }
34 
35 G_DEFINE_TYPE(FlEGLImage, fl_egl_image, G_TYPE_OBJECT)
36 
37 static void fl_egl_image_dispose(GObject* object) {
38  FlEGLImage* self = FL_EGL_IMAGE(object);
39 
40  if (self->image != EGL_NO_IMAGE_KHR) {
41  EGLDisplay egl_display = eglGetCurrentDisplay();
42  if (egl_display == EGL_NO_DISPLAY) {
43  g_warning(
44  "Failed to destroy EGL image: Failed to get current EGL display");
45  } else {
46  eglDestroyImageKHR(egl_display, self->image);
47  }
48  }
49 
50  G_OBJECT_CLASS(fl_egl_image_parent_class)->dispose(object);
51 }
52 
53 static void fl_egl_image_class_init(FlEGLImageClass* klass) {
54  G_OBJECT_CLASS(klass)->dispose = fl_egl_image_dispose;
55 }
56 
57 static void fl_egl_image_init(FlEGLImage* self) {}
58 
59 FlEGLImage* fl_egl_image_new(GLuint texture) {
60  FlEGLImage* self =
61  FL_EGL_IMAGE(g_object_new(fl_egl_image_get_type(), nullptr));
62 
63  self->image = create_egl_image(texture);
64 
65  return self;
66 }
67 
68 EGLImage fl_egl_image_get_image(FlEGLImage* image) {
69  g_return_val_if_fail(FL_IS_EGL_IMAGE(image), EGL_NO_IMAGE_KHR);
70  return image->image;
71 }
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
static EGLImage create_egl_image(GLuint texture_id)
Definition: fl_egl_image.cc:16
FlEGLImage * fl_egl_image_new(GLuint texture)
Definition: fl_egl_image.cc:59
static void fl_egl_image_init(FlEGLImage *self)
Definition: fl_egl_image.cc:57
EGLImage fl_egl_image_get_image(FlEGLImage *image)
Definition: fl_egl_image.cc:68
static void fl_egl_image_dispose(GObject *object)
Definition: fl_egl_image.cc:37
static void fl_egl_image_class_init(FlEGLImageClass *klass)
Definition: fl_egl_image.cc:53
GObject parent_instance
Definition: fl_egl_image.cc:11
EGLImage image
Definition: fl_egl_image.cc:13
int64_t texture_id