Blame libfreerdp/gdi/gfx.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * GDI Graphics Pipeline
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
Packit 1fb8d4
 * Copyright 2016 Thincast Technologies GmbH
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit Service 5a9772
#include "../core/update.h"
Packit Service 5a9772
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#include <freerdp/gdi/gfx.h>
Packit 1fb8d4
#include <freerdp/gdi/region.h>
Packit 1fb8d4
Packit 1fb8d4
#define TAG FREERDP_TAG("gdi")
Packit 1fb8d4
Packit 1fb8d4
static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
Packit 1fb8d4
{
Packit Service 5a9772
	const UINT32 align = alignment;
Packit 1fb8d4
	const UINT32 pad = align - (widthInBytes % alignment);
Packit 1fb8d4
	UINT32 scanline = widthInBytes;
Packit 1fb8d4
Packit 1fb8d4
	if (align != pad)
Packit 1fb8d4
		scanline += pad;
Packit 1fb8d4
Packit 1fb8d4
	return scanline;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
Packit 1fb8d4
                              const RDPGFX_RESET_GRAPHICS_PDU* resetGraphics)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	UINT32 index;
Packit 1fb8d4
	UINT16 count;
Packit 1fb8d4
	UINT32 DesktopWidth;
Packit 1fb8d4
	UINT32 DesktopHeight;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	UINT16* pSurfaceIds = NULL;
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit 1fb8d4
	rdpUpdate* update = gdi->context->update;
Packit 1fb8d4
	rdpSettings* settings = gdi->context->settings;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit 1fb8d4
	DesktopWidth = resetGraphics->width;
Packit 1fb8d4
	DesktopHeight = resetGraphics->height;
Packit 1fb8d4
Packit 1fb8d4
	if ((DesktopWidth != settings->DesktopWidth) || (DesktopHeight != settings->DesktopHeight))
Packit 1fb8d4
	{
Packit 1fb8d4
		settings->DesktopWidth = DesktopWidth;
Packit 1fb8d4
		settings->DesktopHeight = DesktopHeight;
Packit 1fb8d4
Packit 1fb8d4
		if (update)
Packit 1fb8d4
			update->DesktopResize(gdi->context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	context->GetSurfaceIds(context, &pSurfaceIds, &count);
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < count; index++)
Packit 1fb8d4
	{
Packit Service 5a9772
		surface = (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
Packit 1fb8d4
Packit 1fb8d4
		if (!surface || !surface->outputMapped)
Packit 1fb8d4
			continue;
Packit 1fb8d4
Packit 1fb8d4
		region16_clear(&surface->invalidRegion);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(pSurfaceIds);
Packit 1fb8d4
Packit Service 5a9772
	if (!freerdp_client_codecs_reset(gdi->context->codecs, FREERDP_CODEC_ALL, gdi->width,
Packit Service 5a9772
	                                 gdi->height))
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	rc = CHANNEL_RC_OK;
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT rc = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	UINT32 surfaceX, surfaceY;
Packit 1fb8d4
	RECTANGLE_16 surfaceRect;
Packit 1fb8d4
	const RECTANGLE_16* rects;
Packit 1fb8d4
	UINT32 i, nbRects;
Packit Service 5a9772
	double sx, sy;
Packit 1fb8d4
	rdpUpdate* update = gdi->context->update;
Packit 1fb8d4
Packit 1fb8d4
	if (gdi->suppressOutput)
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	surfaceX = surface->outputOriginX;
Packit 1fb8d4
	surfaceY = surface->outputOriginY;
Packit 1fb8d4
	surfaceRect.left = 0;
Packit 1fb8d4
	surfaceRect.top = 0;
Packit Service 5a9772
	surfaceRect.right = surface->mappedWidth;
Packit Service 5a9772
	surfaceRect.bottom = surface->mappedHeight;
Packit Service 5a9772
	region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion), &surfaceRect);
Packit Service 5a9772
	sx = surface->outputTargetWidth / (double)surface->mappedWidth;
Packit Service 5a9772
	sy = surface->outputTargetHeight / (double)surface->mappedHeight;
Packit 1fb8d4
Packit 1fb8d4
	if (!(rects = region16_rects(&surface->invalidRegion, &nbRects)) || !nbRects)
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
Packit Service 5a9772
	if (!update_begin_paint(update))
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < nbRects; i++)
Packit 1fb8d4
	{
Packit Service 5a9772
		const UINT32 nXSrc = rects[i].left;
Packit Service 5a9772
		const UINT32 nYSrc = rects[i].top;
Packit Service 5a9772
		const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
Packit Service 5a9772
		const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
Packit Service 5a9772
		const UINT32 swidth = rects[i].right - rects[i].left;
Packit Service 5a9772
		const UINT32 sheight = rects[i].bottom - rects[i].top;
Packit Service 5a9772
		const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
Packit Service 5a9772
		const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
Packit Service 5a9772
Packit Service 5a9772
		if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
Packit Service 5a9772
		                         dwidth, dheight, surface->data, surface->format, surface->scanline,
Packit Service 5a9772
		                         nXSrc, nYSrc, swidth, sheight))
Packit 1fb8d4
			return CHANNEL_RC_NULL_DATA;
Packit 1fb8d4
Packit Service 5a9772
		gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
Packit Service 5a9772
		                     (INT32)dheight);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rc = CHANNEL_RC_OK;
Packit 1fb8d4
fail:
Packit Service 5a9772
Packit Service 5a9772
	if (!update_end_paint(update))
Packit Service 5a9772
		rc = ERROR_INTERNAL_ERROR;
Packit Service 5a9772
Packit 1fb8d4
	region16_clear(&(surface->invalidRegion));
Packit 1fb8d4
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT16 count;
Packit 1fb8d4
	UINT16 index;
