0005-cumulative-patch-for-hdr.patch 63.5 KB
Newer Older
Halla Rempt's avatar
Halla Rempt committed
1
diff --git a/src/3rdparty/angle/src/common/platform.h b/src/3rdparty/angle/src/common/platform.h
Halla Rempt's avatar
Halla Rempt committed
2
index fb251da57..2e1799455 100644
Halla Rempt's avatar
Halla Rempt committed
3 4
--- a/src/3rdparty/angle/src/common/platform.h
+++ b/src/3rdparty/angle/src/common/platform.h
Dmitry Kazakov's avatar
Dmitry Kazakov committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
@@ -59,12 +59,14 @@
 #   endif
 
 #   if defined(ANGLE_ENABLE_D3D11)
-#include <d3d10_1.h>
-#include <d3d11.h>
-#include <d3d11_3.h>
-#include <d3dcompiler.h>
-#include <dxgi.h>
-#include <dxgi1_2.h>
+#       include <d3d10_1.h>
+#       include <d3d11.h>
+#       include <dxgi.h>
+#       include <d3d11_1.h>
+#       include <d3d11_3.h>
+#       include <dxgi1_2.h>
+#       include <dxgi1_4.h> // WARNING: This is actually D3D12!
+#       include <d3dcompiler.h>
 #   endif
 
 #if defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
Halla Rempt's avatar
Halla Rempt committed
26
diff --git a/src/3rdparty/angle/src/libANGLE/Caps.cpp b/src/3rdparty/angle/src/libANGLE/Caps.cpp
Halla Rempt's avatar
Halla Rempt committed
27
index 44da2bbe2..208845745 100644
Halla Rempt's avatar
Halla Rempt committed
28 29
--- a/src/3rdparty/angle/src/libANGLE/Caps.cpp
+++ b/src/3rdparty/angle/src/libANGLE/Caps.cpp
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
@@ -1101,7 +1101,10 @@ DisplayExtensions::DisplayExtensions()
       displayTextureShareGroup(false),
       createContextClientArrays(false),
       programCacheControl(false),
-      robustResourceInitialization(false)
+      robustResourceInitialization(false),
+      colorspaceSRGB(false),
+      colorspaceSCRGBLinear(false),
+      colorspaceBt2020PQ(false)
 {
 }
 
@@ -1146,6 +1149,9 @@ std::vector<std::string> DisplayExtensions::getStrings() const
     InsertExtensionString("EGL_ANGLE_create_context_client_arrays",              createContextClientArrays,          &extensionStrings);
     InsertExtensionString("EGL_ANGLE_program_cache_control",                     programCacheControl,                &extensionStrings);
     InsertExtensionString("EGL_ANGLE_robust_resource_initialization",            robustResourceInitialization,       &extensionStrings);
+    InsertExtensionString("EGL_KHR_gl_colorspace",                               colorspaceSRGB,                     &extensionStrings);
+    InsertExtensionString("EGL_EXT_gl_colorspace_scrgb_linear",                  colorspaceSCRGBLinear,              &extensionStrings);
+    InsertExtensionString("EGL_EXT_gl_colorspace_bt2020_pq",                     colorspaceBt2020PQ,                 &extensionStrings);
     // TODO(jmadill): Enable this when complete.
     //InsertExtensionString("KHR_create_context_no_error",                       createContextNoError,               &extensionStrings);
     // clang-format on
Halla Rempt's avatar
Halla Rempt committed
52
diff --git a/src/3rdparty/angle/src/libANGLE/Caps.h b/src/3rdparty/angle/src/libANGLE/Caps.h
Halla Rempt's avatar
Halla Rempt committed
53
index 64bdf9711..8157af580 100644
Halla Rempt's avatar
Halla Rempt committed
54 55
--- a/src/3rdparty/angle/src/libANGLE/Caps.h
+++ b/src/3rdparty/angle/src/libANGLE/Caps.h
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
@@ -692,6 +692,15 @@ struct DisplayExtensions
 
     // EGL_ANGLE_robust_resource_initialization
     bool robustResourceInitialization;
+
+    // EGL_KHR_gl_colorspace
+    bool colorspaceSRGB;
+
+    // EGL_EXT_gl_colorspace_scrgb_linear
+    bool colorspaceSCRGBLinear;
+
+    // EGL_EXT_gl_colorspace_bt2020_pq
+    bool colorspaceBt2020PQ;
 };
 
 struct DeviceExtensions
Halla Rempt's avatar
Halla Rempt committed
72
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/RendererD3D.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/RendererD3D.h
Halla Rempt's avatar
Halla Rempt committed
73
index dcc98f2ec..b8ee63562 100644
Halla Rempt's avatar
Halla Rempt committed
74 75
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/RendererD3D.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/RendererD3D.h
76 77 78 79 80 81 82 83 84 85
@@ -130,7 +130,8 @@ class RendererD3D : public BufferFactoryD3D, public MultisampleTextureInitialize
                                           GLenum backBufferFormat,
                                           GLenum depthBufferFormat,
                                           EGLint orientation,
-                                          EGLint samples) = 0;
+                                          EGLint samples,
+                                          EGLint colorSpace) = 0;
     virtual egl::Error getD3DTextureInfo(const egl::Config *configuration,
                                          IUnknown *d3dTexture,
                                          EGLint *width,
Halla Rempt's avatar
Halla Rempt committed
86
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.cpp
Halla Rempt's avatar
Halla Rempt committed
87
index 7657aef79..efd4dd1a2 100644
Halla Rempt's avatar
Halla Rempt committed
88 89
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.cpp
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
@@ -22,6 +22,27 @@
 namespace rx
 {
 
+GLenum renderTargetFormatFromColorSpace(egl::Display *display, GLenum baseFormat, EGLint colorSpace)
+{
+    GLenum result = baseFormat;
+
+    /**
+     * If sRGB extension is supported, we should change the surface format
+     * to a specific one that does support automated gamma conversion.
+     *
+     * TODO: openGL doesn't support BGRA-sRGB texture format, so creation of
+     *       textures in this format technically is not supported!
+     */
+    if (display->getExtensions().colorspaceSRGB &&
+        baseFormat == GL_RGBA8_OES &&
+        colorSpace == EGL_GL_COLORSPACE_SRGB_KHR)
+    {
+        result = GL_SRGB8_ALPHA8;
+    }
+
+    return result;
+}
+
 SurfaceD3D::SurfaceD3D(const egl::SurfaceState &state,
                        RendererD3D *renderer,
                        egl::Display *display,
@@ -34,7 +55,8 @@ SurfaceD3D::SurfaceD3D(const egl::SurfaceState &state,
       mDisplay(display),
       mFixedSize(window == nullptr || attribs.get(EGL_FIXED_SIZE_ANGLE, EGL_FALSE) == EGL_TRUE),
       mOrientation(static_cast<EGLint>(attribs.get(EGL_SURFACE_ORIENTATION_ANGLE, 0))),
-      mRenderTargetFormat(state.config->renderTargetFormat),
+      mColorSpace(static_cast<EGLint>(attribs.get(EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_LINEAR_KHR))),
+      mRenderTargetFormat(renderTargetFormatFromColorSpace(display, state.config->renderTargetFormat, mColorSpace)),
       mDepthStencilFormat(state.config->depthStencilFormat),
       mSwapChain(nullptr),
       mSwapIntervalDirty(true),
@@ -148,7 +170,7 @@ egl::Error SurfaceD3D::resetSwapChain(const egl::Display *display)
 
     mSwapChain =
         mRenderer->createSwapChain(mNativeWindow, mShareHandle, mD3DTexture, mRenderTargetFormat,
-                                   mDepthStencilFormat, mOrientation, mState.config->samples);
+                                   mDepthStencilFormat, mOrientation, mState.config->samples, mColorSpace);
     if (!mSwapChain)
     {
         return egl::EglBadAlloc();
Halla Rempt's avatar
Halla Rempt committed
137
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.h
Halla Rempt's avatar
Halla Rempt committed
138
index 01d257324..ccb793d42 100644
Halla Rempt's avatar
Halla Rempt committed
139 140
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/SurfaceD3D.h
141 142 143 144 145 146 147 148
@@ -90,6 +90,7 @@ class SurfaceD3D : public SurfaceImpl
 
     bool mFixedSize;
     GLint mOrientation;
+    EGLint mColorSpace;
 
     GLenum mRenderTargetFormat;
     GLenum mDepthStencilFormat;
Halla Rempt's avatar
Halla Rempt committed
149
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
Halla Rempt's avatar
Halla Rempt committed
150
index b0ef9abdd..f0e497b52 100644
Halla Rempt's avatar
Halla Rempt committed
151 152
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
@@ -465,6 +465,7 @@ Renderer11::Renderer11(egl::Display *display)
     mRenderer11DeviceCaps.supportsConstantBufferOffsets = false;
     mRenderer11DeviceCaps.supportsVpRtIndexWriteFromVertexShader = false;
     mRenderer11DeviceCaps.supportsDXGI1_2               = false;
+    mRenderer11DeviceCaps.supportsDXGI1_4               = false;
     mRenderer11DeviceCaps.B5G6R5support                 = 0;
     mRenderer11DeviceCaps.B4G4R4A4support               = 0;
     mRenderer11DeviceCaps.B5G5R5A1support               = 0;
@@ -918,6 +919,7 @@ egl::Error Renderer11::initializeDevice()
 
     // Gather stats on DXGI and D3D feature level
     ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.SupportsDXGI1_2", mRenderer11DeviceCaps.supportsDXGI1_2);
+    ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.SupportsDXGI1_4", mRenderer11DeviceCaps.supportsDXGI1_4);
 
     ANGLEFeatureLevel angleFeatureLevel = GetANGLEFeatureLevel(mRenderer11DeviceCaps.featureLevel);
 
Dmitry Kazakov's avatar
Dmitry Kazakov committed
169
@@ -1002,6 +1004,10 @@ void Renderer11::populateRenderer11DeviceCaps()
170 171 172 173 174 175 176 177 178 179
     IDXGIAdapter2 *dxgiAdapter2 = d3d11::DynamicCastComObject<IDXGIAdapter2>(mDxgiAdapter);
     mRenderer11DeviceCaps.supportsDXGI1_2 = (dxgiAdapter2 != nullptr);
     SafeRelease(dxgiAdapter2);
+
+    IDXGIAdapter3 *dxgiAdapter3 = d3d11::DynamicCastComObject<IDXGIAdapter3>(mDxgiAdapter);
+    mRenderer11DeviceCaps.supportsDXGI1_4 = (dxgiAdapter3 != nullptr);
+    SafeRelease(dxgiAdapter3);
 }
 
 gl::SupportedSampleSet Renderer11::generateSampleSetForEGLConfig(
Dmitry Kazakov's avatar
Dmitry Kazakov committed
180
@@ -1241,6 +1247,11 @@ void Renderer11::generateDisplayExtensions(egl::DisplayExtensions *outExtensions
181 182 183 184
 
     // All D3D feature levels support robust resource init
     outExtensions->robustResourceInitialization = true;
+
Dmitry Kazakov's avatar
Dmitry Kazakov committed
185 186 187 188
+    // color space selection supported in DXGI 1.4 only
+    outExtensions->colorspaceSRGB = mRenderer11DeviceCaps.supportsDXGI1_4;
+    outExtensions->colorspaceSCRGBLinear = mRenderer11DeviceCaps.supportsDXGI1_4;
+    outExtensions->colorspaceBt2020PQ = mRenderer11DeviceCaps.supportsDXGI1_4;
189 190 191
 }
 
 gl::Error Renderer11::flush()
Dmitry Kazakov's avatar
Dmitry Kazakov committed
192
@@ -1436,10 +1447,11 @@ SwapChainD3D *Renderer11::createSwapChain(NativeWindowD3D *nativeWindow,
193 194 195 196 197 198 199 200 201 202 203 204 205
                                           GLenum backBufferFormat,
                                           GLenum depthBufferFormat,
                                           EGLint orientation,
-                                          EGLint samples)
+                                          EGLint samples,
+                                          EGLint colorSpace)
 {
     return new SwapChain11(this, GetAs<NativeWindow11>(nativeWindow), shareHandle, d3dTexture,
-                           backBufferFormat, depthBufferFormat, orientation, samples);
+                           backBufferFormat, depthBufferFormat, orientation, samples, colorSpace);
 }
 
 void *Renderer11::getD3DDevice()
Halla Rempt's avatar
Halla Rempt committed
206
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
Halla Rempt's avatar
Halla Rempt committed
207
index a8c24e681..3516bf779 100644
Halla Rempt's avatar
Halla Rempt committed
208 209
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
@@ -49,6 +49,7 @@ struct Renderer11DeviceCaps
 
     D3D_FEATURE_LEVEL featureLevel;
     bool supportsDXGI1_2;                // Support for DXGI 1.2
+    bool supportsDXGI1_4;               // Support for DXGI 1.4
     bool supportsClearView;              // Support for ID3D11DeviceContext1::ClearView
     bool supportsConstantBufferOffsets;  // Support for Constant buffer offset
     bool supportsVpRtIndexWriteFromVertexShader;  // VP/RT can be selected in the Vertex Shader
@@ -138,7 +139,8 @@ class Renderer11 : public RendererD3D
                                   GLenum backBufferFormat,
                                   GLenum depthBufferFormat,
                                   EGLint orientation,
-                                  EGLint samples) override;
+                                  EGLint samples,
+                                  EGLint colorSpace) override;
     egl::Error getD3DTextureInfo(const egl::Config *configuration,
                                  IUnknown *d3dTexture,
                                  EGLint *width,
Halla Rempt's avatar
Halla Rempt committed
228
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp
Halla Rempt's avatar
Halla Rempt committed
229
index dcfd06484..fc967b90d 100644
Halla Rempt's avatar
Halla Rempt committed
230 231
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
232
@@ -18,6 +18,11 @@
233 234 235
 #include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
 #include "third_party/trace_event/trace_event.h"
 
Dmitry Kazakov's avatar
Dmitry Kazakov committed
236 237 238 239
+#if 0
+// used only for HDR metadata configuration options
+#include <dxgi1_5.h>
+#endif
240 241 242 243
+
 // Precompiled shaders
 #include "libANGLE/renderer/d3d/d3d11/shaders/compiled/passthrough2d11vs.h"
 #include "libANGLE/renderer/d3d/d3d11/shaders/compiled/passthroughrgba2d11ps.h"
Dmitry Kazakov's avatar
Dmitry Kazakov committed
244
@@ -56,12 +61,14 @@ SwapChain11::SwapChain11(Renderer11 *renderer,
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                          GLenum backBufferFormat,
                          GLenum depthBufferFormat,
                          EGLint orientation,
-                         EGLint samples)
+                         EGLint samples,
+                         EGLint colorSpace)
     : SwapChainD3D(shareHandle, d3dTexture, backBufferFormat, depthBufferFormat),
       mRenderer(renderer),
       mWidth(-1),
       mHeight(-1),
       mOrientation(orientation),
