Skip to content

Commit

Permalink
Make sure render operations are done in the render thread and clear t…
Browse files Browse the repository at this point in the history
…exture when stopping (Fixes #42 )
  • Loading branch information
EIREXE committed Nov 17, 2024
1 parent c279d87 commit 1880e50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
23 changes: 22 additions & 1 deletion ffmpeg_video_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ void FFmpegVideoStreamPlayback::stop_internal() {
playback_position = 0.0f;
decoder->seek(playback_position, true);
just_seeked = true;
texture.unref();
}
if (yuv_converter.is_valid()) {
yuv_converter->clear_output_texture();
}
playing = false;
}
Expand Down Expand Up @@ -387,7 +391,9 @@ YUVGPUConverter::~YUVGPUConverter() {
}

if (out_texture.is_valid() && out_texture->get_texture_rd_rid().is_valid()) {
FREE_RD_RID(out_texture->get_texture_rd_rid());
RID out_rid = out_texture->get_texture_rd_rid();
out_texture->set_texture_rd_rid(RID());
FREE_RD_RID(out_rid);
}

if (pipeline.is_valid()) {
Expand All @@ -399,6 +405,13 @@ YUVGPUConverter::~YUVGPUConverter() {
}
}

void YUVGPUConverter::_clear_texture_internal() {
if (out_texture.is_valid() && out_texture->get_texture_rd_rid().is_valid()) {
RD *rd = RS::get_singleton()->get_rendering_device();
rd->texture_clear(out_texture->get_texture_rd_rid(), Color(0, 0, 0, 0), 0, 1, 0, 1);
}
}

void YUVGPUConverter::_ensure_pipeline() {
if (pipeline.is_valid()) {
return;
Expand Down Expand Up @@ -586,6 +599,10 @@ void YUVGPUConverter::set_frame_size(const Vector2i &p_frame_size) {
}

void YUVGPUConverter::convert() {
RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &YUVGPUConverter::_convert_internal));
}

void YUVGPUConverter::_convert_internal() {
// First we must ensure everything we need exists
_ensure_pipeline();
_ensure_plane_textures();
Expand Down Expand Up @@ -622,6 +639,10 @@ Ref<Texture2D> YUVGPUConverter::get_output_texture() const {
return out_texture;
}

void YUVGPUConverter::clear_output_texture() {
RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &YUVGPUConverter::_clear_texture_internal));
}

YUVGPUConverter::YUVGPUConverter() {
out_texture.instantiate();
}
5 changes: 4 additions & 1 deletion ffmpeg_video_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ class YUVGPUConverter : public RefCounted {
Error _ensure_output_texture();
RID _create_uniform_set(const RID &p_texture_rd_rid);
void _upload_plane_images();

void _clear_texture_internal();
void _convert_internal();
public:
void set_plane_image(int p_plane_idx, Ref<Image> p_image);
Vector2i get_frame_size() const;
void set_frame_size(const Vector2i &p_frame_size);
void convert();
Ref<Texture2D> get_output_texture() const;
void clear_output_texture();

YUVGPUConverter();
~YUVGPUConverter();
};
Expand Down

0 comments on commit 1880e50

Please sign in to comment.