Packit Service 5a9772
	UINT status = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	UINT16* pSurfaceIds = NULL;
Packit 1fb8d4
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit 1fb8d4
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit 1fb8d4
	context->GetSurfaceIds(context, &pSurfaceIds, &count);
Packit Service 5a9772
	status = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < count; index++)
Packit 1fb8d4
	{
Packit Service 5a9772
		surface = (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
Packit 1fb8d4
Packit Service 5a9772
		if (!surface)
Packit Service 5a9772
			continue;
Packit Service 5a9772
Packit Service 5a9772
		/* Already handled in UpdateSurfaceArea callbacks */
Packit Service 5a9772
		if (context->UpdateSurfaceArea)
Packit Service 5a9772
		{
Packit Service 5a9772
			if (surface->windowId != 0)
Packit Service 5a9772
				continue;
Packit Service 5a9772
		}
Packit Service 5a9772
Packit Service 5a9772
		if (!surface->outputMapped)
Packit 1fb8d4
			continue;
Packit 1fb8d4
Packit 1fb8d4
		status = gdi_OutputUpdate(gdi, surface);
Packit 1fb8d4
Packit 1fb8d4
		if (status != CHANNEL_RC_OK)
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(pSurfaceIds);
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_StartFrame(RdpgfxClientContext* context, const RDPGFX_START_FRAME_PDU* startFrame)
Packit 1fb8d4
{
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit 1fb8d4
	gdi->inGfxFrame = TRUE;
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_EndFrame(RdpgfxClientContext* context, const RDPGFX_END_FRAME_PDU* endFrame)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT status = CHANNEL_RC_NOT_INITIALIZED;
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit 1fb8d4
	IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	gdi->inGfxFrame = FALSE;
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
Packit Service 5a9772
                                            const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!freerdp_image_copy(surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
Packit Service 5a9772
	                        cmd->width, cmd->height, cmd->data, cmd->format, 0, 0, 0, NULL,
Packit Service 5a9772
	                        FREERDP_FLIP_NONE))
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	invalidRect.left = cmd->left;
Packit 1fb8d4
	invalidRect.top = cmd->top;
Packit 1fb8d4
	invalidRect.right = cmd->right;
Packit 1fb8d4
	invalidRect.bottom = cmd->bottom;
Packit Service 5a9772
	region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
Packit Service 5a9772
	                      &invalidRect);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
Packit 1fb8d4
                                        const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT status = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	REGION16 invalidRegion;
Packit 1fb8d4
	const RECTANGLE_16* rects;
Packit 1fb8d4
	UINT32 nrRects, x;
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
Packit 1fb8d4
	region16_init(&invalidRegion);
Packit 1fb8d4
Packit Service 5a9772
	if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
Packit Service 5a9772
	                         surface->data, surface->format, surface->scanline, surface->height,
Packit Service 5a9772
	                         &invalidRegion))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Failed to process RemoteFX message");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rects = region16_rects(&invalidRegion, &nrRects);
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
Packit Service 5a9772
	                      nrRects, rects);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	for (x = 0; x < nrRects; x++)
Packit 1fb8d4
		region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]);
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit Service 5a9772
	region16_uninit(&invalidRegion);
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
Packit Service 5a9772
                                          const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
	INT32 rc;
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
Packit Service 5a9772
	                      surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
Packit 1fb8d4
	                      surface->width, surface->height, &gdi->palette);
Packit 1fb8d4
Packit 1fb8d4
	if (rc < 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "clear_decompress failure: %" PRId32 "", rc);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	invalidRect.left = cmd->left;
Packit 1fb8d4
	invalidRect.top = cmd->top;
Packit 1fb8d4
	invalidRect.right = cmd->right;
Packit 1fb8d4
	invalidRect.bottom = cmd->bottom;
Packit Service 5a9772
	region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
Packit Service 5a9772
	                      &invalidRect);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
Packit 1fb8d4
                                      const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	BYTE* DstData = NULL;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	DstData = surface->data;
Packit 1fb8d4
Packit Service 5a9772
	if (!planar_decompress(surface->codecs->planar, cmd->data, cmd->length, cmd->width, cmd->height,
Packit Service 5a9772
	                       DstData, surface->format, surface->scanline, cmd->left, cmd->top,
Packit 1fb8d4
	                       cmd->width, cmd->height, FALSE))
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	invalidRect.left = cmd->left;
Packit 1fb8d4
	invalidRect.top = cmd->top;
Packit 1fb8d4
	invalidRect.right = cmd->right;
Packit 1fb8d4
	invalidRect.bottom = cmd->bottom;
Packit Service 5a9772
	region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
Packit Service 5a9772
	                      &invalidRect);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
Packit 1fb8d4
                                      const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
#ifdef WITH_GFX_H264
Packit 1fb8d4
	INT32 rc;
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RDPGFX_H264_METABLOCK* meta;
Packit 1fb8d4
	RDPGFX_AVC420_BITMAP_STREAM* bs;
Packit Service 5a9772
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!surface->h264)
Packit 1fb8d4
	{
Packit 1fb8d4
		surface->h264 = h264_context_new(FALSE);
Packit 1fb8d4
Packit 1fb8d4
		if (!surface->h264)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "%s: unable to create h264 context", __FUNCTION__);
Packit 1fb8d4
			return ERROR_NOT_ENOUGH_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!h264_context_reset(surface->h264, surface->width, surface->height))
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!surface->h264)
Packit Service 5a9772
		return ERROR_NOT_SUPPORTED;
Packit Service 5a9772
Packit Service 5a9772
	bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
Packit 1fb8d4
Packit 1fb8d4
	if (!bs)
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	meta = &(bs->meta);
Packit 1fb8d4
	rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
