From 6e883c535b91c55289d54aa639199ca0a4decaec Mon Sep 17 00:00:00 2001
From: Gunnar Sletta <gunnar@sletta.org>
Date: Thu, 23 Oct 2014 11:28:22 +0200
Subject: [PATCH] Fix pixelgrid snapping of native text on retina displays.

Change 63e6c9ada82dc8f16e705cef5f89292784b7ace4 introduced snapping to
the pixel grid in the vertex shader for native text, but this code was
broken on retina displays because it assumed integer only positions.
Fix it by including the retina scale factor into the rounding.

Task-number: QTBUG-38702
Change-Id: I84492b02d64f263c9fe030790e04cf79b0dc4e2f
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
---
 .../scenegraph/qsgdefaultglyphnode_p.cpp      | 28 ++++++++++++-------
 src/quick/scenegraph/shaders/styledtext.vert  |  3 +-
 .../scenegraph/shaders/styledtext_core.vert   |  3 +-
 src/quick/scenegraph/shaders/textmask.vert    |  3 +-
 .../scenegraph/shaders/textmask_core.vert     |  3 +-
 5 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
index b30a504da9..5cca474ea1 100644
--- a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
+++ b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp
@@ -59,6 +59,21 @@ static inline QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity)
     return QVector4D(c.x() * o, c.y() * o, c.z() * o, o);
 }
 
+static inline int qsg_device_pixel_ratio(QOpenGLContext *ctx)
+{
+    int devicePixelRatio = 1;
+    if (ctx->surface()->surfaceClass() == QSurface::Window) {
+        QWindow *w = static_cast<QWindow *>(ctx->surface());
+        if (QQuickWindow *qw = qobject_cast<QQuickWindow *>(w))
+            devicePixelRatio = qw->effectiveDevicePixelRatio();
+        else
+            devicePixelRatio = w->devicePixelRatio();
+    } else {
+        devicePixelRatio = ctx->screen()->devicePixelRatio();
+    }
+    return devicePixelRatio;
+}
+
 class QSGTextMaskShader : public QSGMaterialShader
 {
 public:
@@ -102,6 +117,7 @@ void QSGTextMaskShader::initialize()
     m_matrix_id = program()->uniformLocation("matrix");
     m_color_id = program()->uniformLocation("color");
     m_textureScale_id = program()->uniformLocation("textureScale");
+    program()->setUniformValue("dpr", (float) qsg_device_pixel_ratio(QOpenGLContext::currentContext()));
 }
 
 void QSGTextMaskShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
@@ -351,16 +367,8 @@ void QSGTextMaskMaterial::init(QFontEngine::GlyphFormat glyphFormat)
                         : QFontEngine::Format_A32;
         }
 
-        qreal devicePixelRatio;
-        if (ctx->surface()->surfaceClass() == QSurface::Window) {
-            QWindow *w = static_cast<QWindow *>(ctx->surface());
-            if (QQuickWindow *qw = qobject_cast<QQuickWindow *>(w))
-                devicePixelRatio = qw->effectiveDevicePixelRatio();
-            else
-                devicePixelRatio = w->devicePixelRatio();
-        } else {
-            devicePixelRatio = ctx->screen()->devicePixelRatio();
-        }
+        qreal devicePixelRatio = qsg_device_pixel_ratio(ctx);
+
 
         QTransform glyphCacheTransform = QTransform::fromScale(devicePixelRatio, devicePixelRatio);
         if (!fontEngine->supportsTransformation(glyphCacheTransform))
diff --git a/src/quick/scenegraph/shaders/styledtext.vert b/src/quick/scenegraph/shaders/styledtext.vert
index 14fefc2564..7001bbc262 100644
--- a/src/quick/scenegraph/shaders/styledtext.vert
+++ b/src/quick/scenegraph/shaders/styledtext.vert
@@ -1,6 +1,7 @@
 uniform highp mat4 matrix;
 uniform highp vec2 textureScale;
 uniform highp vec2 shift;
+uniform highp float dpr;
 
 attribute highp vec4 vCoord;
 attribute highp vec2 tCoord;
@@ -12,5 +13,5 @@ void main()
 {
      sampleCoord = tCoord * textureScale;
      shiftedSampleCoord = (tCoord - shift) * textureScale;
-     gl_Position = matrix * floor(vCoord + 0.5);
+     gl_Position = matrix * floor(vCoord * dpr + 0.5) / dpr;
 }
diff --git a/src/quick/scenegraph/shaders/styledtext_core.vert b/src/quick/scenegraph/shaders/styledtext_core.vert
index 65bdb66814..c522877bb3 100644
--- a/src/quick/scenegraph/shaders/styledtext_core.vert
+++ b/src/quick/scenegraph/shaders/styledtext_core.vert
@@ -9,10 +9,11 @@ out vec2 shiftedSampleCoord;
 uniform mat4 matrix;
 uniform vec2 textureScale;
 uniform vec2 shift;
+uniform float dpr;
 
 void main()
 {
      sampleCoord = tCoord * textureScale;
      shiftedSampleCoord = (tCoord - shift) * textureScale;
-     gl_Position = matrix * round(vCoord);
+     gl_Position = matrix * round(vCoord * dpr) / dpr;
 }
diff --git a/src/quick/scenegraph/shaders/textmask.vert b/src/quick/scenegraph/shaders/textmask.vert
index dd8918839e..4c678270d0 100644
--- a/src/quick/scenegraph/shaders/textmask.vert
+++ b/src/quick/scenegraph/shaders/textmask.vert
@@ -1,5 +1,6 @@
 uniform highp mat4 matrix;
 uniform highp vec2 textureScale;
+uniform highp float dpr;
 
 attribute highp vec4 vCoord;
 attribute highp vec2 tCoord;
@@ -9,5 +10,5 @@ varying highp vec2 sampleCoord;
 void main()
 {
      sampleCoord = tCoord * textureScale;
-     gl_Position = matrix * floor(vCoord + 0.5);
+     gl_Position = matrix * floor(vCoord * dpr + 0.5) / dpr;
 }
diff --git a/src/quick/scenegraph/shaders/textmask_core.vert b/src/quick/scenegraph/shaders/textmask_core.vert
index d145d33195..f996040f70 100644
--- a/src/quick/scenegraph/shaders/textmask_core.vert
+++ b/src/quick/scenegraph/shaders/textmask_core.vert
@@ -7,9 +7,10 @@ out vec2 sampleCoord;
 
 uniform mat4 matrix;
 uniform vec2 textureScale;
+uniform float dpr;
 
 void main()
 {
      sampleCoord = tCoord * textureScale;
-     gl_Position = matrix * round(vCoord);
+     gl_Position = matrix * round(vCoord * dpr) / dpr;
 }
-- 
GitLab