+      mColorSpace(colorSpace),
       mAppCreatedShareHandle(mShareHandle != nullptr),
       mSwapInterval(0),
       mPassThroughResourcesInit(false),
Dmitry Kazakov's avatar
Dmitry Kazakov committed
260
@@ -620,10 +627,92 @@ EGLint SwapChain11::reset(const gl::Context *context,
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
             mSwapChain1 = d3d11::DynamicCastComObject<IDXGISwapChain1>(mSwapChain);
         }
 
+        if (mRenderer->getRenderer11DeviceCaps().supportsDXGI1_4)
+        {
+            IDXGISwapChain3 *swapChain3 = d3d11::DynamicCastComObject<IDXGISwapChain3>(mSwapChain);
+
+            printf("*** EGL colorSpace: 0x%X\n", mColorSpace);
+            printf("*** EGL format: 0x%X\n", mOffscreenRenderTargetFormat);
+            printf("*** Native format: 0x%X\n", getSwapChainNativeFormat());
+
+            if (mColorSpace != EGL_GL_COLORSPACE_LINEAR_KHR) {
+                DXGI_COLOR_SPACE_TYPE nativeColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
+                switch (mColorSpace)
+                {
+                case EGL_GL_COLORSPACE_SRGB_KHR:
+                    nativeColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
+                    break;
+                case EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT:
+                    nativeColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
+                    break;
+                case EGL_GL_COLORSPACE_BT2020_PQ_EXT:
+                    nativeColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020;
+                    break;
+                default:
+                    ASSERT(0 && "Unsupported colorspace requested");
+                }
+
+                printf("*** Native colorSpace: 0x%X\n", nativeColorSpace);
+
+                UINT supported = 0;
+                result = swapChain3->CheckColorSpaceSupport(nativeColorSpace, &supported);
+                ASSERT(SUCCEEDED(result));
+                if (!(supported & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) {
+                    SafeRelease(swapChain3);
+                    return EGL_BAD_MATCH;
+                }
+
+                result = swapChain3->SetColorSpace1(nativeColorSpace);
+                ASSERT(SUCCEEDED(result));
+            }
+
+            SafeRelease(swapChain3);
+
+#if 0
+
+            IDXGISwapChain4 *swapChain4 = d3d11::DynamicCastComObject<IDXGISwapChain4>(mSwapChain);
+
+            DXGI_HDR_METADATA_HDR10 md;
+            md.RedPrimary[0] = 0.680 * 50000;
+            md.RedPrimary[1] = 0.320 * 50000;
+            md.GreenPrimary[0] = 0.265 * 50000;
+            md.GreenPrimary[1] = 0.690 * 50000;
+            md.BluePrimary[0] = 0.150 * 50000;
+            md.BluePrimary[1] = 0.060 * 50000;
+            md.WhitePoint[0] = 0.3127 * 50000;
+            md.WhitePoint[1] = 0.3290 * 50000;
+            md.MaxMasteringLuminance = 1000 * 10000;
+            md.MinMasteringLuminance = 0.001 * 10000;
+            md.MaxContentLightLevel = 1000;
+            md.MaxFrameAverageLightLevel = 200;
+            result = swapChain4->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_HDR10, sizeof(md), &md);
+            printf("*** Result hdr 0x%X\n", result);
+            SafeRelease(swapChain4);
+#endif
+        }
+
         ID3D11Texture2D *backbufferTex = nullptr;
         result                         = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D),
                                        reinterpret_cast<LPVOID *>(&backbufferTex));
         ASSERT(SUCCEEDED(result));
+
+        // TODO: recover rendering to sRGB
+        //
+        // D3D11_RENDER_TARGET_VIEW_DESC offscreenRTVDesc;
+        // offscreenRTVDesc.Format = getSwapChainNativeFormat();
+        //
+        // if (mColorSpace == EGL_GL_COLORSPACE_SRGB_KHR) {
+        //     if (offscreenRTVDesc.Format == DXGI_FORMAT_R8G8B8A8_UNORM) {
+        //         offscreenRTVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
+        //     }
+        //
+        //     if (offscreenRTVDesc.Format == DXGI_FORMAT_B8G8R8A8_UNORM) {
+        //         offscreenRTVDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
+        //     }
+        // }
+        //
+        // printf("*** Render target format: 0x%X\n", offscreenRTVDesc.Format);
+
         const auto &format =
             d3d11::Format::Get(mOffscreenRenderTargetFormat, mRenderer->getRenderer11DeviceCaps());
         mBackBufferTexture.set(backbufferTex, format);
Halla Rempt's avatar
Halla Rempt committed
353
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.h
Halla Rempt's avatar
Halla Rempt committed
354
index eca068210..2a4b9ba27 100644
Halla Rempt's avatar
Halla Rempt committed
355 356
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.h
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
@@ -28,7 +28,8 @@ class SwapChain11 final : public SwapChainD3D
                 GLenum backBufferFormat,
                 GLenum depthBufferFormat,
                 EGLint orientation,
