rtgui_driver.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * File : driver.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-04 Bernard first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtgui/driver.h>
  16. #include <rtgui/region.h>
  17. #include <rtgui/rtgui_system.h>
  18. #include <string.h>
  19. extern const struct rtgui_graphic_driver_ops *rtgui_pixel_device_get_ops(int pixel_format);
  20. extern const struct rtgui_graphic_driver_ops *rtgui_framebuffer_get_ops(int pixel_format);
  21. static struct rtgui_graphic_driver _driver;
  22. static struct rtgui_graphic_driver *_current_driver = &_driver;
  23. #ifdef RTGUI_USING_VFRAMEBUFFER
  24. #ifndef RTGUI_VFB_PIXEL_FMT
  25. #define RTGUI_VFB_PIXEL_FMT RTGRAPHIC_PIXEL_FORMAT_RGB565
  26. #endif
  27. #include <rtgui/dc.h>
  28. static struct rtgui_graphic_driver _vfb_driver = {0};
  29. static void _graphic_driver_vmode_init(void)
  30. {
  31. if (_vfb_driver.width != _driver.width || _vfb_driver.height != _driver.height)
  32. {
  33. if (_vfb_driver.framebuffer != RT_NULL) rtgui_free((void*)_vfb_driver.framebuffer);
  34. _vfb_driver.device = RT_NULL;
  35. _vfb_driver.pixel_format = RTGUI_VFB_PIXEL_FMT;
  36. _vfb_driver.bits_per_pixel = rtgui_color_get_bits(RTGUI_VFB_PIXEL_FMT);
  37. _vfb_driver.width = _driver.width;
  38. _vfb_driver.height = _driver.height;
  39. _vfb_driver.pitch = _driver.width * _UI_BITBYTES(_vfb_driver.bits_per_pixel);
  40. _vfb_driver.framebuffer = rtgui_malloc(_vfb_driver.height * _vfb_driver.pitch);
  41. rt_memset(_vfb_driver.framebuffer, 0, _vfb_driver.height * _vfb_driver.pitch);
  42. _vfb_driver.ext_ops = RT_NULL;
  43. _vfb_driver.ops = rtgui_framebuffer_get_ops(_vfb_driver.pixel_format);
  44. }
  45. }
  46. void rtgui_graphic_driver_vmode_enter(void)
  47. {
  48. rtgui_screen_lock(RT_WAITING_FOREVER);
  49. _current_driver = &_vfb_driver;
  50. }
  51. RTM_EXPORT(rtgui_graphic_driver_vmode_enter);
  52. void rtgui_graphic_driver_vmode_exit(void)
  53. {
  54. _current_driver = &_driver;
  55. rtgui_screen_unlock();
  56. }
  57. RTM_EXPORT(rtgui_graphic_driver_vmode_exit);
  58. rt_bool_t rtgui_graphic_driver_is_vmode(void)
  59. {
  60. if (_current_driver == &_vfb_driver)
  61. return RT_TRUE;
  62. return RT_FALSE;
  63. }
  64. RTM_EXPORT(rtgui_graphic_driver_is_vmode);
  65. struct rtgui_dc*
  66. rtgui_graphic_driver_get_rect_buffer(const struct rtgui_graphic_driver *driver,
  67. struct rtgui_rect *r)
  68. {
  69. int w, h;
  70. struct rtgui_dc_buffer *buffer;
  71. rt_uint8_t *pixel, *dst;
  72. struct rtgui_rect src, rect;
  73. /* use virtual framebuffer in default */
  74. if (driver == RT_NULL) driver = _current_driver;
  75. if (r == RT_NULL)
  76. {
  77. rtgui_graphic_driver_get_rect(driver, &rect);
  78. }
  79. else
  80. {
  81. rtgui_graphic_driver_get_rect(driver, &src);
  82. rect = *r;
  83. rtgui_rect_intersect(&src, &rect);
  84. }
  85. w = rtgui_rect_width (rect);
  86. h = rtgui_rect_height(rect);
  87. if (!(w && h) || driver->framebuffer == RT_NULL)
  88. return RT_NULL;
  89. /* create buffer DC */
  90. buffer = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(driver->pixel_format, w, h);
  91. if (buffer == RT_NULL)
  92. return (struct rtgui_dc*)buffer;
  93. /* get source pixel */
  94. pixel = (rt_uint8_t*)driver->framebuffer
  95. + rect.y1 * driver->pitch
  96. + rect.x1 * rtgui_color_get_bpp(driver->pixel_format);
  97. dst = buffer->pixel;
  98. while (h--)
  99. {
  100. rt_memcpy(dst, pixel, buffer->pitch);
  101. dst += buffer->pitch;
  102. pixel += driver->pitch;
  103. }
  104. return (struct rtgui_dc*)buffer;
  105. }
  106. RTM_EXPORT(rtgui_graphic_driver_get_rect_buffer);
  107. #else
  108. rt_bool_t rtgui_graphic_driver_is_vmode(void)
  109. {
  110. return RT_FALSE;
  111. }
  112. RTM_EXPORT(rtgui_graphic_driver_is_vmode);
  113. #endif
  114. /* get default driver */
  115. struct rtgui_graphic_driver *rtgui_graphic_driver_get_default(void)
  116. {
  117. return _current_driver;
  118. }
  119. RTM_EXPORT(rtgui_graphic_driver_get_default);
  120. void rtgui_graphic_driver_get_rect(const struct rtgui_graphic_driver *driver, rtgui_rect_t *rect)
  121. {
  122. RT_ASSERT(rect != RT_NULL);
  123. /* use default driver */
  124. if (driver == RT_NULL)
  125. driver = _current_driver;
  126. rect->x1 = rect->y1 = 0;
  127. rect->x2 = driver->width;
  128. rect->y2 = driver->height;
  129. }
  130. RTM_EXPORT(rtgui_graphic_driver_get_rect);
  131. rt_err_t rtgui_graphic_set_device(rt_device_t device)
  132. {
  133. rt_err_t result;
  134. struct rt_device_graphic_info info;
  135. struct rtgui_graphic_ext_ops *ext_ops;
  136. /* get framebuffer address */
  137. result = rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &info);
  138. if (result != RT_EOK)
  139. {
  140. /* get device information failed */
  141. return -RT_ERROR;
  142. }
  143. /* if the first set graphic device */
  144. if (_driver.width == 0 || _driver.height == 0)
  145. {
  146. rtgui_rect_t rect;
  147. rtgui_get_mainwin_rect(&rect);
  148. if (rect.x2 == 0 || rect.y2 == 0)
  149. {
  150. rtgui_rect_init(&rect, 0, 0, info.width, info.height);
  151. /* re-set main-window */
  152. rtgui_set_mainwin_rect(&rect);
  153. }
  154. }
  155. /* initialize framebuffer driver */
  156. _driver.device = device;
  157. _driver.pixel_format = info.pixel_format;
  158. _driver.bits_per_pixel = info.bits_per_pixel;
  159. _driver.width = info.width;
  160. _driver.height = info.height;
  161. _driver.pitch = _driver.width * _UI_BITBYTES(_driver.bits_per_pixel);
  162. _driver.framebuffer = info.framebuffer;
  163. /* get graphic extension operations */
  164. result = rt_device_control(device, RTGRAPHIC_CTRL_GET_EXT, &ext_ops);
  165. if (result == RT_EOK)
  166. {
  167. _driver.ext_ops = ext_ops;
  168. }
  169. if (info.framebuffer != RT_NULL)
  170. {
  171. /* is a frame buffer device */
  172. _driver.ops = rtgui_framebuffer_get_ops(_driver.pixel_format);
  173. }
  174. else
  175. {
  176. /* is a pixel device */
  177. _driver.ops = rtgui_pixel_device_get_ops(_driver.pixel_format);
  178. }
  179. #ifdef RTGUI_USING_HW_CURSOR
  180. /* set default cursor image */
  181. rtgui_cursor_set_image(RTGUI_CURSOR_ARROW);
  182. #endif
  183. #ifdef RTGUI_USING_VFRAMEBUFFER
  184. _graphic_driver_vmode_init();
  185. #endif
  186. return RT_EOK;
  187. }
  188. RTM_EXPORT(rtgui_graphic_set_device);
  189. /* screen update */
  190. void rtgui_graphic_driver_screen_update(const struct rtgui_graphic_driver *driver, rtgui_rect_t *rect)
  191. {
  192. if (driver->device != RT_NULL)
  193. {
  194. struct rt_device_rect_info rect_info;
  195. rect_info.x = rect->x1;
  196. rect_info.y = rect->y1;
  197. rect_info.width = rect->x2 - rect->x1;
  198. rect_info.height = rect->y2 - rect->y1;
  199. rt_device_control(driver->device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  200. }
  201. }
  202. RTM_EXPORT(rtgui_graphic_driver_screen_update);
  203. void rtgui_graphic_driver_set_framebuffer(void *fb)
  204. {
  205. if (_current_driver)
  206. _current_driver->framebuffer = fb;
  207. else
  208. _driver.framebuffer = fb;
  209. }
  210. /* get video frame buffer */
  211. rt_uint8_t *rtgui_graphic_driver_get_framebuffer(const struct rtgui_graphic_driver *driver)
  212. {
  213. if (driver == RT_NULL) driver = _current_driver;
  214. return (rt_uint8_t *)driver->framebuffer;
  215. }
  216. RTM_EXPORT(rtgui_graphic_driver_get_framebuffer);
  217. /*
  218. * FrameBuffer type driver
  219. */
  220. #define GET_PIXEL(dst, x, y, type) \
  221. (type *)((rt_uint8_t*)((dst)->framebuffer) + (y) * (dst)->pitch + (x) * _UI_BITBYTES((dst)->bits_per_pixel))
  222. static void _rgb565_set_pixel(rtgui_color_t *c, int x, int y)
  223. {
  224. *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565(*c);
  225. }
  226. static void _rgb565_get_pixel(rtgui_color_t *c, int x, int y)
  227. {
  228. rt_uint16_t pixel;
  229. pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);
  230. /* get pixel from color */
  231. *c = rtgui_color_from_565(pixel);
  232. }
  233. static void _rgb565_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  234. {
  235. rt_ubase_t index;
  236. rt_uint16_t pixel;
  237. rt_uint16_t *pixel_ptr;
  238. /* get pixel from color */
  239. pixel = rtgui_color_to_565(*c);
  240. /* get pixel pointer in framebuffer */
  241. pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);
  242. for (index = x1; index < x2; index ++)
  243. {
  244. *pixel_ptr = pixel;
  245. pixel_ptr ++;
  246. }
  247. }
  248. static void _rgb565_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
  249. {
  250. struct rtgui_graphic_driver *drv;
  251. rt_uint8_t *dst;
  252. rt_uint16_t pixel;
  253. rt_ubase_t index;
  254. drv = rtgui_graphic_get_device();
  255. pixel = rtgui_color_to_565(*c);
  256. dst = GET_PIXEL(drv, x, y1, rt_uint8_t);
  257. for (index = y1; index < y2; index ++)
  258. {
  259. *(rt_uint16_t *)dst = pixel;
  260. dst += drv->pitch;
  261. }
  262. }
  263. static void _rgb565p_set_pixel(rtgui_color_t *c, int x, int y)
  264. {
  265. *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565p(*c);
  266. }
  267. static void _rgb565p_get_pixel(rtgui_color_t *c, int x, int y)
  268. {
  269. rt_uint16_t pixel;
  270. pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);
  271. /* get pixel from color */
  272. *c = rtgui_color_from_565p(pixel);
  273. }
  274. static void _rgb565p_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  275. {
  276. rt_ubase_t index;
  277. rt_uint16_t pixel;
  278. rt_uint16_t *pixel_ptr;
  279. /* get pixel from color */
  280. pixel = rtgui_color_to_565p(*c);
  281. /* get pixel pointer in framebuffer */
  282. pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);
  283. for (index = x1; index < x2; index ++)
  284. {
  285. *pixel_ptr = pixel;
  286. pixel_ptr ++;
  287. }
  288. }
  289. static void _rgb565p_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
  290. {
  291. struct rtgui_graphic_driver *drv;
  292. rt_uint8_t *dst;
  293. rt_uint16_t pixel;
  294. rt_ubase_t index;
  295. drv = rtgui_graphic_get_device();
  296. pixel = rtgui_color_to_565p(*c);
  297. dst = GET_PIXEL(drv, x, y1, rt_uint8_t);
  298. for (index = y1; index < y2; index ++)
  299. {
  300. *(rt_uint16_t *)dst = pixel;
  301. dst += drv->pitch;
  302. }
  303. }
  304. /* draw raw hline */
  305. static void framebuffer_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
  306. {
  307. struct rtgui_graphic_driver *drv;
  308. rt_uint8_t *dst;
  309. drv = rtgui_graphic_get_device();
  310. dst = GET_PIXEL(drv, x1, y, rt_uint8_t);
  311. rt_memcpy(dst, pixels,
  312. (x2 - x1) * _UI_BITBYTES(drv->bits_per_pixel));
  313. }
  314. const struct rtgui_graphic_driver_ops _framebuffer_rgb565_ops =
  315. {
  316. _rgb565_set_pixel,
  317. _rgb565_get_pixel,
  318. _rgb565_draw_hline,
  319. _rgb565_draw_vline,
  320. framebuffer_draw_raw_hline,
  321. };
  322. const struct rtgui_graphic_driver_ops _framebuffer_rgb565p_ops =
  323. {
  324. _rgb565p_set_pixel,
  325. _rgb565p_get_pixel,
  326. _rgb565p_draw_hline,
  327. _rgb565p_draw_vline,
  328. framebuffer_draw_raw_hline,
  329. };
  330. #define FRAMEBUFFER (drv->framebuffer)
  331. #define MONO_PIXEL(framebuffer, x, y) \
  332. ((rt_uint8_t**)(framebuffer))[y/8][x]
  333. static void _mono_set_pixel(rtgui_color_t *c, int x, int y)
  334. {
  335. struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
  336. if (*c == white)
  337. MONO_PIXEL(FRAMEBUFFER, x, y) &= ~(1 << (y % 8));
  338. else
  339. MONO_PIXEL(FRAMEBUFFER, x, y) |= (1 << (y % 8));
  340. }
  341. static void _mono_get_pixel(rtgui_color_t *c, int x, int y)
  342. {
  343. struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
  344. if (MONO_PIXEL(FRAMEBUFFER, x, y) & (1 << (y % 8)))
  345. *c = black;
  346. else
  347. *c = white;
  348. }
  349. static void _mono_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  350. {
  351. struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
  352. rt_ubase_t index;
  353. if (*c == white)
  354. for (index = x1; index < x2; index ++)
  355. {
  356. MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y % 8));
  357. }
  358. else
  359. for (index = x1; index < x2; index ++)
  360. {
  361. MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y % 8));
  362. }
  363. }
  364. static void _mono_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
  365. {
  366. struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
  367. rt_ubase_t index;
  368. if (*c == white)
  369. for (index = y1; index < y2; index ++)
  370. {
  371. MONO_PIXEL(FRAMEBUFFER, x, index) &= ~(1 << (index % 8));
  372. }
  373. else
  374. for (index = y1; index < y2; index ++)
  375. {
  376. MONO_PIXEL(FRAMEBUFFER, x, index) |= (1 << (index % 8));
  377. }
  378. }
  379. /* draw raw hline */
  380. static void _mono_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
  381. {
  382. struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
  383. rt_ubase_t index;
  384. for (index = x1; index < x2; index ++)
  385. {
  386. if (pixels[index / 8] && (1 << (index % 8)))
  387. MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y % 8));
  388. else
  389. MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y % 8));
  390. }
  391. }
  392. const struct rtgui_graphic_driver_ops _framebuffer_mono_ops =
  393. {
  394. _mono_set_pixel,
  395. _mono_get_pixel,
  396. _mono_draw_hline,
  397. _mono_draw_vline,
  398. _mono_draw_raw_hline,
  399. };
  400. const struct rtgui_graphic_driver_ops *rtgui_framebuffer_get_ops(int pixel_format)
  401. {
  402. switch (pixel_format)
  403. {
  404. case RTGRAPHIC_PIXEL_FORMAT_MONO:
  405. return &_framebuffer_mono_ops;
  406. case RTGRAPHIC_PIXEL_FORMAT_GRAY4:
  407. break;
  408. case RTGRAPHIC_PIXEL_FORMAT_GRAY16:
  409. break;
  410. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  411. return &_framebuffer_rgb565_ops;
  412. case RTGRAPHIC_PIXEL_FORMAT_RGB565P:
  413. return &_framebuffer_rgb565p_ops;
  414. default:
  415. RT_ASSERT(0);
  416. break;
  417. }
  418. return RT_NULL;
  419. }
  420. /*
  421. * Pixel type driver
  422. */
  423. #define gfx_device (rtgui_graphic_get_device()->device)
  424. #define gfx_device_ops rt_graphix_ops(gfx_device)
  425. static void _pixel_mono_set_pixel(rtgui_color_t *c, int x, int y)
  426. {
  427. rt_uint8_t pixel;
  428. pixel = rtgui_color_to_mono(*c);
  429. gfx_device_ops->set_pixel((char *)&pixel, x, y);
  430. }
  431. static void _pixel_rgb565p_set_pixel(rtgui_color_t *c, int x, int y)
  432. {
  433. rt_uint16_t pixel;
  434. pixel = rtgui_color_to_565p(*c);
  435. gfx_device_ops->set_pixel((char *)&pixel, x, y);
  436. }
  437. static void _pixel_rgb565_set_pixel(rtgui_color_t *c, int x, int y)
  438. {
  439. rt_uint16_t pixel;
  440. pixel = rtgui_color_to_565(*c);
  441. gfx_device_ops->set_pixel((char *)&pixel, x, y);
  442. }
  443. static void _pixel_rgb888_set_pixel(rtgui_color_t *c, int x, int y)
  444. {
  445. rt_uint32_t pixel;
  446. pixel = rtgui_color_to_888(*c);
  447. gfx_device_ops->set_pixel((char *)&pixel, x, y);
  448. }
  449. static void _pixel_mono_get_pixel(rtgui_color_t *c, int x, int y)
  450. {
  451. rt_uint8_t pixel;
  452. gfx_device_ops->get_pixel((char *)&pixel, x, y);
  453. *c = rtgui_color_from_mono(pixel);
  454. }
  455. static void _pixel_rgb565p_get_pixel(rtgui_color_t *c, int x, int y)
  456. {
  457. rt_uint16_t pixel;
  458. gfx_device_ops->get_pixel((char *)&pixel, x, y);
  459. *c = rtgui_color_from_565p(pixel);
  460. }
  461. static void _pixel_rgb565_get_pixel(rtgui_color_t *c, int x, int y)
  462. {
  463. rt_uint16_t pixel;
  464. gfx_device_ops->get_pixel((char *)&pixel, x, y);
  465. *c = rtgui_color_from_565(pixel);
  466. }
  467. static void _pixel_rgb888_get_pixel(rtgui_color_t *c, int x, int y)
  468. {
  469. rt_uint32_t pixel;
  470. gfx_device_ops->get_pixel((char *)&pixel, x, y);
  471. *c = rtgui_color_from_888(pixel);
  472. }
  473. static void _pixel_mono_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  474. {
  475. rt_uint8_t pixel;
  476. pixel = rtgui_color_to_mono(*c);
  477. gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
  478. }
  479. static void _pixel_rgb565p_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  480. {
  481. rt_uint16_t pixel;
  482. pixel = rtgui_color_to_565p(*c);
  483. gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
  484. }
  485. static void _pixel_rgb565_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  486. {
  487. rt_uint16_t pixel;
  488. pixel = rtgui_color_to_565(*c);
  489. gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
  490. }
  491. static void _pixel_rgb888_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
  492. {
  493. rt_uint32_t pixel;
  494. pixel = rtgui_color_to_888(*c);
  495. gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
  496. }
  497. static void _pixel_mono_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
  498. {
  499. rt_uint8_t pixel;
  500. pixel = rtgui_color_to_mono(*c);
  501. gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
  502. }
  503. static void _pixel_rgb565p_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
  504. {
  505. rt_uint16_t pixel;
  506. pixel = rtgui_color_to_565p(*c);
  507. gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
  508. }
  509. static void _pixel_rgb565_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
  510. {
  511. rt_uint16_t pixel;
  512. pixel = rtgui_color_to_565(*c);
  513. gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
  514. }
  515. static void _pixel_rgb888_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
  516. {
  517. rt_uint32_t pixel;
  518. pixel = rtgui_color_to_888(*c);
  519. gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
  520. }
  521. static void _pixel_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
  522. {
  523. if (x2 > x1)
  524. gfx_device_ops->blit_line((char *)pixels, x1, y, (x2 - x1));
  525. else
  526. gfx_device_ops->blit_line((char *)pixels, x2, y, (x1 - x2));
  527. }
  528. const struct rtgui_graphic_driver_ops _pixel_mono_ops =
  529. {
  530. _pixel_mono_set_pixel,
  531. _pixel_mono_get_pixel,
  532. _pixel_mono_draw_hline,
  533. _pixel_mono_draw_vline,
  534. _pixel_draw_raw_hline,
  535. };
  536. const struct rtgui_graphic_driver_ops _pixel_rgb565p_ops =
  537. {
  538. _pixel_rgb565p_set_pixel,
  539. _pixel_rgb565p_get_pixel,
  540. _pixel_rgb565p_draw_hline,
  541. _pixel_rgb565p_draw_vline,
  542. _pixel_draw_raw_hline,
  543. };
  544. const struct rtgui_graphic_driver_ops _pixel_rgb565_ops =
  545. {
  546. _pixel_rgb565_set_pixel,
  547. _pixel_rgb565_get_pixel,
  548. _pixel_rgb565_draw_hline,
  549. _pixel_rgb565_draw_vline,
  550. _pixel_draw_raw_hline,
  551. };
  552. const struct rtgui_graphic_driver_ops _pixel_rgb888_ops =
  553. {
  554. _pixel_rgb888_set_pixel,
  555. _pixel_rgb888_get_pixel,
  556. _pixel_rgb888_draw_hline,
  557. _pixel_rgb888_draw_vline,
  558. _pixel_draw_raw_hline,
  559. };
  560. const struct rtgui_graphic_driver_ops *rtgui_pixel_device_get_ops(int pixel_format)
  561. {
  562. switch (pixel_format)
  563. {
  564. case RTGRAPHIC_PIXEL_FORMAT_MONO:
  565. return &_pixel_mono_ops;
  566. case RTGRAPHIC_PIXEL_FORMAT_RGB565:
  567. return &_pixel_rgb565_ops;
  568. case RTGRAPHIC_PIXEL_FORMAT_RGB565P:
  569. return &_pixel_rgb565p_ops;
  570. case RTGRAPHIC_PIXEL_FORMAT_RGB888:
  571. return &_pixel_rgb888_ops;
  572. }
  573. return RT_NULL;
  574. }
  575. /*
  576. * Hardware cursor
  577. */
  578. #ifdef RTGUI_USING_HW_CURSOR
  579. void rtgui_cursor_set_position(rt_uint16_t x, rt_uint16_t y)
  580. {
  581. rt_uint32_t value;
  582. if (_current_driver->device != RT_NULL)
  583. {
  584. value = (x << 16 | y);
  585. rt_device_control(_driver.device, RT_DEVICE_CTRL_CURSOR_SET_POSITION, &value);
  586. }
  587. }
  588. void rtgui_cursor_set_image(enum rtgui_cursor_type type)
  589. {
  590. rt_uint32_t value;
  591. if (_current_driver->device != RT_NULL)
  592. {
  593. value = type;
  594. rt_device_control(_driver.device, RT_DEVICE_CTRL_CURSOR_SET_TYPE, &value);
  595. }
  596. };
  597. #endif