Windows: closeTexture hands the texture to UnregisterTexture's callback and frees its frame buffers there; frames pushed after close are dropped. Without "recycle" the object is kept for the process lifetime (the session may still hold its pointer); an engine that shuts down keeps its open textures the same way. A duplicate createTexture key gets -1, getTexturePtr of an unknown key 0. macOS: closeTexture drops the frame and closes the texture to later frames; only "recycle" removes it from the plugin. Linux: the frame handed to the engine last is freed at that engine's next copy_pixels or once the engine has shut down, not at close: from Flutter 3.32 the raster thread can still be uploading it. Tests: windows/test/run_tests.sh (plain, --asan, --tsan) and linux/test/run_tests.sh (plain, --asan). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
2.7 KiB
C++
96 lines
2.7 KiB
C++
// Modified by the Kitedesk fork (2026, B252): no UnregisterTexture in the
|
|
// destructor; Terminate and FreeBuffers for closeTexture.
|
|
|
|
#include "texture_rgba.h"
|
|
#include <iostream>
|
|
|
|
TextureRgba::TextureRgba(flutter::TextureRegistrar *texture_registrar)
|
|
{
|
|
this->texture_registrar_ = texture_registrar;
|
|
// TODO: flutter only support PixelBuffer in 2023.02.17.
|
|
auto buffer = flutter::PixelBufferTexture([=](size_t width, size_t height) -> const FlutterDesktopPixelBuffer *
|
|
{ return this->buffer(); });
|
|
texture_ = std::make_unique<flutter::TextureVariant>(buffer);
|
|
this->texture_id_ = texture_registrar->RegisterTexture(texture_.get());
|
|
}
|
|
|
|
void TextureRgba::MarkVideoFrameAvailable(
|
|
std::vector<uint8_t> &buffer, size_t width, size_t height)
|
|
{
|
|
if (buffer.empty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Do not check bellow. Because the data may have paddings per row.
|
|
// buffer.size() != width * height * 4
|
|
|
|
const std::lock_guard<std::mutex> lock(mutex_);
|
|
int bg_index = fg_index_ ^ 1;
|
|
buffer.swap(buffer_tmp_[bg_index]);
|
|
width_[bg_index] = width;
|
|
height_[bg_index] = height;
|
|
if (!buff_ready_)
|
|
{
|
|
buff_ready_ = true;
|
|
texture_registrar_->MarkTextureFrameAvailable(texture_id_);
|
|
}
|
|
}
|
|
|
|
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<std::mutex> lock(mutex_);
|
|
if (terminated_)
|
|
{
|
|
// Closed while the session still pushes frames to this pointer.
|
|
return;
|
|
}
|
|
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()
|
|
{
|
|
const std::lock_guard<std::mutex> lock(mutex_);
|
|
if (terminated_)
|
|
{
|
|
// Closed; the engine may draw the texture until it has unregistered it.
|
|
return nullptr;
|
|
}
|
|
if (buff_ready_)
|
|
{
|
|
fg_index_ ^= 1;
|
|
flutter_pixel_buffer_.buffer = static_cast<const uint8_t *>(buffer_tmp_[fg_index_].data());
|
|
flutter_pixel_buffer_.width = width_[fg_index_];
|
|
flutter_pixel_buffer_.height = height_[fg_index_];
|
|
buff_ready_ = false;
|
|
}
|
|
return &this->flutter_pixel_buffer_;
|
|
}
|
|
|
|
void TextureRgba::Terminate()
|
|
{
|
|
const std::lock_guard<std::mutex> lock(mutex_);
|
|
terminated_ = true;
|
|
}
|
|
|
|
void TextureRgba::FreeBuffers()
|
|
{
|
|
const std::lock_guard<std::mutex> lock(mutex_);
|
|
std::vector<uint8_t>().swap(buffer_tmp_[0]);
|
|
std::vector<uint8_t>().swap(buffer_tmp_[1]);
|
|
flutter_pixel_buffer_.buffer = nullptr;
|
|
buff_ready_ = false;
|
|
}
|