-                EGLint samples);
+                EGLint samples,
+                EGLint colorSpace);
     ~SwapChain11() override;
 
     EGLint resize(const gl::Context *context,
@@ -93,6 +94,7 @@ class SwapChain11 final : public SwapChainD3D
     EGLint mWidth;
     EGLint mHeight;
     const EGLint mOrientation;
+    EGLint mColorSpace;
     bool mAppCreatedShareHandle;
     unsigned int mSwapInterval;
     bool mPassThroughResourcesInit;
Halla Rempt's avatar
Halla Rempt committed
375
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/win32/NativeWindow11Win32.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/win32/NativeWindow11Win32.cpp
Halla Rempt's avatar
Halla Rempt committed
376
index 5394e3d3e..af52c41d0 100644
Halla Rempt's avatar
Halla Rempt committed
377 378
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/win32/NativeWindow11Win32.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/win32/NativeWindow11Win32.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
379 380 381 382 383 384 385 386 387 388 389
@@ -146,6 +146,9 @@ HRESULT NativeWindow11Win32::createSwapChain(ID3D11Device *device,
 
     // Use IDXGIFactory2::CreateSwapChainForHwnd if DXGI 1.2 is available to create a
     // DXGI_SWAP_EFFECT_SEQUENTIAL swap chain.
+    //
+    // NOTE: in non-flip mode HDR rendering is not supported, so use it
+    //       by default
     IDXGIFactory2 *factory2 = d3d11::DynamicCastComObject<IDXGIFactory2>(factory);
     if (factory2 != nullptr)
     {
@@ -158,9 +161,9 @@ HRESULT NativeWindow11Win32::createSwapChain(ID3D11Device *device,
390 391 392 393 394 395 396 397 398 399 400
         swapChainDesc.SampleDesc.Quality    = 0;
         swapChainDesc.BufferUsage =
             DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT | DXGI_USAGE_BACK_BUFFER;
-        swapChainDesc.BufferCount   = 1;
+        swapChainDesc.BufferCount   = 2;
         swapChainDesc.Scaling       = DXGI_SCALING_STRETCH;
-        swapChainDesc.SwapEffect    = DXGI_SWAP_EFFECT_SEQUENTIAL;
+        swapChainDesc.SwapEffect    = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
         swapChainDesc.AlphaMode     = DXGI_ALPHA_MODE_UNSPECIFIED;
         swapChainDesc.Flags         = 0;
         IDXGISwapChain1 *swapChain1 = nullptr;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
401
@@ -176,7 +179,7 @@ HRESULT NativeWindow11Win32::createSwapChain(ID3D11Device *device,
402 403 404 405 406 407 408 409
     }
 
     DXGI_SWAP_CHAIN_DESC swapChainDesc               = {};
-    swapChainDesc.BufferCount                        = 1;
+    swapChainDesc.BufferCount                        = 2;
     swapChainDesc.BufferDesc.Format                  = format;
     swapChainDesc.BufferDesc.Width                   = width;
     swapChainDesc.BufferDesc.Height                  = height;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
410
@@ -191,6 +194,16 @@ HRESULT NativeWindow11Win32::createSwapChain(ID3D11Device *device,
411 412 413 414 415
     swapChainDesc.SampleDesc.Count   = samples;
     swapChainDesc.SampleDesc.Quality = 0;
     swapChainDesc.Windowed           = TRUE;
+
+    /**
Dmitry Kazakov's avatar
Dmitry Kazakov committed
416 417 418 419
+     * NOTE1: in discard mode the swap chain doesn't support partial
+     *        presentatiopn with Present1() call. Though it is not a big
+     *        problem, because in case DXGI 1.2 is supported this code is
+     *        unreachable.
420
+     *
Dmitry Kazakov's avatar
Dmitry Kazakov committed
421 422
+     * NOTE2: Flip modes are not supported on Windows 7 and the like,
+     *        so use a legacy mode as a fallback
423
+     */
Dmitry Kazakov's avatar
Dmitry Kazakov committed
424
     swapChainDesc.SwapEffect         = DXGI_SWAP_EFFECT_DISCARD;
425 426
 
     HRESULT result = factory->CreateSwapChain(device, &swapChainDesc, swapChain);
Halla Rempt's avatar
Halla Rempt committed
427
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
Halla Rempt's avatar
Halla Rempt committed
428
index 75c629886..58596169a 100644
Halla Rempt's avatar
Halla Rempt committed
429 430
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
Halla Rempt's avatar
Halla Rempt committed
431
@@ -718,8 +718,10 @@ SwapChainD3D *Renderer9::createSwapChain(NativeWindowD3D *nativeWindow,
432 433 434 435 436 437 438 439 440 441 442
                                          GLenum backBufferFormat,
                                          GLenum depthBufferFormat,
                                          EGLint orientation,
-                                         EGLint samples)
+                                         EGLint samples,
+                                         EGLint colorSpace)
 {
+    UNUSED_VARIABLE(colorSpace);
     return new SwapChain9(this, GetAs<NativeWindow9>(nativeWindow), shareHandle, d3dTexture,
                           backBufferFormat, depthBufferFormat, orientation);
 }
Halla Rempt's avatar
Halla Rempt committed
443
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.h
Halla Rempt's avatar
Halla Rempt committed
444
index 9ddee45f0..ce4bb201e 100644
Halla Rempt's avatar
Halla Rempt committed
445 446
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d9/Renderer9.h
447 448 449 450 451 452 453 454 455 456
@@ -92,7 +92,8 @@ class Renderer9 : public RendererD3D
                                   GLenum backBufferFormat,
                                   GLenum depthBufferFormat,
                                   EGLint orientation,
-                                  EGLint samples) override;
+                                  EGLint samples,
+                                  EGLint colorSpace) override;
     egl::Error getD3DTextureInfo(const egl::Config *configuration,
                                  IUnknown *d3dTexture,
                                  EGLint *width,
Halla Rempt's avatar
Halla Rempt committed
457
diff --git a/src/3rdparty/angle/src/libANGLE/validationEGL.cpp b/src/3rdparty/angle/src/libANGLE/validationEGL.cpp
Halla Rempt's avatar
Halla Rempt committed
458
index 13a3a9e28..858d7ee92 100644
Halla Rempt's avatar
Halla Rempt committed
459 460
--- a/src/3rdparty/angle/src/libANGLE/validationEGL.cpp
+++ b/src/3rdparty/angle/src/libANGLE/validationEGL.cpp
461 462 463 464 465 466 467 468
@@ -885,6 +885,32 @@ Error ValidateCreateWindowSurface(Display *display, Config *config, EGLNativeWin
                                               "either EGL_TRUE or EGL_FALSE.";
               }
               break;
+          case EGL_GL_COLORSPACE:
+
+              if (!displayExtensions.colorspaceSRGB)
+              {
Dmitry Kazakov's avatar
Dmitry Kazakov committed
469
+                  return EglBadAttribute() << "EGL_KHR_gl_colorspace is not supported on this platform.";
470 471 472 473 474 475
+              }
+
+              if (value == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT)
+              {
+                  if (!displayExtensions.colorspaceSCRGBLinear)
+                  {
Dmitry Kazakov's avatar
Dmitry Kazakov committed
476
+                      return EglBadAttribute() << "EGL_EXT_gl_colorspace_scrgb_linear is not supported on this platform.";
477 478 479 480 481 482
+                  }
+              }
+              else if (value == EGL_GL_COLORSPACE_BT2020_PQ_EXT)
+              {
+                  if (!displayExtensions.colorspaceBt2020PQ)
+                  {
Dmitry Kazakov's avatar
Dmitry Kazakov committed
483
+                      return EglBadAttribute() << "EGL_EXT_gl_colorspace_bt2020_pq is not supported on this platform.";
484 485 486 487
+                  }
+              }
+              else if (value != EGL_GL_COLORSPACE_SRGB_KHR && value != EGL_GL_COLORSPACE_LINEAR_KHR)
+              {
Dmitry Kazakov's avatar
Dmitry Kazakov committed
488
+                  return EglBadAttribute() << "Unknown EGL color space requested";
489 490 491 492 493
+              }
+              break;
 
           default:
               return EglBadAttribute();
Dmitry Kazakov's avatar
Dmitry Kazakov committed
494 495 496
@@ -977,6 +1003,33 @@ Error ValidateCreatePbufferSurface(Display *display, Config *config, const Attri
               }
               break;
497
 
Dmitry Kazakov's avatar
Dmitry Kazakov committed
498
+        case EGL_GL_COLORSPACE:
499
+
Dmitry Kazakov's avatar
Dmitry Kazakov committed
500 501 502 503
+            if (!displayExtensions.colorspaceSRGB)
+            {
+                return EglBadAttribute() << "EGL_KHR_gl_colorspace is not supported on this platform.";
+            }
504
+
Dmitry Kazakov's avatar
Dmitry Kazakov committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
+            if (value == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT)
+            {
+                if (!displayExtensions.colorspaceSCRGBLinear)
+                {
+                    return EglBadAttribute() << "EGL_EXT_gl_colorspace_scrgb_linear is not supported on this platform.";
+                }
+            }
+            else if (value == EGL_GL_COLORSPACE_BT2020_PQ_EXT)
+            {
+                if (!displayExtensions.colorspaceBt2020PQ)
+                {
+                    return EglBadAttribute() << "EGL_EXT_gl_colorspace_bt2020_pq is not supported on this platform.";
+                }
+            }
+            else if (value != EGL_GL_COLORSPACE_SRGB_KHR && value != EGL_GL_COLORSPACE_LINEAR_KHR)
+            {
+                return EglBadAttribute() << "Unknown EGL color space requested";
+            }
+            break;
524
+
Dmitry Kazakov's avatar
Dmitry Kazakov committed
525 526
           default:
               return EglBadAttribute();
527
         }
Halla Rempt's avatar
Halla Rempt committed
528
diff --git a/src/gui/kernel/qsurfaceformat.cpp b/src/gui/kernel/qsurfaceformat.cpp
Halla Rempt's avatar
Halla Rempt committed
529
index 1a814ec21..fc8b9c4f4 100644
Halla Rempt's avatar
Halla Rempt committed
530 531
--- a/src/gui/kernel/qsurfaceformat.cpp
+++ b/src/gui/kernel/qsurfaceformat.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
@@ -221,6 +221,17 @@ public:
     set, the window will be created with an sRGB-capable default
     framebuffer. Note that some platforms may return windows with a sRGB-capable
     default framebuffer even when not requested explicitly.
+
+    \value scRGBColorSpace When \c{EGL_EXT_gl_colorspace_scrgb_linear}
+    is supported by the platform and this value is set, the window will
+    be created with an scRGB-capable default framebuffer. Note that some
+    platforms may return windows with a scRGB-capable default framebuffer
+    even when not requested explicitly. It usually happens when the application
+    requests 16-bit surface format.
+
+    \value bt2020PQColorSpace When \c{EGL_EXT_gl_colorspace_bt2020_pq}
+    is supported by the platform and this value is set, the window will
+    be created with an bt2020 PQ default framebuffer.
  */
 
 /*!
Halla Rempt's avatar
Halla Rempt committed
550
diff --git a/src/gui/kernel/qsurfaceformat.h b/src/gui/kernel/qsurfaceformat.h
Halla Rempt's avatar
Halla Rempt committed
551
index ed63eb8bb..9ba6a29b7 100644
Halla Rempt's avatar
Halla Rempt committed
552 553
--- a/src/gui/kernel/qsurfaceformat.h
+++ b/src/gui/kernel/qsurfaceformat.h
Dmitry Kazakov's avatar
Dmitry Kazakov committed
554 555 556 557 558 559 560 561 562 563 564
@@ -87,7 +87,9 @@ public:
 
     enum ColorSpace {
         DefaultColorSpace,
-        sRGBColorSpace
+        sRGBColorSpace,
+        scRGBColorSpace,
+        bt2020PQColorSpace
     };
     Q_ENUM(ColorSpace)
 
Halla Rempt's avatar
Halla Rempt committed
565
diff --git a/src/gui/opengl/qopenglframebufferobject.cpp b/src/gui/opengl/qopenglframebufferobject.cpp
Halla Rempt's avatar
Halla Rempt committed
566
index cae3d516c..ccdccb637 100644
Halla Rempt's avatar
Halla Rempt committed
567 568
--- a/src/gui/opengl/qopenglframebufferobject.cpp
+++ b/src/gui/opengl/qopenglframebufferobject.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
@@ -545,10 +545,13 @@ void QOpenGLFramebufferObjectPrivate::initTexture(int idx)
     ColorAttachment &color(colorAttachments[idx]);
 
     GLuint pixelType = GL_UNSIGNED_BYTE;
-    if (color.internalFormat == GL_RGB10_A2 || color.internalFormat == GL_RGB10)
+    if (color.internalFormat == GL_RGB10_A2 || color.internalFormat == GL_RGB10) {
         pixelType = GL_UNSIGNED_INT_2_10_10_10_REV;
-    else if (color.internalFormat == GL_RGB16  || color.internalFormat == GL_RGBA16)
+    } else if (color.internalFormat == GL_RGB16  || color.internalFormat == GL_RGBA16) {
         pixelType = GL_UNSIGNED_SHORT;
+    } else if (color.internalFormat == GL_RGBA16F) {
+        pixelType = GL_HALF_FLOAT;
+    }
 
     funcs.glTexImage2D(target, 0, color.internalFormat, color.size.width(), color.size.height(), 0,
                        GL_RGBA, pixelType, NULL);
Halla Rempt's avatar
Halla Rempt committed
585
diff --git a/src/gui/opengl/qopengltextureblitter.cpp b/src/gui/opengl/qopengltextureblitter.cpp
Halla Rempt's avatar
Halla Rempt committed
586
index b65df9dc8..5f6dbff29 100644
Halla Rempt's avatar
Halla Rempt committed
587 588
--- a/src/gui/opengl/qopengltextureblitter.cpp
+++ b/src/gui/opengl/qopengltextureblitter.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
@@ -131,14 +131,85 @@ static const char vertex_shader[] =
     "}";
 
 static const char fragment_shader[] =
-    "varying highp vec2 uv;"
-    "uniform sampler2D textureSampler;"
-    "uniform bool swizzle;"
-    "uniform highp float opacity;"
+    "varying highp vec2 uv;\n"
+    "uniform sampler2D textureSampler;\n"
+    "uniform bool swizzle;\n"
+    "uniform highp float opacity;\n"
+    "#if defined SCRGB_TO_SRGB\n"
+    "highp vec4 linearToSRGB(highp vec4 value)\n"
+    "{\n"
+    "   bvec4 cutoff = lessThan(value, vec4(0.0031308));\n"
+    "   const highp vec2 a1 = vec2(0.055, 0.0);\n"
+    "   const highp vec2 c2 = vec2(1.055, 1.0);\n"
+    "   const highp vec2 m3 = vec2(2.4, 1.0);\n"
+    "   const highp vec2 c4 = vec2(12.92, 1.0);\n"
+    "   highp vec4 higher = c2.xxxy * pow(value, 1.0 / m3.xxxy) - a1.xxxy;\n"
+    "   highp vec4 lower = value * c4.xxxy;\n"
+    "   return mix(higher, lower, vec4(cutoff));\n"
+    "}\n"
+    "#endif\n"
+    "#if defined SRGB_TO_SCRGB || defined SRGB_TO_BT2020PQ || defined SCRGB_TO_BT2020PQ\n"
+    "highp vec4 sRgbToLinear(highp vec4 sRGB)\n"
+    "{\n"
+    "   bvec4 cutoff = lessThan(sRGB, vec4(0.04045));\n"
+    "   const highp vec2 a1 = vec2(0.055, 0.0);\n"
+    "   const highp vec2 c2 = vec2(1.055, 1.0);\n"
+    "   const highp vec2 m3 = vec2(2.4, 1.0);\n"
+    "   const highp vec2 c4 = vec2(12.92, 1.0);\n"
+    "   highp vec4 higher = pow((sRGB + a1.xxxy) / c2.xxxy, m3.xxxy);\n"
+    "   highp vec4 lower = sRGB / c4.xxxy;\n"
+    "   return mix(higher, lower, vec4(cutoff));\n"
+    "}\n"
+    "#endif\n"
+    "#if defined SRGB_TO_BT2020PQ || defined SCRGB_TO_BT2020PQ\n"
+    "highp vec4 applySmpte2084Curve(highp vec4 L)\n"
+    "{"
+    "   const highp vec2 m1 = vec2(2610.0 / 4096.0 / 4.0, 1.0);\n"
+    "   const highp vec2 m2 = vec2(2523.0 / 4096.0 * 128.0, 1.0);\n"
+    "   const highp vec2 a1 = vec2(3424.0 / 4096.0, 0.0);\n"
+    "   const highp vec2 c2 = vec2(2413.0 / 4096.0 * 32.0, 1.0);\n"
+    "   const highp vec2 c3 = vec2(2392.0 / 4096.0 * 32.0, 1.0);\n"
+    "   const highp vec2 a4 = vec2(1.0, 0.0);\n"
+    "   highp vec4 Lp = pow(L, m1.xxxy);\n"
+    "   highp vec4 res = pow((a1.xxxy + c2.xxxy * Lp) / (a4.xxxy + c3.xxxy * Lp), m2.xxxy);\n"
+    "   return res;"
+    "}\n"
+    "#endif\n"
+    "#if defined SRGB_TO_BT2020PQ || defined SCRGB_TO_BT2020PQ\n"
+    "highp vec4 scRgbToBt2020pq(highp vec4 value)\n"
+    "{\n"
+    "   const highp mat4 convMat = "
+    "      mat4(0.627402, 0.069095, 0.016394, 0.0,"
+    "           0.329292, 0.919544, 0.088028, 0.0,"
+    "           0.043306, 0.011360, 0.895578, 0.0,"
+    "           0.0,      0.0,      0.0,      1.0);"
+    ""
+    "   value = convMat * value;\n"
+    "   return applySmpte2084Curve(0.008 * value);"
+    "}\n"
+    "#endif\n"
+    "#if defined SRGB_TO_BT2020PQ\n"
+    "highp vec4 sRgbToBt2020pq(highp vec4 value)\n"
+    "{\n"
+    "   value = sRgbToLinear(value);"
+    "   return scRgbToBt2020pq(value);"
+    "}\n"
+    "#endif\n"
+    "\n"
     "void main() {"
     "   highp vec4 tmpFragColor = texture2D(textureSampler,uv);"
-    "   tmpFragColor.a *= opacity;"
-    "   gl_FragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;"
+    "   tmpFragColor.a *= opacity;\n"
+    "   tmpFragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;\n"
+    "#if defined SRGB_TO_SCRGB\n"
+    "   tmpFragColor = sRgbToLinear(tmpFragColor);\n"
+    "#elif defined SRGB_TO_BT2020PQ\n"
+    "   tmpFragColor = sRgbToBt2020pq(tmpFragColor);\n"
+    "#elif defined SCRGB_TO_BT2020PQ\n"
+    "   tmpFragColor = scRgbToBt2020pq(tmpFragColor);\n"
+    "#elif defined SCRGB_TO_SRGB\n"
+    "   tmpFragColor = linearToSRGB(tmpFragColor);\n"
+    "#endif\n"
+    "   gl_FragColor = tmpFragColor;"
     "}";
 
 static const char fragment_shader_external_oes[] =
@@ -187,6 +258,23 @@ private:
     GLenum m_target;
 };
 
+class ColorSpaceConversion : public QPair<QSurfaceFormat::ColorSpace, QSurfaceFormat::ColorSpace>
+{
+public:
+    ColorSpaceConversion() { };
+    ColorSpaceConversion(QSurfaceFormat::ColorSpace srcColorSpace,
+                         QSurfaceFormat::ColorSpace dstColorSpace)
+        : QPair(srcColorSpace, dstColorSpace)
+    { }
+
+    QSurfaceFormat::ColorSpace source() const {
+        return first;
+    }
+    QSurfaceFormat::ColorSpace destination() const {
+        return second;
+    }
+};
+
 class QOpenGLTextureBlitterPrivate
 {
 public:
@@ -197,16 +285,29 @@ public:
     };
 
     enum ProgramIndex {
-        TEXTURE_2D,
-        TEXTURE_EXTERNAL_OES
+        TEXTURE_2D = 0,
+        TEXTURE_2D_SRGB_TO_SCRGB,
+        TEXTURE_2D_SCRGB_TO_SRGB,
+        TEXTURE_2D_SRGB_TO_BT2020PQ,
+        TEXTURE_2D_SCRGB_TO_BT2020PQ,
+        TEXTURE_EXTERNAL_OES,
+
+        PROGRAM_COUNT
     };
 
     QOpenGLTextureBlitterPrivate() :
         swizzle(false),
         opacity(1.0f),
         vao(new QOpenGLVertexArrayObject),
-        currentTarget(TEXTURE_2D)
-    { }
+        currentTarget(GL_NONE),
+        colorSpaceConversion(0)
+    {
+        supportedColorSpaceConversions << ColorSpaceConversion(QSurfaceFormat::DefaultColorSpace, QSurfaceFormat::DefaultColorSpace);
+        supportedColorSpaceConversions << ColorSpaceConversion(QSurfaceFormat::sRGBColorSpace, QSurfaceFormat::scRGBColorSpace);
+        supportedColorSpaceConversions << ColorSpaceConversion(QSurfaceFormat::scRGBColorSpace, QSurfaceFormat::sRGBColorSpace);
+        supportedColorSpaceConversions << ColorSpaceConversion(QSurfaceFormat::sRGBColorSpace, QSurfaceFormat::bt2020PQColorSpace);
+        supportedColorSpaceConversions << ColorSpaceConversion(QSurfaceFormat::scRGBColorSpace, QSurfaceFormat::bt2020PQColorSpace);
+    }
 
     bool buildProgram(ProgramIndex idx, const char *vs, const char *fs);
 
@@ -214,6 +315,7 @@ public:
     void blit(GLuint texture, const QMatrix4x4 &vertexTransform, QOpenGLTextureBlitter::Origin origin);
 
     void prepareProgram(const QMatrix4x4 &vertexTransform);
+    int calcColorSpaceConversionIndex(QSurfaceFormat::ColorSpace srcColorSpace, QSurfaceFormat::ColorSpace dstColorSpace);
 
     QOpenGLBuffer vertexBuffer;
     QOpenGLBuffer textureBuffer;
@@ -239,18 +341,48 @@ public:
         bool swizzle;
         float opacity;
         TextureMatrixUniform textureMatrixUniformState;
-    } programs[2];
+    } programs[PROGRAM_COUNT];
     bool swizzle;
     float opacity;
     QScopedPointer<QOpenGLVertexArrayObject> vao;
     GLenum currentTarget;
+
+    int colorSpaceConversion;
+    QVector<ColorSpaceConversion> supportedColorSpaceConversions;
 };
 
-static inline QOpenGLTextureBlitterPrivate::ProgramIndex targetToProgramIndex(GLenum target)
+int QOpenGLTextureBlitterPrivate::calcColorSpaceConversionIndex(QSurfaceFormat::ColorSpace srcColorSpace, QSurfaceFormat::ColorSpace dstColorSpace)
+{
+    // TODO: auto-detect destination color space of the surface
+    //       in case of default color space
+
+    // disable color management if at least one of the color
+    // spaces is declared as default
+    if (srcColorSpace == QSurfaceFormat::DefaultColorSpace ||
+        dstColorSpace == QSurfaceFormat::DefaultColorSpace) {
+
+        return 0;
+    }
+
+    // disable color management if source and destination color
+    // spaces are the same
+    if (srcColorSpace == dstColorSpace) {
+        return 0;
+    }
+
+    ColorSpaceConversion conversion(srcColorSpace, dstColorSpace);
+    return supportedColorSpaceConversions.indexOf(conversion);
+}
+
+static inline QOpenGLTextureBlitterPrivate::ProgramIndex targetToProgramIndex(GLenum target, int colorSpaceConversion)
 {
     switch (target) {
-    case GL_TEXTURE_2D:
-        return QOpenGLTextureBlitterPrivate::TEXTURE_2D;
+    case GL_TEXTURE_2D: {
+        QOpenGLTextureBlitterPrivate::ProgramIndex index =
+            QOpenGLTextureBlitterPrivate::ProgramIndex(
+                int(QOpenGLTextureBlitterPrivate::TEXTURE_2D) + colorSpaceConversion);
+        return index;
+    }
     case GL_TEXTURE_EXTERNAL_OES:
         return QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES;
     default:
@@ -261,7 +393,7 @@ static inline QOpenGLTextureBlitterPrivate::ProgramIndex targetToProgramIndex(GL
 
 void QOpenGLTextureBlitterPrivate::prepareProgram(const QMatrix4x4 &vertexTransform)
 {
-    Program *program = &programs[targetToProgramIndex(currentTarget)];
+    Program *program = &programs[targetToProgramIndex(currentTarget, colorSpaceConversion)];
 
     vertexBuffer.bind();
     program->glProgram->setAttributeBuffer(program->vertexCoordAttribPos, GL_FLOAT, 0, 3, 0);
@@ -293,7 +425,7 @@ void QOpenGLTextureBlitterPrivate::blit(GLuint texture,
     TextureBinder binder(currentTarget, texture);
     prepareProgram(vertexTransform);
 
-    Program *program = &programs[targetToProgramIndex(currentTarget)];
+    Program *program = &programs[targetToProgramIndex(currentTarget, colorSpaceConversion)];
     program->glProgram->setUniformValue(program->textureTransformUniformPos, textureTransform);
     program->textureMatrixUniformState = User;
 
@@ -307,7 +439,7 @@ void QOpenGLTextureBlitterPrivate::blit(GLuint texture,
     TextureBinder binder(currentTarget, texture);
     prepareProgram(vertexTransform);
 
-    Program *program = &programs[targetToProgramIndex(currentTarget)];
+    Program *program = &programs[targetToProgramIndex(currentTarget, colorSpaceConversion)];
     if (origin == QOpenGLTextureBlitter::OriginTopLeft) {
         if (program->textureMatrixUniformState != IdentityFlipped) {
             QMatrix3x3 flipped;
@@ -408,6 +540,28 @@ bool QOpenGLTextureBlitter::create()
     } else {
         if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D, vertex_shader, fragment_shader))
             return false;
+
+        // TODO: create non-default transformations on-demand
+        {
+            const QString shader = QString("#define SRGB_TO_SCRGB\n %1").arg(fragment_shader);
+            if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D_SRGB_TO_SCRGB, vertex_shader, shader.toLatin1().constData()))
+                return false;
+        }
+        {
+            const QString shader = QString("#define SCRGB_TO_SRGB\n %1").arg(fragment_shader);
+            if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D_SCRGB_TO_SRGB, vertex_shader, shader.toLatin1().constData()))
+                return false;
+        }
+        {
+            const QString shader = QString("#define SRGB_TO_BT2020PQ\n %1").arg(fragment_shader);
+            if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D_SRGB_TO_BT2020PQ, vertex_shader, shader.toLatin1().constData()))
+                return false;
+        }
+        {
+            const QString shader = QString("#define SCRGB_TO_BT2020PQ\n %1").arg(fragment_shader);
+            if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_2D_SCRGB_TO_BT2020PQ, vertex_shader, shader.toLatin1().constData()))
+                return false;
+        }
         if (supportsExternalOESTarget())
             if (!d->buildProgram(QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES, vertex_shader, fragment_shader_external_oes))
                 return false;
@@ -455,6 +609,8 @@ void QOpenGLTextureBlitter::destroy()
         return;
     Q_D(QOpenGLTextureBlitter);
     d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D].glProgram.reset();
+    d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D_SRGB_TO_SCRGB].glProgram.reset();
+    d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_2D_SRGB_TO_BT2020PQ].glProgram.reset();
     d->programs[QOpenGLTextureBlitterPrivate::TEXTURE_EXTERNAL_OES].glProgram.reset();
     d->vertexBuffer.destroy();
     d->textureBuffer.destroy();
@@ -484,15 +640,26 @@ bool QOpenGLTextureBlitter::supportsExternalOESTarget() const
 
     \sa release(), blit()
  */
-void QOpenGLTextureBlitter::bind(GLenum target)
+void QOpenGLTextureBlitter::bind(GLenum target,
+                                 QSurfaceFormat::ColorSpace srcColorSpace,
+                                 QSurfaceFormat::ColorSpace dstColorSpace)
 {
     Q_D(QOpenGLTextureBlitter);
 
     if (d->vao->isCreated())
         d->vao->bind();
 
+    const int index = d->calcColorSpaceConversionIndex(srcColorSpace, dstColorSpace);
+
+    if (index >= 0) {
+        d->colorSpaceConversion = index;
+    } else {
+        qWarning() << "QOpenGLTextureBlitter::bind(): color space conversion is not supported" << srcColorSpace << dstColorSpace;
+        d->colorSpaceConversion = 0; // noop conversion
+    }
+
     d->currentTarget = target;
-    QOpenGLTextureBlitterPrivate::Program *p = &d->programs[targetToProgramIndex(target)];
+    QOpenGLTextureBlitterPrivate::Program *p = &d->programs[targetToProgramIndex(target, d->colorSpaceConversion)];
     p->glProgram->bind();
 
     d->vertexBuffer.bind();
@@ -506,6 +673,21 @@ void QOpenGLTextureBlitter::bind(GLenum target)
     d->textureBuffer.release();
 }
 
+void QOpenGLTextureBlitter::rebind(GLenum target, QSurfaceFormat::ColorSpace srcColorSpace, QSurfaceFormat::ColorSpace dstColorSpace)
+{
+    Q_D(QOpenGLTextureBlitter);
+
+    if (d->vao->isCreated() &&
+        d->currentTarget == target &&
+        d->colorSpaceConversion == d->calcColorSpaceConversionIndex(srcColorSpace, dstColorSpace)) {
+
+        // the blitter is already configured in the correct state, so just skip it
+        return;
+    }
+
+    bind(target, srcColorSpace, dstColorSpace);
+}
+
 /*!
     Unbinds the graphics resources used by the blitter.
 
@@ -514,7 +696,7 @@ void QOpenGLTextureBlitter::bind(GLenum target)
 void QOpenGLTextureBlitter::release()
 {
     Q_D(QOpenGLTextureBlitter);
-    d->programs[targetToProgramIndex(d->currentTarget)].glProgram->release();
+    d->programs[targetToProgramIndex(d->currentTarget, d->colorSpaceConversion)].glProgram->release();
     if (d->vao->isCreated())
         d->vao->release();
 }
Halla Rempt's avatar
Halla Rempt committed
925
diff --git a/src/gui/opengl/qopengltextureblitter.h b/src/gui/opengl/qopengltextureblitter.h
Halla Rempt's avatar
Halla Rempt committed
926
index 2f7c6b1a0..3c87e4e2b 100644
Halla Rempt's avatar
Halla Rempt committed
927 928
--- a/src/gui/opengl/qopengltextureblitter.h
+++ b/src/gui/opengl/qopengltextureblitter.h
Dmitry Kazakov's avatar
Dmitry Kazakov committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
@@ -48,6 +48,9 @@
 #include <QtGui/QMatrix3x3>
 #include <QtGui/QMatrix4x4>
 
+// TODO: less includes!!!
+#include <QSurfaceFormat>
+
 QT_BEGIN_NAMESPACE
 
 class QOpenGLTextureBlitterPrivate;
@@ -69,7 +72,14 @@ public:
 
     bool supportsExternalOESTarget() const;
 
-    void bind(GLenum target = GL_TEXTURE_2D);
+    void bind(GLenum target = GL_TEXTURE_2D,
+              QSurfaceFormat::ColorSpace srcColorSpace = QSurfaceFormat::DefaultColorSpace,
+              QSurfaceFormat::ColorSpace dstColorSpace = QSurfaceFormat::DefaultColorSpace);
+
+    void rebind(GLenum target = GL_TEXTURE_2D,
+                QSurfaceFormat::ColorSpace srcColorSpace = QSurfaceFormat::DefaultColorSpace,
+                QSurfaceFormat::ColorSpace dstColorSpace = QSurfaceFormat::DefaultColorSpace);
+
     void release();
 
     void setRedBlueSwizzle(bool swizzle);
Halla Rempt's avatar
Halla Rempt committed
955
diff --git a/src/gui/painting/qplatformbackingstore.cpp b/src/gui/painting/qplatformbackingstore.cpp
Halla Rempt's avatar
Halla Rempt committed
956
index c71d82546..8dd96a66b 100644
Halla Rempt's avatar
Halla Rempt committed
957 958
--- a/src/gui/painting/qplatformbackingstore.cpp
+++ b/src/gui/painting/qplatformbackingstore.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
@@ -132,6 +132,7 @@ struct QBackingstoreTextureInfo
     QRect rect;
     QRect clipRect;
     QPlatformTextureList::Flags flags;
+    QSurfaceFormat::ColorSpace colorSpace;
 };
 
 Q_DECLARE_TYPEINFO(QBackingstoreTextureInfo, Q_MOVABLE_TYPE);
@@ -181,6 +182,12 @@ QPlatformTextureList::Flags QPlatformTextureList::flags(int index) const
     return d->textures.at(index).flags;
 }
 
+QSurfaceFormat::ColorSpace QPlatformTextureList::colorSpace(int index) const
+{
+    Q_D(const QPlatformTextureList);
+    return d->textures.at(index).colorSpace;
+}
+
 QRect QPlatformTextureList::geometry(int index) const
 {
     Q_D(const QPlatformTextureList);
@@ -209,7 +216,7 @@ bool QPlatformTextureList::isLocked() const
 }
 
 void QPlatformTextureList::appendTexture(void *source, GLuint textureId, const QRect &geometry,
-                                         const QRect &clipRect, Flags flags)
+                                         const QRect &clipRect, Flags flags, QSurfaceFormat::ColorSpace colorSpace)
 {
     Q_D(QPlatformTextureList);
     QBackingstoreTextureInfo bi;
@@ -218,6 +225,7 @@ void QPlatformTextureList::appendTexture(void *source, GLuint textureId, const Q
     bi.rect = geometry;
     bi.clipRect = clipRect;
     bi.flags = flags;
+    bi.colorSpace = colorSpace;
     d->textures.append(bi);
 }
 
@@ -300,6 +308,7 @@ static void blitTextureForWidget(const QPlatformTextureList *textures, int idx,
     if (srgb && canUseSrgb)
         funcs->glEnable(GL_FRAMEBUFFER_SRGB);
 
+    blitter->rebind(GL_TEXTURE_2D, textures->colorSpace(idx), window->format().colorSpace());
     blitter->blit(textures->textureId(idx), target, source);
 
     if (srgb && canUseSrgb)
@@ -433,6 +442,11 @@ void QPlatformBackingStore::composeAndFlush(QWindow *window, const QRegion &regi
         funcs->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
 
     if (textureId) {
+        // GUI texture is always in sRGB color space
+        d_ptr->blitter->rebind(GL_TEXTURE_2D,
+                               QSurfaceFormat::sRGBColorSpace,
+                               window->format().colorSpace());
+
         if (d_ptr->needsSwizzle)
             d_ptr->blitter->setRedBlueSwizzle(true);
         // The backingstore is for the entire tlw.
Halla Rempt's avatar
Halla Rempt committed
1017
diff --git a/src/gui/painting/qplatformbackingstore.h b/src/gui/painting/qplatformbackingstore.h
Halla Rempt's avatar
Halla Rempt committed
1018
index de5ba964d..f8887bd4c 100644
Halla Rempt's avatar
Halla Rempt committed
1019 1020
--- a/src/gui/painting/qplatformbackingstore.h
+++ b/src/gui/painting/qplatformbackingstore.h
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
@@ -95,11 +95,13 @@ public:
     QRect clipRect(int index) const;
     void *source(int index);
     Flags flags(int index) const;
+    QSurfaceFormat::ColorSpace colorSpace(int index) const;
     void lock(bool on);
     bool isLocked() const;
 
     void appendTexture(void *source, GLuint textureId, const QRect &geometry,
-                       const QRect &clipRect = QRect(), Flags flags = 0);
+                       const QRect &clipRect = QRect(), Flags flags = 0,
+                       QSurfaceFormat::ColorSpace colorSpace = QSurfaceFormat::DefaultColorSpace);
     void clear();
 
  Q_SIGNALS:
Halla Rempt's avatar
Halla Rempt committed
1036
diff --git a/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp b/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp
Halla Rempt's avatar
Halla Rempt committed
1037
index 40400e2a1..5d44e6245 100644
Halla Rempt's avatar
Halla Rempt committed
1038 1039
--- a/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp
+++ b/src/platformsupport/platformcompositor/qopenglcompositorbackingstore.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1040 1041 1042 1043 1044 1045 1046 1047 1048
@@ -230,7 +230,7 @@ void QOpenGLCompositorBackingStore::composeAndFlush(QWindow *window, const QRegi
     m_textures->clear();
     for (int i = 0; i < textures->count(); ++i)
         m_textures->appendTexture(textures->source(i), textures->textureId(i), textures->geometry(i),
-                                  textures->clipRect(i), textures->flags(i));
+                                  textures->clipRect(i), textures->flags(i), textures->colorSpace(i));
 
     updateTexture();
     m_textures->appendTexture(nullptr, m_bsTexture, window->geometry());
Halla Rempt's avatar
Halla Rempt committed
1049
diff --git a/src/plugins/platforms/windows/qwindowseglcontext.cpp b/src/plugins/platforms/windows/qwindowseglcontext.cpp
Halla Rempt's avatar
Halla Rempt committed
1050
index 063e81150..4cd745eac 100644
Halla Rempt's avatar
Halla Rempt committed
1051 1052
--- a/src/plugins/platforms/windows/qwindowseglcontext.cpp
+++ b/src/plugins/platforms/windows/qwindowseglcontext.cpp
Halla Rempt's avatar
Halla Rempt committed
1053 1054 1055 1056 1057
@@ -151,8 +151,9 @@ bool QWindowsLibEGL::init()
     eglGetCurrentDisplay = RESOLVE((EGLDisplay (EGLAPIENTRY *)(void)), eglGetCurrentDisplay);
     eglSwapBuffers = RESOLVE((EGLBoolean (EGLAPIENTRY *)(EGLDisplay , EGLSurface)), eglSwapBuffers);
     eglGetProcAddress = RESOLVE((QFunctionPointer (EGLAPIENTRY * )(const char *)), eglGetProcAddress);
+    eglQueryString = RESOLVE((const char* (EGLAPIENTRY *)(EGLDisplay, EGLint)), eglQueryString);
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1058
 
Halla Rempt's avatar
Halla Rempt committed
1059 1060 1061
-    if (!eglGetError || !eglGetDisplay || !eglInitialize || !eglGetProcAddress)
+    if (!eglGetError || !eglGetDisplay || !eglInitialize || !eglGetProcAddress || !eglQueryString)
         return false;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1062
 
Halla Rempt's avatar
Halla Rempt committed
1063 1064
     eglGetPlatformDisplayEXT = nullptr;
@@ -197,8 +198,15 @@ bool QWindowsLibGLESv2::init()
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1065 1066
 }
 
Halla Rempt's avatar
Halla Rempt committed
1067 1068 1069 1070 1071 1072
 QWindowsEGLStaticContext::QWindowsEGLStaticContext(EGLDisplay display)
-    : m_display(display)
+    : m_display(display),
+      m_hasSRGBColorSpaceSupport(false),
+      m_hasSCRGBColorSpaceSupport(false),
+      m_hasBt2020PQColorSpaceSupport(false)
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1073
 {
Halla Rempt's avatar
Halla Rempt committed
1074 1075 1076 1077
+    const char *eglExtensions = libEGL.eglQueryString(display, EGL_EXTENSIONS);
+    m_hasSRGBColorSpaceSupport = strstr(eglExtensions, "EGL_KHR_gl_colorspace") != nullptr;
+    m_hasSCRGBColorSpaceSupport = strstr(eglExtensions, "EGL_EXT_gl_colorspace_scrgb_linear") != nullptr;
+    m_hasBt2020PQColorSpaceSupport = strstr(eglExtensions, "EGL_EXT_gl_colorspace_bt2020_pq") != nullptr;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1078 1079
 }
 
Halla Rempt's avatar
Halla Rempt committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
 bool QWindowsEGLStaticContext::initializeAngle(QWindowsOpenGLTester::Renderers preferredType, HDC dc,
@@ -297,11 +305,48 @@ QWindowsOpenGLContext *QWindowsEGLStaticContext::createContext(QOpenGLContext *c
     return new QWindowsEGLContext(this, context->format(), context->shareHandle());
 }
 
-void *QWindowsEGLStaticContext::createWindowSurface(void *nativeWindow, void *nativeConfig, int *err)
+void *QWindowsEGLStaticContext::createWindowSurface(void *nativeWindow, void *nativeConfig,
+                                                    QSurfaceFormat::ColorSpace colorSpace, int *err)
 {
     *err = 0;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1090
+
Halla Rempt's avatar
Halla Rempt committed
1091 1092
+    EGLint eglColorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
+    bool colorSpaceSupported = false;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1093
+
Halla Rempt's avatar
Halla Rempt committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
+    switch (colorSpace) {
+    case QSurfaceFormat::DefaultColorSpace:
+        colorSpaceSupported = m_hasSRGBColorSpaceSupport;
+        break;
+    case QSurfaceFormat::sRGBColorSpace:
+        eglColorSpace = EGL_GL_COLORSPACE_SRGB_KHR;
+        colorSpaceSupported = m_hasSRGBColorSpaceSupport;
+        break;
+    case QSurfaceFormat::scRGBColorSpace:
+        eglColorSpace = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
+        colorSpaceSupported = m_hasSCRGBColorSpaceSupport;
+        break;
+    case QSurfaceFormat::bt2020PQColorSpace:
+        eglColorSpace = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
+        colorSpaceSupported = m_hasBt2020PQColorSpaceSupport;
+        break;
+    }
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1111
+
Halla Rempt's avatar
Halla Rempt committed
1112
+    QVector<EGLint> attributes;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1113
+
Halla Rempt's avatar
Halla Rempt committed
1114 1115 1116
+    if (colorSpaceSupported) {
+        attributes << EGL_GL_COLORSPACE << eglColorSpace;
+    }
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1117
+
Halla Rempt's avatar
Halla Rempt committed
1118
+    attributes << EGL_NONE;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1119
+
Halla Rempt's avatar
Halla Rempt committed
1120 1121 1122
+    if (!colorSpaceSupported && colorSpace != QSurfaceFormat::DefaultColorSpace) {
+        qWarning().nospace() << __FUNCTION__ << ": Requested color space is not supported by EGL implementation: " << colorSpace << " (egl: 0x" << hex << eglColorSpace << ")";
+    }
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1123 1124
+
+
Halla Rempt's avatar
Halla Rempt committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
     EGLSurface surface = libEGL.eglCreateWindowSurface(m_display, nativeConfig,
-                                                       static_cast<EGLNativeWindowType>(nativeWindow), nullptr);
+                                                       static_cast<EGLNativeWindowType>(nativeWindow),
+                                                       attributes.constData());
     if (surface == EGL_NO_SURFACE) {
         *err = libEGL.eglGetError();
         qWarning("%s: Could not create the EGL window surface: 0x%x", __FUNCTION__, *err);
@@ -349,6 +394,7 @@ QSurfaceFormat QWindowsEGLStaticContext::formatFromConfig(EGLDisplay display, EG
     format.setSamples(sampleCount);
     format.setStereo(false);
     format.setSwapInterval(referenceFormat.swapInterval());
+    format.setColorSpace(referenceFormat.colorSpace());
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1137
 
Halla Rempt's avatar
Halla Rempt committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
     // Clear the EGL error state because some of the above may
     // have errored out because the attribute is not applicable
@@ -378,7 +424,6 @@ QSurfaceFormat QWindowsEGLStaticContext::formatFromConfig(EGLDisplay display, EG
     \internal
     \ingroup qt-lighthouse-win
 */
-
 QWindowsEGLContext::QWindowsEGLContext(QWindowsEGLStaticContext *staticContext,
                                        const QSurfaceFormat &format,
                                        QPlatformOpenGLContext *share)
@@ -483,6 +528,8 @@ bool QWindowsEGLContext::makeCurrent(QPlatformSurface *surface)
             // Simulate context loss as the context is useless.
             QWindowsEGLStaticContext::libEGL.eglDestroyContext(m_eglDisplay, m_eglContext);
             m_eglContext = EGL_NO_CONTEXT;
+        } else if (err == EGL_BAD_MATCH) {
+            qCDebug(lcQpaGl) << "Got bad match in createWindowSurface() for context" << this << "Check color space configuration.";
         }
         return false;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1156
     }
