Flutter Linux Embedder
fl_framebuffer.cc File Reference
#include "fl_framebuffer.h"
#include <epoxy/gl.h>

Go to the source code of this file.

Classes

struct  _FlFramebuffer
 

Functions

static void fl_framebuffer_dispose (GObject *object)
 
static void fl_framebuffer_class_init (FlFramebufferClass *klass)
 
static void fl_framebuffer_init (FlFramebuffer *self)
 
static bool check_supports_implicit_msaa ()
 
static bool check_supports_offscreen_msaa ()
 
static GLuint create_texture (GLint format, size_t width, size_t height)
 
static void attach_depth_stencil (GLuint depth_stencil)
 
FlFramebuffer * fl_framebuffer_new (GLint format, size_t width, size_t height, gboolean depth_stencil)
 
FlFramebuffer * fl_framebuffer_new_multisample (GLint format, size_t width, size_t height, gboolean use_msaa)
 
GLuint fl_framebuffer_get_id (FlFramebuffer *self)
 
GLuint fl_framebuffer_get_texture_id (FlFramebuffer *self)
 
size_t fl_framebuffer_get_width (FlFramebuffer *self)
 
size_t fl_framebuffer_get_height (FlFramebuffer *self)
 

Function Documentation

◆ attach_depth_stencil()

static void attach_depth_stencil ( GLuint  depth_stencil)
static

Definition at line 89 of file fl_framebuffer.cc.

89  {
90  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
91  GL_RENDERBUFFER, depth_stencil);
92  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
93  GL_RENDERBUFFER, depth_stencil);
94 }

Referenced by fl_framebuffer_new(), and fl_framebuffer_new_multisample().

◆ check_supports_implicit_msaa()

static bool check_supports_implicit_msaa ( )
static

Definition at line 62 of file fl_framebuffer.cc.

62  {
63  return epoxy_has_gl_extension("GL_EXT_multisampled_render_to_texture");
64 }

Referenced by fl_framebuffer_new_multisample().

◆ check_supports_offscreen_msaa()

static bool check_supports_offscreen_msaa ( )
static

Definition at line 66 of file fl_framebuffer.cc.

66  {
67  if (epoxy_gl_version() >= 30) {
68  GLint max_samples = 0;
69  glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
70  return max_samples >= 4;
71  }
72  return false;
73 }

Referenced by fl_framebuffer_new_multisample().

◆ create_texture()

static GLuint create_texture ( GLint  format,
size_t  width,
size_t  height 
)
static

Definition at line 75 of file fl_framebuffer.cc.

75  {
76  GLuint texture_id;
77  glGenTextures(1, &texture_id);
78  glBindTexture(GL_TEXTURE_2D, texture_id);
79  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
80  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
81  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
82  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
83  glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
84  GL_UNSIGNED_BYTE, nullptr);
85  glBindTexture(GL_TEXTURE_2D, 0);
86  return texture_id;
87 }
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
uint32_t uint32_t * format
int64_t texture_id

References format, height, texture_id, and width.

Referenced by fl_framebuffer_new(), and fl_framebuffer_new_multisample().

◆ fl_framebuffer_class_init()

static void fl_framebuffer_class_init ( FlFramebufferClass *  klass)
static

Definition at line 56 of file fl_framebuffer.cc.

56  {
57  G_OBJECT_CLASS(klass)->dispose = fl_framebuffer_dispose;
58 }
static void fl_framebuffer_dispose(GObject *object)

References fl_framebuffer_dispose().

◆ fl_framebuffer_dispose()

static void fl_framebuffer_dispose ( GObject *  object)
static

Definition at line 33 of file fl_framebuffer.cc.

33  {
34  FlFramebuffer* self = FL_FRAMEBUFFER(object);
35 
36  if (self->framebuffer_id != 0) {
37  glDeleteFramebuffers(1, &self->framebuffer_id);
38  self->framebuffer_id = 0;
39  }
40  if (self->texture_id != 0) {
41  glDeleteTextures(1, &self->texture_id);
42  self->texture_id = 0;
43  }
44  if (self->color_renderbuffer != 0) {
45  glDeleteRenderbuffers(1, &self->color_renderbuffer);
46  self->color_renderbuffer = 0;
47  }
48  if (self->depth_stencil != 0) {
49  glDeleteRenderbuffers(1, &self->depth_stencil);
50  self->depth_stencil = 0;
51  }
52 
53  G_OBJECT_CLASS(fl_framebuffer_parent_class)->dispose(object);
54 }

