lv_sdl_drv_display.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* MIT License
  2. *
  3. * Copyright (c) [2020] [Ryan Wendland]
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "luat_base.h"
  24. #ifdef LUAT_USE_LVGL_SDL2
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include "lvgl.h"
  28. #include "lv_conf.h"
  29. #include "lv_sdl_drv_display.h"
  30. static lv_disp_buf_t disp_buf;
  31. static lv_color_t *pixels;
  32. #ifdef NXDK
  33. #include <hal/video.h>
  34. void *framebuffer;
  35. #else
  36. #include <SDL2/SDL.h>
  37. static lv_task_t *sdl_present_task;
  38. SDL_Window *window = NULL;
  39. SDL_Renderer *renderer = NULL;
  40. SDL_Texture *framebuffer = NULL;
  41. #endif
  42. static void sdl_fb_flush(lv_disp_drv_t *disp_drv,
  43. const lv_area_t *area,
  44. lv_color_t *color_p)
  45. {
  46. if(area->x2 < 0 || area->y2 < 0 ||
  47. area->x1 > disp_drv->hor_res - 1 || area->y1 > disp_drv->ver_res - 1) {
  48. lv_disp_flush_ready(disp_drv);
  49. return;
  50. }
  51. #ifdef NXDK
  52. static uint8_t pitch = (LV_COLOR_DEPTH + 7) / 8;
  53. for(uint32_t y = area->y1; y <= area->y2; y++)
  54. {
  55. uint32_t line_start = y * disp_drv->hor_res * pitch;
  56. for(uint32_t x = area->x1; x <= area->x2; x++)
  57. {
  58. lv_color_t *pixel = framebuffer + line_start + x * pitch;
  59. *pixel = *color_p;
  60. color_p++;
  61. }
  62. }
  63. #else
  64. SDL_Rect r;
  65. r.x = area->x1;
  66. r.y = area->y1;
  67. r.w = area->x2 - area->x1 + 1;
  68. r.h = area->y2 - area->y1 + 1;
  69. SDL_UpdateTexture(framebuffer, &r, color_p, r.w * ((LV_COLOR_DEPTH + 7) / 8));
  70. #endif
  71. lv_disp_flush_ready(disp_drv);
  72. }
  73. #ifndef NXDK
  74. static void sdl_present(lv_task_t *task)
  75. {
  76. if (renderer && framebuffer)
  77. {
  78. SDL_RenderCopy(renderer, framebuffer, NULL, NULL);
  79. SDL_RenderPresent(renderer);
  80. }
  81. }
  82. #endif
  83. __attribute__((weak)) void display_wait_cb(lv_disp_drv_t *disp_drv)
  84. {
  85. (void)disp_drv;
  86. //OPTIONAL: Called periodically while lvgl waits for an operation to be completed
  87. // User can execute very simple tasks here or yield the task
  88. }
  89. lv_disp_t *lv_sdl_init_display(const char *win_name, int width, int height)
  90. {
  91. //Setup the display buffer and driver
  92. if (width > LV_HOR_RES_MAX)
  93. width = LV_HOR_RES_MAX;
  94. if (height > LV_VER_RES_MAX)
  95. height = LV_VER_RES_MAX;
  96. pixels = malloc(sizeof(lv_color_t) * width * height);
  97. //printf("pixels %p\n", pixels);
  98. lv_disp_buf_init(&disp_buf, pixels, NULL, width * height);
  99. lv_disp_drv_t disp_drv;
  100. lv_disp_drv_init(&disp_drv);
  101. disp_drv.buffer = &disp_buf;
  102. disp_drv.wait_cb = display_wait_cb;
  103. disp_drv.flush_cb = sdl_fb_flush;
  104. disp_drv.hor_res = width;
  105. disp_drv.ver_res = height;
  106. #ifdef NXDK
  107. #define PCRTC_START 0x00600800
  108. framebuffer = MmAllocateContiguousMemoryEx(LV_HOR_RES_MAX * LV_VER_RES_MAX * ((LV_COLOR_DEPTH + 7) / 8),
  109. 0x00000000, 0x7FFFFFFF,
  110. 0x1000,
  111. PAGE_READWRITE |
  112. PAGE_WRITECOMBINE);
  113. assert (framebuffer != NULL);
  114. RtlZeroMemory(framebuffer, LV_HOR_RES_MAX * LV_VER_RES_MAX * ((LV_COLOR_DEPTH + 7) / 8));
  115. XVideoFlushFB();
  116. VIDEOREG(PCRTC_START) = (unsigned int)MmGetPhysicalAddress(framebuffer);
  117. #else
  118. if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0)
  119. printf("SDL_InitSubSystem failed: %s\n", SDL_GetError());
  120. window = SDL_CreateWindow(win_name,
  121. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  122. width, height, 0);
  123. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
  124. #if (LV_COLOR_DEPTH == 16)
  125. #if LV_COLOR_16_SWAP
  126. printf("SDL_PIXELFORMAT_BGR565\n");
  127. Uint32 color_depth = SDL_PIXELFORMAT_BGR565;
  128. #else
  129. printf("SDL_PIXELFORMAT_RGB565\n");
  130. Uint32 color_depth = SDL_PIXELFORMAT_RGB565;
  131. #endif
  132. #else
  133. printf("SDL_PIXELFORMAT_ARGB8888\n");
  134. Uint32 color_depth = SDL_PIXELFORMAT_ARGB8888;
  135. #endif
  136. framebuffer = SDL_CreateTexture(renderer,
  137. color_depth,
  138. SDL_TEXTUREACCESS_STREAMING,
  139. width,
  140. height);
  141. sdl_present_task = lv_task_create(sdl_present, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_HIGHEST, NULL);
  142. #endif
  143. return lv_disp_drv_register(&disp_drv);
  144. }
  145. void lv_sdl_deinit_display(void)
  146. {
  147. #ifdef NXDK
  148. XVideoFlushFB();
  149. MmFreeContiguousMemory(framebuffer);
  150. #else
  151. lv_task_del(sdl_present_task);
  152. SDL_DestroyTexture(framebuffer);
  153. SDL_DestroyRenderer(renderer);
  154. SDL_DestroyWindow(window);
  155. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  156. #endif
  157. }
  158. #endif