From 1c3764ec89b898e58c79eccd7cca3aeef6f03cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Lu=C3=ADs=20Vaz=20Silva?= Date: Thu, 11 Apr 2024 21:14:09 -0300 Subject: [PATCH] remove copying from CreateTexBuffer --- src/common/textures/bitmap.h | 2 ++ src/common/textures/texture.cpp | 22 +++++++++++++++++----- src/common/textures/textures.h | 5 ++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/common/textures/bitmap.h b/src/common/textures/bitmap.h index 418f256c2b..7f52bc5c02 100644 --- a/src/common/textures/bitmap.h +++ b/src/common/textures/bitmap.h @@ -253,6 +253,8 @@ public: 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, diff --git a/src/common/textures/texture.cpp b/src/common/textures/texture.cpp index dd693d0870..af335ca31e 100644 --- a/src/common/textures/texture.cpp +++ b/src/common/textures/texture.cpp @@ -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); diff --git a/src/common/textures/textures.h b/src/common/textures/textures.h index 3e4578cfb4..8fcdd3b239 100644 --- a/src/common/textures/textures.h +++ b/src/common/textures/textures.h @@ -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.) @@ -177,7 +178,7 @@ struct FTextureBuffer ~FTextureBuffer() { - if (mBuffer) delete[] mBuffer; + if (mBuffer && mFreeBuffer) delete[] mBuffer; } FTextureBuffer(const FTextureBuffer &other) = delete; @@ -187,6 +188,7 @@ struct FTextureBuffer mWidth = other.mWidth; mHeight = other.mHeight; mContentId = other.mContentId; + mFreeBuffer = other.mFreeBuffer; other.mBuffer = nullptr; } @@ -196,6 +198,7 @@ struct FTextureBuffer mWidth = other.mWidth; mHeight = other.mHeight; mContentId = other.mContentId; + mFreeBuffer = other.mFreeBuffer; other.mBuffer = nullptr; return *this; }