1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // CompositorNativeWindow11.h: Implementation of NativeWindow11 using Windows.UI.Composition APIs
8 // which work in both Win32 and WinRT contexts.
9 
10 #ifndef LIBANGLE_RENDERER_D3D_D3D11_CONVERGED_COMPOSITORNATIVEWINDOW11_H_
11 #define LIBANGLE_RENDERER_D3D_D3D11_CONVERGED_COMPOSITORNATIVEWINDOW11_H_
12 
13 #include "libANGLE/renderer/d3d/d3d11/NativeWindow11.h"
14 
15 #include <dispatcherqueue.h>
16 #include <versionhelpers.h>
17 #include <windows.foundation.metadata.h>
18 #include <windows.ui.composition.h>
19 #include <windows.ui.composition.interop.h>
20 #include <wrl.h>
21 
22 namespace rx
23 {
24 
25 class RoHelper
26 {
27   public:
28     RoHelper();
29     ~RoHelper();
30     bool WinRtAvailable() const;
31     bool SupportedWindowsRelease();
32     HRESULT GetStringReference(PCWSTR source, HSTRING *act, HSTRING_HEADER *header);
33     HRESULT GetActivationFactory(const HSTRING act, const IID &interfaceId, void **fac);
34     HRESULT WindowsCompareStringOrdinal(HSTRING one, HSTRING two, int *result);
35     HRESULT CreateDispatcherQueueController(
36         DispatcherQueueOptions options,
37         ABI::Windows::System::IDispatcherQueueController **dispatcherQueueController);
38     HRESULT WindowsDeleteString(HSTRING one);
39     HRESULT RoInitialize(RO_INIT_TYPE type);
40     void RoUninitialize();
41 
42   private:
43     using WindowsCreateStringReference_ = HRESULT __stdcall(PCWSTR,
44                                                             UINT32,
45                                                             HSTRING_HEADER *,
46                                                             HSTRING *);
47 
48     using GetActivationFactory_ = HRESULT __stdcall(HSTRING, REFIID, void **);
49 
50     using WindowsCompareStringOrginal_ = HRESULT __stdcall(HSTRING, HSTRING, int *);
51 
52     using WindowsDeleteString_ = HRESULT __stdcall(HSTRING);
53 
54     using CreateDispatcherQueueController_ =
55         HRESULT __stdcall(DispatcherQueueOptions,
56                           ABI::Windows::System::IDispatcherQueueController **);
57 
58     using RoInitialize_   = HRESULT __stdcall(RO_INIT_TYPE);
59     using RoUninitialize_ = void __stdcall();
60 
61     WindowsCreateStringReference_ *mFpWindowsCreateStringReference;
62     GetActivationFactory_ *mFpGetActivationFactory;
63     WindowsCompareStringOrginal_ *mFpWindowsCompareStringOrdinal;
64     CreateDispatcherQueueController_ *mFpCreateDispatcherQueueController;
65     WindowsDeleteString_ *mFpWindowsDeleteString;
66     RoInitialize_ *mFpRoInitialize;
67     RoUninitialize_ *mFpRoUninitialize;
68 
69     bool mWinRtAvailable;
70 
71     HMODULE mComBaseModule;
72     HMODULE mCoreMessagingModule;
73 };
74 
75 class CompositorNativeWindow11 : public NativeWindow11
76 {
77   public:
78     CompositorNativeWindow11(EGLNativeWindowType window, bool hasAlpha);
79     ~CompositorNativeWindow11() override;
80 
81     bool initialize() override;
82     bool getClientRect(LPRECT rect) const override;
83     bool isIconic() const override;
84 
85     HRESULT createSwapChain(ID3D11Device *device,
86                             IDXGIFactory *factory,
87                             DXGI_FORMAT format,
88                             UINT width,
89                             UINT height,
90                             UINT samples,
91                             IDXGISwapChain **swapChain) override;
92 
93     void commitChange() override;
94 
95     static bool IsValidNativeWindow(EGLNativeWindowType window);
96 
97     static bool IsSupportedWinRelease();
98 
99   private:
100     static bool IsSpriteVisual(EGLNativeWindowType window);
101 
102     bool mHasAlpha;
103 
104     RoHelper mRoHelper;
105 
106     // Namespace prefix required here for some reason despite using namespace
107     Microsoft::WRL::ComPtr<ABI::Windows::UI::Composition::ISpriteVisual> mHostVisual;
108     Microsoft::WRL::ComPtr<ABI::Windows::UI::Composition::ICompositionBrush> mCompositionBrush;
109     Microsoft::WRL::ComPtr<ABI::Windows::UI::Composition::ICompositionSurface> mSurface;
110     Microsoft::WRL::ComPtr<ABI::Windows::UI::Composition::ICompositionSurfaceBrush> mSurfaceBrush;
111 };
112 
113 }  // namespace rx
114 
115 #endif  // LIBANGLE_RENDERER_D3D_D3D11_CONVERGED_COMPOSITORNATIVEWINDOW11_H_
116