src/video/mir/SDL_mirframebuffer.c
changeset 12391 ca6e41ade79a
parent 12390 60f322c5984f
child 12392 046dcbdbaa74
equal deleted inserted replaced
12390:60f322c5984f 12391:ca6e41ade79a
     1 /*
       
     2   Simple DirectMedia Layer
       
     3   Copyright (C) 1997-2018 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 int
       
    37 MIR_CreateWindowFramebuffer(_THIS, SDL_Window* window, Uint32* format,
       
    38                             void** pixels, int* pitch)
       
    39 {
       
    40     MIR_Data* mir_data = _this->driverdata;
       
    41 
       
    42     mir_data->software = SDL_TRUE;
       
    43 
       
    44     if (MIR_CreateWindow(_this, window) < 0)
       
    45         return SDL_SetError("Failed to create a mir window.");
       
    46 
       
    47     *format = MIR_GetSDLPixelFormat(mir_data->pixel_format);
       
    48     if (*format == SDL_PIXELFORMAT_UNKNOWN)
       
    49         return SDL_SetError("Unknown pixel format");
       
    50 
       
    51     *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
       
    52 
       
    53     *pixels = SDL_malloc(window->h*(*pitch));
       
    54     if (*pixels == NULL)
       
    55         return SDL_OutOfMemory();
       
    56 
       
    57     return 0;
       
    58 }
       
    59 
       
    60 int
       
    61 MIR_UpdateWindowFramebuffer(_THIS, SDL_Window* window,
       
    62                             const SDL_Rect* rects, int numrects)
       
    63 {
       
    64     MIR_Window* mir_window = window->driverdata;
       
    65 
       
    66     MirGraphicsRegion region;
       
    67     MirBufferStream* bs;
       
    68     int i, j, x, y, w, h, start;
       
    69     int bytes_per_pixel, bytes_per_row, s_stride, d_stride;
       
    70     char* s_dest;
       
    71     char* pixels;
       
    72 
       
    73     bs = MIR_mir_window_get_buffer_stream(mir_window->window);
       
    74     MIR_mir_buffer_stream_get_graphics_region(bs, &region);
       
    75 
       
    76     s_dest = region.vaddr;
       
    77     pixels = (char*)window->surface->pixels;
       
    78 
       
    79     s_stride = window->surface->pitch;
       
    80     d_stride = region.stride;
       
    81     bytes_per_pixel = window->surface->format->BytesPerPixel;
       
    82 
       
    83     for (i = 0; i < numrects; i++) {
       
    84         s_dest = region.vaddr;
       
    85         pixels = (char*)window->surface->pixels;
       
    86 
       
    87         x = rects[i].x;
       
    88         y = rects[i].y;
       
    89         w = rects[i].w;
       
    90         h = rects[i].h;
       
    91 
       
    92         if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0)
       
    93             continue;
       
    94 
       
    95         if (x < 0) {
       
    96             x += w;
       
    97             w += rects[i].x;
       
    98         }
       
    99 
       
   100         if (y < 0) {
       
   101             y += h;
       
   102             h += rects[i].y;
       
   103         }
       
   104 
       
   105         if (x + w > window->w)
       
   106             w = window->w - x;
       
   107         if (y + h > window->h)
       
   108             h = window->h - y;
       
   109 
       
   110         start = y * s_stride + x;
       
   111         pixels += start;
       
   112         s_dest += start;
       
   113 
       
   114         bytes_per_row =  bytes_per_pixel * w;
       
   115         for (j = 0; j < h; j++) {
       
   116             SDL_memcpy(s_dest, pixels, bytes_per_row);
       
   117             pixels += s_stride;
       
   118             s_dest += d_stride;
       
   119         }
       
   120     }
       
   121 
       
   122     MIR_mir_buffer_stream_swap_buffers_sync(bs);
       
   123 
       
   124     return 0;
       
   125 }
       
   126 
       
   127 void
       
   128 MIR_DestroyWindowFramebuffer(_THIS, SDL_Window* window)
       
   129 {
       
   130 }
       
   131 
       
   132 #endif /* SDL_VIDEO_DRIVER_MIR */
       
   133 
       
   134 /* vi: set ts=4 sw=4 expandtab: */