IDirectFB 			*dfb;
IDirectFBSurface 	*primarysurface;
IDirectFBDisplayLayer *primarydisplaylayer;

int createPrimarySurface ()
{
	DFBDisplayLayerConfig 	displaylayerconfig;
	DFBRegion 					screenrect = { 0, 0, 799, 599 };
	DFBResult					ret;
	int							screen_width;
	int							screen_height;

	displaylayerconfig.options = (DFBDisplayLayerOptions) ( DLOP_ALPHACHANNEL | DLOP_SRC_COLORKEY);
	displaylayerconfig.flags = (DFBDisplayLayerConfigFlags) (DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_SURFACE_CAPS | DLCONF_PIXELFORMAT /*| DLCONF_BUFFERMODE*/ );
	displaylayerconfig.width = PRIMARY_LAYER_WIDTH;
	displaylayerconfig.height= PRIMARY_LAYER_HEIGHT;
	displaylayerconfig.surface_caps = (DFBSurfaceCapabilities)( DSCAPS_INTERLACED );
	displaylayerconfig.buffermode = DLBM_TRIPLE;
	displaylayerconfig.pixelformat = DSPF_RGB32;

	ret = dfb->GetDisplayLayer( dfb, 0, &primarydisplaylayer ); //layerID for primary layer is 0
	if (ret || !primarydisplaylayer)
	{
		printf("failed to GetDisplayLayer\n");
		return false;
	}

	primarydisplaylayer->GetSurface( primarydisplaylayer, &primarysurface );

  	primarysurface->SetBlittingFlags( primarysurface, (DFBSurfaceBlittingFlags)(DSBLIT_BLEND_COLORALPHA|DSBLIT_SRC_COLORKEY) );
  	primarysurface->SetDrawingFlags( primarysurface, DSDRAW_BLEND );
  	primarysurface->SetColor( primarysurface, 0,0,0,0 );
//		pprimarydisplayer->SetOpacity ( pprimarydisplayer, 50 );
	primarysurface->GetSize (primarysurface, &screen_width, &screen_height);

	screenrect.x1 = 0;
	screenrect.y1 = 0;
	screenrect.x2 = screenrect.x1 + screen_width - 1;
	screenrect.y2 = screenrect.y1 + screen_height - 1;
	primarysurface->SetClip( primarysurface, &screenrect );
	primarysurface->Clear ( primarysurface, 0, 0, 0, 0 );

	return true;
}

bool testGraphicsInUserSurface ( )
{
	DFBSurfaceDescription 	surfacedesc = { (DFBSurfaceDescriptionFlags) 0 };
	IDirectFBSurface 	*pUserSurface, *pSubSurface;
	DFBResult 	err;
	RECT stWndRect;

	createPrimarySurface ();

	surfacedesc.flags = (DFBSurfaceDescriptionFlags) (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT);
	surfacedesc.caps = (DFBSurfaceCapabilities) (DSCAPS_DOUBLE);
   surfacedesc.caps = (DFBSurfaceCapabilities) (surfacedesc.caps | DSCAPS_VIDEOONLY);
	surfacedesc.width = 720;
	surfacedesc.height = 576;
	surfacedesc.pixelformat = DSPF_ARGB;

	err = dfb->CreateSurface( dfb, &surfacedesc, &pUserSurface );
	if (err != DFB_OK)
	{
		printf ("CreateSurface failed: with errcode[%d]\n", err);
		return false;
	}

	stWndRect.Xval = 20;
	stWndRect.Yval = 20;
	stWndRect.width = 40;
	stWndRect.height = 40;

   pUserSurface->SetSrcColorKey( pUserSurface, 0xFF, 0x00, 0xFF );

	drawImageInUserSurface ( pUserSurface, "logo.png", stWndRect );

	pUserSurface->Flip( pUserSurface, NULL, DSFLIP_WAITFORSYNC );

	err = pUserSurface->GetSubSurface(pUserSurface, (DFBRectangle*)&stWndRect, &pSubSurface);
	if (err != DFB_OK)
	{
		printf("failed to get subsurface handle\n");
		pSubSurface = pUserSurface;
	}

   pSubSurface->SetSrcColorKey( pSubSurface, 0xFF, 0x00, 0xFF );

   err = primarysurface->SetBlittingFlags( primarysurface, DSBLIT_SRC_COLORKEY );

   err = primarysurface->Blit (primarysurface, pSubSurface, NULL, 20, 20 );
	if (err != DFB_OK)
	{
		printf("failed to blit user surface over primary surface\n");
		return false;
	}

	primarysurface->Flip( primarysurface, NULL, DSFLIP_WAITFORSYNC );

	return true;
}


