Skip to content
Permalink
Browse files

[client] improve streaming texture performance

  • Loading branch information...
gnif committed Jun 12, 2019
1 parent 6d24dd5 commit 607539a2afbf3e99d50f017984504b3213420cbb
@@ -1 +1 @@
B1-rc5-8-ge3343cbd01+1
B1-rc5-10-g75de895b62+1
@@ -56,6 +56,7 @@ struct EGL_Desktop
struct DesktopShader shader_yuv;

// internals
LG_Lock updateLock;
enum EGL_PixelFormat pixFmt;
unsigned int width, height;
unsigned int pitch;
@@ -140,6 +141,8 @@ bool egl_desktop_init(EGL_Desktop ** desktop)
egl_model_set_default((*desktop)->model);
egl_model_set_texture((*desktop)->model, (*desktop)->texture);

LG_LOCK_INIT((*desktop)->updateLock);

(*desktop)->kbNV = app_register_keybind(SDL_SCANCODE_N, egl_desktop_toggle_nv, *desktop);

(*desktop)->nvMax = option_get_int("egl", "nvGainMax");
@@ -165,6 +168,8 @@ void egl_desktop_free(EGL_Desktop ** desktop)
if (!*desktop)
return;

LG_LOCK_FREE((*desktop)->updateLock);

egl_texture_free(&(*desktop)->texture );
egl_shader_free (&(*desktop)->shader_generic.shader);
egl_shader_free (&(*desktop)->shader_yuv.shader );
@@ -180,6 +185,7 @@ bool egl_desktop_prepare_update(EGL_Desktop * desktop, const bool sourceChanged,
{
if (sourceChanged)
{
LG_LOCK(desktop->updateLock);
switch(format.type)
{
case FRAME_TYPE_BGRA:
@@ -204,24 +210,30 @@ bool egl_desktop_prepare_update(EGL_Desktop * desktop, const bool sourceChanged,

default:
DEBUG_ERROR("Unsupported frame format");
LG_UNLOCK(desktop->updateLock);
return false;
}

desktop->width = format.width;
desktop->height = format.height;
desktop->pitch = format.pitch;
}
desktop->data = data;
desktop->update = true;

desktop->data = data;
desktop->update = true;
/* defer the actual update as the format has changed and we need to issue GL commands first */
LG_UNLOCK(desktop->updateLock);
return true;
}

return true;
/* update the texture now */
return egl_texture_update(desktop->texture, data);
}

bool egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged)
void egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged)
{
if (sourceChanged)
{
LG_LOCK(desktop->updateLock);
if (!egl_texture_setup(
desktop->texture,
desktop->pixFmt,
@@ -232,27 +244,26 @@ bool egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged)
))
{
DEBUG_ERROR("Failed to setup the desktop texture");
return false;
LG_UNLOCK(desktop->updateLock);
return;
}
LG_UNLOCK(desktop->updateLock);
}

if (!desktop->update)
return true;

if (!egl_texture_update(desktop->texture, desktop->data))
if (desktop->update)
{
DEBUG_ERROR("Failed to update the desktop texture");
return false;
desktop->update = false;
egl_texture_update(desktop->texture, desktop->data);
}

desktop->update = false;
return true;
}

void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY, const bool nearest)
bool egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY, const bool nearest)
{
if (!desktop->shader)
return;
return false;

if (egl_texture_process(desktop->texture) != EGL_TEX_STATUS_OK)
return false;

const struct DesktopShader * shader = desktop->shader;
egl_shader_use(shader->shader);
@@ -269,4 +280,5 @@ void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, con
glUniform1i(shader->uNV, 0);

egl_model_render(desktop->model);
return true;
}
@@ -29,5 +29,5 @@ bool egl_desktop_init(EGL_Desktop ** desktop);
void egl_desktop_free(EGL_Desktop ** desktop);

