From 7a3851e74393eb254826face20b8e9af74bbdf0b Mon Sep 17 00:00:00 2001
From: Oliver Wolff <oliver.wolff@theqtcompany.com>
Date: Thu, 17 Mar 2016 15:46:56 +0100
Subject: [PATCH] ANGLE: Use pixel sizes in the XAML swap chain

This is necessary for Qt applications, as they render to GL in physical
pixels. This is consistent with the CoreWindow swap chain behavior.

This includes a partial revert of "ANGLE: Improve Windows Phone Support"
as the modifications to SwapChain11 are incompatible with the XAML swap
chain.

This change only affects Windows Runtime targets.

Change-Id: I401ae81028a9dfdb9da37f51c60844467c476f76
---
 .../libANGLE/renderer/d3d/d3d11/SwapChain11.cpp    | 46 ----------------------
 .../d3d/d3d11/winrt/CoreWindowNativeWindow.cpp     | 25 +-----------
 .../d3d/d3d11/winrt/CoreWindowNativeWindow.h       |  7 +---
 .../d3d/d3d11/winrt/InspectableNativeWindow.cpp    | 20 ++++++++++
 .../d3d/d3d11/winrt/InspectableNativeWindow.h      |  9 +++--
 .../d3d/d3d11/winrt/SwapChainPanelNativeWindow.cpp | 43 ++++++++++++--------
 .../d3d/d3d11/winrt/SwapChainPanelNativeWindow.h   |  7 ++--
 7 files changed, 58 insertions(+), 99 deletions(-)

diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp
index f80f24b..785a83c 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/SwapChain11.cpp
@@ -226,14 +226,8 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
         const bool useSharedResource = !mNativeWindow.getNativeWindow() && mRenderer->getShareHandleSupport();
 
         D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0};
-#if defined(ANGLE_ENABLE_WINDOWS_STORE) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
-        const int textureLength = std::max(backbufferWidth, backbufferHeight);
-        offscreenTextureDesc.Width = textureLength;
-        offscreenTextureDesc.Height = textureLength;
-#else
         offscreenTextureDesc.Width = backbufferWidth;
         offscreenTextureDesc.Height = backbufferHeight;
-#endif
         offscreenTextureDesc.Format = backbufferFormatInfo.texFormat;
         offscreenTextureDesc.MipLevels = 1;
         offscreenTextureDesc.ArraySize = 1;
@@ -346,14 +340,8 @@ EGLint SwapChain11::resetOffscreenDepthBuffer(int backbufferWidth, int backbuffe
             d3d11::GetTextureFormatInfo(mDepthBufferFormat, mRenderer->getRenderer11DeviceCaps());
 
         D3D11_TEXTURE2D_DESC depthStencilTextureDesc;
-#if defined(ANGLE_ENABLE_WINDOWS_STORE) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
-        const int textureLength = std::max(backbufferWidth, backbufferHeight);
-        depthStencilTextureDesc.Width = textureLength;
-        depthStencilTextureDesc.Height = textureLength;
-#else
         depthStencilTextureDesc.Width = backbufferWidth;
         depthStencilTextureDesc.Height = backbufferHeight;
-#endif
         depthStencilTextureDesc.Format = depthBufferFormatInfo.texFormat;
         depthStencilTextureDesc.MipLevels = 1;
         depthStencilTextureDesc.ArraySize = 1;
@@ -438,7 +426,6 @@ EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight)
         return EGL_SUCCESS;
     }
 
-#if !defined(ANGLE_ENABLE_WINDOWS_STORE) || (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
     // Can only call resize if we have already created our swap buffer and resources
     ASSERT(mSwapChain && mBackBufferTexture && mBackBufferRTView && mBackBufferSRView);
 
@@ -496,12 +483,6 @@ EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight)
     mFirstSwap = true;
 
     return resetOffscreenBuffers(backbufferWidth, backbufferHeight);
-#else
-    // Do nothing on Windows Phone apart from updating the internal buffer/width height
-    mWidth = backbufferWidth;
-    mHeight = backbufferHeight;
-    return EGL_SUCCESS;
-#endif
 }
 
 DXGI_FORMAT SwapChain11::getSwapChainNativeFormat() const
@@ -725,27 +706,6 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width,
 
     d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
 
