sdl2-2d.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * QEMU SDL display driver
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
  25. #include "qemu/osdep.h"
  26. #include "ui/console.h"
  27. #include "ui/input.h"
  28. #include "ui/sdl2.h"
  29. void sdl2_2d_update(DisplayChangeListener *dcl,
  30. int x, int y, int w, int h)
  31. {
  32. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  33. DisplaySurface *surf = qemu_console_surface(dcl->con);
  34. SDL_Rect rect;
  35. size_t surface_data_offset;
  36. assert(!scon->opengl);
  37. if (!surf) {
  38. return;
  39. }
  40. if (!scon->texture) {
  41. return;
  42. }
  43. surface_data_offset = surface_bytes_per_pixel(surf) * x +
  44. surface_stride(surf) * y;
  45. rect.x = x;
  46. rect.y = y;
  47. rect.w = w;
  48. rect.h = h;
  49. SDL_UpdateTexture(scon->texture, &rect,
  50. surface_data(surf) + surface_data_offset,
  51. surface_stride(surf));
  52. SDL_RenderClear(scon->real_renderer);
  53. SDL_RenderCopy(scon->real_renderer, scon->texture, NULL, NULL);
  54. SDL_RenderPresent(scon->real_renderer);
  55. }
  56. void sdl2_2d_switch(DisplayChangeListener *dcl,
  57. DisplaySurface *new_surface)
  58. {
  59. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  60. DisplaySurface *old_surface = scon->surface;
  61. int format = 0;
  62. assert(!scon->opengl);
  63. scon->surface = new_surface;
  64. if (scon->texture) {
  65. SDL_DestroyTexture(scon->texture);
  66. scon->texture = NULL;
  67. }
  68. if (!new_surface) {
  69. sdl2_window_destroy(scon);
  70. return;
  71. }
  72. if (!scon->real_window) {
  73. sdl2_window_create(scon);
  74. } else if (old_surface &&
  75. ((surface_width(old_surface) != surface_width(new_surface)) ||
  76. (surface_height(old_surface) != surface_height(new_surface)))) {
  77. sdl2_window_resize(scon);
  78. }
  79. SDL_RenderSetLogicalSize(scon->real_renderer,
  80. surface_width(new_surface),
  81. surface_height(new_surface));
  82. switch (surface_format(scon->surface)) {
  83. case PIXMAN_x1r5g5b5:
  84. format = SDL_PIXELFORMAT_ARGB1555;
  85. break;
  86. case PIXMAN_r5g6b5:
  87. format = SDL_PIXELFORMAT_RGB565;
  88. break;
  89. case PIXMAN_a8r8g8b8:
  90. case PIXMAN_x8r8g8b8:
  91. format = SDL_PIXELFORMAT_ARGB8888;
  92. break;
  93. case PIXMAN_a8b8g8r8:
  94. case PIXMAN_x8b8g8r8:
  95. format = SDL_PIXELFORMAT_ABGR8888;
  96. break;
  97. case PIXMAN_r8g8b8a8:
  98. case PIXMAN_r8g8b8x8:
  99. format = SDL_PIXELFORMAT_RGBA8888;
  100. break;
  101. case PIXMAN_b8g8r8x8:
  102. format = SDL_PIXELFORMAT_BGRX8888;
  103. break;
  104. case PIXMAN_b8g8r8a8:
  105. format = SDL_PIXELFORMAT_BGRA8888;
  106. break;
  107. default:
  108. g_assert_not_reached();
  109. }
  110. scon->texture = SDL_CreateTexture(scon->real_renderer, format,
  111. SDL_TEXTUREACCESS_STREAMING,
  112. surface_width(new_surface),
  113. surface_height(new_surface));
  114. sdl2_2d_redraw(scon);
  115. }
  116. void sdl2_2d_refresh(DisplayChangeListener *dcl)
  117. {
  118. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  119. assert(!scon->opengl);
  120. graphic_hw_update(dcl->con);
  121. sdl2_poll_events(scon);
  122. }
  123. void sdl2_2d_redraw(struct sdl2_console *scon)
  124. {
  125. assert(!scon->opengl);
  126. if (!scon->surface) {
  127. return;
  128. }
  129. sdl2_2d_update(&scon->dcl, 0, 0,
  130. surface_width(scon->surface),
  131. surface_height(scon->surface));
  132. }
  133. bool sdl2_2d_check_format(DisplayChangeListener *dcl,
  134. pixman_format_code_t format)
  135. {
  136. /*
  137. * We let SDL convert for us a few more formats than,
  138. * the native ones. Thes are the ones I have tested.
  139. */
  140. return (format == PIXMAN_x8r8g8b8 ||
  141. format == PIXMAN_a8r8g8b8 ||
  142. format == PIXMAN_a8b8g8r8 ||
  143. format == PIXMAN_x8b8g8r8 ||
  144. format == PIXMAN_b8g8r8x8 ||
  145. format == PIXMAN_b8g8r8a8 ||
  146. format == PIXMAN_r8g8b8x8 ||
  147. format == PIXMAN_r8g8b8a8 ||
  148. format == PIXMAN_x1r5g5b5 ||
  149. format == PIXMAN_r5g6b5);
  150. }