From 6cb14219784d0cba05b2274d9bb5fb847cb4e9cf Mon Sep 17 00:00:00 2001
From: Bradley Austin Davis <bdavis@saintandreas.org>
Date: Mon, 21 Oct 2019 16:04:08 -0700
Subject: [PATCH] Qt patch for mac video

---
 tools/qt-builder/README.md                   |   9 +-
 tools/qt-builder/patches/mac-web-video.patch | 247 +++++++++++++++++++
 2 files changed, 253 insertions(+), 3 deletions(-)
 create mode 100644 tools/qt-builder/patches/mac-web-video.patch

diff --git a/tools/qt-builder/README.md b/tools/qt-builder/README.md
index 956dda4534..59db078aa9 100644
--- a/tools/qt-builder/README.md
+++ b/tools/qt-builder/README.md
@@ -1,8 +1,10 @@
 # General
 This document describes the process to build Qt 5.12.3.
-Note that there are three patches.  The first (to qfloat16.h) is needed to compile QT 5.12.3 on Visual Studio 2017 due to a bug in Visual Studio (*bitset* will not compile.  Note that there is a change in CMakeLists.txt to support this.  
-The second patch is to OpenSL ES audio.
-The third is a patch to QScriptEngine to prevent crashes in QScriptEnginePrivate::reportAdditionalMemoryCost, during garbage collection.  See https://bugreports.qt.io/browse/QTBUG-76176
+Note that there are several patches.  
+* The first (to qfloat16.h) is needed to compile QT 5.12.3 on Visual Studio 2017 due to a bug in Visual Studio (*bitset* will not compile.  Note that there is a change in CMakeLists.txt to support this.  
+* The second patch is to OpenSL ES audio and allow audio echo cancelllation on Android.
+* The third is a patch to QScriptEngine to prevent crashes in QScriptEnginePrivate::reportAdditionalMemoryCost, during garbage collection.  See https://bugreports.qt.io/browse/QTBUG-76176
+* The fourth is a patch which fixes video playback on WebEngineViews on mac.  See https://bugreports.qt.io/browse/QTBUG-70967
 ## Requirements
 ### Windows
 1.  Visual Studio 2017  
@@ -222,6 +224,7 @@ git clone --recursive git://code.qt.io/qt/qt5.git -b 5.12.3 --single-branch
 `cd qt5`  
 `git apply --ignore-space-change --ignore-whitespace patches/aec.patch`  
 `git apply --ignore-space-change --ignore-whitespace patches/qtscript-crash-fix.patch`  
+`git apply --ignore-space-change --ignore-whitespace patches/mac-web-video.patch`  
 `cd ..`  
 #### Configuring
 `mkdir qt5-install`  
diff --git a/tools/qt-builder/patches/mac-web-video.patch b/tools/qt-builder/patches/mac-web-video.patch
new file mode 100644
index 0000000000..2ea81ce18b
--- /dev/null
+++ b/tools/qt-builder/patches/mac-web-video.patch
@@ -0,0 +1,247 @@
+Submodule qtwebengine contains modified content
+diff --git a/qtwebengine/src/core/stream_video_node.cpp b/qtwebengine/src/core/stream_video_node.cpp
+index 29922f86..baa39d3b 100644
+--- a/qtwebengine/src/core/stream_video_node.cpp
++++ b/qtwebengine/src/core/stream_video_node.cpp
+@@ -62,38 +62,45 @@ protected:
+     const char *vertexShader() const override {
+         // Keep in sync with cc::VertexShaderVideoTransform
+         static const char *shader =
+-        "attribute highp vec4 a_position;\n"
+-        "attribute mediump vec2 a_texCoord;\n"
+-        "uniform highp mat4 matrix;\n"
+-        "uniform highp mat4 texMatrix;\n"
+-        "varying mediump vec2 v_texCoord;\n"
+-        "void main() {\n"
+-        "  gl_Position = matrix * a_position;\n"
+-        "  v_texCoord = vec4(texMatrix * vec4(a_texCoord.x, 1.0 - a_texCoord.y, 0.0, 1.0)).xy;\n"
+-        "}";
++            R"SHADER(#version 150 core
++in vec4 a_position;
++in vec2 a_texCoord;
++uniform mat4 matrix;
++uniform mat4 texMatrix;
++out vec2 v_texCoord;
++void main() {
++  gl_Position = matrix * a_position;
++  v_texCoord = vec4(texMatrix * vec4(a_texCoord.x, 1.0 - a_texCoord.y, 0.0, 1.0)).xy;
++}
++            )SHADER";
+         return shader;
+     }
+ 
+     const char *fragmentShader() const override {
+         // Keep in sync with cc::FragmentShaderRGBATexAlpha
+         static const char *shaderExternal  =
+-        "#extension GL_OES_EGL_image_external : require\n"
+-        "varying mediump vec2 v_texCoord;\n"
+-        "uniform samplerExternalOES s_texture;\n"
+-        "uniform lowp float alpha;\n"
+-        "void main() {\n"
+-        "  lowp vec4 texColor = texture2D(s_texture, v_texCoord);\n"
+-        "  gl_FragColor = texColor * alpha;\n"
+-        "}";
++            R"SHADER(#version 150 core
++#extension GL_OES_EGL_image_external : require
++in vec2 v_texCoord;
++uniform samplerExternalOES s_texture;
++uniform float alpha;
++out vec4 fragColor;
++void main() {
++  vec4 texColor = texture(s_texture, v_texCoord);
++  fragColor = texColor * alpha;
++}
++            )SHADER";
+         static const char *shader2DRect =
+-        "#extension GL_ARB_texture_rectangle : require\n"
+-        "varying mediump vec2 v_texCoord;\n"
+-        "uniform sampler2DRect s_texture;\n"
+-        "uniform lowp float alpha;\n"
+-        "void main() {\n"
+-        "  lowp vec4 texColor = texture2DRect(s_texture, v_texCoord);\n"
+-        "  gl_FragColor = texColor * alpha;\n"
+-        "}";
++            R"SHADER(#version 150 core
++in vec2 v_texCoord;
++uniform sampler2D s_texture;
++uniform float alpha;
++out vec4 fragColor;
++void main() {
++  vec4 texColor = texture(s_texture, v_texCoord);
++  fragColor = texColor * alpha;
++}
++            )SHADER";
+         if (m_target == ExternalTarget)
+             return shaderExternal;
+         else
+diff --git a/qtwebengine/src/core/yuv_video_node.cpp b/qtwebengine/src/core/yuv_video_node.cpp
+index 4a436d95..dc4b6ff9 100644
+--- a/qtwebengine/src/core/yuv_video_node.cpp
++++ b/qtwebengine/src/core/yuv_video_node.cpp
+@@ -59,39 +59,41 @@ public:
+     YUVVideoMaterialShader(const gfx::ColorSpace &colorSpace)
+     {
+         static const char *shaderHead =
+-            "varying mediump vec2 v_yaTexCoord;\n"
+-            "varying mediump vec2 v_uvTexCoord;\n"
+-            "uniform sampler2D y_texture;\n"
+-            "uniform sampler2D u_texture;\n"
+-            "uniform sampler2D v_texture;\n"
+-            "uniform mediump float alpha;\n"
+-            "uniform mediump vec4 ya_clamp_rect;\n"
+-            "uniform mediump vec4 uv_clamp_rect;\n";
+-        static const char *shader =
+-            "void main() {\n"
+-            "  mediump vec2 ya_clamped =\n"
+-            "      max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord));\n"
+-            "  mediump float y_raw = texture2D(y_texture, ya_clamped).x;\n"
+-            "  mediump vec2 uv_clamped =\n"
+-            "      max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));\n"
+-            "  mediump float u_unsigned = texture2D(u_texture, uv_clamped).x;\n"
+-            "  mediump float v_unsigned = texture2D(v_texture, uv_clamped).x;\n"
+-            "  mediump vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned);\n"
+-            "  mediump vec3 rgb = DoColorConversion(yuv);\n"
+-            "  gl_FragColor = vec4(rgb, 1.0) * alpha;\n"
+-            "}";
++            R"SHADER(#version 150 core
++in vec2 v_yaTexCoord;
++in vec2 v_uvTexCoord;
++uniform sampler2D y_texture;
++uniform sampler2D u_texture;
++uniform sampler2D v_texture;
++uniform float alpha;
++uniform vec4 ya_clamp_rect;
++uniform vec4 uv_clamp_rect;
++out vec4 fragColor;
++            )SHADER";
++
++        static const char *shader = R"SHADER(
++void main() {
++  vec2 ya_clamped =
++      max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord));
++  float y_raw = texture(y_texture, ya_clamped).x;
++  vec2 uv_clamped =
++      max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));
++  float u_unsigned = texture(u_texture, uv_clamped).x;
++  float v_unsigned = texture(v_texture, uv_clamped).x;
++  vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned);
++  vec3 rgb = DoColorConversion(yuv);
++  fragColor = vec4(rgb, 1.0) * alpha;
++}
++            )SHADER";
++
+         // Invalid or unspecified color spaces should be treated as REC709.
+         gfx::ColorSpace src = colorSpace.IsValid() ? colorSpace : gfx::ColorSpace::CreateREC709();
+         gfx::ColorSpace dst = gfx::ColorSpace::CreateSRGB();
+         std::unique_ptr<gfx::ColorTransform> transform =
+                 gfx::ColorTransform::NewColorTransform(src, dst, gfx::ColorTransform::Intent::INTENT_PERCEPTUAL);
+ 
+-        QByteArray header(shaderHead);
+-        if (QOpenGLContext::currentContext()->isOpenGLES())
+-            header = QByteArray("precision mediump float;\n") + header;
+-
+         m_csShader = QByteArray::fromStdString(transform->GetShaderSource());
+-        m_fragmentShader = header + m_csShader + QByteArray(shader);
++        m_fragmentShader = QByteArray(shaderHead) + m_csShader + QByteArray(shader);
+     }
+     void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
+ 
+@@ -108,20 +110,22 @@ protected:
+     const char *vertexShader() const override {
+         // Keep in sync with logic in VertexShader in components/viz/service/display/shader.cc
+         const char *shader =
+-        "attribute highp vec4 a_position;\n"
+-        "attribute mediump vec2 a_texCoord;\n"
+-        "uniform highp mat4 matrix;\n"
+-        "varying mediump vec2 v_yaTexCoord;\n"
+-        "varying mediump vec2 v_uvTexCoord;\n"
+-        "uniform mediump vec2 yaTexScale;\n"
+-        "uniform mediump vec2 yaTexOffset;\n"
+-        "uniform mediump vec2 uvTexScale;\n"
+-        "uniform mediump vec2 uvTexOffset;\n"
+-        "void main() {\n"
+-        "  gl_Position = matrix * a_position;\n"
+-        "  v_yaTexCoord = a_texCoord * yaTexScale + yaTexOffset;\n"
+-        "  v_uvTexCoord = a_texCoord * uvTexScale + uvTexOffset;\n"
+-        "}";
++        R"SHADER(#version 150 core
++in vec4 a_position;
++in vec2 a_texCoord;
++uniform mat4 matrix;
++out vec2 v_yaTexCoord;
++out vec2 v_uvTexCoord;
++uniform vec2 yaTexScale;
++uniform vec2 yaTexOffset;
++uniform vec2 uvTexScale;
++uniform vec2 uvTexOffset;
++void main() {
++  gl_Position = matrix * a_position;
++  v_yaTexCoord = a_texCoord * yaTexScale + yaTexOffset;
++  v_uvTexCoord = a_texCoord * uvTexScale + uvTexOffset;
++}
++        )SHADER";
+         return shader;
+     }
+ 
+@@ -168,33 +172,35 @@ public:
+     YUVAVideoMaterialShader(const gfx::ColorSpace &colorSpace) : YUVVideoMaterialShader(colorSpace)
+     {
+         static const char *shaderHead =
+-            "varying mediump vec2 v_yaTexCoord;\n"
+-            "varying mediump vec2 v_uvTexCoord;\n"
+-            "uniform sampler2D y_texture;\n"
+-            "uniform sampler2D u_texture;\n"
+-            "uniform sampler2D v_texture;\n"
+-            "uniform sampler2D a_texture;\n"
+-            "uniform mediump float alpha;\n"
+-            "uniform mediump vec4 ya_clamp_rect;\n"
+-            "uniform mediump vec4 uv_clamp_rect;\n";
++        R"SHADER(#version 150 core
++in vec2 v_yaTexCoord;
++in vec2 v_uvTexCoord;
++uniform sampler2D y_texture;
++uniform sampler2D u_texture;
++uniform sampler2D v_texture;
++uniform sampler2D a_texture;
++uniform float alpha;
++uniform vec4 ya_clamp_rect;
++uniform vec4 uv_clamp_rect;
++out vec4 fragColor;
++        )SHADER";
+         static const char *shader =
+-            "void main() {\n"
+-            "  mediump vec2 ya_clamped =\n"
+-            "      max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord));\n"
+-            "  mediump float y_raw = texture2D(y_texture, ya_clamped).x;\n"
+-            "  mediump vec2 uv_clamped =\n"
+-            "      max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));\n"
+-            "  mediump float u_unsigned = texture2D(u_texture, uv_clamped).x;\n"
+-            "  mediump float v_unsigned = texture2D(v_texture, uv_clamped).x;\n"
+-            "  mediump float a_raw = texture2D(a_texture, ya_clamped).x;\n"
+-            "  mediump vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned);\n"
+-            "  mediump vec3 rgb = DoColorConversion(yuv);\n"
+-            "  gl_FragColor = vec4(rgb, 1.0) * (alpha * a_raw);\n"
+-            "}";
+-        QByteArray header(shaderHead);
+-        if (QOpenGLContext::currentContext()->isOpenGLES())
+-            header = QByteArray("precision mediump float;\n") + header;
+-        m_fragmentShader = header + m_csShader + QByteArray(shader);
++        R"SHADER(
++void main() {
++  vec2 ya_clamped =
++      max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord));
++  float y_raw = texture(y_texture, ya_clamped).x;
++  vec2 uv_clamped =
++      max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));
++  float u_unsigned = texture(u_texture, uv_clamped).x;
++  float v_unsigned = texture(v_texture, uv_clamped).x;
++  float a_raw = texture(a_texture, ya_clamped).x;
++  vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned);
++  vec3 rgb = DoColorConversion(yuv);
++  fragColor = vec4(rgb, 1.0) * (alpha * a_raw);
++}
++        )SHADER";
++        m_fragmentShader = QByteArray(shaderHead) + m_csShader + QByteArray(shader);
+     }
+     void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
+