diff --git a/windows/texture_rgba.cpp b/windows/texture_rgba.cpp index e9c5058..d50dcf1 100644 --- a/windows/texture_rgba.cpp +++ b/windows/texture_rgba.cpp @@ -1,6 +1,7 @@ #include "texture_rgba.h" #include + TextureRgba::TextureRgba(flutter::TextureRegistrar *texture_registrar) { this->texture_registrar_ = texture_registrar; @@ -19,28 +20,35 @@ TextureRgba::~TextureRgba() void TextureRgba::MarkVideoFrameAvailable( std::vector &buffer, size_t width, size_t height) { - int bg_index = 0; + if (buffer.empty() || buffer.size() != width * height * 4) { - const std::lock_guard lock(mutex_); - if (last_fg_index_ != fg_index_) - { - flutter_pixel_buffer_.buffer = static_cast(buffer_tmp_[fg_index_].data()); - flutter_pixel_buffer_.width = width_[fg_index_]; - flutter_pixel_buffer_.height = height_[fg_index_]; - last_fg_index_ = fg_index_; - texture_registrar_->MarkTextureFrameAvailable(texture_id_); - } - bg_index = fg_index_ ^ 1; + return; } + const std::lock_guard 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_); + } } +// This function may be called +// event texture_registrar_->MarkTextureFrameAvailable(texture_id_); is not called before. inline const FlutterDesktopPixelBuffer *TextureRgba::buffer() { const std::lock_guard lock(mutex_); - fg_index_ ^= 1; + if (buff_ready_) + { + fg_index_ ^= 1; + flutter_pixel_buffer_.buffer = static_cast(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_; } diff --git a/windows/texture_rgba.h b/windows/texture_rgba.h index 28b7eee..ffcfc47 100644 --- a/windows/texture_rgba.h +++ b/windows/texture_rgba.h @@ -22,13 +22,12 @@ private: flutter::TextureRegistrar* texture_registrar_ = nullptr; std::unique_ptr texture_ = nullptr; int64_t texture_id_; - mutable std::mutex mutex_; - int last_fg_index_ = 0; + std::mutex mutex_; int fg_index_ = 0; - size_t width_[2] = {0, 0}; - size_t height_[2] = {0, 0}; + bool buff_ready_ = false; + size_t width_[2]; + size_t height_[2]; std::vector buffer_tmp_[2]; - Timer timer_; }; #endif // TEXTURE_RGBA_H_ diff --git a/windows/texture_rgba_renderer_plugin_c_api.cpp b/windows/texture_rgba_renderer_plugin_c_api.cpp index 6737dd9..58285f0 100644 --- a/windows/texture_rgba_renderer_plugin_c_api.cpp +++ b/windows/texture_rgba_renderer_plugin_c_api.cpp @@ -1,3 +1,4 @@ +#include #include "include/texture_rgba_renderer/texture_rgba_renderer_plugin_c_api.h" #include @@ -6,14 +7,24 @@ #include "texture_rgba.h" void TextureRgbaRendererPluginCApiRegisterWithRegistrar( - FlutterDesktopPluginRegistrarRef registrar) { - texture_rgba_renderer::TextureRgbaRendererPlugin::RegisterWithRegistrar( - flutter::PluginRegistrarManager::GetInstance() - ->GetRegistrar(registrar)); + FlutterDesktopPluginRegistrarRef registrar) +{ + texture_rgba_renderer::TextureRgbaRendererPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); } -void FlutterRgbaRendererPluginOnRgba(void* texture_rgba, const uint8_t* buffer, int width, int height) { - TextureRgba* rgba = static_cast(texture_rgba); +void FlutterRgbaRendererPluginOnRgba(void *texture_rgba, const uint8_t *buffer, int width, int height) +{ + assert(texture_rgba != nullptr); + assert(buffer != nullptr); + assert(width > 0 && height > 0); + if (texture_rgba == nullptr || buffer == nullptr || width <= 0 || height <= 0) + { + return; + } + + TextureRgba *rgba = static_cast(texture_rgba); std::vector data(buffer, buffer + width * height * 4); rgba->MarkVideoFrameAvailable(data, width, height); }