Skip to content

Refactor: Views should share shaders#185335

Open
DylanO9 wants to merge 1 commit into
flutter:masterfrom
DylanO9:linux-share-view-shaders
Open

Refactor: Views should share shaders#185335
DylanO9 wants to merge 1 commit into
flutter:masterfrom
DylanO9:linux-share-view-shaders

Conversation

@DylanO9

@DylanO9 DylanO9 commented Apr 21, 2026

Copy link
Copy Markdown

This change moves shader creation and management from individual compositors to opengl_manager to enable sharing across views.

Previously, each view’s compositor created and managed its own shaders, resulting in redundant instantiation. This change centralizes shader ownership in opengl_manager, allowing compositors to reuse a single shared shader and access its properties.

Fixes #183911

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Refactor: Views should share shaders

Cleanup: add explicit variables for scale and offset locations

Refactor: line-length formatting fix
@DylanO9 DylanO9 requested a review from a team as a code owner April 21, 2026 09:52
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-linux Building on or for Linux specifically a: desktop Running on desktop team-linux Owned by the Linux platform team labels Apr 21, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Linux platform shell to move OpenGL shader management from FlCompositorOpenGL to FlOpenGLManager, enabling shader sharing across multiple compositors. Feedback identifies a logic error in the vertical offset calculation where width was used instead of height, and potential precision issues due to integer division in scale calculations. It is also recommended to use GLint for uniform locations to correctly handle error states, cache attribute locations to improve rendering performance, and include input validation in the new public getter functions for better robustness.

Comment on lines +56 to +61
GLuint offset_location =
fl_opengl_manager_get_offset_location(self->opengl_manager);
GLuint scale_location =
fl_opengl_manager_get_scale_location(self->opengl_manager);
glUniform2f(offset_location, (2 * x / width) - 1.0, (2 * y / width) - 1.0);
glUniform2f(scale_location, texture_width / width, texture_height / height);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are several issues in this block:

  1. offset_location and scale_location should be GLint to match the return type of glGetUniformLocation (and the proposed change to the manager's getters).
  2. The vertical offset calculation uses width instead of height. This will cause incorrect positioning if the window is not square.
  3. The scale calculation performs integer division because texture_width (size_t) and width (int) are both integral types. This will truncate the scale factor, potentially resulting in 0 (making the layer invisible) or incorrect sizes for non-integer scales. Use floating-point division instead.
  GLint offset_location =
      fl_opengl_manager_get_offset_location(self->opengl_manager);
  GLint scale_location =
      fl_opengl_manager_get_scale_location(self->opengl_manager);
  glUniform2f(offset_location, (2 * x / width) - 1.0, (2 * y / height) - 1.0);
  glUniform2f(scale_location, static_cast<float>(texture_width) / width,
              static_cast<float>(texture_height) / height);

Comment thread engine/src/flutter/shell/platform/linux/fl_compositor_opengl.cc
Comment thread engine/src/flutter/shell/platform/linux/fl_opengl_manager.h
*
* Returns: the location of the scale uniform.
*/
GLuint fl_opengl_manager_get_scale_location(FlOpenGLManager* manager);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

glGetUniformLocation returns a GLint, not a GLuint. See the comment for fl_opengl_manager_get_offset_location.

GLint fl_opengl_manager_get_scale_location(FlOpenGLManager* manager);

Comment thread engine/src/flutter/shell/platform/linux/fl_opengl_manager.cc
Comment on lines +170 to +172
GLuint fl_opengl_manager_get_program(FlOpenGLManager* self) {
return self->program;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Public getter functions should validate the input self using g_return_val_if_fail to ensure robustness. This should be applied to all the new getter functions.

GLuint fl_opengl_manager_get_program(FlOpenGLManager* self) {
  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), 0);
  return self->program;
}

Comment on lines +174 to +176
GLuint fl_opengl_manager_get_vertex_buffer(FlOpenGLManager* self) {
return self->vertex_buffer;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Public getter functions should validate the input self using g_return_val_if_fail to ensure robustness.

GLuint fl_opengl_manager_get_vertex_buffer(FlOpenGLManager* self) {
  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), 0);
  return self->vertex_buffer;
}

Comment on lines +178 to +180
GLuint fl_opengl_manager_get_offset_location(FlOpenGLManager* self) {
return self->offset_location;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The return type should be GLint to match glGetUniformLocation. Also, add validation for the input parameter.

GLint fl_opengl_manager_get_offset_location(FlOpenGLManager* self) {
  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), -1);
  return self->offset_location;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about the GLuint, but yes, input should be validated.

Comment on lines +182 to +184
GLuint fl_opengl_manager_get_scale_location(FlOpenGLManager* self) {
return self->scale_location;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The return type should be GLint to match glGetUniformLocation. Also, add validation for the input parameter.

GLint fl_opengl_manager_get_scale_location(FlOpenGLManager* self) {
  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), -1);
  return self->scale_location;
}

@robert-ancell robert-ancell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. This looks generally correct, but some small things need fixing as pointed out by Gemini. There's also some existing bugs there (GLuint instead of GLint) which would be nice to fix, but these can be done in another PR. The API to FlOpenGLManager has got more complex, instead please make gl_opengl_manager_get_shader() that returns a new object to keep it simpler.

@robert-ancell

Copy link
Copy Markdown
Contributor

See #188143, #188145 and #188144 for changes that have come out of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop engine flutter/engine related. See also e: labels. platform-linux Building on or for Linux specifically team-linux Owned by the Linux platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Share view shaders in Linux embedder

2 participants