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