Halla Rempt's avatar
Halla Rempt committed
1157
diff --git a/src/plugins/platforms/windows/qwindowseglcontext.h b/src/plugins/platforms/windows/qwindowseglcontext.h
Halla Rempt's avatar
Halla Rempt committed
1158
index 8a1e1ddae..9f7742e6f 100644
Halla Rempt's avatar
Halla Rempt committed
1159 1160
--- a/src/plugins/platforms/windows/qwindowseglcontext.h
+++ b/src/plugins/platforms/windows/qwindowseglcontext.h
Halla Rempt's avatar
Halla Rempt committed
1161 1162
@@ -80,6 +80,7 @@ struct QWindowsLibEGL
     QFunctionPointer (EGLAPIENTRY *eglGetProcAddress)(const char *procname);
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1163
 
Halla Rempt's avatar
Halla Rempt committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
     EGLDisplay (EGLAPIENTRY * eglGetPlatformDisplayEXT)(EGLenum platform, void *native_display, const EGLint *attrib_list);
+    const char* (EGLAPIENTRY * eglQueryString)(EGLDisplay dpy, EGLint name);
 
 private:
 #if !defined(QT_STATIC) || defined(QT_OPENGL_DYNAMIC)
@@ -121,7 +122,7 @@ public:
     void *moduleHandle() const override { return libGLESv2.moduleHandle(); }
     QOpenGLContext::OpenGLModuleType moduleType() const override { return QOpenGLContext::LibGLES; }
 