-#if defined(ANGLE_ENABLE_WINDOWS_STORE) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
-    // Create a quad in homogeneous coordinates
-    float x1 = -1.0f;
-    float y1 = -1.0f;
-    float x2 = 1.0f;
-    float y2 = 1.0f;
-
-    const float dim = std::max(mWidth, mHeight);
-    float u1 = 0;
-    float v1 = 0;
-    float u2 = float(width) / dim;
-    float v2 = float(height) / dim;
-
-    const NativeWindow::RotationFlags flags = mNativeWindow.rotationFlags();
-    const bool rotateL = flags == NativeWindow::RotateLeft;
-    const bool rotateR = flags == NativeWindow::RotateRight;
-    d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, rotateL ? u2 : u1, rotateR ? v2 : v1);
-    d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, rotateR ? u2 : u1, rotateL ? v1 : v2);
-    d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, rotateR ? u1 : u2, rotateL ? v2 : v1);
-    d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, rotateL ? u1 : u2, rotateR ? v1 : v2);
-#else
     // Create a quad in homogeneous coordinates
     float x1 = (x / float(mWidth)) * 2.0f - 1.0f;
     float y1 = (y / float(mHeight)) * 2.0f - 1.0f;
@@ -771,7 +731,6 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width,
     d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v2);
     d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v1);
     d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v2);
-#endif
 
     deviceContext->Unmap(mQuadVB, 0);
 
@@ -801,13 +760,8 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width,
     D3D11_VIEWPORT viewport;
     viewport.TopLeftX = 0;
     viewport.TopLeftY = 0;
-#if defined(ANGLE_ENABLE_WINDOWS_STORE) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
-    viewport.Width = (rotateL || rotateR) ? height : width;
-    viewport.Height = (rotateL || rotateR) ? width : height;
-#else
     viewport.Width = static_cast<FLOAT>(mWidth);
     viewport.Height = static_cast<FLOAT>(mHeight);
-#endif
     viewport.MinDepth = 0.0f;
     viewport.MaxDepth = 1.0f;
     deviceContext->RSSetViewports(1, &viewport);
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.cpp
index b074e8c..f401db6 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.cpp
@@ -222,7 +222,7 @@ HRESULT CoreWindowNativeWindow::createSwapChain(ID3D11Device *device,
     return result;
 }
 
-inline HRESULT CoreWindowNativeWindow::scaleSwapChain(const SIZE &windowSize, const RECT &clientRect)
+inline HRESULT CoreWindowNativeWindow::scaleSwapChain(const Size &windowSize, const RECT &clientRect)
 {
     // We don't need to do any additional work to scale CoreWindow swapchains.
     // Using DXGI_SCALING_STRETCH to create the swapchain above does all the necessary work.
@@ -240,27 +240,4 @@ HRESULT GetCoreWindowSizeInPixels(const ComPtr<ABI::Windows::UI::Core::ICoreWind
 
     return result;
 }
-
-static float GetLogicalDpi()
-{
-    ComPtr<ABI::Windows::Graphics::Display::IDisplayPropertiesStatics> displayProperties;
-
-    if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(), displayProperties.GetAddressOf())))
-    {
-        float dpi = 96.0f;
-        if (SUCCEEDED(displayProperties->get_LogicalDpi(&dpi)))
-        {
-            return dpi;
-        }
-    }
-
-    // Return 96 dpi as a default if display properties cannot be obtained.
-    return 96.0f;
-}
-
-long ConvertDipsToPixels(float dips)
-{
-    static const float dipsPerInch = 96.0f;
-    return lround((dips * GetLogicalDpi() / dipsPerInch));
-}
 }
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.h
index 4de235a..fc1cd12 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/CoreWindowNativeWindow.h
@@ -19,8 +19,6 @@ typedef ABI::Windows::Foundation::__FITypedEventHandler_2_Windows__CGraphics__CD
 
 namespace rx
 {
-long ConvertDipsToPixels(float dips);
-
 class CoreWindowNativeWindow : public InspectableNativeWindow, public std::enable_shared_from_this<CoreWindowNativeWindow>
 {
   public:
@@ -36,7 +34,7 @@ class CoreWindowNativeWindow : public InspectableNativeWindow, public std::enabl
                             DXGISwapChain **swapChain) override;
 
   protected:
-    HRESULT scaleSwapChain(const SIZE &windowSize, const RECT &clientRect) override;
+    HRESULT scaleSwapChain(const Size &windowSize, const RECT &clientRect) override;
 
     bool registerForSizeChangeEvents();
     void unregisterForSizeChangeEvents();
@@ -74,8 +72,7 @@ class CoreWindowSizeChangedHandler :
             ABI::Windows::Foundation::Size windowSize;
             if (SUCCEEDED(sizeChangedEventArgs->get_Size(&windowSize)))
             {
-                SIZE windowSizeInPixels = { ConvertDipsToPixels(windowSize.Width), ConvertDipsToPixels(windowSize.Height) };
-                host->setNewClientSize(windowSizeInPixels);
+                host->setNewClientSize(windowSize);
             }
         }
 
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.cpp
index c9b203e..aacfadd 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.cpp
@@ -381,4 +381,24 @@ HRESULT GetOptionalSinglePropertyValue(const ComPtr<ABI::Windows::Foundation::Co
     return result;
 }
 