Packit Service 5a9772
	                       surface->scanline, surface->width, surface->height, meta->regionRects,
Packit 1fb8d4
	                       meta->numRegionRects);
Packit 1fb8d4
Packit 1fb8d4
	if (rc < 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_WARN(TAG, "avc420_decompress failure: %" PRId32 ", ignoring update.", rc);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < meta->numRegionRects; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
Packit Service 5a9772
		                    (RECTANGLE_16*)&(meta->regionRects[i]));
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
Packit Service 5a9772
	                      meta->numRegionRects, meta->regionRects);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
#else
Packit 1fb8d4
	return ERROR_NOT_SUPPORTED;
Packit 1fb8d4
#endif
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
Packit 1fb8d4
                                      const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
#ifdef WITH_GFX_H264
Packit 1fb8d4
	INT32 rc;
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RDPGFX_AVC444_BITMAP_STREAM* bs;
Packit 1fb8d4
	RDPGFX_AVC420_BITMAP_STREAM* avc1;
Packit 1fb8d4
	RDPGFX_H264_METABLOCK* meta1;
Packit 1fb8d4
	RDPGFX_AVC420_BITMAP_STREAM* avc2;
Packit 1fb8d4
	RDPGFX_H264_METABLOCK* meta2;
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!surface->h264)
Packit 1fb8d4
	{
Packit 1fb8d4
		surface->h264 = h264_context_new(FALSE);
Packit 1fb8d4
Packit 1fb8d4
		if (!surface->h264)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "%s: unable to create h264 context", __FUNCTION__);
Packit 1fb8d4
			return ERROR_NOT_ENOUGH_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!h264_context_reset(surface->h264, surface->width, surface->height))
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!surface->h264)
Packit Service 5a9772
		return ERROR_NOT_SUPPORTED;
Packit Service 5a9772
Packit Service 5a9772
	bs = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
Packit 1fb8d4
Packit 1fb8d4
	if (!bs)
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	avc1 = &bs->bitstream[0];
Packit 1fb8d4
	avc2 = &bs->bitstream[1];
Packit 1fb8d4
	meta1 = &avc1->meta;
Packit 1fb8d4
	meta2 = &avc2->meta;
Packit Service 5a9772
	rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
Packit Service 5a9772
	                       avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
Packit Service 5a9772
	                       avc2->data, avc2->length, surface->data, surface->format,
Packit Service 5a9772
	                       surface->scanline, surface->width, surface->height, cmd->codecId);
Packit 1fb8d4
Packit 1fb8d4
	if (rc < 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_WARN(TAG, "avc444_decompress failure: %" PRIu32 ", ignoring update.", status);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < meta1->numRegionRects; i++)
Packit 1fb8d4
	{
Packit Service 5a9772
		region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
Packit Service 5a9772
		                    &(meta1->regionRects[i]));
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
Packit Service 5a9772
	                      meta1->numRegionRects, meta1->regionRects);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < meta2->numRegionRects; i++)
Packit 1fb8d4
	{
Packit Service 5a9772
		region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
Packit Service 5a9772
		                    &(meta2->regionRects[i]));
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
Packit Service 5a9772
	                      meta2->numRegionRects, meta2->regionRects);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
#else
Packit 1fb8d4
	return ERROR_NOT_SUPPORTED;
Packit 1fb8d4
#endif
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride, RECTANGLE_16* rect,
Packit Service 5a9772
                            UINT32 startOffsetX, UINT32 count, BYTE a)
