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
|
DUPL_RETURN OUTPUTMANAGER::ProcessMonoMask(bool IsMono, _Inout_ PTR_INFO* PtrInfo, _Out_ INT* PtrWidth, _Out_ INT* PtrHeight, _Out_ INT* PtrLeft, _Out_ INT* PtrTop, _Outptr_result_bytebuffer_(*PtrHeight * *PtrWidth * BPP) BYTE** InitBuffer, _Out_ D3D11_BOX* Box)
{
// Desktop dimensions
D3D11_TEXTURE2D_DESC FullDesc;
m_SharedSurf->GetDesc(&FullDesc);
INT DesktopWidth = FullDesc.Width;
INT DesktopHeight = FullDesc.Height;
// Pointer position
INT GivenLeft = PtrInfo->Position.x;
INT GivenTop = PtrInfo->Position.y;
// Figure out if any adjustment is needed for out of bound positions
if (GivenLeft < 0)
{
*PtrWidth = GivenLeft + static_cast<INT>(PtrInfo->ShapeInfo.Width);
}
else if ((GivenLeft + static_cast<INT>(PtrInfo->ShapeInfo.Width)) > DesktopWidth)
{
*PtrWidth = DesktopWidth - GivenLeft;
}
else
{
*PtrWidth = static_cast<INT>(PtrInfo->ShapeInfo.Width);
}
if (IsMono)
{
PtrInfo->ShapeInfo.Height = PtrInfo->ShapeInfo.Height / 2;
}
if (GivenTop < 0)
{
*PtrHeight = GivenTop + static_cast<INT>(PtrInfo->ShapeInfo.Height);
}
else if ((GivenTop + static_cast<INT>(PtrInfo->ShapeInfo.Height)) > DesktopHeight)
{
*PtrHeight = DesktopHeight - GivenTop;
}
else
{
*PtrHeight = static_cast<INT>(PtrInfo->ShapeInfo.Height);
}
if (IsMono)
{
PtrInfo->ShapeInfo.Height = PtrInfo->ShapeInfo.Height * 2;
}
*PtrLeft = (GivenLeft < 0) ? 0 : GivenLeft;
*PtrTop = (GivenTop < 0) ? 0 : GivenTop;
// Staging buffer/texture
D3D11_TEXTURE2D_DESC CopyBufferDesc;
CopyBufferDesc.Width = *PtrWidth;
CopyBufferDesc.Height = *PtrHeight;
CopyBufferDesc.MipLevels = 1;
CopyBufferDesc.ArraySize = 1;
CopyBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
CopyBufferDesc.SampleDesc.Count = 1;
CopyBufferDesc.SampleDesc.Quality = 0;
CopyBufferDesc.Usage = D3D11_USAGE_STAGING;
CopyBufferDesc.BindFlags = 0;
CopyBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
CopyBufferDesc.MiscFlags = 0;
ID3D11Texture2D* CopyBuffer = nullptr;
HRESULT hr = m_Device->CreateTexture2D(&CopyBufferDesc, nullptr, &CopyBuffer);
if (FAILED(hr))
{
return ProcessFailure(m_Device, L"Failed creating staging texture for pointer", L"Error", hr, SystemTransitionsExpectedErrors);
}
// Copy needed part of desktop image
Box->left = *PtrLeft;
Box->top = *PtrTop;
Box->right = *PtrLeft + *PtrWidth;
Box->bottom = *PtrTop + *PtrHeight;
m_DeviceContext->CopySubresourceRegion(CopyBuffer, 0, 0, 0, 0, m_SharedSurf, 0, Box);
// QI for IDXGISurface
IDXGISurface* CopySurface = nullptr;
hr = CopyBuffer->QueryInterface(__uuidof(IDXGISurface), (void **)&CopySurface);
CopyBuffer->Release();
CopyBuffer = nullptr;
if (FAILED(hr))
{
return ProcessFailure(nullptr, L"Failed to QI staging texture into IDXGISurface for pointer", L"Error", hr, SystemTransitionsExpectedErrors);
}
// Map pixels
DXGI_MAPPED_RECT MappedSurface;
hr = CopySurface->Map(&MappedSurface, DXGI_MAP_READ);
if (FAILED(hr))
{
CopySurface->Release();
CopySurface = nullptr;
return ProcessFailure(m_Device, L"Failed to map surface for pointer", L"Error", hr, SystemTransitionsExpectedErrors);
}
// New mouseshape buffer
*InitBuffer = new (std::nothrow) BYTE[*PtrWidth * *PtrHeight * BPP];
if (!(*InitBuffer))
{
return ProcessFailure(nullptr, L"Failed to allocate memory for new mouse shape buffer.", L"Error", E_OUTOFMEMORY);
}
UINT* InitBuffer32 = reinterpret_cast<UINT*>(*InitBuffer);
UINT* Desktop32 = reinterpret_cast<UINT*>(MappedSurface.pBits);
UINT DesktopPitchInPixels = MappedSurface.Pitch / sizeof(UINT);
// What to skip (pixel offset)
UINT SkipX = (GivenLeft < 0) ? (-1 * GivenLeft) : (0);
UINT SkipY = (GivenTop < 0) ? (-1 * GivenTop) : (0);
if (IsMono)
{
for (INT Row = 0; Row < *PtrHeight; ++Row)
{
// Set mask
BYTE Mask = 0x80;
Mask = Mask >> (SkipX % 8);
for (INT Col = 0; Col < *PtrWidth; ++Col)
{
// Get masks using appropriate offsets
BYTE AndMask = PtrInfo->PtrShapeBuffer[((Col + SkipX) / 8) + ((Row + SkipY) * (PtrInfo->ShapeInfo.Pitch))] & Mask;
BYTE XorMask = PtrInfo->PtrShapeBuffer[((Col + SkipX) / 8) + ((Row + SkipY + (PtrInfo->ShapeInfo.Height / 2)) * (PtrInfo->ShapeInfo.Pitch))] & Mask;
UINT AndMask32 = (AndMask) ? 0xFFFFFFFF : 0xFF000000;
UINT XorMask32 = (XorMask) ? 0x00FFFFFF : 0x00000000;
// Set new pixel
InitBuffer32[(Row * *PtrWidth) + Col] = (Desktop32[(Row * DesktopPitchInPixels) + Col] & AndMask32) ^ XorMask32;
// Adjust mask
if (Mask == 0x01)
{
Mask = 0x80;
}
else
{
Mask = Mask >> 1;
}
}
}
}
else
{
UINT* Buffer32 = reinterpret_cast<UINT*>(PtrInfo->PtrShapeBuffer);
// Iterate through pixels
for (INT Row = 0; Row < *PtrHeight; ++Row)
{
for (INT Col = 0; Col < *PtrWidth; ++Col)
{
// Set up mask
UINT MaskVal = 0xFF000000 & Buffer32[(Col + SkipX) + ((Row + SkipY) * (PtrInfo->ShapeInfo.Pitch / sizeof(UINT)))];
if (MaskVal)
{
// Mask was 0xFF
InitBuffer32[(Row * *PtrWidth) + Col] = (Desktop32[(Row * DesktopPitchInPixels) + Col] ^ Buffer32[(Col + SkipX) + ((Row + SkipY) * (PtrInfo->ShapeInfo.Pitch / sizeof(UINT)))]) | 0xFF000000;
}
else
{
// Mask was 0x00
InitBuffer32[(Row * *PtrWidth) + Col] = Buffer32[(Col + SkipX) + ((Row + SkipY) * (PtrInfo->ShapeInfo.Pitch / sizeof(UINT)))] | 0xFF000000;
}
}
}
}
// Done with resource
hr = CopySurface->Unmap();
CopySurface->Release();
CopySurface = nullptr;
if (FAILED(hr))
{
return ProcessFailure(m_Device, L"Failed to unmap surface for pointer", L"Error", hr, SystemTransitionsExpectedErrors);
}
return DUPL_RETURN_SUCCESS;
}
//
// Draw mouse provided in buffer to backbuffer
//
DUPL_RETURN OUTPUTMANAGER::DrawMouse(_In_ PTR_INFO* PtrInfo)
{
// Vars to be used
ID3D11Texture2D* MouseTex = nullptr;
ID3D11ShaderResourceView* ShaderRes = nullptr;
ID3D11Buffer* VertexBufferMouse = nullptr;
D3D11_SUBRESOURCE_DATA InitData;
D3D11_TEXTURE2D_DESC Desc;
D3D11_SHADER_RESOURCE_VIEW_DESC SDesc;
// Position will be changed based on mouse position
VERTEX Vertices[NUMVERTICES] =
{
{XMFLOAT3(-1.0f, -1.0f, 0), XMFLOAT2(0.0f, 1.0f)},
{XMFLOAT3(-1.0f, 1.0f, 0), XMFLOAT2(0.0f, 0.0f)},
{XMFLOAT3(1.0f, -1.0f, 0), XMFLOAT2(1.0f, 1.0f)},
{XMFLOAT3(1.0f, -1.0f, 0), XMFLOAT2(1.0f, 1.0f)},
{XMFLOAT3(-1.0f, 1.0f, 0), XMFLOAT2(0.0f, 0.0f)},
{XMFLOAT3(1.0f, 1.0f, 0), XMFLOAT2(1.0f, 0.0f)},
};
D3D11_TEXTURE2D_DESC FullDesc;
m_SharedSurf->GetDesc(&FullDesc);
INT DesktopWidth = FullDesc.Width;
INT DesktopHeight = FullDesc.Height;
// Center of desktop dimensions
INT CenterX = (DesktopWidth / 2);
INT CenterY = (DesktopHeight / 2);
// Clipping adjusted coordinates / dimensions
INT PtrWidth = 0;
INT PtrHeight = 0;
INT PtrLeft = 0;
INT PtrTop = 0;
// Buffer used if necessary (in case of monochrome or masked pointer)
BYTE* InitBuffer = nullptr;
// Used for copying pixels
D3D11_BOX Box;
Box.front = 0;
Box.back = 1;
Desc.MipLevels = 1;
Desc.ArraySize = 1;
Desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
Desc.SampleDesc.Count = 1;
Desc.SampleDesc.Quality = 0;
Desc.Usage = D3D11_USAGE_DEFAULT;
Desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
Desc.CPUAccessFlags = 0;
Desc.MiscFlags = 0;
// Set shader resource properties
SDesc.Format = Desc.Format;
SDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SDesc.Texture2D.MostDetailedMip = Desc.MipLevels - 1;
SDesc.Texture2D.MipLevels = Desc.MipLevels;
switch (PtrInfo->ShapeInfo.Type)
{
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR:
{
PtrLeft = PtrInfo->Position.x;
PtrTop = PtrInfo->Position.y;
PtrWidth = static_cast<INT>(PtrInfo->ShapeInfo.Width);
PtrHeight = static_cast<INT>(PtrInfo->ShapeInfo.Height);
break;
}
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME:
{
ProcessMonoMask(true, PtrInfo, &PtrWidth, &PtrHeight, &PtrLeft, &PtrTop, &InitBuffer, &Box);
break;
}
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR:
{
ProcessMonoMask(false, PtrInfo, &PtrWidth, &PtrHeight, &PtrLeft, &PtrTop, &InitBuffer, &Box);
break;
}
default:
break;
}
// VERTEX creation
Vertices[0].Pos.x = (PtrLeft - CenterX) / (FLOAT)CenterX;
Vertices[0].Pos.y = -1 * ((PtrTop + PtrHeight) - CenterY) / (FLOAT)CenterY;
Vertices[1].Pos.x = (PtrLeft - CenterX) / (FLOAT)CenterX;
Vertices[1].Pos.y = -1 * (PtrTop - CenterY) / (FLOAT)CenterY;
Vertices[2].Pos.x = ((PtrLeft + PtrWidth) - CenterX) / (FLOAT)CenterX;
Vertices[2].Pos.y = -1 * ((PtrTop + PtrHeight) - CenterY) / (FLOAT)CenterY;
Vertices[3].Pos.x = Vertices[2].Pos.x;
Vertices[3].Pos.y = Vertices[2].Pos.y;
Vertices[4].Pos.x = Vertices[1].Pos.x;
Vertices[4].Pos.y = Vertices[1].Pos.y;
Vertices[5].Pos.x = ((PtrLeft + PtrWidth) - CenterX) / (FLOAT)CenterX;
Vertices[5].Pos.y = -1 * (PtrTop - CenterY) / (FLOAT)CenterY;
// Set texture properties
Desc.Width = PtrWidth;
Desc.Height = PtrHeight;
// Set up init data
InitData.pSysMem = (PtrInfo->ShapeInfo.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR) ? PtrInfo->PtrShapeBuffer : InitBuffer;
InitData.SysMemPitch = (PtrInfo->ShapeInfo.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR) ? PtrInfo->ShapeInfo.Pitch : PtrWidth * BPP;
InitData.SysMemSlicePitch = 0;
// Create mouseshape as texture
HRESULT hr = m_Device->CreateTexture2D(&Desc, &InitData, &MouseTex);
if (FAILED(hr))
{
return ProcessFailure(m_Device, L"Failed to create mouse pointer texture", L"Error", hr, SystemTransitionsExpectedErrors);
}
// Create shader resource from texture
hr = m_Device->CreateShaderResourceView(MouseTex, &SDesc, &ShaderRes);
if (FAILED(hr))
{
MouseTex->Release();
MouseTex = nullptr;
return ProcessFailure(m_Device, L"Failed to create shader resource from mouse pointer texture", L"Error", hr, SystemTransitionsExpectedErrors);
}
D3D11_BUFFER_DESC BDesc;
ZeroMemory(&BDesc, sizeof(D3D11_BUFFER_DESC));
BDesc.Usage = D3D11_USAGE_DEFAULT;
BDesc.ByteWidth = sizeof(VERTEX) * NUMVERTICES;
BDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
BDesc.CPUAccessFlags = 0;
ZeroMemory(&InitData, sizeof(D3D11_SUBRESOURCE_DATA));
InitData.pSysMem = Vertices;
// Create vertex buffer
hr = m_Device->CreateBuffer(&BDesc, &InitData, &VertexBufferMouse);
if (FAILED(hr))
{
ShaderRes->Release();
ShaderRes = nullptr;
MouseTex->Release();
MouseTex = nullptr;
return ProcessFailure(m_Device, L"Failed to create mouse pointer vertex buffer in OutputManager", L"Error", hr, SystemTransitionsExpectedErrors);
}
// Set resources
FLOAT BlendFactor[4] = {0.f, 0.f, 0.f, 0.f};
UINT Stride = sizeof(VERTEX);
UINT Offset = 0;
m_DeviceContext->IASetVertexBuffers(0, 1, &VertexBufferMouse, &Stride, &Offset);
m_DeviceContext->OMSetBlendState(m_BlendState, BlendFactor, 0xFFFFFFFF);
m_DeviceContext->OMSetRenderTargets(1, &m_RTV, nullptr);
m_DeviceContext->VSSetShader(m_VertexShader, nullptr, 0);
m_DeviceContext->PSSetShader(m_PixelShader, nullptr, 0);
m_DeviceContext->PSSetShaderResources(0, 1, &ShaderRes);
m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerLinear);
// Draw
m_DeviceContext->Draw(NUMVERTICES, 0);
// Clean
if (VertexBufferMouse)
{
VertexBufferMouse->Release();
VertexBufferMouse = nullptr;
}
if (ShaderRes)
{
ShaderRes->Release();
ShaderRes = nullptr;
}
if (MouseTex)
{
MouseTex->Release();
MouseTex = nullptr;
}
if (InitBuffer)
{
delete [] InitBuffer;
InitBuffer = nullptr;
}
return DUPL_RETURN_SUCCESS;
}
|