From c615e9507aed5ae8e18bff7661a5d7f57d999f20 Mon Sep 17 00:00:00 2001 From: 21pages Date: Wed, 27 Nov 2024 11:04:29 +0800 Subject: [PATCH] windows: avoid unnecessary memory allocation Signed-off-by: 21pages --- windows/texture_rgba.cpp | 17 +++++++++++++++++ windows/texture_rgba.h | 2 ++ windows/texture_rgba_renderer_plugin_c_api.cpp | 3 +-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/windows/texture_rgba.cpp b/windows/texture_rgba.cpp index 6984351..91aaacc 100644 --- a/windows/texture_rgba.cpp +++ b/windows/texture_rgba.cpp @@ -39,6 +39,23 @@ void TextureRgba::MarkVideoFrameAvailable( } } +void TextureRgba::MarkVideoFrameAvailable2(const uint8_t* buffer, const size_t buffer_length, size_t width, size_t height) { + if (!buffer || buffer_length < width * height * 4) + { + return; + } + const std::lock_guard lock(mutex_); + int bg_index = fg_index_ ^ 1; + buffer_tmp_[bg_index].assign(buffer, buffer + buffer_length); + width_[bg_index] = width; + height_[bg_index] = height; + if (!buff_ready_) + { + buff_ready_ = true; + texture_registrar_->MarkTextureFrameAvailable(texture_id_); + } +} + // This function may be called // event texture_registrar_->MarkTextureFrameAvailable(texture_id_); is not called before. inline const FlutterDesktopPixelBuffer *TextureRgba::buffer() diff --git a/windows/texture_rgba.h b/windows/texture_rgba.h index ffcfc47..8273039 100644 --- a/windows/texture_rgba.h +++ b/windows/texture_rgba.h @@ -13,6 +13,8 @@ public: void MarkVideoFrameAvailable(std::vector& buffer, size_t width, size_t height); + void MarkVideoFrameAvailable2(const uint8_t* buffer, size_t buffer_length, size_t width, size_t height); + int64_t texture_id() const { return texture_id_; }; const FlutterDesktopPixelBuffer* buffer(); diff --git a/windows/texture_rgba_renderer_plugin_c_api.cpp b/windows/texture_rgba_renderer_plugin_c_api.cpp index 8d64947..6d7a55b 100644 --- a/windows/texture_rgba_renderer_plugin_c_api.cpp +++ b/windows/texture_rgba_renderer_plugin_c_api.cpp @@ -25,6 +25,5 @@ void FlutterRgbaRendererPluginOnRgba(void *texture_rgba, const uint8_t *buffer, } TextureRgba *rgba = static_cast(texture_rgba); - std::vector data(buffer, buffer + len); - rgba->MarkVideoFrameAvailable(data, width, height); + rgba->MarkVideoFrameAvailable2(buffer, len, width, height); }