Flutter Linux Embedder
fl_framebuffer.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_framebuffer.h"
6 
7 #include <epoxy/gl.h>
8 
9 struct _FlFramebuffer {
10  GObject parent_instance;
11 
12  // Width of framebuffer in pixels.
13  size_t width;
14 
15  // Height of framebuffer in pixels.
16  size_t height;
17 
18  // Framebuffer ID.
20 
21  // Texture backing framebuffer.
22  GLuint texture_id;
23 
24  // Color renderbuffer backing framebuffer (if using offscreen MSAA).
26 
27  // Depth and stencil renderbuffer associated with this framebuffer.
28  GLuint depth_stencil;
29 };
30 
31 G_DEFINE_TYPE(FlFramebuffer, fl_framebuffer, G_TYPE_OBJECT)
32 
33 static void fl_framebuffer_dispose(GObject* object) {
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 }
55 
56 static void fl_framebuffer_class_init(FlFramebufferClass* klass) {
57  G_OBJECT_CLASS(klass)->dispose = fl_framebuffer_dispose;
58 }
59 
60 static void fl_framebuffer_init(FlFramebuffer* self) {}
61 
63  return epoxy_has_gl_extension("GL_EXT_multisampled_render_to_texture");
64 }
65 
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 }
74 
75 static GLuint create_texture(GLint format, size_t width, size_t height) {
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 }
88 
89 static void attach_depth_stencil(GLuint depth_stencil) {
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 }
95 
96 FlFramebuffer* fl_framebuffer_new(GLint format,
97  size_t width,
98  size_t height,
99  gboolean depth_stencil) {
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 }
123 
125  size_t width,
126  size_t height,
127  gboolean use_msaa) {
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 }
193 
194 GLuint fl_framebuffer_get_id(FlFramebuffer* self) {
195  return self->framebuffer_id;
196 }
197 
198 GLuint fl_framebuffer_get_texture_id(FlFramebuffer* self) {
199  return self->texture_id;
200 }
201 
202 size_t fl_framebuffer_get_width(FlFramebuffer* self) {
203  return self->width;
204 }
205 
206 size_t fl_framebuffer_get_height(FlFramebuffer* self) {
207  return self->height;
208 }
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
size_t fl_framebuffer_get_height(FlFramebuffer *self)
static void fl_framebuffer_init(FlFramebuffer *self)
GLuint fl_framebuffer_get_id(FlFramebuffer *self)
static void fl_framebuffer_class_init(FlFramebufferClass *klass)
static void fl_framebuffer_dispose(GObject *object)
static bool check_supports_offscreen_msaa()
static bool check_supports_implicit_msaa()
size_t fl_framebuffer_get_width(FlFramebuffer *self)
static void attach_depth_stencil(GLuint depth_stencil)
GLuint fl_framebuffer_get_texture_id(FlFramebuffer *self)
FlFramebuffer * fl_framebuffer_new(GLint format, size_t width, size_t height, gboolean depth_stencil)
static GLuint create_texture(GLint format, size_t width, size_t height)
FlFramebuffer * fl_framebuffer_new_multisample(GLint format, size_t width, size_t height, gboolean use_msaa)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
uint32_t uint32_t * format
GObject parent_instance
GLuint color_renderbuffer
int64_t texture_id