bool egl_desktop_prepare_update(EGL_Desktop * desktop, const bool sourceChanged, const LG_RendererFormat format, const uint8_t * data);
bool egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged);
void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY, const bool nearest);
void egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged);
bool egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY, const bool nearest);
@@ -318,9 +318,6 @@ bool egl_on_frame_event(void * opaque, const LG_RendererFormat format, const uin
return false;
}

if (!this->waitFadeTime)
this->waitFadeTime = microtime() + SPLASH_FADE_TIME;

return true;
}

@@ -493,8 +490,12 @@ bool egl_render(void * opaque, SDL_Window * window)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

egl_desktop_render(this->desktop, this->translateX, this->translateY, this->scaleX, this->scaleY, this->useNearest);
egl_cursor_render(this->cursor);
if (egl_desktop_render(this->desktop, this->translateX, this->translateY, this->scaleX, this->scaleY, this->useNearest))
{
if (!this->waitFadeTime)
this->waitFadeTime = microtime() + SPLASH_FADE_TIME;
egl_cursor_render(this->cursor);
}

if (!this->waitDone)
{
@@ -535,13 +536,9 @@ bool egl_render(void * opaque, SDL_Window * window)
eglSwapBuffers(this->display, this->surface);

// defer texture uploads until after the flip to avoid stalling
if (!egl_desktop_perform_update(this->desktop, this->sourceChanged))
{
DEBUG_ERROR("Failed to perform the desktop update");
return false;
}
this->sourceChanged = false;
egl_desktop_perform_update(this->desktop, this->sourceChanged);

this->sourceChanged = false;
return true;
}

@@ -32,6 +32,7 @@ struct EGL_Texture
enum EGL_PixelFormat pixFmt;
size_t width, height;
bool streaming;
bool ready;

int textureCount;
GLuint textures[3];
@@ -44,10 +45,12 @@ struct EGL_Texture

bool hasPBO;
GLuint pbo[2];
int pboIndex;
bool needsUpdate;
int pboRIndex;
int pboWIndex;
int pboCount;
size_t pboBufferSize;
void * pboMap[2];
GLsync pboSync[2];
};

bool egl_texture_init(EGL_Texture ** texture)
@@ -81,6 +84,9 @@ void egl_texture_free(EGL_Texture ** texture)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, (*texture)->pbo[i]);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);

if ((*texture)->pboSync[i])
glDeleteSync((*texture)->pboSync[i]);
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glDeleteBuffers(2, (*texture)->pbo);
@@ -98,6 +104,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
texture->width = width;
texture->height = height;
texture->streaming = streaming;
texture->ready = false;

switch(pixFmt)
{
@@ -210,8 +217,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
texture->pboBufferSize,
NULL,
GL_MAP_PERSISTENT_BIT |
GL_MAP_WRITE_BIT |
GL_MAP_COHERENT_BIT
GL_MAP_WRITE_BIT
);

texture->pboMap[i] = glMapBufferRange(
@@ -221,7 +227,8 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
GL_MAP_PERSISTENT_BIT |
GL_MAP_WRITE_BIT |
GL_MAP_UNSYNCHRONIZED_BIT |
GL_MAP_INVALIDATE_BUFFER_BIT
GL_MAP_INVALIDATE_BUFFER_BIT |
GL_MAP_FLUSH_EXPLICIT_BIT
);

