From 1e3621f943c17a7ad6e3f22cb7ed692ccef0b3e9 Mon Sep 17 00:00:00 2001 From: Gaurav Ujjwal Date: Sun, 24 Mar 2024 00:14:39 +0530 Subject: [PATCH] Align pixel format with common server convention This a workaround for servers which don't respect the pixel format sent by AVNC. --- app/src/main/cpp/native-vnc.cpp | 15 +++++++++++++++ .../java/com/gaurav/avnc/ui/vnc/gl/Shaders.kt | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/native-vnc.cpp b/app/src/main/cpp/native-vnc.cpp index 01fb294a..19b47f88 100644 --- a/app/src/main/cpp/native-vnc.cpp +++ b/app/src/main/cpp/native-vnc.cpp @@ -293,6 +293,17 @@ Java_com_gaurav_avnc_vnc_VncClient_nativeConfigure(JNIEnv *env, jobject thiz, jl client->appData.qualityLevel = image_quality; if (use_raw_encoding) client->appData.encodingsString = "raw"; + + // Change pixel format to match with the default format used by most VNC + // servers. Technically, we should not have to this as VNC servers have to + // respect whatever pixel format client prefers. But there are some wierd + // servers which ignore pixel format sent by client. As a result, users + // blame AVNC for not rendering the colors properly. + // Note: This change affects how OpenGL Texture for framebuffer is rendered, + // and it only works for 24 bit-per-pixel density. + client->format.redShift = 16; + client->format.greenShift = 8; + client->format.blueShift = 0; } extern "C" @@ -480,6 +491,10 @@ Java_com_gaurav_avnc_vnc_VncClient_nativeUploadFrameTexture(JNIEnv *env, jobject GL_RGBA, GL_UNSIGNED_BYTE, client->frameBuffer); + + // Note: client->frameBuffer data is actually in 'BGRA' format, instead of 'RGBA'. + // But OpenGL ES doesn't support that directly. So we use 'GL_RGBA' here, and flip + // the components to correct order inside fragment shader. } UNLOCK(ex->mutex); diff --git a/app/src/main/java/com/gaurav/avnc/ui/vnc/gl/Shaders.kt b/app/src/main/java/com/gaurav/avnc/ui/vnc/gl/Shaders.kt index 3fef6fd8..44084f29 100644 --- a/app/src/main/java/com/gaurav/avnc/ui/vnc/gl/Shaders.kt +++ b/app/src/main/java/com/gaurav/avnc/ui/vnc/gl/Shaders.kt @@ -31,6 +31,6 @@ object Shaders { varying vec2 v_TextureCoordinates; void main() { - gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates); + gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates).bgra; }""" } \ No newline at end of file