Packit Service 5a9772
{
Packit Service 5a9772
	UINT32 y;
Packit Service 5a9772
	UINT32 written = 0;
Packit Service 5a9772
	BOOL first = TRUE;
Packit Service 5a9772
	const UINT32 bpp = GetBytesPerPixel(format);
Packit Service 5a9772
Packit Service 5a9772
	for (y = rect->top; y < rect->bottom; y++)
Packit Service 5a9772
	{
Packit Service 5a9772
		UINT32 x;
Packit Service 5a9772
		BYTE* line = &data[stride * y];
Packit Service 5a9772
Packit Service 5a9772
		for (x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
Packit Service 5a9772
		{
Packit Service 5a9772
			UINT32 color;
Packit Service 5a9772
			BYTE r, g, b;
Packit Service 5a9772
			BYTE* src;
Packit Service 5a9772
Packit Service 5a9772
			if (written == count)
Packit Service 5a9772
				return TRUE;
Packit Service 5a9772
Packit Service 5a9772
			src = &line[x * bpp];
Packit Service 5a9772
			color = ReadColor(src, format);
Packit Service 5a9772
			SplitColor(color, format, &r, &g, &b, NULL, NULL);
Packit Service 5a9772
			color = FreeRDPGetColor(format, r, g, b, a);
Packit Service 5a9772
			WriteColor(src, format, color);
Packit Service 5a9772
			written++;
Packit Service 5a9772
		}
Packit Service 5a9772
Packit Service 5a9772
		first = FALSE;
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	return TRUE;
Packit Service 5a9772
}
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
Packit 1fb8d4
                                     const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit Service 5a9772
	UINT16 alphaSig, compressed;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit Service 5a9772
	wStream s;
Packit Service 5a9772
	Stream_StaticInit(&s, cmd->data, cmd->length);
Packit Service 5a9772
Packit Service 5a9772
	if (Stream_GetRemainingLength(&s) < 4)
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(&s, alphaSig);
Packit Service 5a9772
	Stream_Read_UINT16(&s, compressed);
Packit 1fb8d4
Packit Service 5a9772
	if (alphaSig != 0x414C)
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit Service 5a9772
	if (compressed == 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		UINT32 x, y;
Packit Service 5a9772
Packit Service 5a9772
		if (Stream_GetRemainingLength(&s) < cmd->height * cmd->width)
Packit Service 5a9772
			return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit Service 5a9772
		for (y = cmd->top; y < cmd->top + cmd->height; y++)
Packit Service 5a9772
		{
Packit Service 5a9772
			BYTE* line = &surface->data[surface->scanline * y];
Packit Service 5a9772
Packit Service 5a9772
			for (x = cmd->left; x < cmd->left + cmd->width; x++)
Packit Service 5a9772
			{
Packit Service 5a9772
				UINT32 color;
Packit Service 5a9772
				BYTE r, g, b, a;
Packit Service 5a9772
				BYTE* src = &line[x * GetBytesPerPixel(surface->format)];
Packit Service 5a9772
				Stream_Read_UINT8(&s, a);
Packit Service 5a9772
				color = ReadColor(src, surface->format);
Packit Service 5a9772
				SplitColor(color, surface->format, &r, &g, &b, NULL, NULL);
Packit Service 5a9772
				color = FreeRDPGetColor(surface->format, r, g, b, a);
Packit Service 5a9772
				WriteColor(src, surface->format, color);
Packit Service 5a9772
			}
Packit Service 5a9772
		}
Packit Service 5a9772
	}
Packit Service 5a9772
	else
Packit Service 5a9772
	{
Packit Service 5a9772
		UINT32 startOffsetX = 0;
Packit Service 5a9772
		RECTANGLE_16 rect;
Packit Service 5a9772
		rect.left = cmd->left;
Packit Service 5a9772
		rect.top = cmd->top;
Packit Service 5a9772
		rect.right = cmd->left + cmd->width;
Packit Service 5a9772
		rect.bottom = cmd->top + cmd->height;
Packit Service 5a9772
Packit Service 5a9772
		while (rect.top < rect.bottom)
Packit Service 5a9772
		{
Packit Service 5a9772
			UINT32 count;
Packit Service 5a9772
			BYTE a;
Packit Service 5a9772
Packit Service 5a9772
			if (Stream_GetRemainingLength(&s) < 2)
Packit Service 5a9772
				return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit Service 5a9772
			Stream_Read_UINT8(&s, a);
Packit Service 5a9772
			Stream_Read_UINT8(&s, count);
Packit Service 5a9772
Packit Service 5a9772
			if (count >= 0xFF)
Packit Service 5a9772
			{
Packit Service 5a9772
				if (Stream_GetRemainingLength(&s) < 2)
Packit Service 5a9772
					return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit Service 5a9772
				Stream_Read_UINT16(&s, count);
Packit Service 5a9772
Packit Service 5a9772
				if (count >= 0xFFFF)
Packit Service 5a9772
				{
Packit Service 5a9772
					if (Stream_GetRemainingLength(&s) < 4)
Packit Service 5a9772
						return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit Service 5a9772
					Stream_Read_UINT32(&s, count);
Packit Service 5a9772
				}
Packit Service 5a9772
			}
Packit Service 5a9772
Packit Service 5a9772
			if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
Packit Service 5a9772
			                     startOffsetX, count, a))
Packit Service 5a9772
				return ERROR_INTERNAL_ERROR;
Packit Service 5a9772
Packit Service 5a9772
			startOffsetX += count;
Packit Service 5a9772
Packit Service 5a9772
			while (startOffsetX >= cmd->width)
Packit Service 5a9772
			{
Packit Service 5a9772
				startOffsetX -= cmd->width;
Packit Service 5a9772
				rect.top++;
Packit Service 5a9772
			}
Packit Service 5a9772
		}
Packit Service 5a9772
	}
Packit 1fb8d4
Packit 1fb8d4
	invalidRect.left = cmd->left;
Packit 1fb8d4
	invalidRect.top = cmd->top;
Packit 1fb8d4
	invalidRect.right = cmd->right;
Packit 1fb8d4
	invalidRect.bottom = cmd->bottom;
Packit Service 5a9772
	region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
Packit Service 5a9772
	                      &invalidRect);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
Packit Service 5a9772
                                           const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
	INT32 rc;
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	REGION16 invalidRegion;
Packit 1fb8d4
	const RECTANGLE_16* rects;
Packit 1fb8d4
	UINT32 nrRects, x;
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * Note: Since this comes via a Wire-To-Surface-2 PDU the
Packit 1fb8d4
	 * cmd's top/left/right/bottom/width/height members are always zero!
Packit 1fb8d4
	 * The update region is determined during decompression.
Packit 1fb8d4
	 */
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "%s: unable to retrieve surfaceData for surfaceId=%" PRIu32 "", __FUNCTION__,
Packit 1fb8d4
		         cmd->surfaceId);
Packit 1fb8d4
		return ERROR_NOT_FOUND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rc = progressive_create_surface_context(surface->codecs->progressive, cmd->surfaceId,
Packit 1fb8d4
	                                        surface->width, surface->height);
Packit 1fb8d4
Packit 1fb8d4
	if (rc < 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "progressive_create_surface_context failure: %" PRId32 "", rc);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	region16_init(&invalidRegion);
Packit Service 5a9772
	rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
Packit Service 5a9772
	                            surface->format, surface->scanline, cmd->left, cmd->top,
Packit 1fb8d4
	                            &invalidRegion, cmd->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (rc < 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "progressive_decompress failure: %" PRId32 "", rc);
Packit 1fb8d4
		region16_uninit(&invalidRegion);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rects = region16_rects(&invalidRegion, &nrRects);
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
Packit Service 5a9772
	                      nrRects, rects);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	for (x = 0; x < nrRects; x++)
Packit 1fb8d4
		region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]);
