src/video/mir/SDL_mirframebuffer.c
author bschaefer
Sun, 21 Feb 2016 15:19:35 -0800
changeset 10089 25fda20d0173
parent 9998 f67cf37e9cd4
child 10094 fdfdae3f6039
permissions -rw-r--r--
Fix API/ABI breakage in Mir 0.13/0.14.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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     SDL_PIXELFORMAT_RGB24,    /* mir_pixel_format_rgb_888   */
    44     SDL_PIXELFORMAT_RGB565,   /* mir_pixel_format_rgb_565   */
    45     SDL_PIXELFORMAT_RGBA5551, /* mir_pixel_format_rgba_5551 */
    46     SDL_PIXELFORMAT_RGBA4444  /* mir_pixel_format_rgba_4444 */
    47 };
    48 
    49 Uint32
    50 MIR_GetSDLPixelFormat(MirPixelFormat format)
    51 {
    52     return mir_pixel_format_to_sdl_format[format];
    53 }
    54 
    55 int
    56 MIR_CreateWindowFramebuffer(_THIS, SDL_Window* window, Uint32* format,
    57                             void** pixels, int* pitch)
    58 {
    59     MIR_Data* mir_data = _this->driverdata;
    60 
    61     mir_data->software = SDL_TRUE;
    62 
    63     if (MIR_CreateWindow(_this, window) < 0)
    64         return SDL_SetError("Failed to created a mir window.");
    65 
    66     *format = MIR_GetSDLPixelFormat(mir_data->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     return 0;
    77 }
    78 
    79 int
    80 MIR_UpdateWindowFramebuffer(_THIS, SDL_Window* window,
    81                             const SDL_Rect* rects, int numrects)
    82 {
    83     MIR_Window* mir_window = window->driverdata;
    84 
    85     MirGraphicsRegion region;
    86     MirBufferStream* bs;
    87     int i, j, x, y, w, h, start;
    88     int bytes_per_pixel, bytes_per_row, s_stride, d_stride;
    89     char* s_dest;
    90     char* pixels;
    91 
    92     bs = MIR_mir_surface_get_buffer_stream(mir_window->surface);
    93     MIR_mir_buffer_stream_get_graphics_region(bs, &region);
    94 
    95     s_dest = region.vaddr;
    96     pixels = (char*)window->surface->pixels;
    97 
    98     s_stride = window->surface->pitch;
    99     d_stride = region.stride;
   100     bytes_per_pixel = window->surface->format->BytesPerPixel;
   101 
   102     for (i = 0; i < numrects; i++) {
   103         s_dest = region.vaddr;
   104         pixels = (char*)window->surface->pixels;
   105 
   106         x = rects[i].x;
   107         y = rects[i].y;
   108         w = rects[i].w;
   109         h = rects[i].h;
   110 
   111         if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0)
   112             continue;
   113 
   114         if (x < 0) {
   115             x += w;
   116             w += rects[i].x;
   117         }
   118 
   119         if (y < 0) {
   120             y += h;
   121             h += rects[i].y;
   122         }
   123 
   124         if (x + w > window->w)
   125             w = window->w - x;
   126         if (y + h > window->h)
   127             h = window->h - y;
   128 
   129         start = y * s_stride + x;
   130         pixels += start;
   131         s_dest += start;
   132 
   133         bytes_per_row =  bytes_per_pixel * w;
   134         for (j = 0; j < h; j++) {
   135             memcpy(s_dest, pixels, bytes_per_row);
   136             pixels += s_stride;
   137             s_dest += d_stride;
   138         }
   139     }
   140 
   141     MIR_mir_buffer_stream_swap_buffers_sync(bs);
   142 
   143     return 0;
   144 }
   145 
   146 void
   147 MIR_DestroyWindowFramebuffer(_THIS, SDL_Window* window)
   148 {
   149 }
   150 
   151 #endif /* SDL_VIDEO_DRIVER_MIR */
   152 
   153 /* vi: set ts=4 sw=4 expandtab: */