demo_view_benchmark.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <stdlib.h>
  2. #include <rtgui/dc.h>
  3. #include <rtgui/dc_hw.h>
  4. #include <rtgui/rtgui_system.h>
  5. #include <rtgui/widgets/container.h>
  6. #include "demo_view.h"
  7. #define RAND(x1, x2) ((rand() % (x2 - x1)) + x1)
  8. #define _int_swap(x, y) do {x ^= y; y ^= x; x ^= y; } while(0)
  9. static struct rtgui_container *container = RT_NULL;
  10. static int running = 0;
  11. static rt_tick_t ticks;
  12. static long long area;
  13. static rt_bool_t _benchmark_onshow(struct rtgui_object *obj, struct rtgui_event *ev)
  14. {
  15. rtgui_widget_focus(RTGUI_WIDGET(obj));
  16. return RT_TRUE;
  17. }
  18. void _onidle(struct rtgui_object *object, rtgui_event_t *event)
  19. {
  20. rtgui_color_t color;
  21. rtgui_rect_t rect, draw_rect;
  22. struct rtgui_dc *dc;
  23. /* 获得控件所属的DC */
  24. // dc = rtgui_dc_hw_create(RTGUI_WIDGET(container));
  25. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(container));
  26. if (dc == RT_NULL)
  27. return;
  28. demo_view_get_logic_rect(RTGUI_CONTAINER(container), &rect);
  29. draw_rect.x1 = RAND(rect.x1, rect.x2);
  30. draw_rect.y1 = RAND(rect.y1, rect.y2);
  31. draw_rect.x2 = RAND(rect.x1, rect.x2);
  32. draw_rect.y2 = RAND(rect.y1, rect.y2);
  33. if(draw_rect.x1 > draw_rect.x2) _int_swap(draw_rect.x1, draw_rect.x2);
  34. if(draw_rect.y1 > draw_rect.y2) _int_swap(draw_rect.y1, draw_rect.y2);
  35. area += rtgui_rect_width(draw_rect) * rtgui_rect_height(draw_rect);
  36. color = RTGUI_RGB(rand() % 255, rand() % 255, rand() % 255);
  37. RTGUI_WIDGET_BACKGROUND(container) = color;
  38. rtgui_dc_fill_rect(dc, &draw_rect);
  39. /* 绘图完成 */
  40. rtgui_dc_end_drawing(dc);
  41. if(rt_tick_get()-ticks >= RT_TICK_PER_SECOND)
  42. {
  43. char buf[16];
  44. sprintf(buf, "%.2f", (double)area/(800*480));
  45. rt_kprintf("frames per second: %s fps\n", buf);
  46. area = 0;
  47. ticks = rt_tick_get();
  48. }
  49. }
  50. void _draw_default(struct rtgui_object *object, rtgui_event_t *event)
  51. {
  52. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  53. struct rtgui_dc *dc;
  54. rtgui_rect_t rect;
  55. /* 因为用的是demo container,上面本身有一部分控件,所以在绘图时先要让demo container先绘图 */
  56. rtgui_container_event_handler(object, event);
  57. /* 获得控件所属的DC */
  58. dc = rtgui_dc_begin_drawing(widget);
  59. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  60. return;
  61. /* 获得demo container允许绘图的区域 */
  62. demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect);
  63. /* 擦除所有 */
  64. RTGUI_WIDGET_BACKGROUND(widget) = default_background;
  65. rtgui_dc_fill_rect(dc, &rect);
  66. /* 显示提示 */
  67. rtgui_dc_draw_text(dc, "按任意键开始/停止测试...", &rect);
  68. /* 绘图完成 */
  69. rtgui_dc_end_drawing(dc);
  70. }
  71. rt_bool_t benchmark_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  72. {
  73. if (event->type == RTGUI_EVENT_PAINT)
  74. {
  75. _draw_default(object, event);
  76. }
  77. else if (event->type == RTGUI_EVENT_SHOW)
  78. {
  79. rtgui_container_event_handler(object, event);
  80. _benchmark_onshow(object, event);
  81. }
  82. else if (event->type == RTGUI_EVENT_KBD)
  83. {
  84. struct rtgui_event_kbd *kbd = (struct rtgui_event_kbd *)event;
  85. if (kbd->key == RTGUIK_LEFT || kbd->key == RTGUIK_RIGHT)
  86. return RT_FALSE;
  87. if (RTGUI_KBD_IS_UP(kbd))
  88. {
  89. if (running)
  90. {
  91. /* stop */
  92. rtgui_app_set_onidle(RT_NULL);
  93. _draw_default(object, event);
  94. }
  95. else
  96. {
  97. /* run */
  98. ticks = rt_tick_get();
  99. area = 0;
  100. rtgui_app_set_onidle(_onidle);
  101. }
  102. running = !running;
  103. }
  104. return RT_TRUE;
  105. }
  106. else
  107. {
  108. /* 调用默认的事件处理函数 */
  109. return rtgui_container_event_handler(object, event);
  110. }
  111. return RT_FALSE;
  112. }
  113. rtgui_container_t *demo_view_benchmark(void)
  114. {
  115. srand(100);
  116. container = demo_view("绘图测试");
  117. RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  118. rtgui_object_set_event_handler(RTGUI_OBJECT(container), benchmark_event_handler);
  119. return container;
  120. }