[client] egl: use atomic members instead of locking the entire state

This commit is contained in:
Geoffrey McRae 2020-05-21 08:20:30 +10:00
parent f8ff3faf78
commit a6d6a49f82
2 changed files with 19 additions and 40 deletions

View file

@ -1 +1 @@
B1-199-gd899c26617+1 B1-200-gf8ff3faf78+1

View file

@ -19,7 +19,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "texture.h" #include "texture.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/locking.h"
#include "common/framebuffer.h" #include "common/framebuffer.h"
#include "debug.h" #include "debug.h"
#include "utils.h" #include "utils.h"
@ -44,7 +43,7 @@ struct Tex
union TexState union TexState
{ {
uint32_t v; _Atomic(uint32_t) v;
struct struct
{ {
/* /*
@ -53,7 +52,7 @@ union TexState
* s = schedule * s = schedule
* d = display * d = display
*/ */
int8_t w, u, s, d; _Atomic(int8_t) w, u, s, d;
}; };
}; };
@ -74,7 +73,6 @@ struct EGL_Texture
GLenum dataType; GLenum dataType;
size_t pboBufferSize; size_t pboBufferSize;
LG_Lock lock;
union TexState state; union TexState state;
struct Tex tex[TEXTURE_COUNT]; struct Tex tex[TEXTURE_COUNT];
}; };
@ -89,7 +87,6 @@ bool egl_texture_init(EGL_Texture ** texture)
} }
memset(*texture, 0, sizeof(EGL_Texture)); memset(*texture, 0, sizeof(EGL_Texture));
LG_LOCK_INIT((*texture)->lock);
return true; return true;
} }
@ -118,7 +115,6 @@ void egl_texture_free(EGL_Texture ** texture)
} }
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
LG_LOCK_FREE((*texture)->lock);
free(*texture); free(*texture);
*texture = NULL; *texture = NULL;
} }
@ -133,7 +129,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
texture->stride = stride; texture->stride = stride;
texture->streaming = streaming; texture->streaming = streaming;
texture->ready = false; texture->ready = false;
texture->state.v = 0; atomic_store_explicit(&texture->state.v, 0, memory_order_relaxed);
switch(pixFmt) switch(pixFmt)
{ {
@ -302,9 +298,7 @@ bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
/* NOTE: DO NOT use any gl commands here as streaming must be thread safe */ /* NOTE: DO NOT use any gl commands here as streaming must be thread safe */
union TexState s; union TexState s;
LG_LOCK(texture->lock); s.v = atomic_load_explicit(&texture->state.v, memory_order_acquire);
s.v = texture->state.v;
LG_UNLOCK(texture->lock);
const uint8_t next = (s.w + 1) % TEXTURE_COUNT; const uint8_t next = (s.w + 1) % TEXTURE_COUNT;
if (next == s.u) if (next == s.u)
@ -314,10 +308,7 @@ bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
} }
memcpy(texture->tex[s.w].map, buffer, texture->pboBufferSize); memcpy(texture->tex[s.w].map, buffer, texture->pboBufferSize);
atomic_store_explicit(&texture->state.w, next, memory_order_release);
LG_LOCK(texture->lock);
texture->state.w = next;
LG_UNLOCK(texture->lock);
} }
else else
{ {
@ -341,9 +332,7 @@ bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * fr
return false; return false;
union TexState s; union TexState s;
LG_LOCK(texture->lock); s.v = atomic_load_explicit(&texture->state.v, memory_order_acquire);
s.v = texture->state.v;
LG_UNLOCK(texture->lock);
const uint8_t next = (s.w + 1) % TEXTURE_COUNT; const uint8_t next = (s.w + 1) % TEXTURE_COUNT;
if (next == s.u) if (next == s.u)
@ -362,10 +351,7 @@ bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * fr
texture->stride texture->stride
); );
LG_LOCK(texture->lock); atomic_store_explicit(&texture->state.w, next, memory_order_release);
texture->state.w = next;
LG_UNLOCK(texture->lock);
return true; return true;
} }
@ -375,9 +361,7 @@ enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
return EGL_TEX_STATUS_OK; return EGL_TEX_STATUS_OK;
union TexState s; union TexState s;
LG_LOCK(texture->lock); s.v = atomic_load_explicit(&texture->state.v, memory_order_acquire);
s.v = texture->state.v;
LG_UNLOCK(texture->lock);
const uint8_t nextu = (s.u + 1) % TEXTURE_COUNT; const uint8_t nextu = (s.u + 1) % TEXTURE_COUNT;
if (s.u == s.w || nextu == s.s || nextu == s.d) if (s.u == s.w || nextu == s.s || nextu == s.d)
@ -399,21 +383,16 @@ enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
texture->tex[s.u].sync = texture->tex[s.u].sync =
glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
LG_LOCK(texture->lock); atomic_store_explicit(&texture->state.u, nextu, memory_order_release);
texture->state.u = nextu;
LG_UNLOCK(texture->lock);
texture->ready = true; texture->ready = true;
return EGL_TEX_STATUS_OK; return EGL_TEX_STATUS_OK;
} }
enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture) enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
{ {
union TexState s; union TexState s;
LG_LOCK(texture->lock); s.v = atomic_load_explicit(&texture->state.v, memory_order_acquire);
s.v = texture->state.v;
LG_UNLOCK(texture->lock);
if (texture->streaming) if (texture->streaming)
{ {
@ -429,9 +408,8 @@ enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
glDeleteSync(texture->tex[s.s].sync); glDeleteSync(texture->tex[s.s].sync);
texture->tex[s.s].sync = 0; texture->tex[s.s].sync = 0;
LG_LOCK(texture->lock); s.s = (s.s + 1) % TEXTURE_COUNT;
texture->state.s = (s.s + 1) % TEXTURE_COUNT; atomic_store_explicit(&texture->state.s, s.s, memory_order_release);
LG_UNLOCK(texture->lock);
break; break;
case GL_TIMEOUT_EXPIRED: case GL_TIMEOUT_EXPIRED:
@ -445,11 +423,12 @@ enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
} }
} }
LG_LOCK(texture->lock); const int8_t nextd = (s.d + 1) % TEXTURE_COUNT;
const int8_t nextd = (texture->state.d + 1) % TEXTURE_COUNT; if (s.d != s.s && nextd != s.s)
if (texture->state.d != texture->state.s && nextd != texture->state.s) {
texture->state.d = nextd; s.d = nextd;
LG_UNLOCK(texture->lock); atomic_store_explicit(&texture->state.d, nextd, memory_order_release);
}
} }
for(int i = 0; i < texture->planeCount; ++i) for(int i = 0; i < texture->planeCount; ++i)