SDL_mirframebuffer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
  20. */
  21. #include "../../SDL_internal.h"
  22. #if SDL_VIDEO_DRIVER_MIR
  23. #include "SDL_mirevents.h"
  24. #include "SDL_mirframebuffer.h"
  25. #include "SDL_mirwindow.h"
  26. #include "SDL_mirdyn.h"
  27. static const Uint32 mir_pixel_format_to_sdl_format[] = {
  28. SDL_PIXELFORMAT_UNKNOWN, /* mir_pixel_format_invalid */
  29. SDL_PIXELFORMAT_ABGR8888, /* mir_pixel_format_abgr_8888 */
  30. SDL_PIXELFORMAT_BGR888, /* mir_pixel_format_xbgr_8888 */
  31. SDL_PIXELFORMAT_ARGB8888, /* mir_pixel_format_argb_8888 */
  32. SDL_PIXELFORMAT_RGB888, /* mir_pixel_format_xrgb_8888 */
  33. SDL_PIXELFORMAT_BGR24 /* mir_pixel_format_bgr_888 */
  34. };
  35. Uint32
  36. MIR_GetSDLPixelFormat(MirPixelFormat format)
  37. {
  38. return mir_pixel_format_to_sdl_format[format];
  39. }
  40. int
  41. MIR_CreateWindowFramebuffer(_THIS, SDL_Window* window, Uint32* format,
  42. void** pixels, int* pitch)
  43. {
  44. MIR_Data* mir_data = _this->driverdata;
  45. MIR_Window* mir_window;
  46. MirSurfaceParameters surfaceparm;
  47. if (MIR_CreateWindow(_this, window) < 0)
  48. return SDL_SetError("Failed to created a mir window.");
  49. mir_window = window->driverdata;
  50. MIR_mir_surface_get_parameters(mir_window->surface, &surfaceparm);
  51. *format = MIR_GetSDLPixelFormat(surfaceparm.pixel_format);
  52. if (*format == SDL_PIXELFORMAT_UNKNOWN)
  53. return SDL_SetError("Unknown pixel format");
  54. *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
  55. *pixels = SDL_malloc(window->h*(*pitch));
  56. if (*pixels == NULL)
  57. return SDL_OutOfMemory();
  58. mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
  59. if (!MIR_mir_surface_is_valid(mir_window->surface)) {
  60. const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
  61. return SDL_SetError("Failed to created a mir surface: %s", error);
  62. }
  63. return 0;
  64. }
  65. int
  66. MIR_UpdateWindowFramebuffer(_THIS, SDL_Window* window,
  67. const SDL_Rect* rects, int numrects)
  68. {
  69. MIR_Window* mir_window = window->driverdata;
  70. MirGraphicsRegion region;
  71. int i, j, x, y, w, h, start;
  72. int bytes_per_pixel, bytes_per_row, s_stride, d_stride;
  73. char* s_dest;
  74. char* pixels;
  75. MIR_mir_surface_get_graphics_region(mir_window->surface, &region);
  76. s_dest = region.vaddr;
  77. pixels = (char*)window->surface->pixels;
  78. s_stride = window->surface->pitch;
  79. d_stride = region.stride;
  80. bytes_per_pixel = window->surface->format->BytesPerPixel;
  81. for (i = 0; i < numrects; i++) {
  82. s_dest = region.vaddr;
  83. pixels = (char*)window->surface->pixels;
  84. x = rects[i].x;
  85. y = rects[i].y;
  86. w = rects[i].w;
  87. h = rects[i].h;
  88. if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0)
  89. continue;
  90. if (x < 0) {
  91. x += w;
  92. w += rects[i].x;
  93. }
  94. if (y < 0) {
  95. y += h;
  96. h += rects[i].y;
  97. }
  98. if (x + w > window->w)
  99. w = window->w - x;
  100. if (y + h > window->h)
  101. h = window->h - y;
  102. start = y * s_stride + x;
  103. pixels += start;
  104. s_dest += start;
  105. bytes_per_row = bytes_per_pixel * w;
  106. for (j = 0; j < h; j++) {
  107. memcpy(s_dest, pixels, bytes_per_row);
  108. pixels += s_stride;
  109. s_dest += d_stride;
  110. }
  111. }
  112. MIR_mir_surface_swap_buffers_sync(mir_window->surface);
  113. return 0;
  114. }
  115. void
  116. MIR_DestroyWindowFramebuffer(_THIS, SDL_Window* window)
  117. {
  118. }
  119. #endif /* SDL_VIDEO_DRIVER_MIR */
  120. /* vi: set ts=4 sw=4 expandtab: */