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.
icculus@8153
     1
/*
icculus@8153
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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 */
bschaefer@10089
    42
    SDL_PIXELFORMAT_BGR24,    /* mir_pixel_format_bgr_888   */
bschaefer@10089
    43
    SDL_PIXELFORMAT_RGB24,    /* mir_pixel_format_rgb_888   */
bschaefer@10089
    44
    SDL_PIXELFORMAT_RGB565,   /* mir_pixel_format_rgb_565   */
bschaefer@10089
    45
    SDL_PIXELFORMAT_RGBA5551, /* mir_pixel_format_rgba_5551 */
bschaefer@10089
    46
    SDL_PIXELFORMAT_RGBA4444  /* mir_pixel_format_rgba_4444 */
icculus@8153
    47
};
icculus@8153
    48
icculus@8153
    49
Uint32
icculus@8153
    50
MIR_GetSDLPixelFormat(MirPixelFormat format)
icculus@8153
    51
{
icculus@8153
    52
    return mir_pixel_format_to_sdl_format[format];
icculus@8153
    53
}
icculus@8153
    54
icculus@8153
    55
int
icculus@8153
    56
MIR_CreateWindowFramebuffer(_THIS, SDL_Window* window, Uint32* format,
icculus@8153
    57
                            void** pixels, int* pitch)
icculus@8153
    58
{
icculus@8153
    59
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
    60
brandon@8749
    61
    mir_data->software = SDL_TRUE;
brandon@8749
    62
icculus@8153
    63
    if (MIR_CreateWindow(_this, window) < 0)
icculus@8153
    64
        return SDL_SetError("Failed to created a mir window.");
icculus@8153
    65
bschaefer@10089
    66
    *format = MIR_GetSDLPixelFormat(mir_data->pixel_format);
icculus@8153
    67
    if (*format == SDL_PIXELFORMAT_UNKNOWN)
icculus@8153
    68
        return SDL_SetError("Unknown pixel format");
icculus@8153
    69
icculus@8153
    70
    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
icculus@8153
    71
icculus@8153
    72
    *pixels = SDL_malloc(window->h*(*pitch));
icculus@8153
    73
    if (*pixels == NULL)
icculus@8153
    74
        return SDL_OutOfMemory();
icculus@8153
    75
icculus@8153
    76
    return 0;
icculus@8153
    77
}
icculus@8153
    78
icculus@8153
    79
int
icculus@8153
    80
MIR_UpdateWindowFramebuffer(_THIS, SDL_Window* window,
icculus@8153
    81
                            const SDL_Rect* rects, int numrects)
icculus@8153
    82
{
icculus@8153
    83
    MIR_Window* mir_window = window->driverdata;
icculus@8153
    84
icculus@8153
    85
    MirGraphicsRegion region;
bschaefer@10089
    86
    MirBufferStream* bs;
icculus@8153
    87
    int i, j, x, y, w, h, start;
icculus@8153
    88
    int bytes_per_pixel, bytes_per_row, s_stride, d_stride;
icculus@8300
    89
    char* s_dest;
icculus@8300
    90
    char* pixels;
icculus@8153
    91
bschaefer@10089
    92
    bs = MIR_mir_surface_get_buffer_stream(mir_window->surface);
bschaefer@10089
    93
    MIR_mir_buffer_stream_get_graphics_region(bs, &region);
icculus@8153
    94
icculus@8300
    95
    s_dest = region.vaddr;
icculus@8300
    96
    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
bschaefer@10089
   141
    MIR_mir_buffer_stream_swap_buffers_sync(bs);
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: */