-    void *createWindowSurface(void *nativeWindow, void *nativeConfig, int *err) override;
+    void *createWindowSurface(void *nativeWindow, void *nativeConfig, QSurfaceFormat::ColorSpace colorSpace, int *err) override;
     void destroyWindowSurface(void *nativeSurface) override;
 
     QSurfaceFormat formatFromConfig(EGLDisplay display, EGLConfig config, const QSurfaceFormat &referenceFormat);
@@ -135,6 +136,9 @@ private:
                                 EGLDisplay *display, EGLint *major, EGLint *minor);
 
     const EGLDisplay m_display;
+    bool m_hasSRGBColorSpaceSupport;
+    bool m_hasSCRGBColorSpaceSupport;
+    bool m_hasBt2020PQColorSpaceSupport;
 };
 
 class QWindowsEGLContext : public QWindowsOpenGLContext
Halla Rempt's avatar
Halla Rempt committed
1188
diff --git a/src/plugins/platforms/windows/qwindowsnativeinterface.cpp b/src/plugins/platforms/windows/qwindowsnativeinterface.cpp
Halla Rempt's avatar
Halla Rempt committed
1189
index ed945ec4b..1c5be4415 100644
Halla Rempt's avatar
Halla Rempt committed
1190 1191
--- a/src/plugins/platforms/windows/qwindowsnativeinterface.cpp
+++ b/src/plugins/platforms/windows/qwindowsnativeinterface.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
@@ -40,6 +40,7 @@
 #include "qwindowsnativeinterface.h"
 #include "qwindowsclipboard.h"
 #include "qwindowswindow.h"
