SDL  2.0
video.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 2017 BlackBerry Limited
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 #include "../../SDL_internal.h"
22 #include "../SDL_sysvideo.h"
23 #include "sdl_qnx.h"
24 
25 static screen_context_t context;
26 static screen_event_t event;
27 
28 /**
29  * Initializes the QNX video plugin.
30  * Creates the Screen context and event handles used for all window operations
31  * by the plugin.
32  * @param _THIS
33  * @return 0 if successful, -1 on error
34  */
35 static int
37 {
38  SDL_VideoDisplay display;
39 
40  if (screen_create_context(&context, 0) < 0) {
41  return -1;
42  }
43 
44  if (screen_create_event(&event) < 0) {
45  return -1;
46  }
47 
48  SDL_zero(display);
49 
50  if (SDL_AddVideoDisplay(&display) < 0) {
51  return -1;
52  }
53 
54  _this->num_displays = 1;
55  return 0;
56 }
57 
58 static void
60 {
61 }
62 
63 /**
64  * Creates a new native Screen window and associates it with the given SDL
65  * window.
66  * @param _THIS
67  * @param window SDL window to initialize
68  * @return 0 if successful, -1 on error
69  */
70 static int
72 {
73  window_impl_t *impl;
74  int size[2];
75  int numbufs;
76  int format;
77  int usage;
78 
79  impl = SDL_calloc(1, sizeof(*impl));
80  if (impl == NULL) {
81  return -1;
82  }
83 
84  // Create a native window.
85  if (screen_create_window(&impl->window, context) < 0) {
86  goto fail;
87  }
88 
89  // Set the native window's size to match the SDL window.
90  size[0] = window->w;
91  size[1] = window->h;
92 
93  if (screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_SIZE,
94  size) < 0) {
95  goto fail;
96  }
97 
98  if (screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_SOURCE_SIZE,
99  size) < 0) {
100  goto fail;
101  }
102 
103  // Create window buffer(s).
104  if (window->flags & SDL_WINDOW_OPENGL) {
105  if (glGetConfig(&impl->conf, &format) < 0) {
106  goto fail;
107  }
108  numbufs = 2;
109 
110  usage = SCREEN_USAGE_OPENGL_ES2;
111  if (screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_USAGE,
112  &usage) < 0) {
113  return -1;
114  }
115  } else {
116  format = SCREEN_FORMAT_RGBX8888;
117  numbufs = 1;
118  }
119 
120  // Set pixel format.
121  if (screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_FORMAT,
122  &format) < 0) {
123  goto fail;
124  }
125 
126  // Create buffer(s).
127  if (screen_create_window_buffers(impl->window, numbufs) < 0) {
128  goto fail;
129  }
130 
131  window->driverdata = impl;
132  return 0;
133 
134 fail:
135  if (impl->window) {
136  screen_destroy_window(impl->window);
137  }
138 
139  SDL_free(impl);
140  return -1;
141 }
142 
143 /**
144  * Gets a pointer to the Screen buffer associated with the given window. Note
145  * that the buffer is actually created in createWindow().
146  * @param _THIS
147  * @param window SDL window to get the buffer for
148  * @param[out] pixles Holds a pointer to the window's buffer
149  * @param[out] format Holds the pixel format for the buffer
150  * @param[out] pitch Holds the number of bytes per line
151  * @return 0 if successful, -1 on error
152  */
153 static int
155  void ** pixels, int *pitch)
156 {
157  window_impl_t *impl = (window_impl_t *)window->driverdata;
158  screen_buffer_t buffer;
159 
160  // Get a pointer to the buffer's memory.
161  if (screen_get_window_property_pv(impl->window, SCREEN_PROPERTY_BUFFERS,
162  (void **)&buffer) < 0) {
163  return -1;
164  }
165 
166  if (screen_get_buffer_property_pv(buffer, SCREEN_PROPERTY_POINTER,
167  pixels) < 0) {
168  return -1;
169  }
170 
171  // Set format and pitch.
172  if (screen_get_buffer_property_iv(buffer, SCREEN_PROPERTY_STRIDE,
173  pitch) < 0) {
174  return -1;
175  }
176 
177  *format = SDL_PIXELFORMAT_RGB888;
178  return 0;
179 }
180 
181 /**
182  * Informs the window manager that the window needs to be updated.
183  * @param _THIS
184  * @param window The window to update
185  * @param rects An array of reectangular areas to update
186  * @param numrects Rect array length
187  * @return 0 if successful, -1 on error
188  */
189 static int
191  int numrects)
192 {
193  window_impl_t *impl = (window_impl_t *)window->driverdata;
194  screen_buffer_t buffer;
195 
196  if (screen_get_window_property_pv(impl->window, SCREEN_PROPERTY_BUFFERS,
197  (void **)&buffer) < 0) {
198  return -1;
199  }
200 
201  screen_post_window(impl->window, buffer, numrects, (int *)rects, 0);
202  screen_flush_context(context, 0);
203  return 0;
204 }
205 
206 /**
207  * Runs the main event loop.
208  * @param _THIS
209  */
210 static void
212 {
213  int type;
214 
215  for (;;) {
216  if (screen_get_event(context, event, 0) < 0) {
217  break;
218  }
219 
220  if (screen_get_event_property_iv(event, SCREEN_PROPERTY_TYPE, &type)
221  < 0) {
222  break;
223  }
224 
225  if (type == SCREEN_EVENT_NONE) {
226  break;
227  }
228 
229  switch (type) {
230  case SCREEN_EVENT_KEYBOARD:
232  break;
233 
234  default:
235  break;
236  }
237  }
238 }
239 
240 /**
241  * Updates the size of the native window using the geometry of the SDL window.
242  * @param _THIS
243  * @param window SDL window to update
244  */
245 static void
247 {
248  window_impl_t *impl = (window_impl_t *)window->driverdata;
249  int size[2];
250 
251  size[0] = window->w;
252  size[1] = window->h;
253 
254  screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_SIZE, size);
255  screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_SOURCE_SIZE,
256  size);
257 }
258 
259 /**
260  * Makes the native window associated with the given SDL window visible.
261  * @param _THIS
262  * @param window SDL window to update
263  */
264 static void
266 {
267  window_impl_t *impl = (window_impl_t *)window->driverdata;
268  const int visible = 1;
269 
270  screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_VISIBLE,
271  &visible);
272 }
273 
274 /**
275  * Makes the native window associated with the given SDL window invisible.
276  * @param _THIS
277  * @param window SDL window to update
278  */
279 static void
281 {
282  window_impl_t *impl = (window_impl_t *)window->driverdata;
283  const int visible = 0;
284 
285  screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_VISIBLE,
286  &visible);
287 }
288 
289 /**
290  * Destroys the native window associated with the given SDL window.
291  * @param _THIS
292  * @param window SDL window that is being destroyed
293  */
294 static void
296 {
297  window_impl_t *impl = (window_impl_t *)window->driverdata;
298 
299  if (impl) {
300  screen_destroy_window(impl->window);
301  window->driverdata = NULL;
302  }
303 }
304 
305 /**
306  * Frees the plugin object created by createDevice().
307  * @param device Plugin object to free
308  */
309 static void
311 {
312  SDL_free(device);
313 }
314 
315 /**
316  * Creates the QNX video plugin used by SDL.
317  * @param devindex Unused
318  * @return Initialized device if successful, NULL otherwise
319  */
320 static SDL_VideoDevice *
321 createDevice(int devindex)
322 {
324 
325  device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
326  if (device == NULL) {
327  return NULL;
328  }
329 
330  device->driverdata = NULL;
331  device->VideoInit = videoInit;
332  device->VideoQuit = videoQuit;
333  device->CreateSDLWindow = createWindow;
336  device->SetWindowSize = setWindowSize;
337  device->ShowWindow = showWindow;
338  device->HideWindow = hideWindow;
339  device->PumpEvents = pumpEvents;
340  device->DestroyWindow = destroyWindow;
341 
342  device->GL_LoadLibrary = glLoadLibrary;
346  device->GL_SwapWindow = glSwapWindow;
347  device->GL_MakeCurrent = glMakeCurrent;
350 
351  device->free = deleteDevice;
352  return device;
353 }
354 
355 static int
357 {
358  return 1;
359 }
360 
362  "qnx", "QNX Screen",
364 };
static int updateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
Definition: video.c:190
int glGetConfig(EGLConfig *pconf, int *pformat)
Definition: gl.c:68
static int videoInit(_THIS)
Definition: video.c:36
SDL_GLContext glCreateContext(_THIS, SDL_Window *window)
Definition: gl.c:171
static int available()
Definition: video.c:356
void(* free)(_THIS)
Definition: SDL_sysvideo.h:394
void glUnloadLibrary(_THIS)
Definition: gl.c:282
EGLConfig conf
Definition: sdl_qnx.h:33
int(* GL_SetSwapInterval)(_THIS, int interval)
Definition: SDL_sysvideo.h:261
void(* ShowWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:223
static screen_context_t context
Definition: video.c:25
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld [DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp local skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
int glLoadLibrary(_THIS, const char *name)
Definition: gl.c:136
void(* SetWindowSize)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:216
static void setWindowSize(_THIS, SDL_Window *window)
Definition: video.c:246
static void hideWindow(_THIS, SDL_Window *window)
Definition: video.c:280
int SDL_AddVideoDisplay(const SDL_VideoDisplay *display)
Definition: SDL_video.c:606
int(* GL_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:255
void glDeleteContext(_THIS, SDL_GLContext context)
Definition: gl.c:272
static void pumpEvents(_THIS)
Definition: video.c:211
int glSetSwapInterval(_THIS, int interval)
Definition: gl.c:218
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
void(* HideWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:224
static SDL_AudioDeviceID device
Definition: loopwave.c:37
GLsizeiptr const void GLenum usage
static SDL_VideoDevice * createDevice(int devindex)
Definition: video.c:321
SDL_GLContext(* GL_CreateContext)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:258
#define _THIS
int(* GL_MakeCurrent)(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: SDL_sysvideo.h:259
#define SDL_free
struct _cl_event * event
static int createWindow(_THIS, SDL_Window *window)
Definition: video.c:71
screen_window_t window
Definition: sdl_qnx.h:31
VideoBootStrap QNX_bootstrap
Definition: video.c:361
void(* DestroyWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:235
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
static void showWindow(_THIS, SDL_Window *window)
Definition: video.c:265
static int createWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
Definition: video.c:154
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
GLsizeiptr size
void(* GL_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:257
#define NULL
Definition: begin_code.h:164
int(* CreateSDLWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:211
GLuint buffer
void(* VideoQuit)(_THIS)
Definition: SDL_sysvideo.h:167
#define SDL_calloc
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
static screen_event_t event
Definition: video.c:26
int(* GL_SwapWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:263
The type used to identify a window.
Definition: SDL_sysvideo.h:73
uint32_t Uint32
Definition: SDL_stdinc.h:203
static void destroyWindow(_THIS, SDL_Window *window)
Definition: video.c:295
void * glGetProcAddress(_THIS, const char *proc)
Definition: gl.c:158
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
int(* VideoInit)(_THIS)
Definition: SDL_sysvideo.h:161
int(* UpdateWindowFramebuffer)(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
Definition: SDL_sysvideo.h:237
void * driverdata
Definition: SDL_sysvideo.h:111
void(* GL_DeleteContext)(_THIS, SDL_GLContext context)
Definition: SDL_sysvideo.h:264
void handleKeyboardEvent(screen_event_t event)
Definition: keyboard.c:99
static void videoQuit(_THIS)
Definition: video.c:59
EGLSurface EGLint * rects
Definition: eglext.h:282
int glMakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: gl.c:249
int glSwapWindow(_THIS, SDL_Window *window)
Definition: gl.c:234
Uint32 flags
Definition: SDL_sysvideo.h:83
int(* CreateWindowFramebuffer)(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
Definition: SDL_sysvideo.h:236
void *(* GL_GetProcAddress)(_THIS, const char *proc)
Definition: SDL_sysvideo.h:256
static void deleteDevice(SDL_VideoDevice *device)
Definition: video.c:310
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
void(* PumpEvents)(_THIS)
Definition: SDL_sysvideo.h:281