add graph_sdl

This commit is contained in:
houzh 2023-03-07 14:01:14 +00:00
parent 224dce484e
commit 155bdb1887
3 changed files with 335 additions and 29 deletions

26
cmake/modules/FindSDL2.cmake Executable file
View File

@ -0,0 +1,26 @@
include(FindPkgConfig)
pkg_check_modules(PC_SDL sdl2)
find_path(SDL2_INCLUDE_DIRS
NAMES SDL.h
HINTS ${PC_SDL_INCLUDEDIR}
${PC_SDL_INCLUDE_DIRS}
)
find_library(SDL2_LIBRARIES
NAMES SDL2
HINTS ${PC_SDL_LIBDIR}
${PC_SDL_LIBRARY_DIRS}
)
if(SDL2_INCLUDE_DIRS AND SDL2_LIBRARIES)
set(SDL2_FOUND TRUE)
set(SDL2_LIBRARY ${SDL2_LIBRARIES})
set(SDL2_INCLUDE_DIR ${SDL2_INCLUDE_DIRS})
set(SDL2_VERSION ${PC_SDL_VERSION})
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(sdl2 FOUND_VAR SDL2_FOUND VERSION_VAR SDL2_VERSION
REQUIRED_VARS SDL2_INCLUDE_DIRS SDL2_INCLUDE_DIR SDL2_LIBRARIES SDL2_LIBRARY)
mark_as_advanced(SDL2_LIBRARIES SDL2_LIBRARY SDL2_INCLUDE_DIRS SDL2_INCLUDE_DIR SDL2_VERSION)
endif()

View File

@ -13,14 +13,17 @@ if (ENABLE_DTV)
ngl_disp.c ngl_os.c
)
endif()
include(CheckIncludeFile)
check_include_file(poll.h HAVE_POLL_H)
check_include_file(sys/epoll.h HAVE_EPOLL_H)
check_include_file(linux/input.h HAVE_INPUT_H)
check_include_file(execinfo.h HAVE_EXECINFO_H)
check_include_file(drm.h HAVE_DRM_H)
find_package(PkgConfig)
pkg_check_modules(PC_DRM dri)
find_package(SDL2 REQUIRED)
find_path(
DRM_INCLUDE_DIR
@ -36,7 +39,7 @@ find_library(DRM_LIBRARY
)
if(DRM_LIBRARY AND DRM_INCLUDE_DIR)
list(APPEND X64LIBS ${DRM_LIBRARY})
list(APPEND X64_LIBRARIES ${DRM_LIBRARY})
endif()
if(HAVE_POLL_H)
@ -61,44 +64,37 @@ if( ENABLE_RFB )
endif()
string(TOLOWER ${CMAKE_SYSTEM_NAME} SYSNAME_LOWER)
if ( SYSNAME_LOWER MATCHES "linux" OR SYSNAME_LOWER MATCHES "cygwin" )
find_package(X11)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 gtk+-3.0)
message("X11_INCLUDE_DIR=${X11_INCLUDE_DIR} ${X11_FOUND} ${GTK3_LIBRARIES} ${GTK3_FOUND} ${}")
if (FALSE AND DRM_LIBRARY)
list(APPEND X64_SRCS gfxdrm.cc graph_drm.cc)
list(APPEND X64LIBS ${DRM_LIBRARY})
elseif(FALSE)
list(APPEND X64_SRCS graph_fb.c)
elseif( X11_FOUND )
list(APPEND X64_SRCS graph_xlib.c)
list(APPEND X64LIBS ${X11_LIBRARIES})
elseif( GTK3_FOUND )
list(APPEND X64_SRCS graph_gtk.c)
list(APPEND X64LIBS ${GTK3_LIBRARIES} ${DRM_LIBRARY})
else()
list(APPEND X64_SRCS graph_rfb.c)
list(APPEND X64LIBS vncserver)
endif()
elseif (WIN32)
list(APPEND X64_SRCS graph_win32.c)
list(APPEND X64LIBS win32.lib)
if (SDL2_FOUND)
list(APPEND X64_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
list(APPEND X64_LIBRARIES ${SDL2_LIBRARIES})
list(APPEND X64_SRCS graph_sdl.c)
endif()
list(APPEND X64LIBS pthread)
find_package(X11)
find_package(PkgConfig REQUIRED)
if( X11_FOUND AND NOT SDL2_FOUND)
list(APPEND X64_SRCS graph_xlib.c)
list(APPEND X64_LIBRARIES ${X11_LIBRARIES})
list(APPEND X64_INCLUDE_DIRS ${X11_INCLUDE_DIR})
endif()
if(NOT X64_LIBRARIES)
list(APPEND X64_SRCS graph_rfb.c)
list(APPEND X64LIBS vncserver)
endif()
list(APPEND X64)LIBRARIES pthread)
find_package(ZLIB)
include_directories(./
../common ../include
${CMAKE_SOURCE_DIR}/src/gui/
${CMAKE_SOURCE_DIR}/src/gui/core
${DRM_INCLUDE_DIR}
${X11_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIR}
${X64_INCLUDE_DIRS}
)
message("CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR}")
message("CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR} X64_SRCS=${X64_SRCS}")
add_library(tvhal SHARED ${X64_SRCS} )
target_link_libraries(tvhal PRIVATE ${X64LIBS})
target_link_libraries(tvhal PRIVATE ${X64_LIBRARIES})
set(PREFIX ${CMAKE_INSTALL_PREFIX})
set(NONPKG_LIBS "-L${CMAKE_INSTALL_PREFIX}/lib")