+#include "qwindowsscreen.h"
 #include "qwindowscontext.h"
 #include "qwindowscursor.h"
 #include "qwindowsopenglcontext.h"
@@ -124,6 +125,21 @@ void *QWindowsNativeInterface::nativeResourceForWindow(const QByteArray &resourc
     return nullptr;
 }
 
+void *QWindowsNativeInterface::nativeResourceForScreen(const QByteArray &resource, QScreen *screen)
+{
+    if (!screen || !screen->handle()) {
+        qWarning("%s: '%s' requested for null screen or screen without handle.", __FUNCTION__, resource.constData());
+        return 0;
+    }
+    QWindowsScreen *bs = static_cast<QWindowsScreen *>(screen->handle());
+    int type = resourceType(resource);
+    if (type == HandleType)
+        return bs->handle();
+
+    qWarning("%s: Invalid key '%s' requested.", __FUNCTION__, resource.constData());
+    return 0;
+}
+
 #ifndef QT_NO_CURSOR
 void *QWindowsNativeInterface::nativeResourceForCursor(const QByteArray &resource, const QCursor &cursor)
 {
Halla Rempt's avatar
Halla Rempt committed
1222
diff --git a/src/plugins/platforms/windows/qwindowsnativeinterface.h b/src/plugins/platforms/windows/qwindowsnativeinterface.h
Halla Rempt's avatar
Halla Rempt committed
1223
index e6f8aae8f..ce395dc5a 100644
Halla Rempt's avatar
Halla Rempt committed
1224 1225
--- a/src/plugins/platforms/windows/qwindowsnativeinterface.h
+++ b/src/plugins/platforms/windows/qwindowsnativeinterface.h
Halla Rempt's avatar
Halla Rempt committed
1226
@@ -74,6 +74,7 @@ public:
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1227 1228 1229 1230 1231 1232 1233
     void *nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context) override;
 #endif
     void *nativeResourceForWindow(const QByteArray &resource, QWindow *window) override;
