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