Packit 1fb8d4
Packit 1fb8d4
	region16_uninit(&invalidRegion);
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
fail:
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SurfaceCommand(RdpgfxClientContext* context, const RDPGFX_SURFACE_COMMAND* cmd)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit Service 5a9772
	rdpGdi* gdi;
Packit 1fb8d4
Packit 1fb8d4
	if (!context || !cmd)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit Service 5a9772
	gdi = (rdpGdi*)context->custom;
Packit Service 5a9772
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit 1fb8d4
	WLog_Print(gdi->log, WLOG_TRACE,
Packit Service 5a9772
	           "surfaceId=%" PRIu32 ", codec=%" PRIu32 ", contextId=%" PRIu32 ", format=%s, "
Packit Service 5a9772
	           "left=%" PRIu32 ", top=%" PRIu32 ", right=%" PRIu32 ", bottom=%" PRIu32
Packit Service 5a9772
	           ", width=%" PRIu32 ", height=%" PRIu32 " "
Packit Service 5a9772
	           "length=%" PRIu32 ", data=%p, extra=%p",
Packit Service 5a9772
	           cmd->surfaceId, cmd->codecId, cmd->contextId, FreeRDPGetColorFormatName(cmd->format),
Packit Service 5a9772
	           cmd->left, cmd->top, cmd->right, cmd->bottom, cmd->width, cmd->height, cmd->length,
Packit Service 5a9772
	           (void*)cmd->data, (void*)cmd->extra);
Packit 1fb8d4
Packit 1fb8d4
	switch (cmd->codecId)
Packit 1fb8d4
	{
Packit 1fb8d4
		case RDPGFX_CODECID_UNCOMPRESSED:
Packit 1fb8d4
			status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_CAVIDEO:
Packit 1fb8d4
			status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_CLEARCODEC:
Packit 1fb8d4
			status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_PLANAR:
Packit 1fb8d4
			status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_AVC420:
Packit 1fb8d4
			status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_AVC444v2:
Packit 1fb8d4
		case RDPGFX_CODECID_AVC444:
Packit 1fb8d4
			status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_ALPHA:
Packit 1fb8d4
			status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_CAPROGRESSIVE:
Packit 1fb8d4
			status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case RDPGFX_CODECID_CAPROGRESSIVE_V2:
Packit Service 5a9772
			WLog_WARN(TAG, "SurfaceCommand 0x%08" PRIX32 " not implemented", cmd->codecId);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit Service 5a9772
			WLog_WARN(TAG, "Invalid SurfaceCommand 0x%08" PRIX32 "", cmd->codecId);
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT
Packit Service 5a9772
gdi_DeleteEncodingContext(RdpgfxClientContext* context,
Packit Service 5a9772
                          const RDPGFX_DELETE_ENCODING_CONTEXT_PDU* deleteEncodingContext)
Packit 1fb8d4
{
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_CreateSurface(RdpgfxClientContext* context,
Packit 1fb8d4
                              const RDPGFX_CREATE_SURFACE_PDU* createSurface)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)calloc(1, sizeof(gdiGfxSurface));
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	surface->codecs = gdi->context->codecs;
Packit 1fb8d4
Packit 1fb8d4
	if (!surface->codecs)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(surface);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	surface->surfaceId = createSurface->surfaceId;
Packit Service 5a9772
	surface->width = gfx_align_scanline(createSurface->width, 16);
Packit Service 5a9772
	surface->height = gfx_align_scanline(createSurface->height, 16);
Packit Service 5a9772
	surface->mappedWidth = createSurface->width;
Packit Service 5a9772
	surface->mappedHeight = createSurface->height;
Packit Service 5a9772
	surface->outputTargetWidth = createSurface->width;
Packit Service 5a9772
	surface->outputTargetHeight = createSurface->height;
Packit 1fb8d4
Packit 1fb8d4
	switch (createSurface->pixelFormat)
Packit 1fb8d4
	{
Packit 1fb8d4
		case GFX_PIXEL_FORMAT_ARGB_8888:
Packit 1fb8d4
			surface->format = PIXEL_FORMAT_BGRA32;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case GFX_PIXEL_FORMAT_XRGB_8888:
Packit 1fb8d4
			surface->format = PIXEL_FORMAT_BGRX32;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			free(surface);
Packit Service 5a9772
			goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	surface->scanline = gfx_align_scanline(surface->width * 4, 16);
Packit Service 5a9772
	surface->data = (BYTE*)_aligned_malloc(surface->scanline * surface->height, 16);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface->data)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(surface);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	surface->outputMapped = FALSE;
Packit 1fb8d4
	region16_init(&surface->invalidRegion);
Packit Service 5a9772
	rc = context->SetSurfaceData(context, surface->surfaceId, (void*)surface);
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
Packit 1fb8d4
                              const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	rdpCodecs* codecs = NULL;
Packit 1fb8d4
	gdiGfxSurface* surface = NULL;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (surface)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (surface->windowId != 0)
Packit Service 5a9772
			rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
Packit Service 5a9772
			                  surface->windowId);
Packit Service 5a9772
Packit 1fb8d4
#ifdef WITH_GFX_H264
Packit 1fb8d4
		h264_context_free(surface->h264);
Packit 1fb8d4
#endif
Packit 1fb8d4
		region16_uninit(&surface->invalidRegion);
Packit 1fb8d4
		codecs = surface->codecs;
Packit 1fb8d4
		_aligned_free(surface->data);
