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

Go to the source code of this file.

Classes

struct  _FlCompositorOpenGLShader
 

Functions

 G_DEFINE_TYPE (FlCompositorOpenGLShader, fl_compositor_opengl_shader, G_TYPE_OBJECT) static gchar *get_shader_log(GLuint shader)
 
static gchar * get_program_log (GLuint program)
 
static void setup_shader (FlCompositorOpenGLShader *self)
 
static void fl_compositor_opengl_shader_dispose (GObject *object)
 
static void fl_compositor_opengl_shader_class_init (FlCompositorOpenGLShaderClass *klass)
 
static void fl_compositor_opengl_shader_init (FlCompositorOpenGLShader *self)
 
FlCompositorOpenGLShader * fl_compositor_opengl_shader_new (FlOpenGLManager *opengl_manager)
 
void fl_compositor_opengl_shader_use (FlCompositorOpenGLShader *self)
 
void fl_compositor_opengl_shader_set_offset (FlCompositorOpenGLShader *self, double x, double y)
 
void fl_compositor_opengl_shader_set_scale (FlCompositorOpenGLShader *self, double x, double y)
 

Variables

static const char * vertex_shader_src
 
static const char * fragment_shader_src
 

Function Documentation

◆ fl_compositor_opengl_shader_class_init()

static void fl_compositor_opengl_shader_class_init ( FlCompositorOpenGLShaderClass *  klass)
static

Definition at line 168 of file fl_compositor_opengl_shader.cc.

169  {
170  G_OBJECT_CLASS(klass)->dispose = fl_compositor_opengl_shader_dispose;
171 }
static void fl_compositor_opengl_shader_dispose(GObject *object)

References fl_compositor_opengl_shader_dispose().

◆ fl_compositor_opengl_shader_dispose()

static void fl_compositor_opengl_shader_dispose ( GObject *  object)
static

Definition at line 143 of file fl_compositor_opengl_shader.cc.

143  {
144  FlCompositorOpenGLShader* self = FL_COMPOSITOR_OPENGL_SHADER(object);
145 
146  if (self->opengl_manager != nullptr) {
147  if (fl_opengl_manager_make_platform_current(self->opengl_manager)) {
148  if (self->program != 0) {
149  glDeleteProgram(self->program);
150  }
151  if (self->vertex_buffer != 0) {
152  glDeleteBuffers(1, &self->vertex_buffer);
153  }
154  } else {
155  g_warning(
156  "Failed to cleanup compositor shaders, unable to make OpenGL context "
157  "current");
158  }
159  }
160  self->program = 0;
161  self->vertex_buffer = 0;
162 
163  g_clear_object(&self->opengl_manager);
164 
165  G_OBJECT_CLASS(fl_compositor_opengl_shader_parent_class)->dispose(object);
166 }
gboolean fl_opengl_manager_make_platform_current(FlOpenGLManager *self)

References fl_opengl_manager_make_platform_current().

Referenced by fl_compositor_opengl_shader_class_init().

◆ fl_compositor_opengl_shader_init()

static void fl_compositor_opengl_shader_init ( FlCompositorOpenGLShader *  self)
static

Definition at line 173 of file fl_compositor_opengl_shader.cc.

173 {}

◆ fl_compositor_opengl_shader_new()

FlCompositorOpenGLShader* fl_compositor_opengl_shader_new ( FlOpenGLManager *  opengl_manager)

Definition at line 175 of file fl_compositor_opengl_shader.cc.

176  {
177  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(opengl_manager), nullptr);
178 
179  FlCompositorOpenGLShader* self = FL_COMPOSITOR_OPENGL_SHADER(
180  g_object_new(fl_compositor_opengl_shader_get_type(), nullptr));
181 
182  self->opengl_manager = FL_OPENGL_MANAGER(g_object_ref(opengl_manager));
183 
184  setup_shader(self);
185 
186  return self;
187 }
G_BEGIN_DECLS FlOpenGLManager * opengl_manager
static void setup_shader(FlCompositorOpenGLShader *self)

References opengl_manager, and setup_shader().

Referenced by fl_compositor_opengl_new().

◆ fl_compositor_opengl_shader_set_offset()

void fl_compositor_opengl_shader_set_offset ( FlCompositorOpenGLShader *  shader,
double  x,
double  y 
)

fl_compositor_opengl_shader_set_offset: @shader: an #FlCompositorOpenGLShader. @x: horizontal offset. @y: vertical offset.

Sets the layer offset uniform. The program must be current (see fl_compositor_opengl_shader_use).

Definition at line 206 of file fl_compositor_opengl_shader.cc.

208  {
209  g_return_if_fail(FL_IS_COMPOSITOR_OPENGL_SHADER(self));
210  glUniform2f(self->offset_location, x, y);
211 }
FlFramebuffer double x
FlFramebuffer double double y

References x, and y.

◆ fl_compositor_opengl_shader_set_scale()

void fl_compositor_opengl_shader_set_scale ( FlCompositorOpenGLShader *  shader,
double  x,
double  y 
)

fl_compositor_opengl_shader_set_scale: @shader: an #FlCompositorOpenGLShader. @x: horizontal scale. @y: vertical scale.

Sets the layer scale uniform. The program must be current (see fl_compositor_opengl_shader_use).

Definition at line 213 of file fl_compositor_opengl_shader.cc.

215  {
216  g_return_if_fail(FL_IS_COMPOSITOR_OPENGL_SHADER(self));
217  glUniform2f(self->scale_location, x, y);
218 }

References x, and y.

◆ fl_compositor_opengl_shader_use()

void fl_compositor_opengl_shader_use ( FlCompositorOpenGLShader *  shader)

fl_compositor_opengl_shader_use: @shader: an #FlCompositorOpenGLShader.

