sdl_fb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #include <stdio.h>
  11. #ifdef _WIN32
  12. #include <sdl.h>
  13. #else
  14. #include <SDL2/SDL.h>
  15. #endif
  16. #include <rtdevice.h>
  17. #include <rtgui/driver.h>
  18. #define DBG_TAG "sdl.fb"
  19. #define DBG_LVL DBG_WARNING
  20. #include <rtdbg.h>
  21. #define SDL_SCREEN_WIDTH 480
  22. #define SDL_SCREEN_HEIGHT 320
  23. extern void rt_hw_exit(void);
  24. #define SDL_SCREEN_FORMAT SDL_PIXELFORMAT_RGB565
  25. struct sdlfb_device
  26. {
  27. struct rt_device parent;
  28. SDL_Renderer *renderer; /* window renderer */
  29. SDL_Surface *surface; /* screen surface */
  30. rt_uint16_t width;
  31. rt_uint16_t height;
  32. };
  33. struct sdlfb_device _device;
  34. /* common device interface */
  35. static rt_err_t sdlfb_init(rt_device_t dev)
  36. {
  37. return RT_EOK;
  38. }
  39. static rt_err_t sdlfb_open(rt_device_t dev, rt_uint16_t oflag)
  40. {
  41. return RT_EOK;
  42. }
  43. static rt_err_t sdlfb_close(rt_device_t dev)
  44. {
  45. SDL_Quit();
  46. return RT_EOK;
  47. }
  48. int sdlfb_info(int *format, int *bpp)
  49. {
  50. int bit_pp; /* texture bits per pixel */
  51. Uint32 Rmask, Gmask, Bmask, Amask; /* masks for pixel format passed into OpenGL */
  52. /* Grab info about format that will be passed into OpenGL */
  53. SDL_PixelFormatEnumToMasks(SDL_SCREEN_FORMAT, &bit_pp, &Rmask, &Gmask,
  54. &Bmask, &Amask);
  55. *bpp = bit_pp;
  56. switch (SDL_SCREEN_FORMAT)
  57. {
  58. case SDL_PIXELFORMAT_RGB565:
  59. *format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  60. break;
  61. case SDL_PIXELFORMAT_RGB888:
  62. *format = RTGRAPHIC_PIXEL_FORMAT_RGB888;
  63. break;
  64. case SDL_PIXELFORMAT_ARGB8888:
  65. *format = RTGRAPHIC_PIXEL_FORMAT_ARGB888;
  66. break;
  67. case SDL_PIXELFORMAT_ABGR8888:
  68. *format = RTGRAPHIC_PIXEL_FORMAT_ABGR888;
  69. }
  70. return 0;
  71. }
  72. static rt_mutex_t sdllock;
  73. static rt_err_t sdlfb_control(rt_device_t dev, int cmd, void *args)
  74. {
  75. struct sdlfb_device *device;
  76. rt_mutex_take(sdllock, RT_WAITING_FOREVER);
  77. device = (struct sdlfb_device *)dev;
  78. RT_ASSERT(device != RT_NULL);
  79. switch (cmd)
  80. {
  81. case RTGRAPHIC_CTRL_GET_INFO:
  82. {
  83. int format, bpp;
  84. struct rt_device_graphic_info *info;
  85. sdlfb_info(&format, &bpp);
  86. info = (struct rt_device_graphic_info *) args;
  87. info->bits_per_pixel = bpp;
  88. info->pixel_format = format;
  89. info->framebuffer = (rt_uint8_t*)device->surface->pixels;
  90. info->width = device->width;
  91. info->height = device->height;
  92. }
  93. break;
  94. case RTGRAPHIC_CTRL_RECT_UPDATE:
  95. {
  96. SDL_Texture * texture;
  97. struct rt_device_rect_info *rect;
  98. SDL_Rect _rect = { 0, 0, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT };
  99. rect = (struct rt_device_rect_info *)args;
  100. SDL_RenderClear(_device.renderer);
  101. texture = SDL_CreateTextureFromSurface(_device.renderer, _device.surface);
  102. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  103. SDL_RenderCopy(_device.renderer, texture, NULL, &_rect);
  104. SDL_RenderPresent(_device.renderer);
  105. SDL_DestroyTexture(texture);
  106. }
  107. break;
  108. case RTGRAPHIC_CTRL_SET_MODE:
  109. {
  110. break;
  111. }
  112. break;
  113. }
  114. rt_mutex_release(sdllock);
  115. return RT_EOK;
  116. }
  117. Uint16 pixels[16 * 16] = { // ...or with raw pixel data:
  118. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  119. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  120. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  121. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  122. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  123. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  124. 0x0fff, 0x0aab, 0x0789, 0x0bcc, 0x0eee, 0x09aa, 0x099a, 0x0ddd,
  125. 0x0fff, 0x0eee, 0x0899, 0x0fff, 0x0fff, 0x1fff, 0x0dde, 0x0dee,
  126. 0x0fff, 0xabbc, 0xf779, 0x8cdd, 0x3fff, 0x9bbc, 0xaaab, 0x6fff,
  127. 0x0fff, 0x3fff, 0xbaab, 0x0fff, 0x0fff, 0x6689, 0x6fff, 0x0dee,
  128. 0xe678, 0xf134, 0x8abb, 0xf235, 0xf678, 0xf013, 0xf568, 0xf001,
  129. 0xd889, 0x7abc, 0xf001, 0x0fff, 0x0fff, 0x0bcc, 0x9124, 0x5fff,
  130. 0xf124, 0xf356, 0x3eee, 0x0fff, 0x7bbc, 0xf124, 0x0789, 0x2fff,
  131. 0xf002, 0xd789, 0xf024, 0x0fff, 0x0fff, 0x0002, 0x0134, 0xd79a,
  132. 0x1fff, 0xf023, 0xf000, 0xf124, 0xc99a, 0xf024, 0x0567, 0x0fff,
  133. 0xf002, 0xe678, 0xf013, 0x0fff, 0x0ddd, 0x0fff, 0x0fff, 0xb689,
  134. 0x8abb, 0x0fff, 0x0fff, 0xf001, 0xf235, 0xf013, 0x0fff, 0xd789,
  135. 0xf002, 0x9899, 0xf001, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0xe789,
  136. 0xf023, 0xf000, 0xf001, 0xe456, 0x8bcc, 0xf013, 0xf002, 0xf012,
  137. 0x1767, 0x5aaa, 0xf013, 0xf001, 0xf000, 0x0fff, 0x7fff, 0xf124,
  138. 0x0fff, 0x089a, 0x0578, 0x0fff, 0x089a, 0x0013, 0x0245, 0x0eff,
  139. 0x0223, 0x0dde, 0x0135, 0x0789, 0x0ddd, 0xbbbc, 0xf346, 0x0467,
  140. 0x0fff, 0x4eee, 0x3ddd, 0x0edd, 0x0dee, 0x0fff, 0x0fff, 0x0dee,
  141. 0x0def, 0x08ab, 0x0fff, 0x7fff, 0xfabc, 0xf356, 0x0457, 0x0467,
  142. 0x0fff, 0x0bcd, 0x4bde, 0x9bcc, 0x8dee, 0x8eff, 0x8fff, 0x9fff,
  143. 0xadee, 0xeccd, 0xf689, 0xc357, 0x2356, 0x0356, 0x0467, 0x0467,
  144. 0x0fff, 0x0ccd, 0x0bdd, 0x0cdd, 0x0aaa, 0x2234, 0x4135, 0x4346,
  145. 0x5356, 0x2246, 0x0346, 0x0356, 0x0467, 0x0356, 0x0467, 0x0467,
  146. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  147. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  148. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  149. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff
  150. };
  151. static void sdlfb_hw_init(void)
  152. {
  153. SDL_Window *win;
  154. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  155. {
  156. fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  157. exit(1);
  158. }
  159. _device.parent.init = sdlfb_init;
  160. _device.parent.open = sdlfb_open;
  161. _device.parent.close = sdlfb_close;
  162. _device.parent.read = RT_NULL;
  163. _device.parent.write = RT_NULL;
  164. _device.parent.control = sdlfb_control;
  165. _device.width = SDL_SCREEN_WIDTH;
  166. _device.height = SDL_SCREEN_HEIGHT;
  167. {
  168. int bpp; /* texture bits per pixel */
  169. Uint32 Rmask, Gmask, Bmask, Amask; /* masks for pixel format passed into OpenGL */
  170. /* Grab info about format that will be passed into OpenGL */
  171. SDL_PixelFormatEnumToMasks(SDL_SCREEN_FORMAT, &bpp, &Rmask, &Gmask,
  172. &Bmask, &Amask);
  173. _device.surface = SDL_CreateRGBSurface(0, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT,
  174. bpp, Rmask, Gmask, Bmask, Amask);
  175. }
  176. win = SDL_CreateWindow("RT-Thread/Persimmon",
  177. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  178. SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
  179. if (win == NULL)
  180. {
  181. fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  182. exit(1);
  183. }
  184. /* set the icon of Window */
  185. {
  186. SDL_Surface *surface;
  187. surface = SDL_CreateRGBSurfaceFrom(pixels, 16, 16, 16, 16 * 2, 0x0f00, 0x00f0, 0x000f, 0xf000);
  188. SDL_SetWindowIcon(win, surface);
  189. }
  190. _device.renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
  191. rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);
  192. sdllock = rt_mutex_create("fb", RT_IPC_FLAG_PRIO);
  193. if (sdllock == RT_NULL)
  194. {
  195. LOG_E("Create mutex for sdlfb failed!");
  196. }
  197. }
  198. #ifdef _WIN32
  199. #include <windows.h>
  200. #include <mmsystem.h>
  201. #else
  202. #include <pthread.h>
  203. #include <signal.h>
  204. #endif
  205. #include <stdio.h>
  206. #include <rtgui/event.h>
  207. #include <rtgui/kbddef.h>
  208. #include <rtgui/rtgui_server.h>
  209. #include <rtgui/rtgui_system.h>
  210. #ifdef _WIN32
  211. static HANDLE sdl_ok_event = NULL;
  212. static DWORD WINAPI sdl_loop(LPVOID lpParam)
  213. #else
  214. static pthread_mutex_t sdl_ok_mutex;
  215. static pthread_cond_t sdl_ok_event;
  216. static void *sdl_loop(void *lpParam)
  217. #endif
  218. {
  219. int quit = 0;
  220. SDL_Event event;
  221. int state = 0;
  222. rt_device_t device;
  223. int timeout = 1000000;
  224. int last_x = -1, last_y = -1;
  225. int motion_x, motion_y;
  226. int motion_button = 0;
  227. int motion_tick = 50;
  228. int mouse_id = 1;
  229. #ifndef _WIN32
  230. sigset_t sigmask, oldmask;
  231. /* set the getchar without buffer */
  232. sigfillset(&sigmask);
  233. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  234. pthread_mutex_lock(&sdl_ok_mutex);
  235. #endif
  236. sdlfb_hw_init();
  237. device = rt_device_find("sdl");
  238. RT_ASSERT(device);
  239. rtgui_graphic_set_device(device);
  240. #ifdef _WIN32
  241. SetEvent(sdl_ok_event);
  242. #else
  243. pthread_cond_signal(&sdl_ok_event);
  244. pthread_mutex_unlock(&sdl_ok_mutex);
  245. #endif
  246. /* handle SDL event */
  247. while (!quit)
  248. {
  249. if (motion_button & RTGUI_MOUSE_BUTTON_DOWN)
  250. {
  251. timeout = motion_tick - SDL_GetTicks();
  252. }
  253. else
  254. {
  255. timeout = 1000 * 1000;
  256. }
  257. int tick = SDL_GetTicks();
  258. SDL_WaitEventTimeout(&event, timeout);
  259. if (SDL_GetTicks() >= motion_tick && (motion_button & RTGUI_MOUSE_BUTTON_DOWN)) /* whether timeout */
  260. {
  261. /* sendout motion event */
  262. struct rtgui_event_mouse emouse;
  263. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  264. emouse.parent.sender = RT_NULL;
  265. emouse.wid = RT_NULL;
  266. emouse.id = mouse_id;
  267. emouse.ts = rt_tick_get();
  268. if ((motion_x > 0 && motion_x < SDL_SCREEN_WIDTH) &&
  269. (motion_y > 0 && motion_y < SDL_SCREEN_HEIGHT))
  270. {
  271. emouse.x = motion_x;
  272. emouse.y = motion_y;
  273. /* init mouse button */
  274. emouse.button = motion_button;
  275. /* send event to server */
  276. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  277. }
  278. /* reset motion tick */
  279. motion_tick = 50 + SDL_GetTicks();
  280. }
  281. switch (event.type)
  282. {
  283. case SDL_MOUSEMOTION:
  284. {
  285. /* save to (x,y) in the motion */
  286. motion_x = ((SDL_MouseMotionEvent *)&event)->x;
  287. motion_y = ((SDL_MouseMotionEvent *)&event)->y;
  288. }
  289. break;
  290. case SDL_MOUSEBUTTONDOWN:
  291. case SDL_MOUSEBUTTONUP:
  292. {
  293. int x, y;
  294. struct rtgui_event_mouse emouse;
  295. SDL_MouseButtonEvent *mb;
  296. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  297. emouse.parent.sender = RT_NULL;
  298. emouse.wid = RT_NULL;
  299. if (event.type == SDL_MOUSEBUTTONDOWN && state == 0)
  300. mouse_id = rt_tick_get();
  301. emouse.id = mouse_id;
  302. emouse.ts = rt_tick_get();
  303. mb = (SDL_MouseButtonEvent *)&event;
  304. x = mb->x;
  305. y = mb->y;
  306. if ((x > 0 && x < SDL_SCREEN_WIDTH) &&
  307. (y > 0 && y < SDL_SCREEN_HEIGHT))
  308. {
  309. emouse.x = x;
  310. emouse.y = y;
  311. /* init mouse button */
  312. emouse.button = 0;
  313. /* set emouse button */
  314. if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
  315. {
  316. emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
  317. }
  318. else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
  319. {
  320. emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
  321. }
  322. else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
  323. {
  324. emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
  325. }
  326. if (mb->type == SDL_MOUSEBUTTONDOWN)
  327. {
  328. emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  329. motion_button = emouse.button;
  330. /* set motion timeout tick */
  331. motion_tick = 50 + SDL_GetTicks();
  332. if (state == 0)
  333. {
  334. /* send event to server */
  335. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  336. last_x = -1; last_y = -1;
  337. state = 1;
  338. }
  339. }
  340. else
  341. {
  342. emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  343. motion_button = 0;
  344. if (state == 1)
  345. {
  346. /* send event to server */
  347. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  348. last_x = -1; last_y = -1;
  349. state = 0;
  350. }
  351. }
  352. }
  353. }
  354. break;
  355. case SDL_KEYUP:
  356. {
  357. struct rtgui_event_kbd ekbd;
  358. ekbd.parent.type = RTGUI_EVENT_KBD;
  359. ekbd.parent.sender = RT_NULL;
  360. ekbd.type = RTGUI_KEYUP;
  361. ekbd.wid = RT_NULL;
  362. ekbd.mod = event.key.keysym.mod;
  363. ekbd.key = event.key.keysym.sym;
  364. /* FIXME: unicode */
  365. ekbd.unicode = 0;
  366. /* send event to server */
  367. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  368. }
  369. break;
  370. case SDL_KEYDOWN:
  371. {
  372. struct rtgui_event_kbd ekbd;
  373. ekbd.parent.type = RTGUI_EVENT_KBD;
  374. ekbd.parent.sender = RT_NULL;
  375. ekbd.type = RTGUI_KEYDOWN;
  376. ekbd.wid = RT_NULL;
  377. ekbd.mod = event.key.keysym.mod;
  378. ekbd.key = event.key.keysym.sym;
  379. /* FIXME: unicode */
  380. ekbd.unicode = 0;
  381. /* send event to server */
  382. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  383. }
  384. break;
  385. case SDL_QUIT:
  386. SDL_Quit();
  387. quit = 1;
  388. break;
  389. default:
  390. break;
  391. }
  392. if (quit)
  393. {
  394. exit(1);
  395. break;
  396. }
  397. }
  398. rt_hw_exit();
  399. return 0;
  400. }
  401. /* start sdl thread */
  402. void rt_hw_sdl_start(void)
  403. {
  404. #ifdef _WIN32
  405. HANDLE thread;
  406. DWORD thread_id;
  407. sdl_ok_event = CreateEvent(NULL,
  408. FALSE,
  409. FALSE,
  410. NULL);
  411. if (sdl_ok_event == NULL)
  412. {
  413. printf("error, create SDL event failed\n");
  414. exit(-1);
  415. }
  416. /* create thread that loop sdl event */
  417. thread = CreateThread(NULL,
  418. 0,
  419. (LPTHREAD_START_ROUTINE)sdl_loop,
  420. 0,
  421. CREATE_SUSPENDED,
  422. &thread_id);
  423. if (thread == NULL)
  424. {
  425. //Display Error Message
  426. return;
  427. }
  428. ResumeThread(thread);
  429. /* wait until SDL LCD device is registered and seted */
  430. WaitForSingleObject(sdl_ok_event, INFINITE);
  431. #else
  432. /* Linux */
  433. pthread_t pid;
  434. int res;
  435. pthread_mutex_init(&sdl_ok_mutex, NULL);
  436. pthread_cond_init(&sdl_ok_event, NULL);
  437. res = pthread_create(&pid, NULL, &sdl_loop, NULL);
  438. if (res)
  439. {
  440. printf("pthread create sdl thread faild, <%d>\n", res);
  441. exit(EXIT_FAILURE);
  442. }
  443. pthread_mutex_lock(&sdl_ok_mutex);
  444. pthread_cond_wait(&sdl_ok_event, &sdl_ok_mutex);
  445. pthread_mutex_unlock(&sdl_ok_mutex);
  446. pthread_mutex_destroy(&sdl_ok_mutex);
  447. pthread_cond_destroy(&sdl_ok_event);
  448. #endif
  449. }