Packit 1fb8d4
		free(surface);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rc = context->SetSurfaceData(context, deleteSurface->surfaceId, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (codecs && codecs->progressive)
Packit 1fb8d4
		progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
Packit 1fb8d4
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT gdi_SolidFill(RdpgfxClientContext* context, const RDPGFX_SOLID_FILL_PDU* solidFill)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT status = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	UINT16 index;
Packit 1fb8d4
	UINT32 color;
Packit 1fb8d4
	BYTE a, r, g, b;
Packit 1fb8d4
	UINT32 nWidth, nHeight;
Packit 1fb8d4
	RECTANGLE_16* rect;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	b = solidFill->fillPixel.B;
Packit 1fb8d4
	g = solidFill->fillPixel.G;
Packit 1fb8d4
	r = solidFill->fillPixel.R;
Packit 1fb8d4
	/* a = solidFill->fillPixel.XA;
Packit 1fb8d4
	 * Ignore alpha channel, this is a solid fill. */
Packit 1fb8d4
	a = 0xFF;
Packit 1fb8d4
	color = FreeRDPGetColor(surface->format, r, g, b, a);
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < solidFill->fillRectCount; index++)
Packit 1fb8d4
	{
Packit 1fb8d4
		rect = &(solidFill->fillRects[index]);
Packit 1fb8d4
		nWidth = rect->right - rect->left;
Packit 1fb8d4
		nHeight = rect->bottom - rect->top;
Packit 1fb8d4
		invalidRect.left = rect->left;
Packit 1fb8d4
		invalidRect.top = rect->top;
Packit 1fb8d4
		invalidRect.right = rect->right;
Packit 1fb8d4
		invalidRect.bottom = rect->bottom;
Packit 1fb8d4
Packit Service 5a9772
		if (!freerdp_image_fill(surface->data, surface->format, surface->scanline, rect->left,
Packit Service 5a9772
		                        rect->top, nWidth, nHeight, color))
Packit Service 5a9772
			goto fail;
Packit 1fb8d4
Packit Service 5a9772
		region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
Packit Service 5a9772
	                      solidFill->fillRectCount, solidFill->fillRects);
Packit Service 5a9772
Packit Service 5a9772
	if (status != CHANNEL_RC_OK)
Packit Service 5a9772
		goto fail;
Packit Service 5a9772
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit 1fb8d4
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
Packit 1fb8d4
                                 const RDPGFX_SURFACE_TO_SURFACE_PDU* surfaceToSurface)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT status = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	UINT16 index;
Packit 1fb8d4
	BOOL sameSurface;
Packit 1fb8d4
	UINT32 nWidth, nHeight;
Packit 1fb8d4
	const RECTANGLE_16* rectSrc;
Packit 1fb8d4
	RDPGFX_POINT16* destPt;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit 1fb8d4
	gdiGfxSurface* surfaceSrc;
Packit 1fb8d4
	gdiGfxSurface* surfaceDst;
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit 1fb8d4
	rectSrc = &(surfaceToSurface->rectSrc);
Packit Service 5a9772
	surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
Packit Service 5a9772
	sameSurface =
Packit Service 5a9772
	    (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest) ? TRUE : FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!sameSurface)
Packit Service 5a9772
		surfaceDst =
Packit Service 5a9772
		    (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
Packit 1fb8d4
	else
Packit 1fb8d4
		surfaceDst = surfaceSrc;
Packit 1fb8d4
Packit 1fb8d4
	if (!surfaceSrc || !surfaceDst)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	nWidth = rectSrc->right - rectSrc->left;
Packit 1fb8d4
	nHeight = rectSrc->bottom - rectSrc->top;
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < surfaceToSurface->destPtsCount; index++)
Packit 1fb8d4
	{
Packit 1fb8d4
		destPt = &surfaceToSurface->destPts[index];
Packit 1fb8d4
Packit Service 5a9772
		if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
Packit Service 5a9772
		                        destPt->x, destPt->y, nWidth, nHeight, surfaceSrc->data,
Packit Service 5a9772
		                        surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
Packit Service 5a9772
		                        rectSrc->top, NULL, FREERDP_FLIP_NONE))
Packit Service 5a9772
			goto fail;
Packit 1fb8d4
Packit 1fb8d4
		invalidRect.left = destPt->x;
Packit 1fb8d4
		invalidRect.top = destPt->y;
Packit 1fb8d4
		invalidRect.right = destPt->x + rectSrc->right;
Packit 1fb8d4
		invalidRect.bottom = destPt->y + rectSrc->bottom;
Packit Service 5a9772
		region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion, &invalidRect);
Packit Service 5a9772
		status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
Packit Service 5a9772
		                      surfaceDst->surfaceId, 1, &invalidRect);
Packit Service 5a9772
Packit Service 5a9772
		if (status != CHANNEL_RC_OK)
Packit Service 5a9772
			goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
Packit 1fb8d4
                               const RDPGFX_SURFACE_TO_CACHE_PDU* surfaceToCache)
Packit 1fb8d4
{
Packit 1fb8d4
	const RECTANGLE_16* rect;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	gdiGfxCacheEntry* cacheEntry;
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit 1fb8d4
	rect = &(surfaceToCache->rectSrc);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit Service 5a9772
	cacheEntry = (gdiGfxCacheEntry*)calloc(1, sizeof(gdiGfxCacheEntry));
Packit 1fb8d4
Packit 1fb8d4
	if (!cacheEntry)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	cacheEntry->width = (UINT32)(rect->right - rect->left);
Packit 1fb8d4
	cacheEntry->height = (UINT32)(rect->bottom - rect->top);
Packit 1fb8d4
	cacheEntry->format = surface->format;
Packit 1fb8d4
	cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * 4, 16);
Packit Service 5a9772
	cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
Packit 1fb8d4
Packit 1fb8d4
	if (!cacheEntry->data)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(cacheEntry);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!freerdp_image_copy(cacheEntry->data, cacheEntry->format, cacheEntry->scanline, 0, 0,
Packit Service 5a9772
	                        cacheEntry->width, cacheEntry->height, surface->data, surface->format,
Packit Service 5a9772
	                        surface->scanline, rect->left, rect->top, NULL, FREERDP_FLIP_NONE))