if (!texture->pboMap[i])
@@ -240,22 +247,23 @@ bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
{
if (texture->streaming)
{
if (texture->needsUpdate)
{
DEBUG_ERROR("Previous frame was not consumed");
return false;
}
/* NOTE: DO NOT use any gl commands here as streaming must be thread safe */

if (++texture->pboIndex == 2)
texture->pboIndex = 0;
if (texture->pboCount == 2)
return true;

/* update the GPU buffer */
memcpy(texture->pboMap[texture->pboIndex], buffer, texture->pboBufferSize);
memcpy(texture->pboMap[texture->pboWIndex], buffer, texture->pboBufferSize);
texture->pboSync[texture->pboWIndex] = 0;

texture->needsUpdate = true;
if (++texture->pboWIndex == 2)
texture->pboWIndex = 0;
++texture->pboCount;
}
else
{
/* Non streaming, this is NOT thread safe */

for(int i = 0; i < texture->textureCount; ++i)
{
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
@@ -268,30 +276,87 @@ bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
return true;
}

void egl_texture_bind(EGL_Texture * texture)
enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
{
if (texture->streaming && texture->needsUpdate)
if (!texture->streaming)
return EGL_TEX_STATUS_OK;

if (texture->pboCount == 0)
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;

/* process any buffers that have not yet been flushed */
int pos = texture->pboRIndex;
for(int i = 0; i < texture->pboCount; ++i)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[texture->pboIndex]);
for(int i = 0; i < texture->textureCount; ++i)
if (texture->pboSync[pos] == 0)
{
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[i][2]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[i][0], texture->planes[i][1],
texture->format, texture->dataType, (const void *)texture->offsets[i]);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[pos]);
glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, texture->pboBufferSize);
texture->pboSync[pos] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);

texture->needsUpdate = false;
if (++pos == 2)
pos = 0;
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

/* wait for the buffer to be ready */
pos = texture->pboRIndex;
switch(glClientWaitSync(texture->pboSync[pos], GL_SYNC_FLUSH_COMMANDS_BIT, 0))
{
case GL_ALREADY_SIGNALED:
case GL_CONDITION_SATISFIED:
break;

case GL_TIMEOUT_EXPIRED:
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;

case GL_WAIT_FAILED:
glDeleteSync(texture->pboSync[pos]);
DEBUG_ERROR("glClientWaitSync failed");
return EGL_TEX_STATUS_ERROR;
}

/* delete the sync and bind the buffer */
glDeleteSync(texture->pboSync[pos]);
texture->pboSync[pos] = 0;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[pos]);

/* update the textures */
for(int i = 0; i < texture->textureCount; ++i)
{
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[i][2]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[i][0], texture->planes[i][1],
texture->format, texture->dataType, (const void *)texture->offsets[i]);
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);

/* advance the read index */
if (++texture->pboRIndex == 2)
texture->pboRIndex = 0;
--texture->pboCount;

texture->ready = true;

return EGL_TEX_STATUS_OK;
}

enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
{
/* if there are no new buffers ready, then just bind the textures */
if (texture->streaming && !texture->ready)
return EGL_TEX_STATUS_NOTREADY;

for(int i = 0; i < texture->textureCount; ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
glBindSampler(i, texture->samplers[i]);
}

return EGL_TEX_STATUS_OK;
}

int egl_texture_count(EGL_Texture * texture)
@@ -34,10 +34,18 @@ enum EGL_PixelFormat
EGL_PF_YUV420
};

enum EGL_TexStatus
{
EGL_TEX_STATUS_NOTREADY,
EGL_TEX_STATUS_OK,
EGL_TEX_STATUS_ERROR
};

bool egl_texture_init(EGL_Texture ** tex);
void egl_texture_free(EGL_Texture ** tex);

bool egl_texture_setup (EGL_Texture * texture, enum EGL_PixelFormat pixfmt, size_t width, size_t height, size_t stride, bool streaming);
bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer);
void egl_texture_bind (EGL_Texture * texture);
int egl_texture_count (EGL_Texture * texture);
bool egl_texture_setup (EGL_Texture * texture, enum EGL_PixelFormat pixfmt, size_t width, size_t height, size_t stride, bool streaming);
bool egl_texture_update (EGL_Texture * texture, const uint8_t * buffer);
enum EGL_TexStatus egl_texture_process(EGL_Texture * texture);
enum EGL_TexStatus egl_texture_bind (EGL_Texture * texture);
int egl_texture_count (EGL_Texture * texture);

0 comments on commit 607539a

Please sign in to comment.
You can’t perform that action at this time.