Binds the shader's vertex buffer, configures the vertex attributes and makes the program current. Requires a valid OpenGL context.

Definition at line 189 of file fl_compositor_opengl_shader.cc.

189  {
190  g_return_if_fail(FL_IS_COMPOSITOR_OPENGL_SHADER(self));
191 
192  glBindBuffer(GL_ARRAY_BUFFER, self->vertex_buffer);
193  GLint position_location = glGetAttribLocation(self->program, "position");
194  glEnableVertexAttribArray(position_location);
195  glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE,
196  sizeof(GLfloat) * 4, 0);
197  GLint texcoord_location = glGetAttribLocation(self->program, "in_texcoord");
198  glEnableVertexAttribArray(texcoord_location);
199  glVertexAttribPointer(texcoord_location, 2, GL_FLOAT, GL_FALSE,
200  sizeof(GLfloat) * 4,
201  reinterpret_cast<void*>(sizeof(GLfloat) * 2));
202 
203  glUseProgram(self->program);
204 }

Referenced by fl_compositor_opengl_present_layers().

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlCompositorOpenGLShader  ,
fl_compositor_opengl_shader  ,
G_TYPE_OBJECT   
)

Definition at line 54 of file fl_compositor_opengl_shader.cc.

59  {
60  GLint log_length;
61  gchar* log;
62 
63  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
64 
65  log = static_cast<gchar*>(g_malloc(log_length + 1));
66  glGetShaderInfoLog(shader, log_length, nullptr, log);
67 
68  return log;
69 }

◆ get_program_log()

static gchar* get_program_log ( GLuint  program)
static

Definition at line 72 of file fl_compositor_opengl_shader.cc.

72  {
73  GLint log_length;
74  gchar* log;
75 
76  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
77 
78  log = static_cast<gchar*>(g_malloc(log_length + 1));
79  glGetProgramInfoLog(program, log_length, nullptr, log);
80 
81  return log;
82 }

Referenced by setup_shader().

◆ setup_shader()

static void setup_shader ( FlCompositorOpenGLShader *  self)
static

Definition at line 84 of file fl_compositor_opengl_shader.cc.

84  {
85  if (!fl_opengl_manager_make_platform_current(self->opengl_manager)) {
86  g_warning(
87  "Failed to setup compositor shaders, unable to make OpenGL context "
88  "current");
89  return;
90  }
91 
92  GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
93  glShaderSource(vertex_shader, 1, &vertex_shader_src, nullptr);
94  glCompileShader(vertex_shader);
95  GLint vertex_compile_status;
96  glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
97  if (vertex_compile_status == GL_FALSE) {
98  g_autofree gchar* shader_log = get_shader_log(vertex_shader);
99  g_warning("Failed to compile vertex shader: %s", shader_log);
100  }
101 
102  GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
103  glShaderSource(fragment_shader, 1, &fragment_shader_src, nullptr);
104  glCompileShader(fragment_shader);
105  GLint fragment_compile_status;
106  glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fragment_compile_status);
107  if (fragment_compile_status == GL_FALSE) {
108  g_autofree gchar* shader_log = get_shader_log(fragment_shader);
109  g_warning("Failed to compile fragment shader: %s", shader_log);
110  }
111 
112  self->program = glCreateProgram();
113  glAttachShader(self->program, vertex_shader);
114  glAttachShader(self->program, fragment_shader);
115  glLinkProgram(self->program);
116 
117  GLint link_status;
118  glGetProgramiv(self->program, GL_LINK_STATUS, &link_status);
119  if (link_status == GL_FALSE) {
120  g_autofree gchar* program_log = get_program_log(self->program);
121  g_warning("Failed to link program: %s", program_log);
122  }
123 
124  self->offset_location = glGetUniformLocation(self->program, "offset");
125  self->scale_location = glGetUniformLocation(self->program, "scale");
126 
127  glDeleteShader(vertex_shader);
128  glDeleteShader(fragment_shader);
129 
130  // The uniform square abcd in two triangles cba + cdb
131  // a--b
132  // | |
133  // c--d
134  GLfloat const vertex_data[] = {-1, -1, 0, 0, 1, 1, 1, 1, -1, 1, 0, 1,
135  -1, -1, 0, 0, 1, -1, 1, 0, 1, 1, 1, 1};
136 
137  glGenBuffers(1, &self->vertex_buffer);
138  glBindBuffer(GL_ARRAY_BUFFER, self->vertex_buffer);
139  glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data,
140  GL_STATIC_DRAW);
141 }
static const char * fragment_shader_src
static gchar * get_program_log(GLuint program)
static const char * vertex_shader_src

References fl_opengl_manager_make_platform_current(), fragment_shader_src, get_program_log(), and vertex_shader_src.

Referenced by fl_compositor_opengl_shader_new().

Variable Documentation

◆ fragment_shader_src

const char* fragment_shader_src
static
Initial value:
=
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"\n"
"uniform sampler2D texture;\n"
"varying vec2 texcoord;\n"
"\n"
"void main() {\n"
" gl_FragColor = texture2D(texture, texcoord);\n"
"}\n"

Definition at line 23 of file fl_compositor_opengl_shader.cc.

Referenced by setup_shader().

◆ vertex_shader_src

const char* vertex_shader_src
static
Initial value:
=
"attribute vec2 position;\n"
"attribute vec2 in_texcoord;\n"
"uniform vec2 offset;\n"
"uniform vec2 scale;\n"
"varying vec2 texcoord;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(offset + position * scale, 0, 1);\n"
" texcoord = in_texcoord;\n"
"}\n"

Definition at line 10 of file fl_compositor_opengl_shader.cc.

Referenced by setup_shader().