Referenced by fl_framebuffer_class_init().

◆ fl_framebuffer_get_height()

size_t fl_framebuffer_get_height ( FlFramebuffer *  framebuffer)

fl_framebuffer_get_height: @framebuffer: an #FlFramebuffer.

Gets the height of the framebuffer in pixels.

Returns: height in pixels.

Definition at line 206 of file fl_framebuffer.cc.

206  {
207  return self->height;
208 }

Referenced by composite_layer(), fl_opengl_frame_composite(), fl_opengl_frame_draw(), and fl_opengl_frame_get_size().

◆ fl_framebuffer_get_id()

GLuint fl_framebuffer_get_id ( FlFramebuffer *  framebuffer)

fl_framebuffer_get_id: @framebuffer: an #FlFramebuffer.

Gets the ID for this framebuffer.

Returns: OpenGL framebuffer id or 0 if creation failed.

Definition at line 194 of file fl_framebuffer.cc.

194  {
195  return self->framebuffer_id;
196 }

Referenced by create_opengl_backing_store(), fl_compositor_opengl_composite_layers(), fl_opengl_frame_composite(), and TEST_F().

◆ fl_framebuffer_get_texture_id()

GLuint fl_framebuffer_get_texture_id ( FlFramebuffer *  framebuffer)

fl_framebuffer_get_texture_id: @framebuffer: an #FlFramebuffer.

Gets the ID of the texture associated with this framebuffer.

Returns: OpenGL texture id or 0 if creation failed.

Definition at line 198 of file fl_framebuffer.cc.

198  {
199  return self->texture_id;
200 }

Referenced by composite_layer(), create_opengl_backing_store(), and TEST_F().

◆ fl_framebuffer_get_width()

size_t fl_framebuffer_get_width ( FlFramebuffer *  framebuffer)

fl_framebuffer_get_width: @framebuffer: an #FlFramebuffer.

Gets the width of the framebuffer in pixels.

Returns: width in pixels.

Definition at line 202 of file fl_framebuffer.cc.

202  {
203  return self->width;
204 }

Referenced by composite_layer(), fl_opengl_frame_composite(), fl_opengl_frame_draw(), and fl_opengl_frame_get_size().

◆ fl_framebuffer_init()

static void fl_framebuffer_init ( FlFramebuffer *  self)
static

Definition at line 60 of file fl_framebuffer.cc.

60 {}

◆ fl_framebuffer_new()

FlFramebuffer* fl_framebuffer_new ( GLint  format,
size_t  width,
size_t  height,
gboolean  depth_stencil 
)

FlFramebuffer:

#FlFramebuffer creates framebuffers and their backing textures for use by the Flutter compositor. fl_framebuffer_new: @format: format, e.g. GL_RGB, GL_BGR @width: width of texture. @height: height of texture. @depth_stencil: TRUE to attach a depth/stencil buffer, which is required to render into this framebuffer.

Creates a new frame buffer. Requires a valid OpenGL context to create.

Returns: a new #FlFramebuffer.

Definition at line 96 of file fl_framebuffer.cc.

99  {
100  FlFramebuffer* self =
101  FL_FRAMEBUFFER(g_object_new(fl_framebuffer_get_type(), nullptr));
102 
103  self->width = width;
104  self->height = height;
105 
106  glGenFramebuffers(1, &self->framebuffer_id);
107  glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffer_id);
108 
109  self->texture_id = create_texture(format, width, height);
110 
111  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
112  self->texture_id, 0);
113 
114  if (depth_stencil) {
115  glGenRenderbuffers(1, &self->depth_stencil);
116  glBindRenderbuffer(GL_RENDERBUFFER, self->depth_stencil);
117  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
118  attach_depth_stencil(self->depth_stencil);
119  }
120 
121  return self;
122 }
static void attach_depth_stencil(GLuint depth_stencil)
static GLuint create_texture(GLint format, size_t width, size_t height)

References attach_depth_stencil(), create_texture(), format, height, and width.

Referenced by fl_opengl_frame_composite(), and TEST_F().

