SDL  2.0
SDL_mirframebuffer.c
Go to the documentation of this file.
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 };
44 
45 Uint32
46 MIR_GetSDLPixelFormat(MirPixelFormat format)
47 {
48  return mir_pixel_format_to_sdl_format[format];
49 }
50 
51 int
53  void** pixels, int* pitch)
54 {
55  MIR_Data* mir_data = _this->driverdata;
56  MIR_Window* mir_window;
57  MirSurfaceParameters surfaceparm;
58 
59  mir_data->software = SDL_TRUE;
60 
61  if (MIR_CreateWindow(_this, window) < 0)
62  return SDL_SetError("Failed to created a mir window.");
63 
64  mir_window = window->driverdata;
65 
66  MIR_mir_surface_get_parameters(mir_window->surface, &surfaceparm);
67 
68  *format = MIR_GetSDLPixelFormat(surfaceparm.pixel_format);
69  if (*format == SDL_PIXELFORMAT_UNKNOWN)
70  return SDL_SetError("Unknown pixel format");
71 
72  *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
73 
74  *pixels = SDL_malloc(window->h*(*pitch));
75  if (*pixels == NULL)
76  return SDL_OutOfMemory();
77 
78  mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
79  if (!MIR_mir_surface_is_valid(mir_window->surface)) {
80  const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
81  return SDL_SetError("Failed to created a mir surface: %s", error);
82  }
83 
84  return 0;
85 }
86 
87 int
89  const SDL_Rect* rects, int numrects)
90 {
91  MIR_Window* mir_window = window->driverdata;
92 
93  MirGraphicsRegion region;
94  int i, j, x, y, w, h, start;
95  int bytes_per_pixel, bytes_per_row, s_stride, d_stride;
96  char* s_dest;
97  char* pixels;
98 
99  MIR_mir_surface_get_graphics_region(mir_window->surface, &region);
100 
101  s_dest = region.vaddr;
102  pixels = (char*)window->surface->pixels;
103 
104  s_stride = window->surface->pitch;
105  d_stride = region.stride;
106  bytes_per_pixel = window->surface->format->BytesPerPixel;
107 
108  for (i = 0; i < numrects; i++) {
109  s_dest = region.vaddr;
110  pixels = (char*)window->surface->pixels;
111 
112  x = rects[i].x;
113  y = rects[i].y;
114  w = rects[i].w;
115  h = rects[i].h;
116 
117  if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0)
118  continue;
119 
120  if (x < 0) {
121  x += w;
122  w += rects[i].x;
123  }
124 
125  if (y < 0) {
126  y += h;
127  h += rects[i].y;
128  }
129 
130  if (x + w > window->w)
131  w = window->w - x;
132  if (y + h > window->h)
133  h = window->h - y;
134 
135  start = y * s_stride + x;
136  pixels += start;
137  s_dest += start;
138 
139  bytes_per_row = bytes_per_pixel * w;
140  for (j = 0; j < h; j++) {
141  memcpy(s_dest, pixels, bytes_per_row);
142  pixels += s_stride;
143  s_dest += d_stride;
144  }
145  }
146 
147  MIR_mir_surface_swap_buffers_sync(mir_window->surface);
148 
149  return 0;
150 }
151 
152 void
154 {
155 }
156 
157 #endif /* SDL_VIDEO_DRIVER_MIR */
158 
159 /* vi: set ts=4 sw=4 expandtab: */
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
int MIR_CreateWindowFramebuffer(_THIS, SDL_Window *sdl_window, Uint32 *format, void **pixels, int *pitch)
Uint8 BytesPerPixel
Definition: SDL_pixels.h:304
int MIR_CreateWindow(_THIS, SDL_Window *window)
SDL_Window * window
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:127
GLuint start
Definition: SDL_opengl.h:1564
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
MirConnection * connection
Definition: SDL_mirvideo.h:34
SDL_bool software
Definition: SDL_mirvideo.h:35
void * pixels
Definition: SDL_surface.h:75
#define _THIS
int MIR_UpdateWindowFramebuffer(_THIS, SDL_Window *sdl_window, const SDL_Rect *rects, int numrects)
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
void MIR_DestroyWindowFramebuffer(_THIS, SDL_Window *sdl_window)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
#define memcpy
Definition: SDL_malloc.c:640
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:71
SDL_Rect rects[MAX_RECTS]
#define SDL_malloc
GLubyte GLubyte GLubyte GLubyte w
void * driverdata
Definition: SDL_sysvideo.h:106
MirSurface * surface
Definition: SDL_mirwindow.h:38
SDL_Surface * surface
Definition: SDL_sysvideo.h:93
int y
Definition: SDL_rect.h:66
GLfloat GLfloat GLfloat GLfloat h
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:42