Flutter Linux Embedder
fl_opengl_frame.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 
6 
7 #include <epoxy/gl.h>
8 
11 
13  GObject parent_instance;
14 
15  // Framebuffer the current frame is composited into.
16  FlFramebuffer* framebuffer;
17 
18  // Copy of the current frame in CPU memory.
19  uint8_t* pixels;
20 };
21 
22 G_DEFINE_TYPE(FlOpenGLFrame, fl_opengl_frame, G_TYPE_OBJECT)
23 
24 static void fl_opengl_frame_dispose(GObject* object) {
25  FlOpenGLFrame* self = FL_OPENGL_FRAME(object);
26 
27  g_clear_object(&self->framebuffer);
28 
29  G_OBJECT_CLASS(fl_opengl_frame_parent_class)->dispose(object);
30 }
31 
32 static void fl_opengl_frame_finalize(GObject* object) {
33  FlOpenGLFrame* self = FL_OPENGL_FRAME(object);
34 
35  g_clear_pointer(&self->pixels, g_free);
36 
37  G_OBJECT_CLASS(fl_opengl_frame_parent_class)->finalize(object);
38 }
39 
40 static void fl_opengl_frame_class_init(FlOpenGLFrameClass* klass) {
41  GObjectClass* object_class = G_OBJECT_CLASS(klass);
42  object_class->dispose = fl_opengl_frame_dispose;
43  object_class->finalize = fl_opengl_frame_finalize;
44 }
45 
46 static void fl_opengl_frame_init(FlOpenGLFrame* self) {}
47 
48 FlOpenGLFrame* fl_opengl_frame_new() {
49  return FL_OPENGL_FRAME(g_object_new(fl_opengl_frame_get_type(), nullptr));
50 }
51 
52 void fl_opengl_frame_composite(FlOpenGLFrame* self,
53  FlCompositorOpenGL* compositor,
54  const FlutterLayer** layers,
55  size_t layers_count) {
56  g_return_if_fail(FL_IS_OPENGL_FRAME(self));
57 
58  size_t width, height;
59  if (layers_count > 0) {
60  width = layers[0]->size.width;
61  height = layers[0]->size.height;
62  } else if (self->framebuffer != nullptr) {
63  // A frame with nothing to rasterize, e.g. everything painted was fully
64  // transparent, has no layers and so no size. Keep the current size so the
65  // frame still matches the window and is drawn (showing the view
66  // background) rather than being skipped - the contents are cleared when
67  // the layers are composited below.
68  width = fl_framebuffer_get_width(self->framebuffer);
69  height = fl_framebuffer_get_height(self->framebuffer);
70  } else {
71  // Nothing has been rendered yet, so there is nothing to clear.
72  return;
73  }
74 
75  if (width == 0 || height == 0) {
76  // A zero-sized layer has no content to show. Drop any existing frame so
77  // the renderer reports no frame rather than an empty framebuffer (a 0x0
78  // framebuffer would otherwise be treated as a valid frame while matching
79  // the "no frame" size of 0x0).
80  g_clear_object(&self->framebuffer);
81  g_clear_pointer(&self->pixels, g_free);
82  return;
83  }
84 
85  // Recreate the framebuffer if the frame size has changed.
86  if (self->framebuffer == nullptr ||
87  fl_framebuffer_get_width(self->framebuffer) != width ||
88  fl_framebuffer_get_height(self->framebuffer) != height) {
89  GLint general_format =
91  g_clear_object(&self->framebuffer);
92  self->framebuffer =
93  fl_framebuffer_new(general_format, width, height, FALSE);
94 
95  // Make a buffer to copy the frame pixels into.
96  self->pixels = g_renew(uint8_t, self->pixels, width * height * 4);
97  }
98 
99  // Bind the target framebuffer so the compositor draws into it, then read the
100  // frame back into CPU memory.
101  GLint saved_draw_framebuffer_binding;
102  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &saved_draw_framebuffer_binding);
103  glBindFramebuffer(GL_DRAW_FRAMEBUFFER,
104  fl_framebuffer_get_id(self->framebuffer));
105 
107 
108  // GTK draws this frame on the main thread using a different OpenGL context
109  // to the one it was rendered with. No explicit synchronization is needed
110  // before that happens because glReadPixels() waits for the rendering to
111  // complete.
112  GLint saved_read_framebuffer_binding;
113  glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &saved_read_framebuffer_binding);
114  glBindFramebuffer(GL_READ_FRAMEBUFFER,
115  fl_framebuffer_get_id(self->framebuffer));
116  glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, self->pixels);
117  glBindFramebuffer(GL_READ_FRAMEBUFFER, saved_read_framebuffer_binding);
118 
119  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, saved_draw_framebuffer_binding);
120 }
121 
122 void fl_opengl_frame_get_size(FlOpenGLFrame* self,
123  size_t* width,
124  size_t* height) {
125  g_return_if_fail(FL_IS_OPENGL_FRAME(self));
126 
127  if (self->framebuffer != nullptr) {
128  *width = fl_framebuffer_get_width(self->framebuffer);
129  *height = fl_framebuffer_get_height(self->framebuffer);
130  } else {
131  *width = 0;
132  *height = 0;
133  }
134 }
135 
136 gboolean fl_opengl_frame_draw(FlOpenGLFrame* self,
137  cairo_t* cr,
138  GdkWindow* window,
139  gint scale_factor,
140  size_t width,
141  size_t height) {
142  g_return_val_if_fail(FL_IS_OPENGL_FRAME(self), FALSE);
143 
144  if (self->framebuffer == nullptr) {
145  return FALSE;
146  }
147 
148  GLint saved_texture_binding;
149  glGetIntegerv(GL_TEXTURE_BINDING_2D, &saved_texture_binding);
150 
151  GLuint texture_id;
152  glGenTextures(1, &texture_id);
153  glBindTexture(GL_TEXTURE_2D, texture_id);
154  GLsizei fb_width =
155  static_cast<GLsizei>(fl_framebuffer_get_width(self->framebuffer));
156  GLsizei fb_height =
157  static_cast<GLsizei>(fl_framebuffer_get_height(self->framebuffer));
158  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fb_width, fb_height, 0, GL_RGBA,
159  GL_UNSIGNED_BYTE, self->pixels);
160 
161  gdk_cairo_draw_from_gl(cr, window, texture_id, GL_TEXTURE, scale_factor, 0, 0,
162  width, height);
163 
164  glDeleteTextures(1, &texture_id);
165 
166  glBindTexture(GL_TEXTURE_2D, saved_texture_binding);
167 
168  glFlush();
169 
170  return TRUE;
171 }
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
void fl_compositor_opengl_composite_layers(FlCompositorOpenGL *self, const FlutterLayer **layers, size_t layers_count)
GLint fl_compositor_opengl_get_frame_format(const FlutterLayer **layers, size_t layers_count)
size_t fl_framebuffer_get_height(FlFramebuffer *self)
GLuint fl_framebuffer_get_id(FlFramebuffer *self)
size_t fl_framebuffer_get_width(FlFramebuffer *self)
FlFramebuffer * fl_framebuffer_new(GLint format, size_t width, size_t height, gboolean depth_stencil)
gboolean fl_opengl_frame_draw(FlOpenGLFrame *self, cairo_t *cr, GdkWindow *window, gint scale_factor, size_t width, size_t height)
static void fl_opengl_frame_class_init(FlOpenGLFrameClass *klass)
static void fl_opengl_frame_init(FlOpenGLFrame *self)
void fl_opengl_frame_get_size(FlOpenGLFrame *self, size_t *width, size_t *height)
static void fl_opengl_frame_finalize(GObject *object)
void fl_opengl_frame_composite(FlOpenGLFrame *self, FlCompositorOpenGL *compositor, const FlutterLayer **layers, size_t layers_count)
FlOpenGLFrame * fl_opengl_frame_new()
static void fl_opengl_frame_dispose(GObject *object)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
const FlutterLayer size_t layers_count
const FlutterLayer ** layers
FlFramebuffer * framebuffer
GObject parent_instance
int64_t texture_id