diff options
author | Jonathan G Rennison <j.g.rennison@gmail.com> | 2021-04-05 23:22:55 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2021-04-10 18:31:42 +0200 |
commit | 39b7ef31f8754a7e9d535d3b061a2e167ccd1338 (patch) | |
tree | 272609f16a811b5f01b6bae37ea0c8b07d7c8d9a | |
parent | fbd0a2e65a5563e8f8059aae1adb0f375f2d3348 (diff) | |
download | openttd-39b7ef31f8754a7e9d535d3b061a2e167ccd1338.tar.xz |
Fix: Data races on cursor state in OpenGL backends
-rw-r--r-- | src/video/cocoa/cocoa_ogl.mm | 2 | ||||
-rw-r--r-- | src/video/opengl.cpp | 21 | ||||
-rw-r--r-- | src/video/opengl.h | 6 | ||||
-rw-r--r-- | src/video/sdl2_opengl_v.cpp | 2 | ||||
-rw-r--r-- | src/video/win32_v.cpp | 2 |
5 files changed, 25 insertions, 8 deletions
diff --git a/src/video/cocoa/cocoa_ogl.mm b/src/video/cocoa/cocoa_ogl.mm index 8d02428e0..f8c2e97e0 100644 --- a/src/video/cocoa/cocoa_ogl.mm +++ b/src/video/cocoa/cocoa_ogl.mm @@ -134,7 +134,7 @@ static bool _allowSoftware; CGLSetCurrentContext(ctx); OpenGLBackend::Get()->Paint(); - if (_cursor.in_window) OpenGLBackend::Get()->DrawMouseCursor(); + OpenGLBackend::Get()->DrawMouseCursor(); [ super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts ]; } diff --git a/src/video/opengl.cpp b/src/video/opengl.cpp index c346fd152..c1c1afaae 100644 --- a/src/video/opengl.cpp +++ b/src/video/opengl.cpp @@ -1053,18 +1053,20 @@ void OpenGLBackend::Paint() */ void OpenGLBackend::DrawMouseCursor() { + if (!this->cursor_in_window) return; + /* Draw cursor on screen */ _cur_dpi = &_screen; - for (uint i = 0; i < _cursor.sprite_count; ++i) { - SpriteID sprite = _cursor.sprite_seq[i].sprite; + for (uint i = 0; i < this->cursor_sprite_count; ++i) { + SpriteID sprite = this->cursor_sprite_seq[i].sprite; /* Sprites are cached by PopulateCursorCache(). */ if (this->cursor_cache.Contains(sprite)) { Sprite *spr = this->cursor_cache.Get(sprite); - this->RenderOglSprite((OpenGLSprite *)spr->data, _cursor.sprite_seq[i].pal, - _cursor.pos.x + _cursor.sprite_pos[i].x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI), - _cursor.pos.y + _cursor.sprite_pos[i].y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI), + this->RenderOglSprite((OpenGLSprite *)spr->data, this->cursor_sprite_seq[i].pal, + this->cursor_pos.x + this->cursor_sprite_pos[i].x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI), + this->cursor_pos.y + this->cursor_sprite_pos[i].y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI), ZOOM_LVL_GUI); } } @@ -1072,6 +1074,9 @@ void OpenGLBackend::DrawMouseCursor() void OpenGLBackend::PopulateCursorCache() { + static_assert(lengthof(_cursor.sprite_seq) == lengthof(this->cursor_sprite_seq)); + static_assert(lengthof(_cursor.sprite_pos) == lengthof(this->cursor_sprite_pos)); + if (this->clear_cursor_cache) { /* We have a pending cursor cache clear to do first. */ this->clear_cursor_cache = false; @@ -1085,7 +1090,13 @@ void OpenGLBackend::PopulateCursorCache() } } + this->cursor_pos = _cursor.pos; + this->cursor_sprite_count = _cursor.sprite_count; + this->cursor_in_window = _cursor.in_window; + for (uint i = 0; i < _cursor.sprite_count; ++i) { + this->cursor_sprite_seq[i] = _cursor.sprite_seq[i]; + this->cursor_sprite_pos[i] = _cursor.sprite_pos[i]; SpriteID sprite = _cursor.sprite_seq[i].sprite; if (!this->cursor_cache.Contains(sprite)) { diff --git a/src/video/opengl.h b/src/video/opengl.h index c17a8536d..7e42b20be 100644 --- a/src/video/opengl.h +++ b/src/video/opengl.h @@ -65,6 +65,12 @@ private: PaletteID last_sprite_pal = (PaletteID)-1; ///< Last uploaded remap palette. bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending. + Point cursor_pos; ///< Cursor position + bool cursor_in_window; ///< Cursor inside this window + PalSpriteID cursor_sprite_seq[16]; ///< Current image of cursor + Point cursor_sprite_pos[16]; ///< Relative position of individual cursor sprites + uint cursor_sprite_count; ///< Number of cursor sprites to draw + OpenGLBackend(); ~OpenGLBackend(); diff --git a/src/video/sdl2_opengl_v.cpp b/src/video/sdl2_opengl_v.cpp index 86dc104dd..5df283751 100644 --- a/src/video/sdl2_opengl_v.cpp +++ b/src/video/sdl2_opengl_v.cpp @@ -174,7 +174,7 @@ void VideoDriver_SDL_OpenGL::Paint() } OpenGLBackend::Get()->Paint(); - if (_cursor.in_window) OpenGLBackend::Get()->DrawMouseCursor(); + OpenGLBackend::Get()->DrawMouseCursor(); SDL_GL_SwapWindow(this->sdl_window); } diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index b4d3946ea..426d74c0a 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -1454,7 +1454,7 @@ void VideoDriver_Win32OpenGL::Paint() } OpenGLBackend::Get()->Paint(); - if (_cursor.in_window) OpenGLBackend::Get()->DrawMouseCursor(); + OpenGLBackend::Get()->DrawMouseCursor(); SwapBuffers(this->dc); } |