18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 07:24:57 +01:00
Files
maszyna/scripting/pythonscreenviewer.cpp
maj00r 939514ea79 fix(python): the old texture deleted under the lock the viewers bind under, a fence not made waited for with glFinish
From the review of #411:

- finish_upload() deleted the old texture before it took the lock of the render target, and the
  windows of python.displaywindows read the name under that lock but bound it after letting go of it.
  A window could bind a name already deleted, or already given to the next upload. In a compatibility
  context (the legacy renderer) that makes a new empty texture: a black frame and a texture never
  deleted. The commit and the publishing of the new name now go under the lock, and the viewer binds
  under it too. A name bound before the deletion stays alive in that window's context while bound.
- A glFenceSync that returns no fence was taken for a signalled one: the image went in place at once,
  with nothing ordering the contexts. The worker now waits for its upload with glFinish then, as
  without sync objects.
2026-11-19 18:00:22 +01:00

274 lines
7.6 KiB
C++

#include "stdafx.h"
#include "scripting/pythonscreenviewer.h"
#include "application/application.h"
#include "gl/shader.h"
#include "gl/vao.h"
#include "utilities/Logs.h"
void texture_window_resize(GLFWwindow *win, int w, int h)
{
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
texwindow->notify_window_size(win, w, h);
}
void texture_window_fb_resize(GLFWwindow *win, int w, int h)
{
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
texwindow->notify_window_fb_size(win, w, h);
}
void texture_window_mouse_button(GLFWwindow *win, int button, int action, int /*mods*/)
{
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
texwindow->notify_click(win, button, action);
}
void texture_window_cursor_pos(GLFWwindow *win, double x, double y)
{
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
texwindow->notify_cursor_pos(win, x, y);
}
python_screen_viewer::python_screen_viewer(std::shared_ptr<python_rt> rt, std::shared_ptr<std::vector<glm::vec2>> touchlist, std::string const &surfacename)
: m_rt(rt), m_touchlist(touchlist), m_initialtexture(rt->shared_tex->get_id())
{
for (const auto &viewport : Global.python_viewports) {
if (viewport.surface == surfacename) {
auto conf = std::make_unique<window_state>();
conf->window_size = viewport.size;
conf->offset = viewport.offset;
conf->scale = viewport.scale;
GLFWmonitor *monitor = Application.find_monitor(viewport.monitor);
if (!monitor && viewport.monitor != "window")
continue;
conf->window = Application.window(-1, true, conf->window_size.x, conf->window_size.y,
monitor, false, Global.python_sharectx);
{
int w;
int h;
glfwGetWindowSize(conf->window, &w, &h);
conf->window_size = glm::ivec2(w, h);
glfwGetFramebufferSize(conf->window, &w, &h);
conf->fb_size = glm::ivec2(w, h);
}
glfwSetWindowUserPointer(conf->window, this);
glfwSetWindowSizeCallback(conf->window, texture_window_resize);
glfwSetFramebufferSizeCallback(conf->window, texture_window_fb_resize);
glfwSetMouseButtonCallback(conf->window, texture_window_mouse_button);
glfwSetCursorPosCallback(conf->window, texture_window_cursor_pos);
m_windows.push_back(std::move(conf));
}
}
if (!m_windows.empty())
m_renderthread = std::make_unique<std::thread>(&python_screen_viewer::threadfunc, this);
}
python_screen_viewer::~python_screen_viewer()
{
m_exit = true;
if (m_renderthread)
m_renderthread->join();
}
void python_screen_viewer::threadfunc()
{
for (auto const &window : m_windows) {
glfwMakeContextCurrent(window->window);
glfwSwapInterval(Global.python_vsync ? 1 : 0);
GLuint v;
glGenVertexArrays(1, &v);
glBindVertexArray(v);
glActiveTexture(GL_TEXTURE0);
if (Global.python_sharectx) {
bind_shared_texture();
}
else {
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
}
if (!Global.gfx_usegles && !Global.gfx_shadergamma)
glEnable(GL_FRAMEBUFFER_SRGB);
gl::program::unbind();
gl::buffer::unbind();
window->ubo = std::make_unique<gl::ubo>(sizeof(gl::scene_ubs), 0, GL_STREAM_DRAW);
gl::shader vert("texturewindow.vert");
gl::shader frag("texturewindow.frag");
window->shader = std::make_unique<gl::program>(std::vector<std::reference_wrapper<const gl::shader>>({vert, frag}));
}
while (!m_exit)
{
auto start_time = std::chrono::high_resolution_clock::now();
for (auto const &window : m_windows) {
unsigned char *image = nullptr;
int format;
int components;
int width;
int height;
if (!Global.python_sharectx) {
std::scoped_lock guard(m_rt->mutex);
if (window->timestamp == m_rt->timestamp)
continue;
window->timestamp = m_rt->timestamp;
if (m_rt->image.empty())
continue;
format = m_rt->format;
components = m_rt->components;
width = m_rt->width;
height = m_rt->height;
size_t size = width * height * (components == GL_RGB ? 3 : 4);
image = new unsigned char[size];
memcpy(image, m_rt->image.data(), size);
}
glfwMakeContextCurrent(window->window);
if (Global.python_sharectx)
bind_shared_texture();
gl::program::unbind();
gl::buffer::unbind();
window->shader->bind();
window->ubo->bind_uniform();
m_ubs.projection = glm::mat4(glm::mat3(glm::translate(glm::scale(glm::mat3(), 1.0f / window->scale), window->offset)));
window->ubo->update(m_ubs);
if (image) {
glTexImage2D(
GL_TEXTURE_2D, 0,
format,
width, height, 0,
components, GL_UNSIGNED_BYTE, image);
delete[] image;
if (Global.python_mipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
glViewport(0, 0, window->fb_size.x, window->fb_size.y);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window->window);
}
auto frametime = std::chrono::high_resolution_clock::now() - start_time;
if ((Global.python_minframetime - frametime).count() > 0.0f)
std::this_thread::sleep_for(Global.python_minframetime - frametime);
}
}
void python_screen_viewer::bind_shared_texture() const
{
// bound each frame: each image uploaded by a worker comes in a texture of its own, and a name deleted comes back for
// a later one, so a name seen before may be another texture already. bound under the lock the drawing thread deletes
// the old texture under, so the name read here is never one already gone
std::scoped_lock guard(m_rt->mutex);
size_t const texture = m_rt->texture_id;
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(texture != 0 ? texture : m_initialtexture));
}
void python_screen_viewer::notify_window_fb_size(GLFWwindow const *window, int w, int h) const
{
for (auto const &conf : m_windows) {
if (conf->window == window) {
conf->fb_size.x = w;
conf->fb_size.y = h;
return;
}
}
}
void python_screen_viewer::notify_window_size(GLFWwindow const *window, int w, int h) const
{
for (auto const &conf : m_windows) {
if (conf->window == window) {
conf->window_size.x = w;
conf->window_size.y = h;
return;
}
}
}
void python_screen_viewer::notify_cursor_pos(GLFWwindow const *window, double x, double y) const
{
for (auto const &conf : m_windows) {
if (conf->window == window) {
conf->cursor_pos.x = static_cast<int>(x);
conf->cursor_pos.y = static_cast<int>(y);
return;
}
}
}
void python_screen_viewer::notify_click(GLFWwindow const *window, int button, int action) const
{
if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS)
return;
for (auto const &conf : m_windows) {
if (conf->window == window) {
auto pos = glm::vec2(conf->cursor_pos) / glm::vec2(conf->window_size);
pos.y = 1.0f - pos.y;
pos = (pos + conf->offset) / conf->scale;
m_touchlist->emplace_back(pos);
return;
}
}
}
python_screen_viewer::window_state::~window_state()
{
if (!window)
return;
if (!Global.python_sharectx) {
GLFWwindow *current = glfwGetCurrentContext();
glfwMakeContextCurrent(window);
ubo = nullptr;
shader = nullptr;
gl::program::unbind();
gl::buffer::unbind();
glfwMakeContextCurrent(current);
}
glfwDestroyWindow(window);
}