+static float GetLogicalDpi()
+{
+    ComPtr<ABI::Windows::Graphics::Display::IDisplayPropertiesStatics> displayProperties;
+    float dpi = 96.0f;
+
+    if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(), displayProperties.GetAddressOf())))
+    {
+        if (SUCCEEDED(displayProperties->get_LogicalDpi(&dpi)))
+        {
+            return dpi;
+        }
+    }
+    return dpi;
+}
+
+long ConvertDipsToPixels(float dips)
+{
+    static const float dipsPerInch = 96.0f;
+    return lround((dips * GetLogicalDpi() / dipsPerInch));
+}
 }
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.h
index 70e5fe7..cdbf40e 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/InspectableNativeWindow.h
@@ -26,6 +26,8 @@ using namespace ABI::Windows::Foundation::Collections;
 
 namespace rx
 {
+long ConvertDipsToPixels(float dips);
+
 class InspectableNativeWindow
 {
   public:
@@ -65,7 +67,7 @@ class InspectableNativeWindow
     }
 
     // setNewClientSize is used by the WinRT size change handler. It isn't used by the rest of ANGLE.
-    void setNewClientSize(const SIZE &newWindowSize)
+    void setNewClientSize(const Size &newWindowSize)
     {
         // If the client doesn't support swapchain resizing then we should have already unregistered from size change handler
         ASSERT(mSupportsSwapChainResize);
@@ -76,7 +78,7 @@ class InspectableNativeWindow
             if (!mSwapChainSizeSpecified)
             {
                 // We don't have to check if a swapchain scale was specified here; the default value is 1.0f which will have no effect.
-                mNewClientRect = { 0, 0, static_cast<long>(newWindowSize.cx * mSwapChainScale), static_cast<long>(newWindowSize.cy * mSwapChainScale) };
+                mNewClientRect = { 0, 0, ConvertDipsToPixels(newSize.Width), ConvertDipsToPixels(newSize.Height) };
                 mClientRectChanged = true;
 
                 // If a scale was specified, then now is the time to apply the scale matrix for the new swapchain size and window size
@@ -106,7 +109,7 @@ class InspectableNativeWindow
     }
 
   protected:
-    virtual HRESULT scaleSwapChain(const SIZE &windowSize, const RECT &clientRect) = 0;
+    virtual HRESULT scaleSwapChain(const Size &windowSize, const RECT &clientRect) = 0;
 
     bool mSupportsSwapChainResize; // Support for IDXGISwapChain::ResizeBuffers method
     bool mSwapChainSizeSpecified;  // If an EGLRenderSurfaceSizeProperty was specified
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.cpp b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.cpp
index 1ed3645..d3ed35b 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.cpp
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.cpp
@@ -171,12 +171,15 @@ bool SwapChainPanelNativeWindow::initialize(EGLNativeWindowType window, IPropert
         {
             SIZE swapChainPanelSize;
             result = GetSwapChainPanelSize(mSwapChainPanel, mSwapChainPanelDispatcher,
-                                           &swapChainPanelSize);
+                                           &swapChainPanelSize, &mSwapChainScale);
+            if (mSwapChainScale != 1.0f)
+                mSwapChainScaleSpecified = true;
 
             if (SUCCEEDED(result))
             {
                 // Update the client rect to account for any swapchain scale factor
-                mClientRect = { 0, 0, static_cast<long>(swapChainPanelSize.cx * mSwapChainScale), static_cast<long>(swapChainPanelSize.cy * mSwapChainScale) };
+                mClientRect = { 0, 0, static_cast<long>(ConvertDipsToPixels(swapChainPanelSize.cx * mSwapChainScale)),
+                    static_cast<long>(ConvertDipsToPixels(swapChainPanelSize.cy * mSwapChainScale)) };
             }
         }
     }
@@ -269,7 +272,6 @@ HRESULT SwapChainPanelNativeWindow::createSwapChain(ID3D11Device *device,
 
     ComPtr<IDXGISwapChain1> newSwapChain;
     ComPtr<ISwapChainPanelNative> swapChainPanelNative;
-    SIZE currentPanelSize = {};
 
     HRESULT result = factory->CreateSwapChainForComposition(device, &swapChainDesc, nullptr, newSwapChain.ReleaseAndGetAddressOf());
 
@@ -304,24 +306,24 @@ HRESULT SwapChainPanelNativeWindow::createSwapChain(ID3D11Device *device,
     {
         if (mSwapChainSizeSpecified || mSwapChainScaleSpecified)
         {
-            result = GetSwapChainPanelSize(mSwapChainPanel, mSwapChainPanelDispatcher,
-                                           &currentPanelSize);
-
-            // Scale the swapchain to fit inside the contents of the panel.
-            if (SUCCEEDED(result))
-            {
-                result = scaleSwapChain(currentPanelSize, mClientRect);
-            }
+            ComPtr<ABI::Windows::UI::Xaml::IUIElement> uiElement;
+            result = mSwapChainPanel.As(&uiElement);
+            ASSERT(SUCCEEDED(result));
+
+            Size currentSize;
+            result = uiElement->get_RenderSize(&currentSize);
+            ASSERT(SUCCEEDED(result));
+            result = scaleSwapChain(currentSize, mClientRect);
         }
     }
 
     return result;
 }
 
-HRESULT SwapChainPanelNativeWindow::scaleSwapChain(const SIZE &windowSize, const RECT &clientRect)
+HRESULT SwapChainPanelNativeWindow::scaleSwapChain(const Size &windowSize, const RECT &clientRect)
 {
-    Size renderScale = {(float)windowSize.cx / (float)clientRect.right,
-                        (float)windowSize.cy / (float)clientRect.bottom};
+    Size renderScale = {windowSize.Width / clientRect.right,
+                        windowSize.Height / clientRect.bottom};
     // Setup a scale matrix for the swap chain
     DXGI_MATRIX_3X2_F scaleMatrix = {};
     scaleMatrix._11 = renderScale.Width;
@@ -340,7 +342,7 @@ HRESULT SwapChainPanelNativeWindow::scaleSwapChain(const SIZE &windowSize, const
 HRESULT GetSwapChainPanelSize(
     const ComPtr<ABI::Windows::UI::Xaml::Controls::ISwapChainPanel> &swapChainPanel,
     const ComPtr<ICoreDispatcher> &dispatcher,
-    SIZE *windowSize)
+    SIZE *windowSize, float *scaleFactor)
 {
     ComPtr<IUIElement> uiElement;
     Size renderSize = {0, 0};
@@ -357,9 +359,16 @@ HRESULT GetSwapChainPanelSize(
 
     if (SUCCEEDED(result))
     {
-        *windowSize = { lround(renderSize.Width), lround(renderSize.Height) };
+        long width = ConvertDipsToPixels(renderSize.Width);
+        long height = ConvertDipsToPixels(renderSize.Height);
+        *windowSize = { width, height };
+
+        if (scaleFactor)
+        {
+            *scaleFactor = renderSize.Width / width;
+        }
     }
 
     return result;
 }
-}
\ No newline at end of file
+}
diff --git a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.h b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.h
index 9cc051d..09d87ad 100644
--- a/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.h
+++ b/src/3rdparty/angle/src/libANGLE/renderer/d3d/d3d11/winrt/SwapChainPanelNativeWindow.h
@@ -28,7 +28,7 @@ class SwapChainPanelNativeWindow : public InspectableNativeWindow, public std::e
                             DXGISwapChain **swapChain) override;
 
   protected:
-    HRESULT scaleSwapChain(const SIZE &windowSize, const RECT &clientRect) override;
+    HRESULT scaleSwapChain(const Size &windowSize, const RECT &clientRect) override;
 
     bool registerForSizeChangeEvents();
     void unregisterForSizeChangeEvents();
@@ -72,8 +72,7 @@ class SwapChainPanelSizeChangedHandler :
             HRESULT result = sizeChangedEventArgs->get_NewSize(&newSize);
             if (SUCCEEDED(result))
             {
-                SIZE windowSize = { lround(newSize.Width), lround(newSize.Height) };
-                host->setNewClientSize(windowSize);
+                host->setNewClientSize(newSize);
             }
         }
 
@@ -87,6 +86,6 @@ class SwapChainPanelSizeChangedHandler :
 HRESULT GetSwapChainPanelSize(
     const ComPtr<ABI::Windows::UI::Xaml::Controls::ISwapChainPanel> &swapChainPanel,
     const ComPtr<ABI::Windows::UI::Core::ICoreDispatcher> &dispatcher,
-    SIZE *windowSize);
+    SIZE *windowSize, float *scaleFactor);
 }
 #endif // LIBANGLE_RENDERER_D3D_D3D11_WINRT_SWAPCHAINPANELNATIVEWINDOW_H_
-- 
2.7.0.windows.1

