Skip to content

Commit

Permalink
Remove BackgroundFiller, use glClear in paintEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jun 10, 2021
1 parent 1c00458 commit 098eb59
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 80 deletions.
49 changes: 0 additions & 49 deletions ui/gl/gl_primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,53 +122,4 @@ void FillTexturedRectangle(
f.glDisableVertexAttribArray(texcoord);
}

void BackgroundFiller::init(QOpenGLFunctions &f) {
_bgBuffer.emplace();
_bgBuffer->setUsagePattern(QOpenGLBuffer::DynamicDraw);
_bgBuffer->create();
_bgProgram.emplace();
LinkProgram(
&*_bgProgram,
VertexShader({ VertexViewportTransform() }),
FragmentShader({ FragmentStaticColor() }));
}

void BackgroundFiller::deinit(QOpenGLFunctions &f) {
_bgProgram.reset();
_bgBuffer.reset();
}

void BackgroundFiller::fill(
QOpenGLFunctions &f,
const QRegion &region,
QSize viewport,
float factor,
const QColor &color) {
const auto rgb = color.toRgb();
if (region.isEmpty()) {
return;
} else if (region.end() - region.begin() == 1
&& (*region.begin()).size() == viewport) {
f.glClearColor(rgb.redF(), rgb.greenF(), rgb.blueF(), rgb.alphaF());
f.glClear(GL_COLOR_BUFFER_BIT);
return;
}
_bgTriangles.resize((region.end() - region.begin()) * 12);
auto coords = _bgTriangles.data();
for (const auto rect : region) {
FillRectTriangleVertices(
coords,
TransformRect(rect, viewport, factor));
coords += 12;
}
_bgProgram->bind();
_bgProgram->setUniformValue("viewport", QSizeF(viewport * factor));
FillTriangles(
f,
_bgTriangles,
&*_bgBuffer,
&*_bgProgram,
rgb);
}

} // namespace Ui::GL
28 changes: 0 additions & 28 deletions ui/gl/gl_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,4 @@ void FillTexturedRectangle(
not_null<QOpenGLShaderProgram*> program,
int skipVertices = 0);

class BackgroundFiller final {
public:
void init(QOpenGLFunctions &f);
void deinit(QOpenGLFunctions &);

void fill(
QOpenGLFunctions &f,
const QRegion &region,
QSize viewport,
float factor,
const QColor &color);

void fill(
QOpenGLFunctions &f,
const QRegion &region,
QSize viewport,
float factor,
const style::color &color) {
return fill(f, region, viewport, factor, color->c);
}

private:
std::optional<QOpenGLBuffer> _bgBuffer;
std::optional<QOpenGLShaderProgram> _bgProgram;
std::vector<float> _bgTriangles;

};

} // namespace Ui::GL
20 changes: 17 additions & 3 deletions ui/gl/gl_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SurfaceOpenGL final
private:
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;
void paintEvent(QPaintEvent *e) override;
void callDeInit();

const std::unique_ptr<Renderer> _renderer;
Expand Down Expand Up @@ -78,8 +78,22 @@ void SurfaceOpenGL::resizeGL(int w, int h) {
_renderer->resize(this, *context()->functions(), w, h);
}

void SurfaceOpenGL::paintGL() {
_renderer->paint(this, *context()->functions());
void SurfaceOpenGL::paintEvent(QPaintEvent *e) {
if (!updatesEnabled() || size().isEmpty() || !isValid()) {
return;
}
makeCurrent();
const auto f = context()->functions();
if (const auto bg = _renderer->clearColor()) {
f->glClearColor(bg->redF(), bg->greenF(), bg->blueF(), bg->alphaF());
f->glClear(GL_COLOR_BUFFER_BIT);
}
f->glViewport(
0,
0,
width() * devicePixelRatio(),
height() * devicePixelRatio());
_renderer->paint(this, *f);
}

void SurfaceOpenGL::callDeInit() {
Expand Down
4 changes: 4 additions & 0 deletions ui/gl/gl_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Renderer {
not_null<QOpenGLWidget*> widget,
QOpenGLFunctions &f);

[[nodiscard]] virtual std::optional<QColor> clearColor() {
return std::nullopt;
}

virtual void paintFallback(
Painter &&p,
const QRegion &clip,
Expand Down

0 comments on commit 098eb59

Please sign in to comment.