2014-09-18 06:58:57 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2014-09-18 06:58:57 +04:00
|
|
|
|
|
|
|
#include "shadow.h"
|
|
|
|
|
|
|
|
#include "shadow_subsystem.h"
|
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
static pfnShadowSubsystemEntry pSubsystemEntry = NULL;
|
2014-09-18 23:43:11 +04:00
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
void shadow_subsystem_set_entry(pfnShadowSubsystemEntry pEntry)
|
2014-09-18 23:43:11 +04:00
|
|
|
{
|
2015-04-08 20:30:57 +03:00
|
|
|
pSubsystemEntry = pEntry;
|
2014-09-18 23:43:11 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
static int shadow_subsystem_load_entry_points(RDP_SHADOW_ENTRY_POINTS* pEntryPoints)
|
2014-09-18 23:43:11 +04:00
|
|
|
{
|
2022-10-13 17:25:36 +03:00
|
|
|
WINPR_ASSERT(pEntryPoints);
|
2014-12-07 02:23:46 +03:00
|
|
|
ZeroMemory(pEntryPoints, sizeof(RDP_SHADOW_ENTRY_POINTS));
|
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
if (!pSubsystemEntry)
|
2014-09-18 23:43:11 +04:00
|
|
|
return -1;
|
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
if (pSubsystemEntry(pEntryPoints) < 0)
|
2014-09-18 23:43:11 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:01:41 +03:00
|
|
|
rdpShadowSubsystem* shadow_subsystem_new(void)
|
2014-09-18 23:43:11 +04:00
|
|
|
{
|
|
|
|
RDP_SHADOW_ENTRY_POINTS ep;
|
|
|
|
rdpShadowSubsystem* subsystem = NULL;
|
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
shadow_subsystem_load_entry_points(&ep);
|
2014-09-18 23:43:11 +04:00
|
|
|
|
|
|
|
if (!ep.New)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
subsystem = ep.New();
|
|
|
|
|
|
|
|
if (!subsystem)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CopyMemory(&(subsystem->ep), &ep, sizeof(RDP_SHADOW_ENTRY_POINTS));
|
2014-09-18 06:58:57 +04:00
|
|
|
|
2014-09-18 21:06:49 +04:00
|
|
|
return subsystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shadow_subsystem_free(rdpShadowSubsystem* subsystem)
|
|
|
|
{
|
2015-04-28 18:00:41 +03:00
|
|
|
if (subsystem && subsystem->ep.Free)
|
2014-09-18 23:43:11 +04:00
|
|
|
subsystem->ep.Free(subsystem);
|
2014-09-18 21:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int shadow_subsystem_init(rdpShadowSubsystem* subsystem, rdpShadowServer* server)
|
|
|
|
{
|
2015-04-28 18:00:41 +03:00
|
|
|
int status = -1;
|
|
|
|
|
|
|
|
if (!subsystem || !subsystem->ep.Init)
|
|
|
|
return -1;
|
2014-09-18 21:06:49 +04:00
|
|
|
|
|
|
|
subsystem->server = server;
|
|
|
|
subsystem->selectedMonitor = server->selectedMonitor;
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(subsystem->MsgPipe = MessagePipe_New()))
|
|
|
|
goto fail;
|
|
|
|
|
2015-05-10 19:00:02 +03:00
|
|
|
if (!(subsystem->updateEvent = shadow_multiclient_new()))
|
2015-04-28 18:00:41 +03:00
|
|
|
goto fail;
|
2014-09-19 06:18:58 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if ((status = subsystem->ep.Init(subsystem)) >= 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (subsystem->MsgPipe)
|
|
|
|
{
|
|
|
|
MessagePipe_Free(subsystem->MsgPipe);
|
|
|
|
subsystem->MsgPipe = NULL;
|
|
|
|
}
|
2014-09-18 21:06:49 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (subsystem->updateEvent)
|
|
|
|
{
|
2015-05-10 19:00:02 +03:00
|
|
|
shadow_multiclient_free(subsystem->updateEvent);
|
2015-04-28 18:00:41 +03:00
|
|
|
subsystem->updateEvent = NULL;
|
|
|
|
}
|
2014-09-18 21:06:49 +04:00
|
|
|
|
|
|
|
return status;
|
2014-09-18 06:58:57 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void shadow_subsystem_free_queued_message(void* obj)
|
2015-04-08 21:13:52 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
wMessage* message = (wMessage*)obj;
|
2015-04-08 21:13:52 +03:00
|
|
|
if (message->Free)
|
|
|
|
{
|
|
|
|
message->Free(message);
|
|
|
|
message->Free = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 21:06:49 +04:00
|
|
|
void shadow_subsystem_uninit(rdpShadowSubsystem* subsystem)
|
2014-09-18 06:58:57 +04:00
|
|
|
{
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!subsystem)
|
|
|
|
return;
|
|
|
|
|
2014-09-18 23:43:11 +04:00
|
|
|
if (subsystem->ep.Uninit)
|
|
|
|
subsystem->ep.Uninit(subsystem);
|
2014-09-18 21:06:49 +04:00
|
|
|
|
|
|
|
if (subsystem->MsgPipe)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
wObject* obj1 = NULL;
|
|
|
|
wObject* obj2 = NULL;
|
2015-04-08 21:13:52 +03:00
|
|
|
/* Release resource in messages before free */
|
2020-05-12 18:16:14 +03:00
|
|
|
obj1 = MessageQueue_Object(subsystem->MsgPipe->In);
|
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
obj1->fnObjectFree = shadow_subsystem_free_queued_message;
|
|
|
|
MessageQueue_Clear(subsystem->MsgPipe->In);
|
2021-06-16 11:41:10 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
obj2 = MessageQueue_Object(subsystem->MsgPipe->Out);
|
|
|
|
obj2->fnObjectFree = shadow_subsystem_free_queued_message;
|
2015-04-08 21:13:52 +03:00
|
|
|
MessageQueue_Clear(subsystem->MsgPipe->Out);
|
2014-09-18 21:06:49 +04:00
|
|
|
MessagePipe_Free(subsystem->MsgPipe);
|
|
|
|
subsystem->MsgPipe = NULL;
|
|
|
|
}
|
2014-09-18 06:58:57 +04:00
|
|
|
|
2014-09-19 06:18:58 +04:00
|
|
|
if (subsystem->updateEvent)
|
2014-09-18 21:06:49 +04:00
|
|
|
{
|
2015-05-10 19:00:02 +03:00
|
|
|
shadow_multiclient_free(subsystem->updateEvent);
|
2014-09-19 06:18:58 +04:00
|
|
|
subsystem->updateEvent = NULL;
|
2014-09-18 21:06:49 +04:00
|
|
|
}
|
2014-09-18 06:58:57 +04:00
|
|
|
}
|
2014-09-18 23:43:11 +04:00
|
|
|
|
|
|
|
int shadow_subsystem_start(rdpShadowSubsystem* subsystem)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
2014-09-18 23:43:11 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!subsystem || !subsystem->ep.Start)
|
2014-09-18 23:43:11 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
status = subsystem->ep.Start(subsystem);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int shadow_subsystem_stop(rdpShadowSubsystem* subsystem)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
2014-09-18 23:43:11 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!subsystem || !subsystem->ep.Stop)
|
2014-09-18 23:43:11 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
status = subsystem->ep.Stop(subsystem);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:01:41 +03:00
|
|
|
UINT32 shadow_enum_monitors(MONITOR_DEF* monitors, UINT32 maxMonitors)
|
2014-09-18 23:43:11 +04:00
|
|
|
{
|
2017-06-06 15:01:41 +03:00
|
|
|
UINT32 numMonitors = 0;
|
2014-09-18 23:43:11 +04:00
|
|
|
RDP_SHADOW_ENTRY_POINTS ep;
|
|
|
|
|
2015-04-08 20:30:57 +03:00
|
|
|
if (shadow_subsystem_load_entry_points(&ep) < 0)
|
2021-06-18 11:00:43 +03:00
|
|
|
return 0;
|
2014-09-18 23:43:11 +04:00
|
|
|
|
|
|
|
numMonitors = ep.EnumMonitors(monitors, maxMonitors);
|
|
|
|
|
|
|
|
return numMonitors;
|
|
|
|
}
|
2015-06-24 18:33:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Common function for subsystem implementation.
|
|
|
|
* This function convert 32bit ARGB format pixels to xormask data
|
|
|
|
* and andmask data and fill into SHADOW_MSG_OUT_POINTER_ALPHA_UPDATE
|
|
|
|
* Caller should free the andMaskData and xorMaskData later.
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
int shadow_subsystem_pointer_convert_alpha_pointer_data(
|
2024-03-05 11:33:36 +03:00
|
|
|
const BYTE* WINPR_RESTRICT pixels, BOOL premultiplied, UINT32 width, UINT32 height,
|
|
|
|
SHADOW_MSG_OUT_POINTER_ALPHA_UPDATE* WINPR_RESTRICT pointerColor)
|
|
|
|
{
|
|
|
|
return shadow_subsystem_pointer_convert_alpha_pointer_data_to_format(
|
|
|
|
pixels, PIXEL_FORMAT_BGRX32, premultiplied, width, height, pointerColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
int shadow_subsystem_pointer_convert_alpha_pointer_data_to_format(
|
|
|
|
const BYTE* pixels, UINT32 format, BOOL premultiplied, UINT32 width, UINT32 height,
|
2019-11-06 17:24:51 +03:00
|
|
|
SHADOW_MSG_OUT_POINTER_ALPHA_UPDATE* pointerColor)
|
2015-06-24 18:33:49 +03:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT32 xorStep = 0;
|
|
|
|
UINT32 andStep = 0;
|
|
|
|
UINT32 andBit = 0;
|
|
|
|
BYTE* andBits = NULL;
|
|
|
|
UINT32 andPixel = 0;
|
2024-03-05 11:33:36 +03:00
|
|
|
const size_t bpp = FreeRDPGetBytesPerPixel(format);
|
2015-06-24 18:33:49 +03:00
|
|
|
|
|
|
|
xorStep = (width * 3);
|
|
|
|
xorStep += (xorStep % 2);
|
|
|
|
|
|
|
|
andStep = ((width + 7) / 8);
|
|
|
|
andStep += (andStep % 2);
|
|
|
|
|
|
|
|
pointerColor->lengthXorMask = height * xorStep;
|
2019-11-06 17:24:51 +03:00
|
|
|
pointerColor->xorMaskData = (BYTE*)calloc(1, pointerColor->lengthXorMask);
|
2015-06-24 18:33:49 +03:00
|
|
|
|
|
|
|
if (!pointerColor->xorMaskData)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pointerColor->lengthAndMask = height * andStep;
|
2019-11-06 17:24:51 +03:00
|
|
|
pointerColor->andMaskData = (BYTE*)calloc(1, pointerColor->lengthAndMask);
|
2015-06-24 18:33:49 +03:00
|
|
|
|
|
|
|
if (!pointerColor->andMaskData)
|
|
|
|
{
|
|
|
|
free(pointerColor->xorMaskData);
|
|
|
|
pointerColor->xorMaskData = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:33:36 +03:00
|
|
|
for (size_t y = 0; y < height; y++)
|
2015-06-24 18:33:49 +03:00
|
|
|
{
|
2024-03-05 11:33:36 +03:00
|
|
|
const BYTE* pSrc8 = &pixels[(width * bpp) * (height - 1 - y)];
|
|
|
|
BYTE* pDst8 = &(pointerColor->xorMaskData[y * xorStep]);
|
2015-06-24 18:33:49 +03:00
|
|
|
|
|
|
|
andBit = 0x80;
|
|
|
|
andBits = &(pointerColor->andMaskData[andStep * y]);
|
|
|
|
|
2024-03-05 11:33:36 +03:00
|
|
|
for (size_t x = 0; x < width; x++)
|
2015-06-24 18:33:49 +03:00
|
|
|
{
|
2024-03-05 11:33:36 +03:00
|
|
|
BYTE B = 0;
|
|
|
|
BYTE G = 0;
|
|
|
|
BYTE R = 0;
|
|
|
|
BYTE A = 0;
|
|
|
|
|
|
|
|
const UINT32 color = FreeRDPReadColor(&pSrc8[x * bpp], format);
|
|
|
|
FreeRDPSplitColor(color, format, &R, &G, &B, &A, NULL);
|
2015-06-24 18:33:49 +03:00
|
|
|
|
|
|
|
andPixel = 0;
|
|
|
|
|
|
|
|
if (A < 64)
|
|
|
|
A = 0; /* pixel cannot be partially transparent */
|
|
|
|
|
|
|
|
if (!A)
|
|
|
|
{
|
|
|
|
/* transparent pixel: XOR = black, AND = 1 */
|
|
|
|
andPixel = 1;
|
|
|
|
B = G = R = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (premultiplied)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
B = (B * 0xFF) / A;
|
|
|
|
G = (G * 0xFF) / A;
|
|
|
|
R = (R * 0xFF) / A;
|
2015-06-24 18:33:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pDst8++ = B;
|
|
|
|
*pDst8++ = G;
|
|
|
|
*pDst8++ = R;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (andPixel)
|
|
|
|
*andBits |= andBit;
|
|
|
|
if (!(andBit >>= 1))
|
|
|
|
{
|
|
|
|
andBits++;
|
|
|
|
andBit = 0x80;
|
|
|
|
}
|
2015-06-24 18:33:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-28 11:20:49 +03:00
|
|
|
|
|
|
|
void shadow_subsystem_frame_update(rdpShadowSubsystem* subsystem)
|
|
|
|
{
|
|
|
|
shadow_multiclient_publish_and_wait(subsystem->updateEvent);
|
|
|
|
}
|