src/video/mir/SDL_mirframebuffer.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 08 Mar 2014 15:34:54 -0500
changeset 8300 8393682d9d96
parent 8159 427e8fdf6f69
child 8749 f34996d809b7
permissions -rw-r--r--
Patched to compile on C89 compilers.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_internal.h"
    27 
    28 #if SDL_VIDEO_DRIVER_MIR
    29 
    30 #include "SDL_mirevents.h"
    31 #include "SDL_mirframebuffer.h"
    32 #include "SDL_mirwindow.h"
    33 
    34 #include "SDL_mirdyn.h"
    35 
    36 static const Uint32 mir_pixel_format_to_sdl_format[] = {
    37     SDL_PIXELFORMAT_UNKNOWN,  /* mir_pixel_format_invalid   */
    38     SDL_PIXELFORMAT_ABGR8888, /* mir_pixel_format_abgr_8888 */
    39     SDL_PIXELFORMAT_BGR888,   /* mir_pixel_format_xbgr_8888 */
    40     SDL_PIXELFORMAT_ARGB8888, /* mir_pixel_format_argb_8888 */
    41     SDL_PIXELFORMAT_RGB888,   /* mir_pixel_format_xrgb_8888 */
    42     SDL_PIXELFORMAT_BGR24     /* mir_pixel_format_bgr_888   */
    43 };
    44 
    45 Uint32
    46 MIR_GetSDLPixelFormat(MirPixelFormat format)
    47 {
    48     return mir_pixel_format_to_sdl_format[format];
    49 }
    50 
    51 int
    52 MIR_CreateWindowFramebuffer(_THIS, SDL_Window* window, Uint32* format,
    53                             void** pixels, int* pitch)
    54 {
    55     MIR_Data* mir_data = _this->driverdata;
    56     MIR_Window* mir_window;
    57     MirSurfaceParameters surfaceparm;
    58 
    59     if (MIR_CreateWindow(_this, window) < 0)
    60         return SDL_SetError("Failed to created a mir window.");
    61 
    62     mir_window = window->driverdata;
    63 
    64     MIR_mir_surface_get_parameters(mir_window->surface, &surfaceparm);
    65 
    66     *format = MIR_GetSDLPixelFormat(surfaceparm.pixel_format);
    67     if (*format == SDL_PIXELFORMAT_UNKNOWN)
    68         return SDL_SetError("Unknown pixel format");
    69 
    70     *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
    71 
    72     *pixels = SDL_malloc(window->h*(*pitch));
    73     if (*pixels == NULL)
    74         return SDL_OutOfMemory();
    75 
    76     mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
    77     if (!MIR_mir_surface_is_valid(mir_window->surface)) {
    78         const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
    79         return SDL_SetError("Failed to created a mir surface: %s", error);
    80     }
    81 
    82     return 0;
    83 }
    84 
    85 int
    86 MIR_UpdateWindowFramebuffer(_THIS, SDL_Window* window,
    87                             const SDL_Rect* rects, int numrects)
    88 {
    89     MIR_Window* mir_window = window->driverdata;
    90 
    91     MirGraphicsRegion region;
    92     int i, j, x, y, w, h, start;
    93     int bytes_per_pixel, bytes_per_row, s_stride, d_stride;
    94     char* s_dest;
    95     char* pixels;
    96 
    97     MIR_mir_surface_get_graphics_region(mir_window->surface, &region);
    98 
    99     s_dest = region.vaddr;
   100     pixels = (char*)window->surface->pixels;
   101 
   102     s_stride = window->surface->pitch;
   103     d_stride = region.stride;
   104     bytes_per_pixel = window->surface->format->BytesPerPixel;
   105 
   106     for (i = 0; i < numrects; i++) {
   107         s_dest = region.vaddr;
   108         pixels = (char*)window->surface->pixels;
   109 
   110         x = rects[i].x;
   111         y = rects[i].y;
   112         w = rects[i].w;
   113         h = rects[i].h;
   114 
   115         if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0)
   116             continue;
   117 
   118         if (x < 0) {
   119             x += w;
   120             w += rects[i].x;
   121         }
   122 
   123         if (y < 0) {
   124             y += h;
   125             h += rects[i].y;
   126         }
   127 
   128         if (x + w > window->w)
   129             w = window->w - x;
   130         if (y + h > window->h)
   131             h = window->h - y;
   132 
   133         start = y * s_stride + x;
   134         pixels += start;
   135         s_dest += start;
   136 
   137         bytes_per_row =  bytes_per_pixel * w;
   138         for (j = 0; j < h; j++) {
   139             memcpy(s_dest, pixels, bytes_per_row);
   140             pixels += s_stride;
   141             s_dest += d_stride;
   142         }
   143     }
   144 
   145     MIR_mir_surface_swap_buffers_sync(mir_window->surface);
   146 
   147     return 0;
   148 }
   149 
   150 void
   151 MIR_DestroyWindowFramebuffer(_THIS, SDL_Window* window)
   152 {
   153 }
   154 
   155 #endif /* SDL_VIDEO_DRIVER_MIR */
   156 
   157 /* vi: set ts=4 sw=4 expandtab: */