#include #include #include #include #define INVALID_COLOR 0x01010101 typedef struct{ int fmt; BYTE ashift; BYTE rshift; BYTE gshift; BYTE bshift; BYTE amask; BYTE rmask; BYTE gmask; BYTE bmask; BYTE aloss; BYTE rloss; BYTE gloss; BYTE bloss; }FORMAT; FORMAT formats[]={ {GPF_UNKNOWN,0,0,0,0, 0,0,0,0}, {GPF_ARGB4444,12, 8,4, 0,0xFF,0xFF,0xFF,0xFF,0,0,0,0}, {GPF_ARGB1555,15,10,5, 0,0x80,0x7C,0x3E,0x1F,0,0,0,0}, {GPF_ARGB, 24,16,8, 0,0xFF,0xFF,0xFF,0xFF,0,0,0,0}, {GPF_ABGR, 24,0 ,8,16,0xFF,0xFF,0xFF,0xFF,0,0,0,0} }; class GRAPH:public testing::Test{ public : static void SetUpTestCase(){ GFXInit(); for(int f=GPF_ARGB4444;f<=GPF_ABGR;f++){ printf("format %d whitepixel=%x\r\n",f,Color2Pixel(f,0xFFFFFFFF)); printf("format %d whitealphapixel=%x\r\n",f,Color2Pixel(f,0x00FFFFFF)); } } static void TearDownTestCase(){ sleep(20); } virtual void SetUp(){ } static UINT Color2Pixel(INT fmt,UINT color){ FORMAT*f=formats+fmt; BYTE a,r,g,b; UINT pixel; a=color>>24; r=color>>16; g=color>>8; b=color; pixel=((a&f->amask)<ashift)|((r&f->rmask)<rshift)|((g&f->gmask)<gshift)|((b&f->bmask)<bshift); return pixel; } virtual void TearDown(){ } unsigned long long gettime(){ struct timeval tv; gettimeofday(&tv,NULL); return tv.tv_sec*1000+tv.tv_usec/1000; } unsigned int getPixel(HANDLE surface,int x,int y){ //for pixel(x,y)out of surface,we return INVALID_COLOR BYTE*buffer; UINT w,h,f,pitch; GFXLockSurface(surface,(void**)&buffer,&pitch); GFXGetSurfaceInfo(surface,&w,&h,(int*)&f); if(x<0||y<0||x>=w||y>=h)return INVALID_COLOR; buffer+=pitch*y; switch(f){ case GPF_ARGB4444: case GPF_ARGB1555:return *(USHORT*)(buffer+2*x); case GPF_ARGB: case GPF_ABGR: case GPF_RGB32:return *(UINT*)(buffer+4*x); } } BOOL errorPixel(HANDLE surface,int x,int y,UINT color){ //check color of pixel(x,y) =color UINT cc=getPixel(surface,x,y); return ((cc!=color)&&(cc!=INVALID_COLOR))||(cc==INVALID_COLOR); } void setRect(GFXRect&r,int x,int y,UINT w,UINT h){ r.x=x;r.y=y;r.w=w;r.h=h; } int CheckColor(HANDLE surface,GFXRect*rec,UINT c){ int rc=0; UINT w,h,f; GFXRect r; GFXGetSurfaceInfo(surface,&w,&h,(int*)&f); if(rec) r=*rec; else setRect(r,0,0,w,h); //check color of rect(inner) if(c==getPixel(surface,r.x,r.y))rc|=1; //topleft if(c==getPixel(surface,r.x+(int)r.w-1,r.y))rc|=2; //topright if(c==getPixel(surface,r.x+(int)r.w-1,r.y+(int)r.h-1))rc|=4;//rightbottom if(c==getPixel(surface,r.x,r.y+(int)r.h-1))rc|=8; //leftbottom //check color outof rect(outer) if(errorPixel(surface,r.x ,r.y-1,c))rc|=0x10; if(errorPixel(surface,r.x-1,r.y-1,c))rc|=0x20; if(errorPixel(surface,r.x-1,r.y+1,c))rc|=0x40; if(errorPixel(surface,r.x+(int)r.w-1 ,r.y-1,c))rc|=0x80; if(errorPixel(surface,r.x+(int)r.w ,r.y-1,c))rc|=0x100; if(errorPixel(surface,r.x+(int)r.w ,r.y,c)||1)rc|=0x200; if(errorPixel(surface,r.x+(int)r.w ,r.y+(int)r.h-1,c))rc|=0x400; if(errorPixel(surface,r.x+(int)r.w ,r.y+r.h,c))rc|=0x800; if(errorPixel(surface,r.x+(int)r.w-1 ,r.y+r.h,c))rc|=0x1000; if(errorPixel(surface,r.x-1,r.y+(int)r.h-1,c))rc|=0x2000; if(errorPixel(surface,r.x-1,r.y+(int)r.h,c))rc|=0x4000; if(errorPixel(surface,r.x,r.y+(int)r.h,c))rc|=0x8000; return rc; } int FillCheckColor(HANDLE surface,GFXRect*rec,UINT c){ GFXFillRect(surface,rec,c); return CheckColor(surface,rec,c); } }; TEST_F(GRAPH,Graph_GetScreen){ UINT w,h; ASSERT_EQ(0,GFXGetScreenSize(&w,&h)); ASSERT_GT(w,0); ASSERT_GT(h,0); } TEST_F(GRAPH,CreateSurface_1){ HANDLE surface=0; UINT width,height; ASSERT_EQ(0,GFXGetScreenSize(&width,&height)); ASSERT_EQ(0,GFXCreateSurface(&surface,width,height,GPF_ARGB,1)); ASSERT_NE((HANDLE)nullptr,surface); GFXRect r={100,100,400,400}; ASSERT_EQ(0,GFXFillRect(surface,&r,0xFFFF00FF)); ASSERT_EQ(0,GFXFlip(surface)); ASSERT_NE((HANDLE)nullptr,surface); sleep(2); ASSERT_EQ(0,GFXDestroySurface(surface)); } TEST_F(GRAPH,CreateSurface_2){ UINT width,height,pitch; int fmts[]={GPF_ARGB4444,GPF_ARGB1555,GPF_ARGB,GPF_ABGR,GPF_RGB32}; int bps[] ={ 2, 2 , 4 , 4 , 4 }; ASSERT_EQ(0,GFXGetScreenSize(&width,&height)); ASSERT_GT(width*height,0); for(int i=0;i0) && (y+sh>0) && (x<(int)width) && (y<(int)height)) ASSERT_EQ(0,GFXBlit(hwsurface,x,y,layers[i],NULL)); else ASSERT_NE(0,GFXBlit(hwsurface,x,y,layers[i],NULL)); } } GFXFlip(hwsurface); } for(int i=0;i<4;i++) GFXDestroySurface(layers[i]); GFXDestroySurface(hwsurface); } TEST_F(GRAPH,FillRect){ HANDLE surface=0; GFXRect r; UINT width,height; ASSERT_EQ(0,GFXGetScreenSize(&width,&height)); ASSERT_EQ(0,GFXCreateSurface(&surface,width,height,GPF_ARGB,1)); ASSERT_NE((HANDLE)nullptr,surface); ASSERT_EQ(0xFFFF,FillCheckColor(surface,NULL,0)); setRect(r,0,0,width,height); ASSERT_EQ(0xFFFF,FillCheckColor(surface,&r,0xFF00000)); setRect(r,1,1,width-2,height-2); ASSERT_EQ(0xFFFF,FillCheckColor(surface,&r,0xFF00FF00)); setRect(r,10,10,width-200,height-200); ASSERT_EQ(0xFFFF,FillCheckColor(surface,&r,0xFF00FFFF)); setRect(r,10,10,width-200,height-200); ASSERT_EQ(0xFFFF,FillCheckColor(surface,&r,0x00000000)); GFXFlip(surface); ASSERT_EQ(0,GFXDestroySurface(surface)); } #define TEST_TIMES 2000 TEST_F(GRAPH,Benchmark_Fill){ HANDLE surface=0; UINT width,height,pitch; UINT *buffer; struct timeval t1,t2; GFXRect r={0,0}; GFXGetScreenSize(&width,&height); GFXCreateSurface(&surface,width,height,GPF_ARGB,1); r.w=width;r.h=height; gettimeofday(&t1,NULL); for(int i=0;imain surface GFXCreateSurface(&surface2,width,height,GPF_ARGB,0);//soft layer GFXFillRect(surface2,NULL,0xFFFF0000); gettimeofday(&t1,NULL); for(int i=0;i