Skip to content

Commit

Permalink
remove copying from CreateTexBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 authored and madame-rachelle committed Apr 18, 2024
1 parent 3b6c834 commit 1c3764e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/common/textures/bitmap.h
Expand Up @@ -253,6 +253,8 @@ class FBitmap
CopyPixelDataRGB(originx, originy, src.GetPixels(), src.GetWidth(), src.GetHeight(), 4, src.GetWidth()*4, 0, CF_BGRA, inf);
}


friend class FTexture;
};

bool ClipCopyPixelRect(const FClipRect *cr, int &originx, int &originy,
Expand Down
22 changes: 17 additions & 5 deletions src/common/textures/texture.cpp
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
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 1c3764e

Please sign in to comment.