This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompositionEffect.h
211 lines (192 loc) · 4.91 KB
/
CompositionEffect.h
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
#pragma once
#include "winrt.h"
#include "crt.h"
class CompositionEffectSource
{
ComPtr<ICompositionEffectSourceParameter> m_effectSourceParamter{nullptr};
public:
CompositionEffectSource(const HSTRING& name)
{
ComPtr<ICompositionEffectSourceParameterFactory> effectSourceParameterFactory{nullptr};
ThrowIfFailed(
GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Composition_CompositionEffectSourceParameter).Get(), &effectSourceParameterFactory)
);
ThrowIfFailed(
effectSourceParameterFactory->Create(name, &m_effectSourceParamter)
);
}
operator ICompositionEffectSourceParameter*()
{
return m_effectSourceParamter.Get();
}
operator IGraphicsEffectSource*()
{
return TryAs<IGraphicsEffectSource>(m_effectSourceParamter).Get();
}
};
class CompositionEffect : public RuntimeClass<RuntimeClassFlags<WinRtClassicComMix>, IGraphicsEffect, IGraphicsEffectSource, IGraphicsEffectD2D1Interop>
{
public:
CompositionEffect(REFCLSID effectId) : m_effectId(effectId)
{
ThrowIfFailed(
GetActivationFactory(
HStringReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(),
&m_propertyValueFactory
)
);
}
virtual ~CompositionEffect() = default;
// IGraphicsEffect
HRESULT STDMETHODCALLTYPE get_Name(HSTRING* name) override
{
return WindowsDuplicateString(m_name, name);
}
HRESULT STDMETHODCALLTYPE put_Name(HSTRING name) override
{
return m_name.Set(name);
}
//
// IGraphicsEffectD2D1Interop
HRESULT STDMETHODCALLTYPE GetEffectId(GUID* id) override
{
if (id)
{
*id = m_effectId;
return S_OK;
}
return E_POINTER;
}
// set property by using name, this is not neccessary for C++ programmers
HRESULT STDMETHODCALLTYPE GetNamedPropertyMapping(LPCWSTR name, UINT* index, GRAPHICS_EFFECT_PROPERTY_MAPPING* mapping) override
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE GetPropertyCount(UINT* count) override
{
if (count)
{
*count = (UINT)m_properties.size();
return S_OK;
}
return E_POINTER;
}
HRESULT STDMETHODCALLTYPE GetProperty(UINT index, IPropertyValue** value) override
{
if (!value)
{
return E_POINTER;
}
if (index < 0 or (index != 0 and m_properties.find(index - 1) == m_properties.end()))
{
return E_INVALIDARG;
}
return m_properties[index].CopyTo(value);
}
HRESULT STDMETHODCALLTYPE GetSource(UINT index, IGraphicsEffectSource** source) override
{
if (!source)
{
return E_POINTER;
}
if (index >= m_effectSources.size() or index < 0)
{
return E_INVALIDARG;
}
return m_effectSources[index].CopyTo(source);
}
HRESULT STDMETHODCALLTYPE GetSourceCount(UINT* count) override
{
if (count)
{
*count = (UINT)m_effectSources.size();
return S_OK;
}
return E_POINTER;
}
//
void SetInput(UINT index, IGraphicsEffectSource* source)
{
if (index < 0 or (index != 0 and m_effectSources.find(index - 1) == m_effectSources.end()))
{
ThrowIfFailed(E_INVALIDARG);
}
m_effectSources[index] = {ComPtr<IGraphicsEffectSource>(source)};
}
void SetInput(IGraphicsEffectSource* source)
{
SetInput(0, source);
}
void SetInput(const vector<IGraphicsEffectSource*>& effectSourceList)
{
for (UINT i = 0; i < effectSourceList.size(); i++)
{
SetInput(i, effectSourceList[i]);
}
}
void SetInput(const vector<ComPtr<IGraphicsEffectSource>>& effectSourceList)
{
for (UINT i = 0; i < effectSourceList.size(); i++)
{
SetInput(i, effectSourceList[i].Get());
}
}
protected:
template <typename T>
ComPtr<IPropertyValue> Property(T value)
{
ThrowIfFailed(E_INVALIDARG);
return nullptr;
}
template <typename T, size_t size>
ComPtr<IPropertyValue> Property(T(&array)[size])
{
ThrowIfFailed(E_INVALIDARG);
return nullptr;
}
template <>
ComPtr<IPropertyValue> Property(float value)
{
ComPtr<IPropertyValue> propertyValue{nullptr};
ThrowIfFailed(
m_propertyValueFactory->CreateSingle(value, &propertyValue)
);
return propertyValue;
}
template <>
ComPtr<IPropertyValue> Property(UINT32 value)
{
ComPtr<IPropertyValue> propertyValue{nullptr};
ThrowIfFailed(
m_propertyValueFactory->CreateUInt32(value, &propertyValue)
);
return propertyValue;
}
template <>
ComPtr<IPropertyValue> Property(boolean value)
{
ComPtr<IPropertyValue> propertyValue{nullptr};
ThrowIfFailed(
m_propertyValueFactory->CreateBoolean(value, &propertyValue)
);
return propertyValue;
}
template <size_t size>
ComPtr<IPropertyValue> Property(float(&array)[size])
{
ComPtr<IPropertyValue> propertyValue{nullptr};
ThrowIfFailed(
m_propertyValueFactory->CreateSingleArray(size, array, &propertyValue)
);
return propertyValue;
}
void SetProperty(UINT index, const ComPtr<IPropertyValue>& value)
{
m_properties[index] = value;
}
CLSID m_effectId{};
HString m_name{};
unordered_map<int, ComPtr<IPropertyValue>> m_properties{};
ComPtr<IPropertyValueStatics> m_propertyValueFactory{nullptr};
unordered_map<int, ComPtr<IGraphicsEffectSource>> m_effectSources{};
};