feat: windows basic impl

This commit is contained in:
Kingtous 2023-02-17 11:35:07 +08:00
parent 59475c0020
commit 1684b7655c
6 changed files with 62 additions and 10 deletions

View file

@ -8,7 +8,7 @@ A texture helper which brings a high level api to handle RGBA data. No frame cop
## Platform Support ## Platform Support
- [x] Linux - [x] Linux
- [ ] Windows (not implemented yet.) - [x] Windows (No gpu acceleration yet, copy required currently.)
- [ ] MacOS (not implemented yet.) - [ ] MacOS (not implemented yet.)
## Getting Started ## Getting Started

View file

@ -4,7 +4,7 @@ version: 0.0.2
homepage: https://github.com/Kingtous/flutter_texture_rgba_renderer homepage: https://github.com/Kingtous/flutter_texture_rgba_renderer
environment: environment:
sdk: '>=2.19.2 <3.0.0' sdk: '>=2.15.0 <3.0.0'
flutter: ">=2.5.0" flutter: ">=2.5.0"
dependencies: dependencies:

View file

@ -2,6 +2,13 @@
TextureRgba::TextureRgba(flutter::TextureRegistrar* texture_registrar) 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());
} }
TextureRgba::~TextureRgba() TextureRgba::~TextureRgba()
@ -9,11 +16,18 @@ TextureRgba::~TextureRgba()
texture_registrar_->UnregisterTexture(texture_id_); texture_registrar_->UnregisterTexture(texture_id_);
} }
void TextureRgba::MarkVideoFrameAvailable(uint8_t* buffer, int32_t width, int32_t height) void TextureRgba::MarkVideoFrameAvailable(const std::vector<uint8_t>& buffer, size_t width, size_t height)
{ {
const std::lock_guard<std::mutex> lock(mutex_); const std::lock_guard<std::mutex> 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_.width = width;
flutter_pixel_buffer_.height = height; flutter_pixel_buffer_.height = height;
texture_registrar_->MarkTextureFrameAvailable(texture_id_); texture_registrar_->MarkTextureFrameAvailable(texture_id_);
} }
inline const FlutterDesktopPixelBuffer* TextureRgba::buffer()
{
return &this->flutter_pixel_buffer_;
}

View file

@ -8,15 +8,18 @@ public:
TextureRgba(flutter::TextureRegistrar* texture_registrar); TextureRgba(flutter::TextureRegistrar* texture_registrar);
~TextureRgba(); ~TextureRgba();
void MarkVideoFrameAvailable(uint8_t* buffer, int32_t width, int32_t height); void MarkVideoFrameAvailable(const std::vector<uint8_t>& buffer, size_t width, size_t height);
int64_t texture_id() const { return texture_id_; }; int64_t texture_id() const { return texture_id_; };
const FlutterDesktopPixelBuffer* buffer();
private: private:
FlutterDesktopPixelBuffer flutter_pixel_buffer_{}; FlutterDesktopPixelBuffer flutter_pixel_buffer_{};
flutter::TextureRegistrar* texture_registrar_ = nullptr; flutter::TextureRegistrar* texture_registrar_ = nullptr;
std::unique_ptr<flutter::TextureVariant> texture_ = nullptr; std::unique_ptr<flutter::TextureVariant> texture_ = nullptr;
int64_t texture_id_; int64_t texture_id_;
mutable std::mutex mutex_; mutable std::mutex mutex_;
std::vector<uint8_t> buffer_tmp_;
}; };

View file

@ -34,7 +34,9 @@ void TextureRgbaRendererPlugin::RegisterWithRegistrar(
registrar->AddPlugin(std::move(plugin)); registrar->AddPlugin(std::move(plugin));
} }
TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() {} TextureRgbaRendererPlugin::TextureRgbaRendererPlugin() {
this->texture_registrar = nullptr;
}
TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() {} TextureRgbaRendererPlugin::~TextureRgbaRendererPlugin() {}
@ -42,9 +44,42 @@ void TextureRgbaRendererPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call, const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("createTexture") == 0) { if (method_call.method_name().compare("createTexture") == 0) {
auto args = std::get<flutter::EncodableMap*>(method_call.arguments()); auto args = std::get<flutter::EncodableMap>(*method_call.arguments());
arg auto key = std::get<int>(args.at(flutter::EncodableValue("key")));
} else { auto [it, add] = textures_.try_emplace(key, nullptr);
if (add) {
// Create a texture.
it->second = std::make_unique<TextureRgba>(this->texture_registrar);
result->Success(flutter::EncodableValue(it->second->texture_id()));
}
}
else if (method_call.method_name().compare("closeTexture") == 0) {
auto args = std::get<flutter::EncodableMap>(*method_call.arguments());
auto key = std::get<int>(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<flutter::EncodableMap>(*method_call.arguments());
auto key = std::get<int>(args.at(flutter::EncodableValue("key")));
if (textures_.find(key) == textures_.end()) {
result->Success(flutter::EncodableValue(false));
}
else {
auto& data = std::get<std::vector<uint8_t>>(args.at(flutter::EncodableValue("data")));
auto height = std::get<int>(args.at(flutter::EncodableValue("height")));
auto width = std::get<int>(args.at(flutter::EncodableValue("width")));
textures_[key]->MarkVideoFrameAvailable(data, width, height);
result->Success(flutter::EncodableValue(true));
}
}
else {
result->NotImplemented(); result->NotImplemented();
} }
} }

View file

@ -30,7 +30,7 @@ class TextureRgbaRendererPlugin : public flutter::Plugin {
void HandleMethodCall( void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call, const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result); std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
std::unordered_map<uint64_t, std::unique_ptr<TextureRgba>> textures_; std::unordered_map<int64_t, std::unique_ptr<TextureRgba>> textures_;
}; };
} // namespace texture_rgba_renderer } // namespace texture_rgba_renderer