rtgui_driver.c 20 KB

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