feat: add onRgba c api on windows

This commit is contained in:
Kingtous 2023-02-18 00:34:46 +08:00
parent 71e77ef3cf
commit 1a164ee5a0
9 changed files with 93 additions and 35 deletions

View file

@ -16,6 +16,8 @@ extern "C" {
FLUTTER_PLUGIN_EXPORT void TextureRgbaRendererPluginCApiRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar);
FLUTTER_PLUGIN_EXPORT void FlutterRgbaRendererPluginOnRgba(void* texture_rgba, const uint8_t* buffer, int width, int height);
#if defined(__cplusplus)
} // extern "C"
#endif

View file

@ -16,13 +16,22 @@ TextureRgba::~TextureRgba()
texture_registrar_->UnregisterTexture(texture_id_);
}
void TextureRgba::MarkVideoFrameAvailable(const std::vector<uint8_t>& buffer, size_t width, size_t height)
{
void TextureRgba::MarkVideoFrameAvailable(
std::vector<uint8_t>& buffer, size_t width, size_t height) {
const std::lock_guard<std::mutex> lock(mutex_);
// FIXME: remove copy.
std::swap(buffer, buffer_tmp_);
flutter_pixel_buffer_.buffer = &buffer_tmp_[0];
flutter_pixel_buffer_.width = width;
flutter_pixel_buffer_.height = height;
texture_registrar_->MarkTextureFrameAvailable(texture_id_);
}
void TextureRgba::MarkVideoFrameAvailablePtr(
const uint8_t* buffer, size_t width, size_t height) {
const std::lock_guard<std::mutex> lock(mutex_);
// FIXME: remove copy.
buffer_tmp_.resize(buffer.size());
std::copy(buffer.begin(), buffer.end(), buffer_tmp_.begin());
flutter_pixel_buffer_.buffer = &buffer_tmp_[0];
flutter_pixel_buffer_.buffer = buffer;
flutter_pixel_buffer_.width = width;
flutter_pixel_buffer_.height = height;
texture_registrar_->MarkTextureFrameAvailable(texture_id_);

View file

@ -8,7 +8,9 @@ public:
TextureRgba(flutter::TextureRegistrar* texture_registrar);
~TextureRgba();
void MarkVideoFrameAvailable(const std::vector<uint8_t>& buffer, size_t width, size_t height);
void MarkVideoFrameAvailable(std::vector<uint8_t>& buffer, size_t width, size_t height);
void MarkVideoFrameAvailablePtr(const uint8_t* buffer, size_t width, size_t height);
int64_t texture_id() const { return texture_id_; };

View file

@ -79,9 +79,22 @@ void TextureRgbaRendererPlugin::HandleMethodCall(
result->Success(flutter::EncodableValue(true));
}
}
else if (method_call.method_name().compare("getTexturePtr") == 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(-1));
}
else {
// Return an address.
void* rgba = (void*) textures_[key].get();
result->Success(flutter::EncodableValue(rgba));
}
}
else {
result->NotImplemented();
}
}
} // namespace texture_rgba_renderer

View file

@ -24,13 +24,13 @@ class TextureRgbaRendererPlugin : public flutter::Plugin {
TextureRgbaRendererPlugin& operator=(const TextureRgbaRendererPlugin&) = delete;
flutter::TextureRegistrar* texture_registrar;
std::unordered_map<int64_t, std::unique_ptr<TextureRgba>> textures_;
private:
// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
std::unordered_map<int64_t, std::unique_ptr<TextureRgba>> textures_;
};
} // namespace texture_rgba_renderer

View file

@ -3,6 +3,7 @@
#include <flutter/plugin_registrar_windows.h>
#include "texture_rgba_renderer_plugin.h"
#include "texture_rgba.h"
void TextureRgbaRendererPluginCApiRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
@ -10,3 +11,8 @@ void TextureRgbaRendererPluginCApiRegisterWithRegistrar(
flutter::PluginRegistrarManager::GetInstance()
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
}
void FlutterRgbaRendererPluginOnRgba(void* texture_rgba, const uint8_t* buffer, int width, int height) {
TextureRgba* rgba = static_cast<TextureRgba*>(texture_rgba);
rgba->MarkVideoFrameAvailablePtr(buffer, width, height);
}