Skip to content

Commit 21a1d72

Browse files
authored
Merge pull request #44 from soreau/wf-background-gl
background: Optimize texture uploading by only calling glTexImage2D once
2 parents 5beaefa + ea65bb6 commit 21a1d72

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

src/background/background.cpp

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,11 @@ void BackgroundGLArea::show_image(Glib::RefPtr<BackgroundImage> next_image)
229229
to_image = next_image;
230230

231231
int width, height;
232-
if (from_image)
233-
{
234-
from_image->generate_adjustments(background->window_width, background->window_height);
235-
width = from_image->source->get_width();
236-
height = from_image->source->get_height();
237-
glBindTexture(GL_TEXTURE_2D, from_tex);
238-
auto format = from_image->source->get_has_alpha() ? GL_RGBA : GL_RGB;
239-
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE,
240-
from_image->source->get_pixels());
241-
}
242232

243233
to_image->generate_adjustments(background->window_width, background->window_height);
244234
width = to_image->source->get_width();
245235
height = to_image->source->get_height();
246-
glBindTexture(GL_TEXTURE_2D, to_tex);
236+
glBindTexture(GL_TEXTURE_2D, to_image->tex_id);
247237
auto format = to_image->source->get_has_alpha() ? GL_RGBA : GL_RGB;
248238
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE,
249239
to_image->source->get_pixels());
@@ -258,6 +248,31 @@ void BackgroundGLArea::show_image(Glib::RefPtr<BackgroundImage> next_image)
258248
this->queue_draw();
259249
}
260250

251+
static GLuint create_texture()
252+
{
253+
GLuint tex;
254+
255+
glGenTextures(1, &tex);
256+
glBindTexture(GL_TEXTURE_2D, tex);
257+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
258+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
259+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
260+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
261+
glBindTexture(GL_TEXTURE_2D, 0);
262+
263+
return tex;
264+
}
265+
266+
BackgroundImage::BackgroundImage()
267+
{
268+
tex_id = create_texture();
269+
}
270+
271+
BackgroundImage::~BackgroundImage()
272+
{
273+
glDeleteTextures(1, &tex_id);
274+
}
275+
261276
Glib::RefPtr<BackgroundImage> WayfireBackground::create_from_file_safe(std::string path)
262277
{
263278
Glib::RefPtr<BackgroundImage> image = Glib::RefPtr<BackgroundImage>(new BackgroundImage());
@@ -451,27 +466,10 @@ void WayfireBackground::reset_cycle_timeout()
451466
}
452467
}
453468

454-
static GLuint create_texture()
455-
{
456-
GLuint tex;
457-
458-
glGenTextures(1, &tex);
459-
glBindTexture(GL_TEXTURE_2D, tex);
460-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
461-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
462-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
463-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
464-
glBindTexture(GL_TEXTURE_2D, 0);
465-
466-
return tex;
467-
}
468-
469469
void BackgroundGLArea::realize()
470470
{
471471
this->make_current();
472472
program = init_shaders();
473-
from_tex = create_texture();
474-
to_tex = create_texture();
475473
}
476474

477475
bool BackgroundGLArea::render(const Glib::RefPtr<Gdk::GLContext>& context)
@@ -509,12 +507,15 @@ bool BackgroundGLArea::render(const Glib::RefPtr<Gdk::GLContext>& context)
509507
glClearColor(0.0, 0.0, 0.0, 1.0);
510508
glClear(GL_COLOR_BUFFER_BIT);
511509
glUseProgram(program);
512-
glActiveTexture(GL_TEXTURE0);
513-
glBindTexture(GL_TEXTURE_2D, from_tex);
514-
GLuint from_tex_uniform = glGetUniformLocation(program, "bg_texture_from");
515-
glUniform1i(from_tex_uniform, 0);
510+
if (from_image)
511+
{
512+
glActiveTexture(GL_TEXTURE0);
513+
glBindTexture(GL_TEXTURE_2D, from_image->tex_id);
514+
GLuint from_tex_uniform = glGetUniformLocation(program, "bg_texture_from");
515+
glUniform1i(from_tex_uniform, 0);
516+
}
516517
glActiveTexture(GL_TEXTURE1);
517-
glBindTexture(GL_TEXTURE_2D, to_tex);
518+
glBindTexture(GL_TEXTURE_2D, to_image->tex_id);
518519
GLuint to_tex_uniform = glGetUniformLocation(program, "bg_texture_to");
519520
glUniform1i(to_tex_uniform, 1);
520521
glEnableVertexAttribArray(0);

src/background/background.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ class BackgroundImageAdjustments
2121
class BackgroundImage
2222
{
2323
public:
24+
BackgroundImage();
25+
~BackgroundImage();
2426
Glib::RefPtr<Gdk::Pixbuf> source;
2527
std::string fill_type;
2628
Glib::RefPtr<BackgroundImageAdjustments> adjustments;
2729

2830
void generate_adjustments(int width, int height);
31+
GLuint tex_id = 0;
2932
};
3033

3134
class BackgroundGLArea : public Gtk::GLArea
@@ -42,8 +45,6 @@ class BackgroundGLArea : public Gtk::GLArea
4245
* pbuf2 is the image from which we are fading. x and y
4346
* are used as offsets when preserve aspect is set. */
4447
Glib::RefPtr<BackgroundImage> to_image, from_image;
45-
GLuint from_tex = 0;
46-
GLuint to_tex = 0;
4748

4849
public:
4950
BackgroundGLArea(WayfireBackground *background);

0 commit comments

Comments
 (0)