From c7decbd6001f9e44da36085759de1503ef9c3063 Mon Sep 17 00:00:00 2001
From: Danny Lin <danny@kdrag0n.dev>
Date: Wed, 14 Apr 2021 15:57:04 -0700
Subject: [PATCH] blur: Invalidate newly-bound framebuffers before rendering
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This signals to the GPU driver that the FBO contents do not need to be
preserved. According to ARM, invalidating framebuffers after rendering
and unbinding them won't do anything on Mali GPUs [1], but it improves
performance with Qualcomm's Adreno GPU drivers.

When tested with 2 layers of another 6-pass blur implementation, this
saves ~100 µs of rendering time on an Adreno 640 GPU at 1440x3040
resolution.

[1] https://community.arm.com/developer/tools-software/graphics/b/blog/posts/mali-performance-2-how-to-correctly-handle-framebuffers

Change-Id: Ib26e904e66fbc95924d14bbe132cf8a0505d4f19
Signed-off-by: Mohammad Hasan Keramat J <ikeramat@protonmail.com>
Signed-off-by: Simão Gomes Viana <devel@superboring.dev>
---
 libs/renderengine/gl/filters/BlurFilter.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libs/renderengine/gl/filters/BlurFilter.cpp b/libs/renderengine/gl/filters/BlurFilter.cpp
index 402b5469e3..f6660a75cd 100644
--- a/libs/renderengine/gl/filters/BlurFilter.cpp
+++ b/libs/renderengine/gl/filters/BlurFilter.cpp
@@ -31,6 +31,9 @@ namespace android {
 namespace renderengine {
 namespace gl {
 
+// This needs to be located in .rodata to get a pointer for the OpenGL API.
+static const GLenum kInvalidateAttachment = GL_COLOR_ATTACHMENT0;
+
 BlurFilter::BlurFilter(GLESRenderEngine& engine)
       : mEngine(engine),
         mCompositionFbo(engine),
@@ -122,6 +125,7 @@ status_t BlurFilter::setAsDrawTarget(const DisplaySettings& display, uint32_t ra
     }
 
     mCompositionFbo.bind();
+    glInvalidateFramebuffer(GL_FRAMEBUFFER, 1, &kInvalidateAttachment);
     glViewport(0, 0, mCompositionFbo.getBufferWidth(), mCompositionFbo.getBufferHeight());
     return NO_ERROR;
 }
@@ -152,6 +156,7 @@ status_t BlurFilter::prepare() {
     GLFramebuffer* draw = &mPingFbo;
     read->bindAsReadBuffer();
     draw->bindAsDrawBuffer();
+    glInvalidateFramebuffer(GL_FRAMEBUFFER, 1, &kInvalidateAttachment);
     glBlitFramebuffer(0, 0,
                       read->getBufferWidth(), read->getBufferHeight(),
                       0, 0,
@@ -162,6 +167,7 @@ status_t BlurFilter::prepare() {
 
     // And now we'll ping pong between our textures, to accumulate the result of various offsets.
     mBlurProgram.useProgram();
+    glInvalidateFramebuffer(GL_FRAMEBUFFER, 1, &kInvalidateAttachment);
     glViewport(0, 0, draw->getBufferWidth(), draw->getBufferHeight());
     for (auto i = 1; i < passes; i++) {
         ATRACE_NAME("BlurFilter::renderPass");
-- 
GitLab