Blame libfreerdp/primitives/prim_copy.c

Packit Service fa4841
/* FreeRDP: A Remote Desktop Protocol Client
Packit Service fa4841
 * Copy operations.
Packit Service fa4841
 * vi:ts=4 sw=4:
Packit Service fa4841
 *
Packit Service fa4841
 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Packit Service fa4841
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Packit Service fa4841
 * not use this file except in compliance with the License. You may obtain
Packit Service fa4841
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Packit Service fa4841
 * Unless required by applicable law or agreed to in writing, software
Packit Service fa4841
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit Service fa4841
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
Packit Service fa4841
 * or implied. See the License for the specific language governing
Packit Service fa4841
 * permissions and limitations under the License.
Packit Service fa4841
 */
Packit Service fa4841
Packit Service fa4841
#ifdef HAVE_CONFIG_H
Packit Service fa4841
#include "config.h"
Packit Service fa4841
#endif
Packit Service fa4841
Packit Service fa4841
#include <string.h>
Packit Service fa4841
#include <freerdp/types.h>
Packit Service fa4841
#include <freerdp/primitives.h>
Packit Service fa4841
#ifdef WITH_IPP
Packit Service b1ea74
#include <ipps.h>
Packit Service b1ea74
#include <ippi.h>
Packit Service fa4841
#endif /* WITH_IPP */
Packit Service fa4841
#include "prim_internal.h"
Packit Service fa4841
Packit Service fa4841
static primitives_t* generic = NULL;
Packit Service fa4841
Packit Service fa4841
/* ------------------------------------------------------------------------- */
Packit Service fa4841
/*static inline BOOL memory_regions_overlap_1d(*/
Packit Service b1ea74
static BOOL memory_regions_overlap_1d(const BYTE* p1, const BYTE* p2, size_t bytes)
Packit Service fa4841
{
Packit Service b1ea74
	const ULONG_PTR p1m = (const ULONG_PTR)p1;
Packit Service b1ea74
	const ULONG_PTR p2m = (const ULONG_PTR)p2;
Packit Service fa4841
Packit Service fa4841
	if (p1m <= p2m)
Packit Service fa4841
	{
Packit Service b1ea74
		if (p1m + bytes > p2m)
Packit Service b1ea74
			return TRUE;
Packit Service fa4841
	}
Packit Service fa4841
	else
Packit Service fa4841
	{
Packit Service b1ea74
		if (p2m + bytes > p1m)
Packit Service b1ea74
			return TRUE;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/* else */
Packit Service fa4841
	return FALSE;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
/* ------------------------------------------------------------------------- */
Packit Service fa4841
/*static inline BOOL memory_regions_overlap_2d( */
Packit Service b1ea74
static BOOL memory_regions_overlap_2d(const BYTE* p1, int p1Step, int p1Size, const BYTE* p2,
Packit Service b1ea74
                                      int p2Step, int p2Size, int width, int height)
Packit Service fa4841
{
Packit Service b1ea74
	ULONG_PTR p1m = (ULONG_PTR)p1;
Packit Service b1ea74
	ULONG_PTR p2m = (ULONG_PTR)p2;
Packit Service fa4841
Packit Service fa4841
	if (p1m <= p2m)
Packit Service fa4841
	{
Packit Service fa4841
		ULONG_PTR p1mEnd = p1m + (height - 1) * p1Step + width * p1Size;
Packit Service fa4841
Packit Service b1ea74
		if (p1mEnd > p2m)
Packit Service b1ea74
			return TRUE;
Packit Service fa4841
	}
Packit Service fa4841
	else
Packit Service fa4841
	{
Packit Service fa4841
		ULONG_PTR p2mEnd = p2m + (height - 1) * p2Step + width * p2Size;
Packit Service fa4841
Packit Service b1ea74
		if (p2mEnd > p1m)
Packit Service b1ea74
			return TRUE;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/* else */
Packit Service fa4841
	return FALSE;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
/* ------------------------------------------------------------------------- */
Packit Service b1ea74
static pstatus_t general_copy_8u(const BYTE* pSrc, BYTE* pDst, INT32 len)
Packit Service fa4841
{
Packit Service b1ea74
	if (memory_regions_overlap_1d(pSrc, pDst, (size_t)len))
Packit Service fa4841
	{
Packit Service b1ea74
		memmove((void*)pDst, (const void*)pSrc, (size_t)len);
Packit Service fa4841
	}
Packit Service fa4841
	else
Packit Service fa4841
	{
Packit Service b1ea74
		memcpy((void*)pDst, (const void*)pSrc, (size_t)len);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	return PRIMITIVES_SUCCESS;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
/* ------------------------------------------------------------------------- */
Packit Service fa4841
/* Copy a block of pixels from one buffer to another.
Packit Service fa4841
 * The addresses are assumed to have been already offset to the upper-left
Packit Service fa4841
 * corners of the source and destination region of interest.
Packit Service fa4841
 */
Packit Service b1ea74
static pstatus_t general_copy_8u_AC4r(const BYTE* pSrc, INT32 srcStep, BYTE* pDst, INT32 dstStep,
Packit Service b1ea74
                                      INT32 width, INT32 height)
Packit Service fa4841
{
Packit Service b1ea74
	const BYTE* src = (const BYTE*)pSrc;
Packit Service b1ea74
	BYTE* dst = (BYTE*)pDst;
Packit Service fa4841
	int rowbytes = width * sizeof(UINT32);
Packit Service fa4841
Packit Service b1ea74
	if ((width == 0) || (height == 0))
Packit Service b1ea74
		return PRIMITIVES_SUCCESS;
Packit Service fa4841
Packit Service b1ea74
	if (memory_regions_overlap_2d(pSrc, srcStep, sizeof(UINT32), pDst, dstStep, sizeof(UINT32),
Packit Service b1ea74
	                              width, height))
Packit Service fa4841
	{
Packit Service fa4841
		do
Packit Service fa4841
		{
Packit Service fa4841
			generic->copy(src, dst, rowbytes);
Packit Service fa4841
			src += srcStep;
Packit Service fa4841
			dst += dstStep;
Packit Service b1ea74
		} while (--height);
Packit Service fa4841
	}
Packit Service fa4841
	else
Packit Service fa4841
	{
Packit Service fa4841
		/* TODO: do it in one operation when the rowdata is adjacent. */
Packit Service fa4841
		do
Packit Service fa4841
		{
Packit Service fa4841
			/* If we find a replacement for memcpy that is consistently
Packit Service fa4841
			 * faster, this could be replaced with that.
Packit Service fa4841
			 */
Packit Service fa4841
			memcpy(dst, src, rowbytes);
Packit Service fa4841
			src += srcStep;
Packit Service fa4841
			dst += dstStep;
Packit Service b1ea74
		} while (--height);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	return PRIMITIVES_SUCCESS;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
#ifdef WITH_IPP
Packit Service fa4841
/* ------------------------------------------------------------------------- */
Packit Service fa4841
/* This is just ippiCopy_8u_AC4R without the IppiSize structure parameter.   */
Packit Service b1ea74
static pstatus_t ippiCopy_8u_AC4r(const BYTE* pSrc, INT32 srcStep, BYTE* pDst, INT32 dstStep,
Packit Service b1ea74
                                  INT32 width, INT32 height)
Packit Service fa4841
{
Packit Service fa4841
	IppiSize roi;
Packit Service b1ea74
	roi.width = width;
Packit Service fa4841
	roi.height = height;
Packit Service b1ea74
	return (pstatus_t)ippiCopy_8u_AC4R(pSrc, srcStep, pDst, dstStep, roi);
Packit Service fa4841
}
Packit Service fa4841
#endif /* WITH_IPP */
Packit Service fa4841
Packit Service fa4841
/* ------------------------------------------------------------------------- */
Packit Service b1ea74
void primitives_init_copy(primitives_t* prims)
Packit Service fa4841
{
Packit Service fa4841
	/* Start with the default. */
Packit Service fa4841
	prims->copy_8u = general_copy_8u;
Packit Service fa4841
	prims->copy_8u_AC4r = general_copy_8u_AC4r;
Packit Service fa4841
	/* This is just an alias with void* parameters */
Packit Service b1ea74
	prims->copy = (__copy_t)(prims->copy_8u);
Packit Service fa4841
}
Packit Service fa4841
Packit Service b1ea74
void primitives_init_copy_opt(primitives_t* prims)
Packit Service fa4841
{
Packit Service fa4841
	generic = primitives_get_generic();
Packit Service fa4841
	primitives_init_copy(prims);
Packit Service fa4841
	/* Pick tuned versions if possible. */
Packit Service fa4841
#ifdef WITH_IPP
Packit Service b1ea74
	prims->copy_8u = (__copy_8u_t)ippsCopy_8u;
Packit Service b1ea74
	prims->copy_8u_AC4r = (__copy_8u_AC4r_t)ippiCopy_8u_AC4r;
Packit Service fa4841
#endif
Packit Service fa4841
	/* Performance with an SSE2 version with no prefetch seemed to be
Packit Service fa4841
	 * all over the map vs. memcpy.
Packit Service fa4841
	 * Sometimes it was significantly faster, sometimes dreadfully slower,
Packit Service fa4841
	 * and it seemed to vary a lot depending on block size and processor.
Packit Service fa4841
	 * Hence, no SSE version is used here unless once can be written that
Packit Service fa4841
	 * is consistently faster than memcpy.
Packit Service fa4841
	 */
Packit Service fa4841
	/* This is just an alias with void* parameters */
Packit Service b1ea74
	prims->copy = (__copy_t)(prims->copy_8u);
Packit Service fa4841
}