summaryrefslogtreecommitdiffstats
path: root/gfx/angle/checkout/src/libANGLE/renderer/d3d/d3d11/converged/CompositorNativeWindow11.cpp
blob: 0e64f78d53c15864beda219ee67882c41398a05f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// CompositorNativeWindow11.cpp: Implementation of NativeWindow11 using Windows.UI.Composition APIs
// which work in both Win32 and WinRT contexts.

#include "libANGLE/renderer/d3d/d3d11/converged/CompositorNativeWindow11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"

#include "common/debug.h"

using namespace Microsoft::WRL;

namespace rx
{

CompositorNativeWindow11::CompositorNativeWindow11(EGLNativeWindowType window, bool hasAlpha)
    : NativeWindow11(window), mHasAlpha(hasAlpha)
{
    ABI::Windows::UI::Composition::ISpriteVisual *inspPtr =
        reinterpret_cast<ABI::Windows::UI::Composition::ISpriteVisual *>(window);
    mHostVisual = Microsoft::WRL::ComPtr<ABI::Windows::UI::Composition::ISpriteVisual>{inspPtr};
}

CompositorNativeWindow11::~CompositorNativeWindow11() = default;

bool CompositorNativeWindow11::initialize()
{
    return true;
}

bool CompositorNativeWindow11::getClientRect(LPRECT rect) const
{
    ComPtr<ABI::Windows::UI::Composition::IVisual> visual;
    mHostVisual.As(&visual);

    ABI::Windows::Foundation::Numerics::Vector2 size;
    HRESULT hr = visual->get_Size(&size);
    if (FAILED(hr))
    {
        return false;
    }

    ABI::Windows::Foundation::Numerics::Vector3 offset;
    hr = visual->get_Offset(&offset);
    if (FAILED(hr))
    {
        return false;
    }

    rect->top    = static_cast<LONG>(offset.Y);
    rect->left   = static_cast<LONG>(offset.X);
    rect->right  = static_cast<LONG>(offset.X) + static_cast<LONG>(size.X);
    rect->bottom = static_cast<LONG>(offset.Y) + static_cast<LONG>(size.Y);

    return true;
}

bool CompositorNativeWindow11::isIconic() const
{
    return false;
}

HRESULT CompositorNativeWindow11::createSwapChain(ID3D11Device *device,
                                                  IDXGIFactory *factory,
                                                  DXGI_FORMAT format,
                                                  UINT width,
                                                  UINT height,
                                                  UINT samples,
                                                  IDXGISwapChain **swapChain)
{
    if (device == nullptr || factory == nullptr || swapChain == nullptr || width == 0 ||
        height == 0)
    {
        return E_INVALIDARG;
    }

    HRESULT hr{E_FAIL};

    ComPtr<ABI::Windows::UI::Composition::ICompositionObject> hostVisual;
    hr = mHostVisual.As(&hostVisual);
    if (FAILED(hr))
    {
        return hr;
    }

    Microsoft::WRL::ComPtr<ABI::Windows::UI::Composition::ICompositor> compositor;
    hr = hostVisual->get_Compositor(&compositor);
    if (FAILED(hr))
    {
        return hr;
    }

    ComPtr<ABI::Windows::UI::Composition::ICompositorInterop> interop;

    hr = compositor.As(&interop);
    if (FAILED(hr))
    {
        return hr;
    }

    ComPtr<IDXGIFactory2> factory2;
    factory2.Attach(d3d11::DynamicCastComObject<IDXGIFactory2>(factory));

    DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
    swapChainDesc.Width                 = width;
    swapChainDesc.Height                = height;
    swapChainDesc.Format                = format;
    swapChainDesc.Stereo                = FALSE;
    swapChainDesc.SampleDesc.Count      = 1;
    swapChainDesc.SampleDesc.Quality    = 0;
    swapChainDesc.BufferUsage =
        DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_BACK_BUFFER | DXGI_USAGE_SHADER_INPUT;
    swapChainDesc.BufferCount = 2;
    swapChainDesc.Scaling     = DXGI_SCALING_STRETCH;
    swapChainDesc.SwapEffect  = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
    swapChainDesc.AlphaMode   = mHasAlpha ? DXGI_ALPHA_MODE_PREMULTIPLIED : DXGI_ALPHA_MODE_IGNORE;
#ifndef ANGLE_ENABLE_WINDOWS_UWP
    swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG::DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
#endif
    Microsoft::WRL::ComPtr<IDXGISwapChain1> swapChain1;
    hr = factory2->CreateSwapChainForComposition(device, &swapChainDesc, nullptr, &swapChain1);
    if (SUCCEEDED(hr))
    {
        swapChain1.CopyTo(swapChain);
    }

    hr = interop->CreateCompositionSurfaceForSwapChain(swapChain1.Get(), &mSurface);
    if (FAILED(hr))
    {
        return hr;
    }

    hr = compositor->CreateSurfaceBrushWithSurface(mSurface.Get(), &mSurfaceBrush);
    if (FAILED(hr))
    {
        return hr;
    }

    hr = mSurfaceBrush.As(&mCompositionBrush);
    if (FAILED(hr))
    {
        return hr;
    }

    hr = mHostVisual->put_Brush(mCompositionBrush.Get());
    if (FAILED(hr))
    {
        return hr;
    }

    return hr;
}

void CompositorNativeWindow11::commitChange()
{
    // Windows::UI::Composition uses an implicit commit model hence no action needed here
}

// static
bool CompositorNativeWindow11::IsValidNativeWindow(EGLNativeWindowType window)
{
    return IsSupportedWinRelease() && IsSpriteVisual(window);
}

// static
bool CompositorNativeWindow11::IsSupportedWinRelease()
{
    RoHelper helper;
    if (!helper.WinRtAvailable())
    {
        return false;
    }

    return helper.SupportedWindowsRelease();
}

bool CompositorNativeWindow11::IsSpriteVisual(EGLNativeWindowType window)
{
    RoHelper helper;

    ABI::Windows::UI::Composition::ISpriteVisual *inspp =
        reinterpret_cast<ABI::Windows::UI::Composition::ISpriteVisual *>(window);
    HSTRING className, spriteClassName;
    HSTRING_HEADER spriteClassNameHeader;

    auto hr = helper.GetStringReference(RuntimeClass_Windows_UI_Composition_SpriteVisual,
                                        &spriteClassName, &spriteClassNameHeader);
    if (FAILED(hr))
    {
        return false;
    }

    hr = inspp->GetRuntimeClassName(&className);
    if (FAILED(hr))
    {
        return false;
    }

    INT32 result = -1;
    hr           = helper.WindowsCompareStringOrdinal(className, spriteClassName, &result);

    helper.WindowsDeleteString(className);

    if (FAILED(hr))
    {
        return false;
    }

    if (result == 0)
    {
        return true;
    }

    return false;
}

// RoHelperImpl

template <typename T>
bool AssignProcAddress(HMODULE comBaseModule, const char *name, T *&outProc)
{
    outProc = reinterpret_cast<T *>(GetProcAddress(comBaseModule, name));
    return *outProc != nullptr;
}

RoHelper::RoHelper()
    : mFpWindowsCreateStringReference(nullptr),
      mFpGetActivationFactory(nullptr),
      mFpWindowsCompareStringOrdinal(nullptr),
      mFpCreateDispatcherQueueController(nullptr),
      mFpWindowsDeleteString(nullptr),
      mFpRoInitialize(nullptr),
      mFpRoUninitialize(nullptr),
      mWinRtAvailable(false),
      mWinRtInitialized(false),
      mComBaseModule(nullptr),
      mCoreMessagingModule(nullptr)
{

#ifdef ANGLE_ENABLE_WINDOWS_UWP
    mFpWindowsCreateStringReference    = &::WindowsCreateStringReference;
    mFpRoInitialize                    = &::RoInitialize;
    mFpRoUninitialize                  = &::RoUninitialize;
    mFpWindowsDeleteString             = &::WindowsDeleteString;
    mFpGetActivationFactory            = &::RoGetActivationFactory;
    mFpWindowsCompareStringOrdinal     = &::WindowsCompareStringOrdinal;
    mFpCreateDispatcherQueueController = &::CreateDispatcherQueueController;
    mWinRtAvailable                    = true;
#else

    mComBaseModule = LoadLibraryA("ComBase.dll");

    if (mComBaseModule == nullptr)
    {
        return;
    }

    if (!AssignProcAddress(mComBaseModule, "WindowsCreateStringReference",
                           mFpWindowsCreateStringReference))
    {
        return;
    }

    if (!AssignProcAddress(mComBaseModule, "RoGetActivationFactory", mFpGetActivationFactory))
    {
        return;
    }

    if (!AssignProcAddress(mComBaseModule, "WindowsCompareStringOrdinal",
                           mFpWindowsCompareStringOrdinal))
    {
        return;
    }

    if (!AssignProcAddress(mComBaseModule, "WindowsDeleteString", mFpWindowsDeleteString))
    {
        return;
    }

    if (!AssignProcAddress(mComBaseModule, "RoInitialize", mFpRoInitialize))
    {
        return;
    }

    if (!AssignProcAddress(mComBaseModule, "RoUninitialize", mFpRoUninitialize))
    {
        return;
    }

    mCoreMessagingModule = LoadLibraryA("coremessaging.dll");

    if (mCoreMessagingModule == nullptr)
    {
        return;
    }

    if (!AssignProcAddress(mCoreMessagingModule, "CreateDispatcherQueueController",
                           mFpCreateDispatcherQueueController))
    {
        return;
    }

    auto result = RoInitialize(RO_INIT_MULTITHREADED);

    if (SUCCEEDED(result) || result == RPC_E_CHANGED_MODE)
    {
        mWinRtAvailable = true;

        if (SUCCEEDED(result))
        {
            mWinRtInitialized = true;
        }
    }
#endif
}

RoHelper::~RoHelper()
{
#ifndef ANGLE_ENABLE_WINDOWS_UWP
    if (mWinRtInitialized)
    {
        RoUninitialize();
    }

    if (mCoreMessagingModule != nullptr)
    {
        FreeLibrary(mCoreMessagingModule);
        mCoreMessagingModule = nullptr;
    }

    if (mComBaseModule != nullptr)
    {
        FreeLibrary(mComBaseModule);
        mComBaseModule = nullptr;
    }
#endif
}

bool RoHelper::WinRtAvailable() const
{
    return mWinRtAvailable;
}

bool RoHelper::SupportedWindowsRelease()
{
    if (!mWinRtAvailable)
    {
        return false;
    }

    HSTRING className, contractName;
    HSTRING_HEADER classNameHeader, contractNameHeader;
    boolean isSupported = false;

    HRESULT hr = GetStringReference(RuntimeClass_Windows_Foundation_Metadata_ApiInformation,
                                    &className, &classNameHeader);

    if (FAILED(hr))
    {
        return !!isSupported;
    }

    Microsoft::WRL::ComPtr<ABI::Windows::Foundation::Metadata::IApiInformationStatics> api;

    hr = GetActivationFactory(
        className, __uuidof(ABI::Windows::Foundation::Metadata::IApiInformationStatics), &api);

    if (FAILED(hr))
    {
        return !!isSupported;
    }

    hr = GetStringReference(L"Windows.Foundation.UniversalApiContract", &contractName,
                            &contractNameHeader);
    if (FAILED(hr))
    {
        return !!isSupported;
    }

    api->IsApiContractPresentByMajor(contractName, 6, &isSupported);

    return !!isSupported;
}

HRESULT RoHelper::GetStringReference(PCWSTR source, HSTRING *act, HSTRING_HEADER *header)
{
    if (!mWinRtAvailable)
    {
        return E_FAIL;
    }

    const wchar_t *str = static_cast<const wchar_t *>(source);

    unsigned int length;
    HRESULT hr = SizeTToUInt32(::wcslen(str), &length);
    if (FAILED(hr))
    {
        return hr;
    }

    return mFpWindowsCreateStringReference(source, length, header, act);
}

HRESULT RoHelper::GetActivationFactory(const HSTRING act, const IID &interfaceId, void **fac)
{
    if (!mWinRtAvailable)
    {
        return E_FAIL;
    }
    auto hr = mFpGetActivationFactory(act, interfaceId, fac);
    return hr;
}

HRESULT RoHelper::WindowsCompareStringOrdinal(HSTRING one, HSTRING two, int *result)
{
    if (!mWinRtAvailable)
    {
        return E_FAIL;
    }
    return mFpWindowsCompareStringOrdinal(one, two, result);
}

HRESULT RoHelper::CreateDispatcherQueueController(
    DispatcherQueueOptions options,
    ABI::Windows::System::IDispatcherQueueController **dispatcherQueueController)
{
    if (!mWinRtAvailable)
    {
        return E_FAIL;
    }
    return mFpCreateDispatcherQueueController(options, dispatcherQueueController);
}

HRESULT RoHelper::WindowsDeleteString(HSTRING one)
{
    if (!mWinRtAvailable)
    {
        return E_FAIL;
    }
    return mFpWindowsDeleteString(one);
}

HRESULT RoHelper::RoInitialize(RO_INIT_TYPE type)
{
    return mFpRoInitialize(type);
}

void RoHelper::RoUninitialize()
{
    mFpRoUninitialize();
}

}  // namespace rx