diff --git a/client/renderers/EGL/compose.c b/client/renderers/EGL/compose.c index e8143494..147bcf59 100644 --- a/client/renderers/EGL/compose.c +++ b/client/renderers/EGL/compose.c @@ -44,9 +44,12 @@ struct EGL_Compose { EGL_Framebuffer * framebuffer; - EGL_Shader * shader; - EGL_Uniform * uViewSize; - EGL_Uniform * uAtlasSize; + /* [0] samples a plain texture, [1] an imported DMA buffer, which needs the + * external image sampler. Which one runs is chosen at setup. */ + EGL_Shader * shader [2]; + EGL_Uniform * uViewSize [2]; + EGL_Uniform * uAtlasSize[2]; + bool useDMA; GLuint vao; GLuint buffers[2]; @@ -68,20 +71,29 @@ bool egl_composeInit(EGL_Compose ** compose) EGL_Compose * this = *compose; - if (!egl_framebufferInit(&this->framebuffer) || - !egl_shaderInit(&this->shader) || - !egl_shaderCompile(this->shader, - b_shader_compose_vert, b_shader_compose_vert_size, - b_shader_compose_frag, b_shader_compose_frag_size, - false, NULL)) + if (!egl_framebufferInit(&this->framebuffer)) { DEBUG_ERROR("Failed to initialize the display composition"); egl_composeFree(compose); return false; } - this->uViewSize = egl_shaderGetUniform(this->shader, "viewSize" ); - this->uAtlasSize = egl_shaderGetUniform(this->shader, "atlasSize"); + for(int i = 0; i < 2; ++i) + { + if (!egl_shaderInit(&this->shader[i]) || + !egl_shaderCompile(this->shader[i], + b_shader_compose_vert, b_shader_compose_vert_size, + b_shader_compose_frag, b_shader_compose_frag_size, + i == 1, NULL)) + { + DEBUG_ERROR("Failed to initialize the display composition"); + egl_composeFree(compose); + return false; + } + + this->uViewSize [i] = egl_shaderGetUniform(this->shader[i], "viewSize" ); + this->uAtlasSize[i] = egl_shaderGetUniform(this->shader[i], "atlasSize"); + } glGenVertexArrays(1, &this->vao); egl_stateBindVertexArray(this->vao); @@ -134,16 +146,19 @@ void egl_composeFree(EGL_Compose ** compose) egl_stateInvalidateShared(); } - egl_shaderFree(&this->shader); + egl_shaderFree(&this->shader[0]); + egl_shaderFree(&this->shader[1]); egl_framebufferFree(&this->framebuffer); free(this); *compose = NULL; } -bool egl_composeSetup(EGL_Compose * this, const LG_RendererFormat * format) +bool egl_composeSetup(EGL_Compose * this, const LG_RendererFormat * format, + bool useDMA) { this->configured = false; this->quadCount = 0; + this->useDMA = useDMA; if (!format->displayCount) return false; @@ -265,10 +280,11 @@ EGL_Texture * egl_composeRun(EGL_Compose * this, EGL_Texture * atlas) glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); + const int variant = this->useDMA ? 1 : 0; egl_textureBind(atlas); - egl_shaderUse(this->shader); - egl_uniform2f(this->uViewSize , this->viewWidth , this->viewHeight ); - egl_uniform2f(this->uAtlasSize, this->atlasWidth, this->atlasHeight); + egl_uniform2f(this->uViewSize [variant], this->viewWidth , this->viewHeight ); + egl_uniform2f(this->uAtlasSize[variant], this->atlasWidth, this->atlasHeight); + egl_shaderUse(this->shader[variant]); egl_stateBindVertexArray(this->vao); glDrawElements(GL_TRIANGLES, this->quadCount * INDICES_PER_DISPLAY, diff --git a/client/renderers/EGL/compose.h b/client/renderers/EGL/compose.h index d02c611a..d14e6db0 100644 --- a/client/renderers/EGL/compose.h +++ b/client/renderers/EGL/compose.h @@ -44,7 +44,8 @@ void egl_composeFree(EGL_Compose ** compose); * Returns false if the format cannot be composed, in which case the caller * should fall back to presenting the raw atlas rather than showing nothing. */ -bool egl_composeSetup(EGL_Compose * compose, const LG_RendererFormat * format); +bool egl_composeSetup(EGL_Compose * compose, const LG_RendererFormat * format, + bool useDMA); /** * Compose `atlas` into the configured view. diff --git a/client/renderers/EGL/desktop.c b/client/renderers/EGL/desktop.c index ac7bc49d..d0d4cacf 100644 --- a/client/renderers/EGL/desktop.c +++ b/client/renderers/EGL/desktop.c @@ -114,6 +114,10 @@ struct EGL_Desktop * width/height below describe the composed view rather than the atlas. */ EGL_Compose * compose; bool composeActive; + /* Compose setup creates GL container objects (VAO, FBO) that only exist in + * the context that made them. The format arrives on the frame thread, so + * setup is deferred to the render thread via this flag. */ + _Atomic(bool) composeSetupPending; }; // forwards @@ -408,21 +412,16 @@ bool egl_desktopSetup(EGL_Desktop * desktop, const LG_RendererFormat format) desktop->composeActive = false; if (format.displayCount) { - if (egl_composeSetup(desktop->compose, &desktop->format)) - { - unsigned int viewWidth, viewHeight; - egl_composeGetSize(desktop->compose, &viewWidth, &viewHeight); - desktop->width = viewWidth; - desktop->height = viewHeight; - desktop->composeActive = true; - - DEBUG_INFO("Composing %u displays from a %ux%u atlas into a %ux%u view", - format.displayCount, format.frameWidth, format.frameHeight, - desktop->width, desktop->height); - } - else - DEBUG_WARN("Display composition unavailable, showing the packed frame"); + /* The GL objects composition needs (VAO, FBO) are container objects and + * only exist in the creating context. This runs on the frame thread's + * context, so only record the intent here; the render thread performs + * the actual setup on its own context before the next draw. */ + desktop->width = format.viewWidth; + desktop->height = format.viewHeight; + atomic_store(&desktop->composeSetupPending, true); } + else + atomic_store(&desktop->composeSetupPending, false); return true; } @@ -518,6 +517,36 @@ bool egl_desktopRender(EGL_Desktop * desktop, unsigned int outputWidth, DEBUG_ERROR("Failed to process the desktop texture"); } + if (atomic_exchange(&desktop->composeSetupPending, false)) + { + if (egl_composeSetup(desktop->compose, &desktop->format, + desktop->useDMA)) + { + unsigned int viewWidth, viewHeight; + egl_composeGetSize(desktop->compose, &viewWidth, &viewHeight); + desktop->width = viewWidth; + desktop->height = viewHeight; + desktop->composeActive = true; + width = viewWidth; + height = viewHeight; + atomic_store(&desktop->processFrame, true); + + DEBUG_INFO("Composing %u displays from a %ux%u atlas into a %ux%u view", + desktop->format.displayCount, + desktop->format.frameWidth, desktop->format.frameHeight, + desktop->width, desktop->height); + } + else + { + DEBUG_WARN("Display composition unavailable, showing the packed frame"); + desktop->composeActive = false; + desktop->width = desktop->format.frameWidth; + desktop->height = desktop->format.frameHeight; + width = desktop->width; + height = desktop->height; + } + } + /* Compose the packed atlas into the presented view before anything else * runs, so post processing, HDR, the cursor and damage all operate on one * ordinary frame and need no multi-display handling of their own. */