+    void *nativeResourceForScreen(const QByteArray &resource, QScreen *screen) override;
 #ifndef QT_NO_CURSOR
     void *nativeResourceForCursor(const QByteArray &resource, const QCursor &cursor) override;
 #endif
Halla Rempt's avatar
Halla Rempt committed
1234
diff --git a/src/plugins/platforms/windows/qwindowsopenglcontext.h b/src/plugins/platforms/windows/qwindowsopenglcontext.h
Halla Rempt's avatar
Halla Rempt committed
1235
index cc6d93d35..61c0e2876 100644
Halla Rempt's avatar
Halla Rempt committed
1236 1237
--- a/src/plugins/platforms/windows/qwindowsopenglcontext.h
+++ b/src/plugins/platforms/windows/qwindowsopenglcontext.h
Halla Rempt's avatar
Halla Rempt committed
1238 1239 1240 1241 1242 1243 1244 1245 1246
@@ -63,7 +63,7 @@ public:
 
     // If the windowing system interface needs explicitly created window surfaces (like EGL),
     // reimplement these.
-    virtual void *createWindowSurface(void * /*nativeWindow*/, void * /*nativeConfig*/, int * /*err*/) { return 0; }
+    virtual void *createWindowSurface(void * /*nativeWindow*/, void * /*nativeConfig*/, QSurfaceFormat::ColorSpace /*colorSpace*/, int * /*err*/) { return 0; }
     virtual void destroyWindowSurface(void * /*nativeSurface*/) { }
 
 protected:
Halla Rempt's avatar
Halla Rempt committed
1247
diff --git a/src/plugins/platforms/windows/qwindowsscreen.cpp b/src/plugins/platforms/windows/qwindowsscreen.cpp
Halla Rempt's avatar
Halla Rempt committed
1248
index 0520f8893..b70b0bbe3 100644
Halla Rempt's avatar
Halla Rempt committed
1249 1250
--- a/src/plugins/platforms/windows/qwindowsscreen.cpp
+++ b/src/plugins/platforms/windows/qwindowsscreen.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
@@ -321,6 +321,11 @@ void QWindowsScreen::handleChanges(const QWindowsScreenData &newData)
     }
 }
 
