From 1684b7655c42ae58af238e24d84396118743074a Mon Sep 17 00:00:00 2001 From: Kingtous Date: Fri, 17 Feb 2023 11:35:07 +0800 Subject: [PATCH] feat: windows basic impl --- README.md | 2 +- pubspec.yaml | 2 +- windows/texture_rgba.cpp | 18 ++++++++-- windows/texture_rgba.h | 5 ++- windows/texture_rgba_renderer_plugin.cpp | 43 +++++++++++++++++++++--- windows/texture_rgba_renderer_plugin.h | 2 +- 6 files changed, 62 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index afc4c51..3b673a0 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A texture helper which brings a high level api to handle RGBA data. No frame cop ## Platform Support - [x] Linux -- [ ] Windows (not implemented yet.) +- [x] Windows (No gpu acceleration yet, copy required currently.) - [ ] MacOS (not implemented yet.) ## Getting Started diff --git a/pubspec.yaml b/pubspec.yaml index 135a7ea..4fc89e4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 0.0.2 homepage: https://github.com/Kingtous/flutter_texture_rgba_renderer environment: - sdk: '>=2.19.2 <3.0.0' + sdk: '>=2.15.0 <3.0.0' flutter: ">=2.5.0" dependencies: diff --git a/windows/texture_rgba.cpp b/windows/texture_rgba.cpp index 397228d..6122448 100644 --- a/windows/texture_rgba.cpp +++ b/windows/texture_rgba.cpp @@ -2,6 +2,13 @@ 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(buffer); + this->texture_id_ = texture_registrar->RegisterTexture(texture_.get()); } TextureRgba::~TextureRgba() @@ -9,11 +16,18 @@ TextureRgba::~TextureRgba() texture_registrar_->UnregisterTexture(texture_id_); } -void TextureRgba::MarkVideoFrameAvailable(uint8_t* buffer, int32_t width, int32_t height) +void TextureRgba::MarkVideoFrameAvailable(const std::vector& buffer, size_t width, size_t height) { const std::lock_guard lock(mutex_); - flutter_pixel_buffer_.buffer = buffer; + buffer_tmp_.resize(buffer.size()); + std::copy(buffer.begin(), buffer.end(), buffer_tmp_.begin()); + flutter_pixel_buffer_.buffer = &buffer_tmp_[0]; flutter_pixel_buffer_.width = width; flutter_pixel_buffer_.height = height; texture_registrar_->MarkTextureFrameAvailable(texture_id_); } + +inline const FlutterDesktopPixelBuffer* TextureRgba::buffer() +{ + return &this->flutter_pixel_buffer_; +} diff --git a/windows/texture_rgba.h b/windows/texture_rgba.h index 99c0f4c..a235514 100644 --- a/windows/texture_rgba.h +++ b/windows/texture_rgba.h @@ -8,15 +8,18 @@ public: TextureRgba(flutter::TextureRegistrar* texture_registrar); ~TextureRgba(); - void MarkVideoFrameAvailable(uint8_t* buffer, int32_t width, int32_t height); + void MarkVideoFrameAvailable(const std::vector& buffer, size_t width, size_t height); int64_t texture_id() const { return texture_id_; }; + const FlutterDesktopPixelBuffer* buffer(); + private: FlutterDesktopPixelBuffer flutter_pixel_buffer_{}; flutter::TextureRegistrar* texture_registrar_ = nullptr; std::unique_ptr texture_ = nullptr; int64_t texture_id_; mutable std::mutex mutex_; + std::vector buffer_tmp_; }; diff --git a/windows/texture_rgba_renderer_plugin.cpp b/windows/texture_rgba_renderer_plugin.cpp index 5e3870a..b6844ce 100644 --- a/windows/texture_rgba_renderer_plugin.cpp +++ b/windows/texture_rgba_renderer_plugin.cpp @@ -34,7 +34,9 @@ void TextureRgbaRendererPlugin::RegisterWithRegistrar( registrar->AddPlugin(std::move(plugin)); } -TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() {} +TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() { + this->texture_registrar = nullptr; +} TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() {} @@ -42,9 +44,42 @@ void TextureRgbaRendererPlugin::HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result) { if (method_call.method_name().compare("createTexture") == 0) { - auto args = std::get(method_call.arguments()); - arg - } else { + auto args = std::get(*method_call.arguments()); + auto key = std::get(args.at(flutter::EncodableValue("key"))); + auto [it, add] = textures_.try_emplace(key, nullptr); + if (add) { + // Create a texture. + it->second = std::make_unique(this->texture_registrar); + result->Success(flutter::EncodableValue(it->second->texture_id())); + } + } + else if (method_call.method_name().compare("closeTexture") == 0) { + auto args = std::get(*method_call.arguments()); + auto key = std::get(args.at(flutter::EncodableValue("key"))); + if (textures_.find(key) == textures_.end()) { + result->Success(flutter::EncodableValue(false)); + } + else { + this->texture_registrar->UnregisterTexture(textures_[key]->texture_id()); + textures_.erase(key); + result->Success(flutter::EncodableValue(true)); + } + } + else if (method_call.method_name().compare("onRgba") == 0) { + auto args = std::get(*method_call.arguments()); + auto key = std::get(args.at(flutter::EncodableValue("key"))); + if (textures_.find(key) == textures_.end()) { + result->Success(flutter::EncodableValue(false)); + } + else { + auto& data = std::get>(args.at(flutter::EncodableValue("data"))); + auto height = std::get(args.at(flutter::EncodableValue("height"))); + auto width = std::get(args.at(flutter::EncodableValue("width"))); + textures_[key]->MarkVideoFrameAvailable(data, width, height); + result->Success(flutter::EncodableValue(true)); + } + } + else { result->NotImplemented(); } } diff --git a/windows/texture_rgba_renderer_plugin.h b/windows/texture_rgba_renderer_plugin.h index 6cf6a48..2d621bf 100644 --- a/windows/texture_rgba_renderer_plugin.h +++ b/windows/texture_rgba_renderer_plugin.h @@ -30,7 +30,7 @@ class TextureRgbaRendererPlugin : public flutter::Plugin { void HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result); - std::unordered_map> textures_; + std::unordered_map> textures_; }; } // namespace texture_rgba_renderer