1 //
2 // Copyright 2014 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 // Trim11.cpp: Trim support utility class.
8 
9 #include "libANGLE/renderer/d3d/d3d11/Trim11.h"
10 #include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
11 #include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
12 
13 #if defined(ANGLE_ENABLE_WINDOWS_UWP)
14 #    include <windows.applicationmodel.core.h>
15 #    include <wrl.h>
16 #    include <wrl/wrappers/corewrappers.h>
17 using namespace Microsoft::WRL;
18 using namespace Microsoft::WRL::Wrappers;
19 using namespace ABI::Windows::ApplicationModel;
20 using namespace ABI::Windows::ApplicationModel::Core;
21 using namespace ABI::Windows::Foundation;
22 using namespace ABI::Windows::Foundation::Collections;
23 #endif
24 
25 namespace rx
26 {
27 
Trim11(rx::Renderer11 * renderer)28 Trim11::Trim11(rx::Renderer11 *renderer) : mRenderer(renderer)
29 {
30     bool result = true;
31     result      = registerForRendererTrimRequest();
32     ASSERT(result);
33 }
34 
~Trim11()35 Trim11::~Trim11()
36 {
37     unregisterForRendererTrimRequest();
38 }
39 
trim()40 void Trim11::trim()
41 {
42     if (!mRenderer)
43     {
44         return;
45     }
46 
47 #if defined(ANGLE_ENABLE_WINDOWS_UWP)
48     ID3D11Device *device      = mRenderer->getDevice();
49     IDXGIDevice3 *dxgiDevice3 = d3d11::DynamicCastComObject<IDXGIDevice3>(device);
50     if (dxgiDevice3)
51     {
52         dxgiDevice3->Trim();
53     }
54     SafeRelease(dxgiDevice3);
55 #endif
56 }
57 
registerForRendererTrimRequest()58 bool Trim11::registerForRendererTrimRequest()
59 {
60 #if defined(ANGLE_ENABLE_WINDOWS_UWP)
61     ICoreApplication *coreApplication = nullptr;
62     HRESULT result                    = GetActivationFactory(
63         HStringReference(RuntimeClass_Windows_ApplicationModel_Core_CoreApplication).Get(),
64         &coreApplication);
65     if (SUCCEEDED(result))
66     {
67         auto suspendHandler = Callback<IEventHandler<SuspendingEventArgs *>>(
68             [this](IInspectable *, ISuspendingEventArgs *) -> HRESULT {
69                 trim();
70                 return S_OK;
71             });
72         result =
73             coreApplication->add_Suspending(suspendHandler.Get(), &mApplicationSuspendedEventToken);
74     }
75     SafeRelease(coreApplication);
76 
77     if (FAILED(result))
78     {
79         return false;
80     }
81 #endif
82     return true;
83 }
84 
unregisterForRendererTrimRequest()85 void Trim11::unregisterForRendererTrimRequest()
86 {
87 #if defined(ANGLE_ENABLE_WINDOWS_UWP)
88     if (mApplicationSuspendedEventToken.value != 0)
89     {
90         ICoreApplication *coreApplication = nullptr;
91         if (SUCCEEDED(GetActivationFactory(
92                 HStringReference(RuntimeClass_Windows_ApplicationModel_Core_CoreApplication).Get(),
93                 &coreApplication)))
94         {
95             coreApplication->remove_Suspending(mApplicationSuspendedEventToken);
96         }
97         mApplicationSuspendedEventToken.value = 0;
98         SafeRelease(coreApplication);
99     }
100 #endif
101 }
102 
103 }  // namespace rx
104