graphic.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #ifndef __GRAPHIC_DM_H__
  11. #define __GRAPHIC_DM_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <drivers/lcd.h>
  15. #include <drivers/core/dm.h>
  16. #include <drivers/byteorder.h>
  17. #include <drivers/classes/graphic.h>
  18. #undef rt_graphix_ops
  19. #define rt_graphix_ops(dev) \
  20. rt_graphic_device_switch_primary(rt_container_of(dev, struct rt_graphic_device, parent))
  21. rt_packed(struct est_timings
  22. {
  23. rt_uint8_t t1;
  24. rt_uint8_t t2;
  25. rt_uint8_t mfg_rsvd;
  26. });
  27. rt_packed(struct std_timing
  28. {
  29. /* Need to multiply by 8 then add 248 */
  30. rt_uint8_t hsize;
  31. rt_uint8_t vfreq_aspect;
  32. });
  33. rt_packed(struct detailed_pixel_timing
  34. {
  35. rt_uint8_t hactive_lo;
  36. rt_uint8_t hblank_lo;
  37. rt_uint8_t hactive_hblank_hi;
  38. rt_uint8_t vactive_lo;
  39. rt_uint8_t vblank_lo;
  40. rt_uint8_t vactive_vblank_hi;
  41. rt_uint8_t hsync_offset_lo;
  42. rt_uint8_t hsync_pulse_width_lo;
  43. rt_uint8_t vsync_offset_pulse_width_lo;
  44. rt_uint8_t hsync_vsync_offset_pulse_width_hi;
  45. rt_uint8_t width_mm_lo;
  46. rt_uint8_t height_mm_lo;
  47. rt_uint8_t width_height_mm_hi;
  48. rt_uint8_t hborder;
  49. rt_uint8_t vborder;
  50. rt_uint8_t misc;
  51. });
  52. rt_packed(struct detailed_data_string
  53. {
  54. rt_uint8_t str[13];
  55. });
  56. rt_packed(struct detailed_data_monitor_range
  57. {
  58. rt_uint8_t min_vfreq;
  59. rt_uint8_t max_vfreq;
  60. rt_uint8_t min_hfreq_khz;
  61. rt_uint8_t max_hfreq_khz;
  62. /* Need to multiply by 10 */
  63. rt_uint8_t pixel_clock_mhz;
  64. rt_uint8_t flags;
  65. union
  66. {
  67. rt_packed(struct
  68. {
  69. rt_uint8_t reserved;
  70. /* Need to multiply by 2 */
  71. rt_uint8_t hfreq_start_khz;
  72. /* Need to divide by 2 */
  73. rt_uint8_t c;
  74. rt_le16_t m;
  75. rt_uint8_t k;
  76. /* Need to divide by 2 */
  77. rt_uint8_t j;
  78. }) gtf2;
  79. rt_packed(struct
  80. {
  81. rt_uint8_t version;
  82. /* High 6 bits: extra clock resolution */
  83. rt_uint8_t data1;
  84. /* Plus low 2 of above: max hactive */
  85. rt_uint8_t data2;
  86. rt_uint8_t supported_aspects;
  87. /* Preferred aspect and blanking support */
  88. rt_uint8_t flags;
  89. rt_uint8_t supported_scalings;
  90. rt_uint8_t preferred_refresh;
  91. }) cvt;
  92. } formula;
  93. });
  94. rt_packed(struct detailed_data_wpindex
  95. {
  96. /* Lower 2 bits each */
  97. rt_uint8_t white_yx_lo;
  98. rt_uint8_t white_x_hi;
  99. rt_uint8_t white_y_hi;
  100. /* Need to divide by 100 then add 1 */
  101. rt_uint8_t gamma;
  102. });
  103. rt_packed(struct cvt_timing
  104. {
  105. rt_uint8_t code[3];
  106. });
  107. rt_packed(struct detailed_non_pixel
  108. {
  109. rt_uint8_t pad1;
  110. #define EDID_DETAIL_EST_TIMINGS 0xf7
  111. #define EDID_DETAIL_CVT_3BYTE 0xf8
  112. #define EDID_DETAIL_COLOR_MGMT_DATA 0xf9
  113. #define EDID_DETAIL_STD_MODES 0xfa
  114. #define EDID_DETAIL_MONITOR_CPDATA 0xfb
  115. #define EDID_DETAIL_MONITOR_NAME 0xfc
  116. #define EDID_DETAIL_MONITOR_RANGE 0xfd
  117. #define EDID_DETAIL_MONITOR_STRING 0xfe
  118. #define EDID_DETAIL_MONITOR_SERIAL 0xff
  119. rt_uint8_t type;
  120. rt_uint8_t pad2;
  121. union
  122. {
  123. struct detailed_data_string str;
  124. struct detailed_data_monitor_range range;
  125. struct detailed_data_wpindex color;
  126. struct std_timing timings[6];
  127. struct cvt_timing cvt[4];
  128. } data;
  129. });
  130. rt_packed(struct detailed_timing
  131. {
  132. /* Need to multiply by 10 KHz */
  133. rt_le16_t pixel_clock;
  134. union
  135. {
  136. struct detailed_pixel_timing pixel_data;
  137. struct detailed_non_pixel other_data;
  138. } data;
  139. });
  140. rt_packed(struct edid
  141. {
  142. rt_uint8_t header[8];
  143. /* Vendor & product info */
  144. rt_uint8_t mfg_id[2];
  145. rt_uint8_t prod_code[2];
  146. rt_le32_t serial;
  147. rt_uint8_t mfg_week;
  148. rt_uint8_t mfg_year;
  149. /* EDID version */
  150. rt_uint8_t version;
  151. rt_uint8_t revision;
  152. /* Display info */
  153. rt_uint8_t input;
  154. rt_uint8_t width_cm;
  155. rt_uint8_t height_cm;
  156. rt_uint8_t gamma;
  157. rt_uint8_t features;
  158. /* Color characteristics */
  159. rt_uint8_t red_green_lo;
  160. rt_uint8_t black_white_lo;
  161. rt_uint8_t red_x;
  162. rt_uint8_t red_y;
  163. rt_uint8_t green_x;
  164. rt_uint8_t green_y;
  165. rt_uint8_t blue_x;
  166. rt_uint8_t blue_y;
  167. rt_uint8_t white_x;
  168. rt_uint8_t white_y;
  169. /* Est. timings and mfg rsvd timings */
  170. struct est_timings established_timings;
  171. /* Standard timings 1-8 */
  172. struct std_timing standard_timings[8];
  173. /* Detailing timings 1-4, 18 * 4 = 72 bytes */
  174. struct detailed_timing detailed_timings[4];
  175. /* Number of 128 byte ext. blocks */
  176. rt_uint8_t extensions;
  177. /* Checksum */
  178. rt_uint8_t checksum;
  179. });
  180. struct rt_graphic_device;
  181. struct rt_graphic_device_ops;
  182. struct rt_graphic_plane_ops;
  183. enum rt_graphic_plane_prop
  184. {
  185. RT_GRAPHIC_PLANE_PROP_Z = 0,
  186. RT_GRAPHIC_PLANE_PROP_ROTATE,
  187. RT_GRAPHIC_PLANE_PROP_ALPHA,
  188. RT_GRAPHIC_PLANE_PROP_MAX,
  189. };
  190. struct rt_graphic_plane
  191. {
  192. rt_list_t list;
  193. char name[RT_NAME_MAX];
  194. int id;
  195. #define RT_GRAPHIC_PLANE_TYPE_OVERLAY 0
  196. #define RT_GRAPHIC_PLANE_TYPE_PRIMARY 1 /* Only one, add before register */
  197. #define RT_GRAPHIC_PLANE_TYPE_CURSOR 2 /* Only one */
  198. rt_uint8_t type;
  199. rt_uint32_t x;
  200. rt_uint32_t y;
  201. rt_uint32_t z;
  202. rt_uint32_t width;
  203. rt_uint32_t height;
  204. #define RT_GRAPHIC_PLANE_ROTATE_0 0 /* +0 degrees */
  205. #define RT_GRAPHIC_PLANE_ROTATE_90 1 /* +90 degrees */
  206. #define RT_GRAPHIC_PLANE_ROTATE_180 2 /* +180 degrees */
  207. #define RT_GRAPHIC_PLANE_ROTATE_270 3 /* +270 degrees */
  208. rt_uint8_t rotate;
  209. rt_uint8_t alpha; /* 0 ~ 100 */
  210. rt_uint32_t line_length;
  211. rt_uint32_t bits_per_pixel;
  212. /* Support color modes: RTGRAPHIC_PIXEL_FORMAT_* */
  213. rt_uint32_t mode;
  214. rt_uint32_t modes_nr;
  215. const rt_uint32_t *modes;
  216. /* fb count = framebuffer_len / screen_len */
  217. void *framebuffer;
  218. rt_size_t screen_len;
  219. rt_size_t framebuffer_len;
  220. struct rt_graphic_device *graphic;
  221. const struct rt_graphic_plane_ops *ops;
  222. rt_uint8_t priv[0];
  223. };
  224. struct rt_graphic_plane_ops
  225. {
  226. rt_err_t (*update)(struct rt_graphic_plane *plane, struct rt_device_rect_info *rect);
  227. rt_err_t (*fb_remap)(struct rt_graphic_plane *plane, rt_uint32_t mode, struct rt_device_rect_info *rect);
  228. rt_err_t (*fb_pan_display)(struct rt_graphic_plane *plane, struct rt_device_rect_info *rect);
  229. rt_err_t (*fb_cleanup)(struct rt_graphic_plane *plane);
  230. rt_err_t (*prop_set)(struct rt_graphic_plane *plane, enum rt_graphic_plane_prop prop, void *value);
  231. };
  232. struct rt_graphic_device
  233. {
  234. struct rt_device parent;
  235. const struct rt_graphic_device_ops *ops;
  236. /* Display Power Manage System */
  237. #define RT_GRAPHIC_DPMS_ON 0
  238. #define RT_GRAPHIC_DPMS_STANDBY 1
  239. #define RT_GRAPHIC_DPMS_SUSPEND 2
  240. #define RT_GRAPHIC_DPMS_OFF 3
  241. rt_uint32_t dpms;
  242. rt_list_t overlay_nodes;
  243. struct rt_graphic_plane *primary_plane;
  244. struct rt_graphic_plane *cursor_plane;
  245. struct rt_dm_ida plane_ida;
  246. /* Display information */
  247. struct edid edid;
  248. #ifdef RT_GRAPHIC_BACKLIGHT
  249. struct rt_backlight_device *backlight;
  250. #endif
  251. #define RT_GRAPHIC_UPDATE_MS 16
  252. struct rt_timer *update_timer;
  253. rt_atomic_t event_notifying;
  254. struct rt_device_notify event_notify;
  255. struct rt_spinlock lock;
  256. };
  257. struct rt_graphic_device_ops
  258. {
  259. rt_err_t (*dpms_switch)(struct rt_graphic_device *gdev, rt_uint32_t dpms);
  260. rt_err_t (*set_brightness)(struct rt_graphic_device *gdev, rt_uint32_t brightness);
  261. rt_err_t (*get_brightness)(struct rt_graphic_device *gdev, rt_uint32_t *out_brightness);
  262. rt_err_t (*get_status)(struct rt_graphic_device *gdev, rt_uint32_t *out_status);
  263. rt_err_t (*wait_vsync)(struct rt_graphic_device *gdev);
  264. rt_err_t (*control)(struct rt_graphic_device *gdev, int cmd, void *args);
  265. /* Switching planes supported by device driver */
  266. struct rt_graphic_plane *(*current_plane)(struct rt_graphic_device *gdev);
  267. };
  268. rt_err_t rt_graphic_device_register(struct rt_graphic_device *gdev);
  269. rt_err_t rt_graphic_device_unregister(struct rt_graphic_device *gdev);
  270. struct rt_graphic_plane *rt_graphic_device_alloc_plane(struct rt_graphic_device *gdev,
  271. rt_size_t priv_size, const struct rt_graphic_plane_ops *ops,
  272. const rt_uint32_t *modes, rt_uint32_t modes_nr, rt_uint8_t type);
  273. void rt_graphic_device_free_plane(struct rt_graphic_plane *plane);
  274. rt_err_t rt_graphic_device_add_plane(struct rt_graphic_device *gdev,
  275. struct rt_graphic_plane *plane);
  276. rt_err_t rt_graphic_device_del_plane(struct rt_graphic_device *gdev,
  277. struct rt_graphic_plane *plane);
  278. void rt_graphic_device_hotplug_event(struct rt_graphic_device *gdev);
  279. rt_err_t rt_graphic_device_update_auto(struct rt_graphic_device *gdev, rt_uint32_t update_ms);
  280. void rt_graphic_device_enter(struct rt_graphic_device *gdev);
  281. void rt_graphic_device_leave(struct rt_graphic_device *gdev);
  282. rt_uint32_t rt_graphic_mode_bpp(rt_uint32_t mode);
  283. struct rt_device_graphic_ops *rt_graphic_device_switch_primary(struct rt_graphic_device *gdev);
  284. rt_err_t rt_graphic_device_simple_edid(struct rt_graphic_device *gdev,
  285. rt_uint32_t width, rt_uint32_t height, rt_uint32_t refresh_hz);
  286. rt_err_t rt_graphic_device_simple_register(struct rt_graphic_device *gdev,
  287. rt_uint32_t width, rt_uint32_t height, rt_uint32_t refresh_hz,
  288. const struct rt_graphic_plane_ops *plane_ops,
  289. const rt_uint32_t *modes, rt_uint32_t modes_nr);
  290. rt_err_t rt_graphic_device_simple_unregister(struct rt_graphic_device *gdev);
  291. #ifdef RT_GRAPHIC_LOGO
  292. rt_err_t rt_graphic_logo_change(void *data, int width, int height, int color_max);
  293. rt_err_t rt_graphic_logo_render(struct rt_graphic_device *gdev);
  294. #else
  295. rt_inline rt_err_t rt_graphic_logo_change(void *data, int width, int height, int color_max)
  296. {
  297. return RT_EOK;
  298. }
  299. rt_inline rt_err_t rt_graphic_logo_render(struct rt_graphic_device *gdev)
  300. {
  301. return RT_EOK;
  302. }
  303. #endif /* RT_GRAPHIC_LOGO */
  304. #endif /* __GRAPHIC_DM_H__ */