Skip to content

Commit

Permalink
UIBufferElement new method to ask for a buffer that doesn't change un…
Browse files Browse the repository at this point in the history
…til resized.

This allows some UIBufferElement to keep the content of that buffer and only draw a difference, if they have their own invalidation mechanism.
  • Loading branch information
Guillaume Piolat committed Dec 19, 2024
1 parent 8a75524 commit 0a1a10f
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions gui/dplug/gui/bufferedelement.d
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ nothrow:
_depthOpacityBuf.destroyFree();
_materialOpacityBuf.destroyFree();
}

override void setDirty(box2i rect, UILayer layer = UILayer.guessFromFlags) nothrow @nogc
{
super.setDirty(rect, layer);
Expand Down Expand Up @@ -169,7 +169,18 @@ nothrow:
~this()
{
_rawBuf.destroyFree();
_opacityBuf.destroyFree();
_opacityBuf.destroyFree();
}

/// Does not initialize buffers.
/// `onDrawBufferedRaw` will be returned the same content,
/// unless the buffer size has changed.
/// This is needed for widgets might want their own Raw
/// and Opacity buffer to stay unchanged, if their size
/// didn't change. This is typically an optimization.
void doNotClearBuffers()
{
_preClearBuffers = false;
}

override void setDirty(box2i rect, UILayer layer = UILayer.guessFromFlags)
Expand All @@ -192,6 +203,8 @@ nothrow:
int newWidth = _position.width;
int newHeight = _position.height;
bool sizeChanged = (currentWidth != newWidth) || (currentHeight != newHeight);

bool preClear = _preClearBuffers;
if (sizeChanged)
{
// If the widget size changed, we must redraw it even if it was not dirtied
Expand All @@ -200,20 +213,25 @@ nothrow:
// Change size of buffers
_opacityBuf.size(newWidth, newHeight);
_rawBuf.size(newWidth, newHeight);

preClear = true;
}

if (_mustBeRedrawn)
{
// opacity buffer originally filled with zeroes
_opacityBuf.fillAll(opacityFullyTransparent);
if (preClear)
{
// opacity buffer originally filled with zeroes
_opacityBuf.fillAll(opacityFullyTransparent);

// RGBA buffer originally filled with black
_rawBuf.fillAll(RGBA(0, 0, 0, 255));
// RGBA buffer originally filled with black
_rawBuf.fillAll(RGBA(0, 0, 0, 255));
}

onDrawBufferedRaw(_rawBuf.toRef(), _opacityBuf.toRef());

// For debug purpose
//_opacityBuf.fill(opacityFullyOpaque);
// For debug purpose
//_opacityBuf.fillAll(opacityFullyOpaque);

_mustBeRedrawn = false;
}
Expand All @@ -234,4 +252,10 @@ private:
OwnedImage!RGBA _rawBuf;
OwnedImage!L8 _opacityBuf;
bool _mustBeRedrawn;

// Buffers are pre-cleared at every draw.
// This allows to draw only non-transparent part,
// but can also be a CPU cost.
// Default = true.
bool _preClearBuffers = true;
}

0 comments on commit 0a1a10f

Please sign in to comment.