+HMONITOR QWindowsScreen::handle() const
+{
+    return m_data.hMonitor;
+}
+
 QRect QWindowsScreen::virtualGeometry(const QPlatformScreen *screen) // cf QScreen::virtualGeometry()
 {
     QRect result;
Halla Rempt's avatar
Halla Rempt committed
1263
diff --git a/src/plugins/platforms/windows/qwindowsscreen.h b/src/plugins/platforms/windows/qwindowsscreen.h
Halla Rempt's avatar
Halla Rempt committed
1264
index 824bcb1ad..33c9effa2 100644
Halla Rempt's avatar
Halla Rempt committed
1265 1266
--- a/src/plugins/platforms/windows/qwindowsscreen.h
+++ b/src/plugins/platforms/windows/qwindowsscreen.h
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1267 1268 1269 1270 1271 1272 1273 1274 1275
@@ -104,6 +104,8 @@ public:
 
     inline void handleChanges(const QWindowsScreenData &newData);
 
+    HMONITOR handle() const;
+
 #ifndef QT_NO_CURSOR
     QPlatformCursor *cursor() const override { return m_cursor.data(); }
     const CursorPtr &cursorPtr() const { return m_cursor; }
Halla Rempt's avatar
Halla Rempt committed
1276
diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp
Halla Rempt's avatar
Halla Rempt committed
1277
index 0376e363f..76a443a89 100644
Halla Rempt's avatar
Halla Rempt committed
1278 1279
--- a/src/plugins/platforms/windows/qwindowswindow.cpp
+++ b/src/plugins/platforms/windows/qwindowswindow.cpp
Halla Rempt's avatar
Halla Rempt committed
1280 1281 1282 1283
@@ -2741,9 +2741,13 @@ void *QWindowsWindow::surface(void *nativeConfig, int *err)
     return 0;
 #endif
 #ifndef QT_NO_OPENGL
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1284 1285 1286
+
+
+
Halla Rempt's avatar
Halla Rempt committed
1287 1288 1289 1290 1291 1292 1293 1294 1295
     if (!m_surface) {
-        if (QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
-            m_surface = staticOpenGLContext->createWindowSurface(m_data.hwnd, nativeConfig, err);
+        if (QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext()) {
+            m_surface = staticOpenGLContext->createWindowSurface(m_data.hwnd, nativeConfig, m_format.colorSpace(), err);
+        }
     }
 
     return m_surface;
Halla Rempt's avatar
Halla Rempt committed
1296
diff --git a/src/widgets/kernel/qopenglwidget.cpp b/src/widgets/kernel/qopenglwidget.cpp
Halla Rempt's avatar
Halla Rempt committed
1297
index 89f860150..0abf707e0 100644
Halla Rempt's avatar
Halla Rempt committed
1298 1299
--- a/src/widgets/kernel/qopenglwidget.cpp
+++ b/src/widgets/kernel/qopenglwidget.cpp
Halla Rempt's avatar
Halla Rempt committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
@@ -568,7 +568,8 @@ public:
           updateBehavior(QOpenGLWidget::NoPartialUpdate),
           requestedSamples(0),
           inPaintGL(false),
-          textureFormat(0)
+          textureFormat(0),
+          textureColorSpace(QSurfaceFormat::DefaultColorSpace)
     {
         requestedFormat = QSurfaceFormat::defaultFormat();
     }
@@ -578,6 +579,7 @@ public:
 
     GLuint textureId() const override;
     QPlatformTextureList::Flags textureListFlags() override;
+    QSurfaceFormat::ColorSpace colorSpace() const override;
 
     void initialize();
     void invokeUserPaint();
@@ -609,6 +611,7 @@ public:
     int requestedSamples;
     bool inPaintGL;
     GLenum textureFormat;
+    QSurfaceFormat::ColorSpace textureColorSpace;
 };
 
 void QOpenGLWidgetPaintDevicePrivate::beginPaint()
@@ -695,6 +698,11 @@ QPlatformTextureList::Flags QOpenGLWidgetPrivate::textureListFlags()
     return flags;
 }
 
+QSurfaceFormat::ColorSpace QOpenGLWidgetPrivate::colorSpace() const
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1331
+{
Halla Rempt's avatar
Halla Rempt committed
1332
+    return textureColorSpace;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1333 1334
+}
+
Halla Rempt's avatar
Halla Rempt committed
1335 1336 1337 1338 1339 1340 1341 1342 1343
 void QOpenGLWidgetPrivate::reset()
 {
     Q_Q(QOpenGLWidget);
@@ -1115,6 +1123,41 @@ void QOpenGLWidget::setTextureFormat(GLenum texFormat)
     d->textureFormat = texFormat;
 }
 
+/*!
+    \return the declared color space of the internal texture of the widget.
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1344
+
Halla Rempt's avatar
Halla Rempt committed
1345 1346
+    The texture's color space will be used when composing the widget
+    into the root window surface.
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1347
+
Halla Rempt's avatar
Halla Rempt committed
1348 1349
+    \note when the color space is set to QSurfaceFormat::DefaultColorSpace,
+    color conversion is effectively disabled.
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1350
+
Halla Rempt's avatar
Halla Rempt committed
1351 1352 1353
+    \since 5.99
+ */
+QSurfaceFormat::ColorSpace QOpenGLWidget::textureColorSpace() const
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1354
+{
Halla Rempt's avatar
Halla Rempt committed
1355 1356
+    Q_D(const QOpenGLWidget);
+    return d->textureColorSpace;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1357 1358
+}
+
Halla Rempt's avatar
Halla Rempt committed
1359 1360
+/*!
+    Sets a custom color space for the internal texture of the widget
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1361
+
Halla Rempt's avatar
Halla Rempt committed
1362 1363
+    The color space of the texture will be compared against the color
+    space of the root surface and conversion will be performed if needed.
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1364
+
Halla Rempt's avatar
Halla Rempt committed
1365 1366 1367
+    \note setting the color space to QSurfaceFormat::DefaultColorSpace will
+    effectively disable color conversion when composing this texture on
+    screen.
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1368
+
Halla Rempt's avatar
Halla Rempt committed
1369 1370 1371
+    \since 5.99
+ */
+void QOpenGLWidget::setTextureColorSpace(QSurfaceFormat::ColorSpace colorSpace)
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1372
+{
Halla Rempt's avatar
Halla Rempt committed
1373 1374
+    Q_D(QOpenGLWidget);
+    d->textureColorSpace = colorSpace;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1375 1376
+}
+
Halla Rempt's avatar
Halla Rempt committed
1377 1378 1379
 /*!
     \return the active internal texture format if the widget has already
     initialized, the requested format if one was set but the widget has not yet
Halla Rempt's avatar
Halla Rempt committed
1380
diff --git a/src/widgets/kernel/qopenglwidget.h b/src/widgets/kernel/qopenglwidget.h
Halla Rempt's avatar
Halla Rempt committed
1381
index 9eb4a9ba5..eff2d9796 100644
Halla Rempt's avatar
Halla Rempt committed
1382 1383
--- a/src/widgets/kernel/qopenglwidget.h
+++ b/src/widgets/kernel/qopenglwidget.h
Halla Rempt's avatar
Halla Rempt committed
1384 1385 1386 1387 1388 1389
@@ -75,6 +75,9 @@ public:
     GLenum textureFormat() const;
     void setTextureFormat(GLenum texFormat);
 
+    QSurfaceFormat::ColorSpace textureColorSpace() const;
+    void setTextureColorSpace(QSurfaceFormat::ColorSpace colorSpace);
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1390
+
Halla Rempt's avatar
Halla Rempt committed
1391
     bool isValid() const;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1392
 
Halla Rempt's avatar
Halla Rempt committed
1393
     void makeCurrent();
Halla Rempt's avatar
Halla Rempt committed
1394
diff --git a/src/widgets/kernel/qwidget_p.h b/src/widgets/kernel/qwidget_p.h
Halla Rempt's avatar
Halla Rempt committed
1395
index 7e4ea2cc0..1ff5af426 100644
Halla Rempt's avatar
Halla Rempt committed
1396 1397
--- a/src/widgets/kernel/qwidget_p.h
+++ b/src/widgets/kernel/qwidget_p.h
Halla Rempt's avatar
Halla Rempt committed
1398 1399 1400 1401 1402 1403 1404 1405
@@ -655,6 +655,7 @@ public:
             ? QPlatformTextureList::StacksOnTop
             : QPlatformTextureList::Flags(0);
     }
+    virtual QSurfaceFormat::ColorSpace colorSpace() const { return QSurfaceFormat::DefaultColorSpace; }
     virtual QImage grabFramebuffer() { return QImage(); }
     virtual void beginBackingStorePainting() { }
     virtual void endBackingStorePainting() { }
Halla Rempt's avatar
Halla Rempt committed
1406
diff --git a/src/widgets/kernel/qwidgetbackingstore.cpp b/src/widgets/kernel/qwidgetbackingstore.cpp
Halla Rempt's avatar
Halla Rempt committed
1407
index a32eb2a03..db6033803 100644
Halla Rempt's avatar
Halla Rempt committed
1408 1409
--- a/src/widgets/kernel/qwidgetbackingstore.cpp
+++ b/src/widgets/kernel/qwidgetbackingstore.cpp
Halla Rempt's avatar
Halla Rempt committed
1410 1411 1412 1413 1414 1415 1416
@@ -1007,7 +1007,7 @@ static void findTextureWidgetsRecursively(QWidget *tlw, QWidget *widget, QPlatfo
     if (wd->renderToTexture) {
         QPlatformTextureList::Flags flags = wd->textureListFlags();
         const QRect rect(widget->mapTo(tlw, QPoint()), widget->size());
-        widgetTextures->appendTexture(widget, wd->textureId(), rect, wd->clipRect(), flags);
+        widgetTextures->appendTexture(widget, wd->textureId(), rect, wd->clipRect(), flags, wd->colorSpace());
     }
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1417
 
Halla Rempt's avatar
Halla Rempt committed
1418
     for (int i = 0; i < wd->children.size(); ++i) {
Halla Rempt's avatar
Halla Rempt committed
1419
diff --git a/src/widgets/widgets/qdockwidget.cpp b/src/widgets/widgets/qdockwidget.cpp
Halla Rempt's avatar
Halla Rempt committed
1420
index 6c871aae2..19fc2d167 100644
Halla Rempt's avatar
Halla Rempt committed
1421 1422
--- a/src/widgets/widgets/qdockwidget.cpp
+++ b/src/widgets/widgets/qdockwidget.cpp
Dmitry Kazakov's avatar
Dmitry Kazakov committed
1423 1424 1425 1426 1427 1428 1429 1430 1431
@@ -1171,6 +1171,8 @@ void QDockWidgetPrivate::setWindowState(bool floating, bool unplug, const QRect
             QMainWindowLayout *mwlayout = qt_mainwindow_layout_from_dock(q);
             if (mwlayout)
                 emit q->dockLocationChanged(mwlayout->dockWidgetArea(q));
+        } else {
+            emit q->dockLocationChanged(Qt::NoDockWidgetArea);
         }
     }
 
Halla Rempt's avatar
Halla Rempt committed
1432
diff --git a/tests/manual/manual.pro b/tests/manual/manual.pro
Halla Rempt's avatar
Halla Rempt committed
1433
index ab00a5ef6..b202ed043 100644
Halla Rempt's avatar
Halla Rempt committed
1434 1435
--- a/tests/manual/manual.pro
+++ b/tests/manual/manual.pro
Halla Rempt's avatar
Halla Rempt committed
1436 1437 1438 1439 1440 1441 1442 1443 1444
@@ -59,7 +59,7 @@ qtabbar
 
 qtConfig(opengl) {
     SUBDIRS += qopengltextureblitter
-    qtConfig(egl): SUBDIRS += qopenglcontext
+    qtConfig(egl): SUBDIRS += qopenglcontext hdr-qopenglwidget
 }
 
 win32: SUBDIRS -= network_remote_stresstest network_stresstest