fix win crash (#3)

* fix win crash

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* better lock guard

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-02-20 23:16:44 +08:00 • committed by GitHub
parent 1306df8554
commit 749acc2f87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 51 deletions

View file

@ -6,11 +6,7 @@ TextureRgba::TextureRgba(flutter::TextureRegistrar *texture_registrar)
this->texture_registrar_ = texture_registrar; this->texture_registrar_ = texture_registrar;
// TODO: flutter only support PixelBuffer in 2023.02.17. // TODO: flutter only support PixelBuffer in 2023.02.17.
auto buffer = flutter::PixelBufferTexture([=](size_t width, size_t height) -> const FlutterDesktopPixelBuffer * auto buffer = flutter::PixelBufferTexture([=](size_t width, size_t height) -> const FlutterDesktopPixelBuffer *
{ { return this->buffer(); });
std::cout << " GL. C++ rendering interval " << timer_.elapsed() << " ms" << std::endl;
timer_.reset();
const std::lock_guard<std::mutex> lock(mutex_);
return this->buffer(); });
texture_ = std::make_unique<flutter::TextureVariant>(buffer); texture_ = std::make_unique<flutter::TextureVariant>(buffer);
this->texture_id_ = texture_registrar->RegisterTexture(texture_.get()); this->texture_id_ = texture_registrar->RegisterTexture(texture_.get());
} }
@ -23,16 +19,28 @@ TextureRgba::~TextureRgba()
void TextureRgba::MarkVideoFrameAvailable( void TextureRgba::MarkVideoFrameAvailable(
std::vector<uint8_t> &buffer, size_t width, size_t height) std::vector<uint8_t> &buffer, size_t width, size_t height)
{ {
const std::lock_guard<std::mutex> lock(mutex_); int bg_index = 0;
// FIXME: remove copy. {
std::swap(buffer, buffer_tmp_); const std::lock_guard<std::mutex> lock(mutex_);
flutter_pixel_buffer_.buffer = &buffer_tmp_[0]; if (last_fg_index_ != fg_index_)
flutter_pixel_buffer_.width = width; {
flutter_pixel_buffer_.height = height; flutter_pixel_buffer_.buffer = static_cast<const uint8_t *>(buffer_tmp_[fg_index_].data());
texture_registrar_->MarkTextureFrameAvailable(texture_id_); 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;
}
buffer.swap(buffer_tmp_[bg_index]);
width_[bg_index] = width;
height_[bg_index] = height;
} }
inline const FlutterDesktopPixelBuffer *TextureRgba::buffer() inline const FlutterDesktopPixelBuffer *TextureRgba::buffer()
{ {
const std::lock_guard<std::mutex> lock(mutex_);
fg_index_ ^= 1;
return &this->flutter_pixel_buffer_; return &this->flutter_pixel_buffer_;
} }

View file

@ -1,45 +1,10 @@
#ifndef TEXTURE_RGBA_H_
#define TEXTURE_RGBA_H_
#include <flutter/texture_registrar.h> #include <flutter/texture_registrar.h>
#include <mutex> #include <mutex>
#include <chrono> #include <chrono>
#pragma once
using namespace std;
using namespace std::chrono;
class Timer
{
public:
Timer() : m_begin(high_resolution_clock::now()) {}
void reset() { m_begin = high_resolution_clock::now(); }
int64_t elapsed() const
{
return duration_cast<chrono::milliseconds>(high_resolution_clock::now() - m_begin).count();
}
int64_t elapsed_micro() const
{
return duration_cast<chrono::microseconds>(high_resolution_clock::now() - m_begin).count();
}
int64_t elapsed_nano() const
{
return duration_cast<chrono::nanoseconds>(high_resolution_clock::now() - m_begin).count();
}
int64_t elapsed_seconds() const
{
return duration_cast<chrono::seconds>(high_resolution_clock::now() - m_begin).count();
}
int64_t elapsed_minutes() const
{
return duration_cast<chrono::minutes>(high_resolution_clock::now() - m_begin).count();
}
int64_t elapsed_hours() const
{
return duration_cast<chrono::hours>(high_resolution_clock::now() - m_begin).count();
}
private:
time_point<high_resolution_clock> m_begin;
};
class TextureRgba class TextureRgba
{ {
public: public:
@ -58,7 +23,12 @@ private:
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_; int last_fg_index_ = 0;
int fg_index_ = 0;
size_t width_[2] = {0, 0};
size_t height_[2] = {0, 0};
std::vector<uint8_t> buffer_tmp_[2];
Timer timer_; Timer timer_;
}; };
#endif // TEXTURE_RGBA_H_