diff --git a/Outputs/CRT/CRT.cpp b/Outputs/CRT/CRT.cpp index e1a75b3f5..8f656e4c5 100644 --- a/Outputs/CRT/CRT.cpp +++ b/Outputs/CRT/CRT.cpp @@ -110,6 +110,7 @@ Flywheel::SyncEvent CRT::get_next_horizontal_sync_event(bool hsync_is_requested, void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divider, bool hsync_requested, bool vsync_requested, const bool vsync_charging, const Scan::Type type, uint16_t tex_x, uint16_t tex_y) { + std::unique_lock output_lock = openGL_output_builder_.get_output_lock(); number_of_cycles *= time_multiplier_; bool is_output_run = ((type == Scan::Type::Level) || (type == Scan::Type::Data)); @@ -189,8 +190,6 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi } else { - openGL_output_builder_.lock_output(); - // Get and write all those previously unwritten output ys const uint16_t output_y = openGL_output_builder_.get_composite_output_y(); @@ -221,8 +220,6 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi (*(uint16_t *)&input_buffer[position + SourceVertexOffsetOfOutputStart + 2]) = output_y; } }); - - openGL_output_builder_.unlock_output(); } is_writing_composite_run_ ^= true; } @@ -320,24 +317,11 @@ void CRT::output_blank(unsigned int number_of_cycles) void CRT::output_level(unsigned int number_of_cycles) { - if(!openGL_output_builder_.array_builder.is_full()) - { - Scan scan { - .type = Scan::Type::Level, - .number_of_cycles = number_of_cycles, -// .tex_x = openGL_output_builder_.texture_builder.get_last_write_x_position(), -// .tex_y = openGL_output_builder_.texture_builder.get_last_write_y_position() - }; - output_scan(&scan); - } - else - { - Scan scan { - .type = Scan::Type::Blank, - .number_of_cycles = number_of_cycles - }; - output_scan(&scan); - } + Scan scan { + .type = Scan::Type::Level, + .number_of_cycles = number_of_cycles, + }; + output_scan(&scan); } void CRT::output_colour_burst(unsigned int number_of_cycles, uint8_t phase, uint8_t amplitude) @@ -353,26 +337,13 @@ void CRT::output_colour_burst(unsigned int number_of_cycles, uint8_t phase, uint void CRT::output_data(unsigned int number_of_cycles, unsigned int source_divider) { - if(!openGL_output_builder_.array_builder.is_full()) - { - openGL_output_builder_.texture_builder.reduce_previous_allocation_to(number_of_cycles / source_divider); - Scan scan { - .type = Scan::Type::Data, - .number_of_cycles = number_of_cycles, -// .tex_x = openGL_output_builder_.texture_builder.get_last_write_x_position(), -// .tex_y = openGL_output_builder_.texture_builder.get_last_write_y_position(), - .source_divider = source_divider - }; - output_scan(&scan); - } - else - { - Scan scan { - .type = Scan::Type::Blank, - .number_of_cycles = number_of_cycles - }; - output_scan(&scan); - } + openGL_output_builder_.texture_builder.reduce_previous_allocation_to(number_of_cycles / source_divider); + Scan scan { + .type = Scan::Type::Data, + .number_of_cycles = number_of_cycles, + .source_divider = source_divider + }; + output_scan(&scan); } Outputs::CRT::Rect CRT::get_rect_for_area(int first_line_after_sync, int number_of_lines, int first_cycle_after_sync, int number_of_cycles, float aspect_ratio) diff --git a/Outputs/CRT/CRT.hpp b/Outputs/CRT/CRT.hpp index 4baa2a84f..85e495c95 100644 --- a/Outputs/CRT/CRT.hpp +++ b/Outputs/CRT/CRT.hpp @@ -192,6 +192,7 @@ class CRT { */ inline uint8_t *allocate_write_area(size_t required_length) { + std::unique_lock output_lock = openGL_output_builder_.get_output_lock(); return openGL_output_builder_.texture_builder.allocate_write_area(required_length); } diff --git a/Outputs/CRT/Internals/ArrayBuilder.cpp b/Outputs/CRT/Internals/ArrayBuilder.cpp index 53a97e1b1..61a9be2fb 100644 --- a/Outputs/CRT/Internals/ArrayBuilder.cpp +++ b/Outputs/CRT/Internals/ArrayBuilder.cpp @@ -23,7 +23,6 @@ ArrayBuilder::ArrayBuilder(size_t input_size, size_t output_size, std::function< bool ArrayBuilder::is_full() { bool was_full; - std::lock_guard guard(buffer_mutex_); was_full = is_full_; return was_full; } @@ -40,7 +39,6 @@ uint8_t *ArrayBuilder::get_output_storage(size_t size) void ArrayBuilder::flush(const std::function &function) { - std::lock_guard guard(buffer_mutex_); if(!is_full_) { size_t input_size, output_size; @@ -67,7 +65,6 @@ ArrayBuilder::Submission ArrayBuilder::submit() { ArrayBuilder::Submission submission; - std::lock_guard guard(buffer_mutex_); submission.input_size = input_.submit(true); submission.output_size = output_.submit(false); if(is_full_) @@ -102,7 +99,6 @@ ArrayBuilder::Buffer::~Buffer() uint8_t *ArrayBuilder::get_storage(size_t size, Buffer &buffer) { - std::lock_guard guard(buffer_mutex_); uint8_t *pointer = buffer.get_storage(size); if(!pointer) is_full_ = true; return pointer; diff --git a/Outputs/CRT/Internals/ArrayBuilder.hpp b/Outputs/CRT/Internals/ArrayBuilder.hpp index 0be0644c6..135a84c24 100644 --- a/Outputs/CRT/Internals/ArrayBuilder.hpp +++ b/Outputs/CRT/Internals/ArrayBuilder.hpp @@ -9,9 +9,9 @@ #ifndef ArrayBuilder_hpp #define ArrayBuilder_hpp -#include -#include +#include #include +#include #include "OpenGL.hpp" @@ -92,7 +92,6 @@ class ArrayBuilder { } output_, input_; uint8_t *get_storage(size_t size, Buffer &buffer); - std::mutex buffer_mutex_; bool is_full_; }; diff --git a/Outputs/CRT/Internals/CRTOpenGL.hpp b/Outputs/CRT/Internals/CRTOpenGL.hpp index d8a921b92..2c8718eac 100644 --- a/Outputs/CRT/Internals/CRTOpenGL.hpp +++ b/Outputs/CRT/Internals/CRTOpenGL.hpp @@ -90,6 +90,7 @@ class OpenGLOutputBuilder { GLsync fence_; public: + // These two are protected by output_mutex_. TextureBuilder texture_builder; ArrayBuilder array_builder; @@ -98,12 +99,11 @@ class OpenGLOutputBuilder { inline void set_colour_format(ColourSpace colour_space, unsigned int colour_cycle_numerator, unsigned int colour_cycle_denominator) { - output_mutex_.lock(); + std::lock_guard output_guard(output_mutex_); colour_space_ = colour_space; colour_cycle_numerator_ = colour_cycle_numerator; colour_cycle_denominator_ = colour_cycle_denominator; set_colour_space_uniforms(); - output_mutex_.unlock(); } inline void set_visible_area(Rect visible_area) @@ -111,14 +111,9 @@ class OpenGLOutputBuilder { visible_area_ = visible_area; } - inline void lock_output() + inline std::unique_lock get_output_lock() { - output_mutex_.lock(); - } - - inline void unlock_output() - { - output_mutex_.unlock(); + return std::unique_lock(output_mutex_); } inline OutputDevice get_output_device()