◆ fl_framebuffer_new_multisample()

FlFramebuffer* fl_framebuffer_new_multisample ( GLint  format,
size_t  width,
size_t  height,
gboolean  use_msaa 
)

fl_framebuffer_new_multisample: @format: texture format, e.g. GL_RGBA, GL_BGRA_EXT. @width: width of framebuffer in pixels. @height: height of framebuffer in pixels. @use_msaa: TRUE to enable multisample anti-aliasing (MSAA) attachments.

Creates a new frame buffer, configuring MSAA attachments if supported and @use_msaa is TRUE.

When @use_msaa is TRUE, the framebuffer will configure 4x MSAA attachments using either implicit MSAA (multisampled texture via GL_EXT_multisampled_render_to_texture) with the requested @format or offscreen MSAA (multisample color renderbuffer). If offscreen MSAA is used, the framebuffer is backed by a GL_RGBA8 renderbuffer instead of a texture (fl_framebuffer_get_texture_id() returns 0) and must be resolved into a single-sample texture via glBlitFramebuffer before compositing.

If @use_msaa is FALSE or MSAA is not supported by the OpenGL context, a standard single-sample texture with @format and depth/stencil renderbuffer are created.

Returns: a new #FlFramebuffer.

Definition at line 124 of file fl_framebuffer.cc.

127  {
128  FlFramebuffer* self =
129  FL_FRAMEBUFFER(g_object_new(fl_framebuffer_get_type(), nullptr));
130 
131  self->width = width;
132  self->height = height;
133 
134  glGenFramebuffers(1, &self->framebuffer_id);
135  glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffer_id);
136 
137  if (use_msaa) {
139  // Implicit MSAA uses GL_EXT_multisampled_render_to_texture, where the
140  // OpenGL driver automatically resolves multisamples into the attached
141  // texture when rendering is completed.
142  self->texture_id = create_texture(format, width, height);
143  glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
144  GL_TEXTURE_2D, self->texture_id, 0,
145  4);
146 
147  glGenRenderbuffers(1, &self->depth_stencil);
148  glBindRenderbuffer(GL_RENDERBUFFER, self->depth_stencil);
149  glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, 4,
150  GL_DEPTH24_STENCIL8, width, height);
151  attach_depth_stencil(self->depth_stencil);
152  } else if (check_supports_offscreen_msaa()) {
153  // Offscreen MSAA uses a multisample renderbuffer instead of a texture.
154  // The multisample resolve occurs in fl_compositor_opengl_composite_layers
155  // via glBlitFramebuffer (or composite_layer) into the compositor's
156  // single-sample framebuffer texture.
157  glGenRenderbuffers(1, &self->color_renderbuffer);
158  glBindRenderbuffer(GL_RENDERBUFFER, self->color_renderbuffer);
159  glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, width,
160  height);
161  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
162  GL_RENDERBUFFER, self->color_renderbuffer);
163 
164  glGenRenderbuffers(1, &self->depth_stencil);
165  glBindRenderbuffer(GL_RENDERBUFFER, self->depth_stencil);
166  glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8,
167  width, height);
168  attach_depth_stencil(self->depth_stencil);
169  } else {
170  self->texture_id = create_texture(format, width, height);
171  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
172  GL_TEXTURE_2D, self->texture_id, 0);
173 
174  glGenRenderbuffers(1, &self->depth_stencil);
175  glBindRenderbuffer(GL_RENDERBUFFER, self->depth_stencil);
176  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width,
177  height);
178  attach_depth_stencil(self->depth_stencil);
179  }
180  } else {
181  self->texture_id = create_texture(format, width, height);
182  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
183  self->texture_id, 0);
184 
185  glGenRenderbuffers(1, &self->depth_stencil);
186  glBindRenderbuffer(GL_RENDERBUFFER, self->depth_stencil);
187  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
188  attach_depth_stencil(self->depth_stencil);
189  }
190 
191  return self;
192 }
static bool check_supports_offscreen_msaa()
static bool check_supports_implicit_msaa()

References attach_depth_stencil(), check_supports_implicit_msaa(), check_supports_offscreen_msaa(), create_texture(), format, height, and width.

Referenced by create_opengl_backing_store(), and TEST_F().