Packit 1fb8d4
	{
Packit Service 5a9772
		free(cacheEntry->data);
Packit 1fb8d4
		free(cacheEntry);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (void*)cacheEntry);
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
Packit 1fb8d4
                               const RDPGFX_CACHE_TO_SURFACE_PDU* cacheToSurface)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT status = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	UINT16 index;
Packit 1fb8d4
	RDPGFX_POINT16* destPt;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit 1fb8d4
	gdiGfxCacheEntry* cacheEntry;
Packit 1fb8d4
	RECTANGLE_16 invalidRect;
Packit Service 5a9772
	rdpGdi* gdi = (rdpGdi*)context->custom;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
Packit Service 5a9772
	cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface || !cacheEntry)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < cacheToSurface->destPtsCount; index++)
Packit 1fb8d4
	{
Packit 1fb8d4
		destPt = &cacheToSurface->destPts[index];
Packit 1fb8d4
Packit Service 5a9772
		if (!freerdp_image_copy(surface->data, surface->format, surface->scanline, destPt->x,
Packit Service 5a9772
		                        destPt->y, cacheEntry->width, cacheEntry->height, cacheEntry->data,
Packit Service 5a9772
		                        cacheEntry->format, cacheEntry->scanline, 0, 0, NULL,
Packit Service 5a9772
		                        FREERDP_FLIP_NONE))
Packit Service 5a9772
			goto fail;
Packit 1fb8d4
Packit 1fb8d4
		invalidRect.left = destPt->x;
Packit 1fb8d4
		invalidRect.top = destPt->y;
Packit 1fb8d4
		invalidRect.right = destPt->x + cacheEntry->width;
Packit 1fb8d4
		invalidRect.bottom = destPt->y + cacheEntry->height;
Packit Service 5a9772
		region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect);
Packit Service 5a9772
		status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
Packit Service 5a9772
		                      surface->surfaceId, 1, &invalidRect);
Packit Service 5a9772
Packit Service 5a9772
		if (status != CHANNEL_RC_OK)
Packit Service 5a9772
			goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
Packit 1fb8d4
	if (!gdi->inGfxFrame)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_NOT_INITIALIZED;
Packit 1fb8d4
		IFCALLRET(context->UpdateSurfaces, status, context);
Packit 1fb8d4
	}
Packit Service 5a9772
	else
Packit Service 5a9772
		status = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
Packit 1fb8d4
                                 const RDPGFX_CACHE_IMPORT_REPLY_PDU* cacheImportReply)
Packit 1fb8d4
{
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
Packit 1fb8d4
                                const RDPGFX_EVICT_CACHE_ENTRY_PDU* evictCacheEntry)
Packit 1fb8d4
{
Packit 1fb8d4
	gdiGfxCacheEntry* cacheEntry;
Packit Service 5a9772
	UINT rc = ERROR_NOT_FOUND;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
Packit 1fb8d4
Packit 1fb8d4
	if (cacheEntry)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(cacheEntry->data);
Packit 1fb8d4
		free(cacheEntry);
Packit Service 5a9772
		rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot, NULL);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
Packit 1fb8d4
                                   const RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU* surfaceToOutput)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	gdiGfxSurface* surface;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
Packit 1fb8d4
Packit 1fb8d4
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	surface->outputMapped = TRUE;
Packit 1fb8d4
	surface->outputOriginX = surfaceToOutput->outputOriginX;
Packit 1fb8d4
	surface->outputOriginY = surfaceToOutput->outputOriginY;
Packit Service 5a9772
	surface->outputTargetWidth = surface->mappedWidth;
Packit Service 5a9772
	surface->outputTargetHeight = surface->mappedHeight;
Packit 1fb8d4
	region16_clear(&surface->invalidRegion);
Packit Service 5a9772
	rc = CHANNEL_RC_OK;
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit Service 5a9772
}
Packit Service 5a9772
Packit Service 5a9772
static UINT
Packit Service 5a9772
gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
Packit Service 5a9772
                             const RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU* surfaceToOutput)
Packit Service 5a9772
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit Service 5a9772
	gdiGfxSurface* surface;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
Packit Service 5a9772
Packit Service 5a9772
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit Service 5a9772
Packit Service 5a9772
	surface->outputMapped = TRUE;
Packit Service 5a9772
	surface->outputOriginX = surfaceToOutput->outputOriginX;
Packit Service 5a9772
	surface->outputOriginY = surfaceToOutput->outputOriginY;
Packit Service 5a9772
	surface->outputTargetWidth = surfaceToOutput->targetWidth;
Packit Service 5a9772
	surface->outputTargetHeight = surfaceToOutput->targetHeight;
Packit Service 5a9772
	region16_clear(&surface->invalidRegion);
Packit Service 5a9772
	rc = CHANNEL_RC_OK;
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
Packit 1fb8d4
                                   const RDPGFX_MAP_SURFACE_TO_WINDOW_PDU* surfaceToWindow)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit Service 5a9772
	gdiGfxSurface* surface;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
Packit Service 5a9772
Packit Service 5a9772
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit Service 5a9772
Packit Service 5a9772
	if (surface->windowId != 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		if (surface->windowId != surfaceToWindow->windowId)
Packit Service 5a9772
			goto fail;
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	surface->windowId = surfaceToWindow->windowId;
Packit Service 5a9772
	surface->mappedWidth = surfaceToWindow->mappedWidth;
Packit Service 5a9772
	surface->mappedHeight = surfaceToWindow->mappedHeight;
Packit Service 5a9772
	surface->outputTargetWidth = surfaceToWindow->mappedWidth;
Packit Service 5a9772
	surface->outputTargetHeight = surfaceToWindow->mappedHeight;
Packit Service 5a9772
	rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
Packit Service 5a9772
	                  surfaceToWindow->surfaceId, surfaceToWindow->windowId);
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT
Packit Service 5a9772
gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
Packit Service 5a9772
                             const RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU* surfaceToWindow)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT rc = ERROR_INTERNAL_ERROR;
