Skip to content

Commit

Permalink
remove copying from CreateTexBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 committed Apr 12, 2024
1 parent 3af0bd4 commit 198b600
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/common/textures/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,29 @@ FTextureBuffer FTexture::CreateTexBuffer(int translation, int flags)

if (!checkonly)
{
buffer = new unsigned char[W * (H + 1) * 4];
memset(buffer, 0, W * (H + 1) * 4);

auto remap = translation <= 0 || IsLuminosityTranslation(translation) ? nullptr : GPalette.TranslationToTable(translation);
if (remap && remap->Inactive) remap = nullptr;
if (remap) translation = remap->Index;
FBitmap bmp(buffer, W * 4, W, H);

int trans;
auto Pixels = GetBgraBitmap(remap ? remap->Palette : nullptr, &trans);
bmp.Blit(exx, exx, Pixels);

if(!exx && Pixels.ClipRect.x == 0 && Pixels.ClipRect.y == 0 && Pixels.ClipRect.width == Pixels.Width && Pixels.ClipRect.height == Pixels.Height && (Pixels.FreeBuffer || !IsLuminosityTranslation(translation)))
{
buffer = Pixels.data;
result.mFreeBuffer = Pixels.FreeBuffer;
Pixels.FreeBuffer = false;
}
else
{
buffer = new unsigned char[W * (H + 1) * 4];
memset(buffer, 0, W * (H + 1) * 4);

FBitmap bmp(buffer, W * 4, W, H);

bmp.Blit(exx, exx, Pixels);
}

if (IsLuminosityTranslation(translation))
{
V_ApplyLuminosityTranslation(LuminosityTranslationDesc::fromInt(translation), buffer, W * H);
Expand Down
5 changes: 4 additions & 1 deletion src/common/textures/textures.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ union FContentIdBuilder
struct FTextureBuffer
{
uint8_t *mBuffer = nullptr;
bool mFreeBuffer = true;
int mWidth = 0;
int mHeight = 0;
uint64_t mContentId = 0; // unique content identifier. (Two images created from the same image source with the same settings will return the same value.)
Expand All @@ -177,7 +178,7 @@ struct FTextureBuffer

~FTextureBuffer()
{
if (mBuffer) delete[] mBuffer;
if (mBuffer && mFreeBuffer) delete[] mBuffer;
}

FTextureBuffer(const FTextureBuffer &other) = delete;
Expand All @@ -187,6 +188,7 @@ struct FTextureBuffer
mWidth = other.mWidth;
mHeight = other.mHeight;
mContentId = other.mContentId;
mFreeBuffer = other.mFreeBuffer;
other.mBuffer = nullptr;
}

Expand All @@ -196,6 +198,7 @@ struct FTextureBuffer
mWidth = other.mWidth;
mHeight = other.mHeight;
mContentId = other.mContentId;
mFreeBuffer = other.mFreeBuffer;
other.mBuffer = nullptr;
return *this;
}
Expand Down

0 comments on commit 198b600

Please sign in to comment.