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