Packit Service 5a9772
	gdiGfxSurface* surface;
Packit Service 5a9772
	EnterCriticalSection(&context->mux);
Packit Service 5a9772
	surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
Packit Service 5a9772
Packit Service 5a9772
	if (!surface)
Packit Service 5a9772
		goto fail;
Packit Service 5a9772
Packit Service 5a9772
	if (surface->windowId != 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		if (surface->windowId != surfaceToWindow->windowId)
Packit Service 5a9772
			goto fail;
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	surface->windowId = surfaceToWindow->windowId;
Packit Service 5a9772
	surface->mappedWidth = surfaceToWindow->mappedWidth;
Packit Service 5a9772
	surface->mappedHeight = surfaceToWindow->mappedHeight;
Packit Service 5a9772
	surface->outputTargetWidth = surfaceToWindow->targetWidth;
Packit Service 5a9772
	surface->outputTargetHeight = surfaceToWindow->targetHeight;
Packit Service 5a9772
	rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
Packit Service 5a9772
	                  surfaceToWindow->surfaceId, surfaceToWindow->windowId);
Packit Service 5a9772
fail:
Packit Service 5a9772
	LeaveCriticalSection(&context->mux);
Packit Service 5a9772
	return rc;
Packit Service 5a9772
}
Packit Service 5a9772
Packit Service 5a9772
BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
Packit Service 5a9772
{
Packit Service 5a9772
	return gdi_graphics_pipeline_init_ex(gdi, gfx, NULL, NULL, NULL);
Packit Service 5a9772
}
Packit Service 5a9772
Packit Service 5a9772
BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
Packit Service 5a9772
                                   pcRdpgfxMapWindowForSurface map,
Packit Service 5a9772
                                   pcRdpgfxUnmapWindowForSurface unmap,
Packit Service 5a9772
                                   pcRdpgfxUpdateSurfaceArea update)
Packit Service 5a9772
{
Packit Service 5a9772
	if (!gdi || !gfx)
Packit Service 5a9772
		return FALSE;
Packit Service 5a9772
Packit 1fb8d4
	gdi->gfx = gfx;
Packit Service 5a9772
	gfx->custom = (void*)gdi;
Packit 1fb8d4
	gfx->ResetGraphics = gdi_ResetGraphics;
Packit 1fb8d4
	gfx->StartFrame = gdi_StartFrame;
Packit 1fb8d4
	gfx->EndFrame = gdi_EndFrame;
Packit 1fb8d4
	gfx->SurfaceCommand = gdi_SurfaceCommand;
Packit 1fb8d4
	gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
Packit 1fb8d4
	gfx->CreateSurface = gdi_CreateSurface;
Packit 1fb8d4
	gfx->DeleteSurface = gdi_DeleteSurface;
Packit 1fb8d4
	gfx->SolidFill = gdi_SolidFill;
Packit 1fb8d4
	gfx->SurfaceToSurface = gdi_SurfaceToSurface;
Packit 1fb8d4
	gfx->SurfaceToCache = gdi_SurfaceToCache;
Packit 1fb8d4
	gfx->CacheToSurface = gdi_CacheToSurface;
Packit 1fb8d4
	gfx->CacheImportReply = gdi_CacheImportReply;
Packit 1fb8d4
	gfx->EvictCacheEntry = gdi_EvictCacheEntry;
Packit 1fb8d4
	gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
Packit 1fb8d4
	gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
Packit Service 5a9772
	gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
Packit Service 5a9772
	gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
Packit 1fb8d4
	gfx->UpdateSurfaces = gdi_UpdateSurfaces;
Packit Service 5a9772
	gfx->MapWindowForSurface = map;
Packit Service 5a9772
	gfx->UnmapWindowForSurface = unmap;
Packit Service 5a9772
	gfx->UpdateSurfaceArea = update;
Packit Service 5a9772
	InitializeCriticalSection(&gfx->mux);
Packit Service 5a9772
	PROFILER_CREATE(gfx->SurfaceProfiler, "GFX-PROFILER");
Packit Service 5a9772
Packit Service 5a9772
	/**
Packit Service 5a9772
	 * gdi->graphicsReset will be removed in FreeRDP v3 from public headers,
Packit Service 5a9772
	 * since the EGFX Reset Graphics PDU seems to be optional.
Packit Service 5a9772
	 * There are still some clients that expect and check it and therefore
Packit Service 5a9772
	 * we simply initialize it with TRUE here for now.
Packit Service 5a9772
	 */
Packit Service 5a9772
	gdi->graphicsReset = TRUE;
Packit Service 5a9772
Packit Service 5a9772
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
Packit 1fb8d4
{
Packit Service 5a9772
	if (gdi)
Packit Service 5a9772
		gdi->gfx = NULL;
Packit Service 5a9772
Packit Service 5a9772
	if (!gfx)
Packit Service 5a9772
		return;
Packit Service 5a9772
Packit 1fb8d4
	gfx->custom = NULL;
Packit Service 5a9772
	DeleteCriticalSection(&gfx->mux);
Packit 1fb8d4
	PROFILER_PRINT_HEADER
Packit 1fb8d4
	PROFILER_PRINT(gfx->SurfaceProfiler)
Packit 1fb8d4
	PROFILER_PRINT_FOOTER
Packit 1fb8d4
	PROFILER_FREE(gfx->SurfaceProfiler)
Packit 1fb8d4
}