Skip to content

Commit

Permalink
Support sub-image updating in Ui::GL::Image.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jun 3, 2021
1 parent 8b7aa44 commit 0df1579
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
52 changes: 37 additions & 15 deletions ui/gl/gl_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,52 @@ void Image::invalidate() {
_storage = base::take(_image);
}

void Image::bind(QOpenGLFunctions &f) {
void Image::bind(QOpenGLFunctions &f, QSize subimage) {
Expects(!_image.isNull());
Expects(subimage.width() <= _image.width()
&& subimage.height() <= _image.height());

_textures.ensureCreated(f);
if (subimage.isNull()) {
subimage = _image.size();
}
if (subimage.isEmpty()) {
_textureSize = subimage;
return;
}
const auto cacheKey = _image.cacheKey();
const auto upload = (_cacheKey != cacheKey);
if (upload) {
_cacheKey = cacheKey;
_index = 1 - _index;
}
_textures.bind(f, _index);
const auto error = f.glGetError();
_textures.bind(f, 0);
if (upload) {
f.glPixelStorei(GL_UNPACK_ROW_LENGTH, _image.bytesPerLine() / 4);
f.glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
_image.width(),
_image.height(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
_image.constBits());
if (_textureSize.width() < subimage.width()
|| _textureSize.height() < subimage.height()) {
_textureSize = subimage;
f.glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
subimage.width(),
subimage.height(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
_image.constBits());
} else {
f.glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
subimage.width(),
subimage.height(),
GL_RGBA,
GL_UNSIGNED_BYTE,
_image.constBits());
}
f.glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
}
Expand All @@ -112,7 +134,7 @@ TexturedRect Image::texturedRect(
texture.y() + (visible.y() - geometry.y()) * yFactor,
visible.width() * xFactor,
visible.height() * yFactor);
const auto dimensions = QSizeF(_image.size());
const auto dimensions = QSizeF(_textureSize);
return {
.geometry = Rect(visible),
.texture = Rect(QRectF(
Expand Down
6 changes: 3 additions & 3 deletions ui/gl/gl_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Image final {
[[nodiscard]] QImage takeImage();
void invalidate();

void bind(QOpenGLFunctions &f);
void bind(QOpenGLFunctions &f, QSize subimage = QSize());
void destroy(QOpenGLFunctions &f);

[[nodiscard]] TexturedRect texturedRect(
Expand All @@ -116,9 +116,9 @@ class Image final {
private:
QImage _image;
QImage _storage;
Textures<2> _textures;
Textures<1> _textures;
qint64 _cacheKey = 0;
int _index = 0;
QSize _textureSize;

};

Expand Down

0 comments on commit 0df1579

Please sign in to comment.