284
src/porting/x64/graph_sdl.c Executable file
View File

@ -0,0 +1,284 @@
#include <cdtypes.h>
#include <cdgraph.h>
#include <cdlog.h>
#include <SDL.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <core/eventcodes.h>
#include <cdinput.h>
static SDL_Window *sdlWindow = NULL;
static SDL_Renderer *sdlRenderer = NULL;
typedef struct{
int dispid;
UINT width;
UINT height;
UINT pitch;
int format;
int ishw;
SDL_Surface*surface;
SDL_Texture*texture;
}FBSURFACE;
static int SDLProc(void*params);
static void SendKeyEvent(int type,SDL_KeyboardEvent*event);
static void SendMouseEvent(int type,SDL_MouseMotionEvent*event);
INT GFXInit(){
if(sdlWindow)return E_OK;
SDL_Init(SDL_INIT_EVERYTHING);
sdlWindow = SDL_CreateWindow("CDROID Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_OPENGL /*| SDL_WINDOW_RESIZABLE*/);
if (!sdlWindow) return E_ERROR;
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0);//基于窗口创建渲染器
LOGI("sdlWindow=%p sdlRenderer=%p",sdlWindow,sdlRenderer);
SDL_CreateThread(SDLProc,NULL,NULL);
return E_OK;
}
INT GFXGetDisplayCount(){
return 1;
}
INT GFXGetDisplaySize(int dispid,UINT*width,UINT*height){
if(dispid<0||dispid>=GFXGetDisplayCount())return E_ERROR;
SDL_GetWindowSize(sdlWindow,(int*)width,(int*)height);
LOGD("screensize=%dx%d",*width,*height);
return E_OK;
}
GFX_ROTATION GFXGetRotation(int dispid){
return ROTATE_0;
}
INT GFXSetRotation(int dispid,GFX_ROTATION r){
return 0;
}
INT GFXLockSurface(HANDLE surface,void**buffer,UINT*pitch){
FBSURFACE*ngs=(FBSURFACE*)surface;
*buffer=ngs->surface->pixels;
*pitch=ngs->pitch;
return 0;
}
INT GFXGetSurfaceInfo(HANDLE surface,UINT*width,UINT*height,INT *format){
FBSURFACE*ngs=(FBSURFACE*)surface;
*width = ngs->width;
*height= ngs->height;
*format= ngs->format;
return E_OK;
}
INT GFXUnlockSurface(HANDLE surface){
return 0;
}
INT GFXSurfaceSetOpacity(HANDLE surface,BYTE alpha){
return 0;//dispLayer->SetOpacity(dispLayer,alpha);
}
INT GFXFillRect(HANDLE surface,const GFXRect*rect,UINT color){
FBSURFACE*ngs=(FBSURFACE*)surface;
UINT x,y;
GFXRect rec={0,0,0,0};
rec.w=ngs->width;
rec.h=ngs->height;
if(rect)rec=*rect;
LOGV("FillRect %p %d,%d-%d,%d color=0x%x pitch=%d",ngs,rec.x,rec.y,rec.w,rec.h,color,ngs->pitch);
SDL_FillRect(ngs->surface,(SDL_Rect*)&rec,color);
return E_OK;
}
INT GFXFlip(HANDLE surface){
FBSURFACE*surf=(FBSURFACE*)surface;
if(surf->ishw){
GFXRect rect={0,0,surf->width,surf->height};
//if(rc)rect=*rc;
}
return 0;
}
INT GFXCreateSurface(int dispid,HANDLE*surface,UINT width,UINT height,INT format,BOOL hwsurface){
FBSURFACE*surf=(FBSURFACE*)malloc(sizeof(FBSURFACE));
surf->dispid=dispid;
surf->width=width;
surf->height=height;
surf->format=format;
surf->ishw=hwsurface;
surf->pitch=width*4;
size_t buffer_size=surf->height*surf->pitch;
UINT rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
surf->surface=SDL_CreateRGBSurface(SDL_SWSURFACE,width, height, 32,rmask, gmask, bmask, amask);
surf->texture=SDL_CreateTextureFromSurface(sdlRenderer,surf->surface);
LOGV("surface=%x buf=%p size=%dx%d hw=%d",surf,surf->surface->pixels,width,height,hwsurface);
*surface=surf;
return E_OK;
}
INT GFXBlit(HANDLE dstsurface,int dx,int dy,HANDLE srcsurface,const GFXRect*srcrect){
unsigned int x,y,sw,sh;
FBSURFACE*ndst=(FBSURFACE*)dstsurface;
FBSURFACE*nsrc=(FBSURFACE*)srcsurface;
GFXRect rs={0,0};
BYTE*pbs=(BYTE*)nsrc->surface->pixels;//buffer;
BYTE*pbd=(BYTE*)ndst->surface->pixels;//buffer;
rs.w=nsrc->width;rs.h=nsrc->height;
if(srcrect)rs=*srcrect;
if(((int)rs.w+dx<=0)||((int)rs.h+dy<=0)||(dx>=(int)ndst->width)||(dy>=(int)ndst->height)||(rs.x<0)||(rs.y<0)){
LOGV("dx=%d,dy=%d rs=(%d,%d-%d,%d)",dx,dy,rs.x,rs.y,rs.w,rs.h);
return E_INVALID_PARA;
}
LOGV("Blit %p[%dx%d] %d,%d-%d,%d -> %p[%dx%d] %d,%d",nsrc,nsrc->width,nsrc->height,
rs.x,rs.y,rs.w,rs.h,ndst,ndst->width,ndst->height,dx,dy);
if(dx<0){rs.x-=dx;rs.w=(int)rs.w+dx; dx=0;}
if(dy<0){rs.y-=dy;rs.h=(int)rs.h+dy;dy=0;}
if(dx+rs.w>ndst->width)rs.w=ndst->width-dx;
if(dy+rs.h>ndst->height)rs.h=ndst->height-dy;
LOGV("Blit %p %d,%d-%d,%d -> %p %d,%d buffer=%p->%p",nsrc,rs.x,rs.y,rs.w,rs.h,ndst,dx,dy,pbs,pbd);
pbs+=rs.y*nsrc->pitch+rs.x*4;
pbd+=dy*ndst->pitch+dx*4;
const int cpw=rs.w*4;
for(y=0;y<rs.h;y++){
memcpy(pbd,pbs,cpw);
pbs+=nsrc->pitch;
pbd+=ndst->pitch;
}
SDL_BlitSurface(nsrc->surface,(const SDL_Rect *)&rs,ndst->surface,(const SDL_Rect *)&rs);
if(ndst->ishw)
SDL_RenderCopy(sdlRenderer,ndst->texture,(const SDL_Rect *)&rs,(const SDL_Rect *)&rs);
return 0;
}
INT GFXDestroySurface(HANDLE surface){
FBSURFACE*surf=(FBSURFACE*)surface;
SDL_FreeSurface(surf->surface);
free(surf);
return 0;
}
#define SENDMOUSE(x,y) {InjectABS(EV_ABS,0,x);\
InjectABS(EV_ABS,1,y);InjectABS(EV_SYN,SYN_REPORT,0);}
static int SDLProc(void*params){
int running=1;
while(running){
SDL_Event event;
if(SDL_WaitEventTimeout(&event,5)==0)continue;
switch(event.type){
case SDL_QUIT:running=0;break;
case SDL_KEYDOWN:
case SDL_KEYUP :SendKeyEvent(event.type,&event.key); break;
case SDL_MOUSEMOTION://SendMouseEvent(event.type,&event.motion);break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:SendMouseEvent(event.type,&event.motion);break;
case SDL_MOUSEWHEEL:event.wheel;break;
case SDL_FINGERDOWN:event.tfinger;break;
case SDL_FINGERUP :event.tfinger;break;
case SDL_FINGERMOTION:event.tfinger;break;
case SDL_DISPLAYEVENT:break;
}
}
}
static void SendMouseEvent(int type,SDL_MouseMotionEvent*event){
InjectEvent(EV_ABS,0,event->x);
InjectEvent(EV_ABS,1,event->y);
if((type==SDL_MOUSEBUTTONDOWN)||(type==SDL_MOUSEBUTTONUP))
InjectABS(EV_KEY,BTN_TOUCH,0);
InjectABS(EV_SYN,SYN_REPORT,0);
}
static void SendKeyEvent(int type,SDL_KeyboardEvent*event){
int keycode=event->keysym.sym;
switch(event->keysym.sym){
case SDLK_0:keycode=KEY_0;break;
case SDLK_1:keycode=KEY_1;break;
case SDLK_2:keycode=KEY_2;break;
case SDLK_3:keycode=KEY_3;break;
case SDLK_4:keycode=KEY_4;break;
case SDLK_5:keycode=KEY_5;break;
case SDLK_6:keycode=KEY_6;break;
case SDLK_7:keycode=KEY_7;break;
case SDLK_8:keycode=KEY_8;break;
case SDLK_9:keycode=KEY_9;break;
case SDLK_F1 :keycode=KEY_F1 ;break;
case SDLK_F2 :keycode=KEY_F2 ;break;
case SDLK_F3 :keycode=KEY_F3 ;break;
case SDLK_F4 :keycode=KEY_F4 ;break;
case SDLK_F5 :keycode=KEY_F5 ;break;
case SDLK_F6 :keycode=KEY_F6 ;break;
case SDLK_F7 :keycode=KEY_F7 ;break;
case SDLK_F8 :keycode=KEY_F8 ;break;
case SDLK_F9 :keycode=KEY_F9 ;break;
case SDLK_F10:keycode=KEY_F10;break;
case SDLK_F11:keycode=KEY_F11;break;
case SDLK_F12:keycode=KEY_F12;break;
case SDLK_TAB:keycode=KEY_TAB;break;
case SDLK_ESCAPE:keycode=KEY_ESCAPE;break;
case SDLK_RETURN:keycode=KEY_ENTER;break;
case SDLK_PAGEDOWN:keycode=KEY_PAGEDOWN;break;
case SDLK_PAGEUP:keycode=KEY_PAGEUP;break;
case SDLK_LEFT:keycode=KEY_LEFT;break;
case SDLK_RIGHT:keycode=KEY_RIGHT;break;
case SDLK_UP:keycode=KEY_UP;break;
case SDLK_DOWN:keycode=KEY_DOWN;break;
case SDLK_MENU:keycode=KEY_MENU;break;
case SDLK_POWER:keycode=KEY_POWER;break;
case SDLK_DELETE:keycode=KEY_DELETE;break;
case SDLK_BACKSPACE:keycode=KEY_BACKSPACE;break;
case SDLK_VOLUMEDOWN:keycode=KEY_VOLUMEDOWN;break;
case SDLK_VOLUMEUP:keycode=KEY_VOLUMEUP;break;
case SDLK_MUTE:keycode=KEY_MUTE;break;
case SDLK_a:keycode=KEY_A;break;
case SDLK_b:keycode=KEY_B;break;
case SDLK_c:keycode=KEY_C;break;
case SDLK_d:keycode=KEY_D;break;
case SDLK_e:keycode=KEY_E;break;
case SDLK_f:keycode=KEY_F;break;
case SDLK_g:keycode=KEY_G;break;
case SDLK_h:keycode=KEY_H;break;
case SDLK_i:keycode=KEY_I;break;
case SDLK_j:keycode=KEY_J;break;
case SDLK_k:keycode=KEY_K;break;
case SDLK_l:keycode=KEY_L;break;
case SDLK_m:keycode=KEY_M;break;
case SDLK_n:keycode=KEY_N;break;
case SDLK_o:keycode=KEY_O;break;
case SDLK_p:keycode=KEY_P;break;
case SDLK_q:keycode=KEY_Q;break;
case SDLK_r:keycode=KEY_R;break;
case SDLK_s:keycode=KEY_S;break;
case SDLK_t:keycode=KEY_T;break;
case SDLK_u:keycode=KEY_U;break;
case SDLK_v:keycode=KEY_V;break;
case SDLK_w:keycode=KEY_W;break;
case SDLK_x:keycode=KEY_X;break;
case SDLK_y:keycode=KEY_Y;break;
case SDLK_z:keycode=KEY_Z;break;
default:return;
}
InjectKey(EV_KEY,keycode,(type==SDL_KEYDOWN));
}