3
* VBox frontends: VBoxSDL (simple frontend based on SDL):
8
* Copyright (C) 2006-2007 innotek GmbH
10
* This file is part of VirtualBox Open Source Edition (OSE), as
11
* available from http://www.virtualbox.org. This file is free software;
12
* you can redistribute it and/or modify it under the terms of the GNU
13
* General Public License as published by the Free Software Foundation,
14
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15
* distribution. VirtualBox OSE is distributed in the hope that it will
16
* be useful, but WITHOUT ANY WARRANTY of any kind.
19
#if defined(RT_OS_WINDOWS) ///@todo someone please explain why we don't follow the book!
24
#include <iprt/assert.h>
25
#include <iprt/stream.h>
26
#include <iprt/string.h>
27
#include <iprt/time.h>
33
#include "SDL_opengl.h"
40
#define ESC_NORM "\033[m"
41
#define ESC_BOLD "\033[1m"
44
static SDL_Surface *gSurfVRAM; /* SDL virtual framebuffer surface */
45
static void *gPtrVRAM; /* allocated virtual framebuffer */
46
static SDL_Surface *gScreen; /* SDL screen surface */
47
static unsigned long guGuestXRes; /* virtual framebuffer width */
48
static unsigned long guGuestYRes; /* virtual framebuffer height */
49
static unsigned long guGuestBpp; /* virtual framebuffer bits per pixel */
50
static unsigned long guMaxScreenWidth; /* max screen width SDL allows */
51
static unsigned long guMaxScreenHeight; /* max screen height SDL allows */
52
static int gfResizable = 1; /* SDL window is resizable */
53
static int gfFullscreen = 0; /* use fullscreen mode */
55
static unsigned long guTextureWidth; /* width of OpenGL texture */
56
static unsigned long guTextureHeight; /* height of OpenGL texture */
57
static unsigned int gTexture;
58
static int gfOpenGL; /* use OpenGL as backend */
60
static unsigned int guLoop = 1000; /* Number of frame redrawings for each test */
62
static void bench(unsigned long w, unsigned long h, unsigned long bpp);
63
static void benchExecute(void);
64
static int checkSDL(const char *fn, int rc);
65
static void checkEvents(void);
68
main(int argc, char **argv)
72
for (int i = 1; i < argc; i++)
75
if (strcmp(argv[i], "-gl") == 0)
81
if (strcmp(argv[i], "-loop") == 0 && ++i < argc)
83
guLoop = atoi(argv[i]);
86
RTPrintf("Unrecognized option '%s'\n", argv[i]);
91
/* Default to DirectX if nothing else set. "windib" would be possible. */
92
if (!getenv("SDL_VIDEODRIVER"))
94
_putenv("SDL_VIDEODRIVER=directx");
99
_putenv("SDL_VIDEO_WINDOW_POS=0,0");
101
setenv("SDL_VIDEO_WINDOW_POS", "0,0", 1);
104
rc = SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
107
RTPrintf("Error: SDL_InitSubSystem failed with message '%s'\n", SDL_GetError());
111
/* output what SDL is capable of */
112
const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
116
RTPrintf("No SDL video info available!\n");
120
RTPrintf("SDL capabilities:\n");
121
RTPrintf(" Hardware surface support: %s\n", videoInfo->hw_available ? "yes" : "no");
122
RTPrintf(" Window manager available: %s\n", videoInfo->wm_available ? "yes" : "no");
123
RTPrintf(" Screen to screen blits accelerated: %s\n", videoInfo->blit_hw ? "yes" : "no");
124
RTPrintf(" Screen to screen colorkey blits accelerated: %s\n", videoInfo->blit_hw_CC ? "yes" : "no");
125
RTPrintf(" Screen to screen alpha blits accelerated: %s\n", videoInfo->blit_hw_A ? "yes" : "no");
126
RTPrintf(" Memory to screen blits accelerated: %s\n", videoInfo->blit_sw ? "yes" : "no");
127
RTPrintf(" Memory to screen colorkey blits accelerated: %s\n", videoInfo->blit_sw_CC ? "yes" : "no");
128
RTPrintf(" Memory to screen alpha blits accelerated: %s\n", videoInfo->blit_sw_A ? "yes" : "no");
129
RTPrintf(" Color fills accelerated: %s\n", videoInfo->blit_fill ? "yes" : "no");
130
RTPrintf(" Video memory in kilobytes: %d\n", videoInfo->video_mem);
131
RTPrintf(" Optimal bpp mode: %d\n", videoInfo->vfmt->BitsPerPixel);
133
RTPrintf("Video driver SDL_VIDEODRIVER / active: %s/%s\n", getenv("SDL_VIDEODRIVER"),
134
SDL_VideoDriverName(buf, sizeof(buf)));
137
"Starting tests. Any key pressed inside the SDL window will abort this\n"
138
"program at the end of the current test. Iterations = %u\n", guLoop);
141
RTPrintf("\n========== "ESC_BOLD"OpenGL is %s"ESC_NORM" ==========\n",
142
gfOpenGL ? "ON" : "OFF");
144
bench( 640, 480, 16); bench( 640, 480, 24); bench( 640, 480, 32);
145
bench(1024, 768, 16); bench(1024, 768, 24); bench(1024, 768, 32);
146
bench(1280, 1024, 16); bench(1280, 1024, 24); bench(1280, 1024, 32);
148
RTPrintf("\nSuccess!\n");
153
* Method that does the actual resize of the guest framebuffer and
154
* then changes the SDL framebuffer setup.
156
static void bench(unsigned long w, unsigned long h, unsigned long bpp)
158
Uint32 Rmask, Gmask, Bmask, Amask = 0;
159
Uint32 Rsize, Gsize, Bsize;
160
Uint32 newWidth, newHeight;
168
/* a different format we support directly? */
206
int sdlFlags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
209
sdlFlags |= SDL_OPENGL;
212
sdlFlags |= SDL_RESIZABLE;
214
sdlFlags |= SDL_FULLSCREEN;
217
* Now we have to check whether there are video mode restrictions
220
/* Get available fullscreen/hardware modes */
221
modes = SDL_ListModes(NULL, sdlFlags);
224
RTPrintf("Error: SDL_ListModes failed with message '%s'\n", SDL_GetError());
228
/* -1 means that any mode is possible (usually non fullscreen) */
229
if (modes != (SDL_Rect **)-1)
232
* according to the SDL documentation, the API guarantees that
233
* the modes are sorted from larger to smaller, so we just
234
* take the first entry as the maximum.
236
guMaxScreenWidth = modes[0]->w;
237
guMaxScreenHeight = modes[0]->h;
242
guMaxScreenWidth = ~0;
243
guMaxScreenHeight = ~0;
246
newWidth = RT_MIN(guMaxScreenWidth, guGuestXRes);
247
newHeight = RT_MIN(guMaxScreenHeight, guGuestYRes);
250
* Now set the screen resolution and get the surface pointer
251
* @todo BPP is not supported!
256
checkSDL("SDL_GL_SetAttribute", SDL_GL_SetAttribute(SDL_GL_RED_SIZE, Rsize));
257
checkSDL("SDL_GL_SetAttribute", SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, Gsize));
258
checkSDL("SDL_GL_SetAttribute", SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, Bsize));
259
checkSDL("SDL_GL_SetAttribute", SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0));
263
RTPrintf("Testing "ESC_BOLD"%ldx%ld@%ld"ESC_NORM"\n", guGuestXRes, guGuestYRes, guGuestBpp);
265
gScreen = SDL_SetVideoMode(newWidth, newHeight, 0, sdlFlags);
268
RTPrintf("SDL_SetVideoMode failed (%s)\n", SDL_GetError());
272
/* first free the current surface */
275
SDL_FreeSurface(gSurfVRAM);
284
if (gScreen->format->BitsPerPixel != guGuestBpp)
286
/* Create a source surface from guest VRAM. */
287
int bytes_per_pixel = (guGuestBpp + 7) / 8;
288
gPtrVRAM = malloc(guGuestXRes * guGuestYRes * bytes_per_pixel);
289
gSurfVRAM = SDL_CreateRGBSurfaceFrom(gPtrVRAM, guGuestXRes, guGuestYRes, guGuestBpp,
290
bytes_per_pixel * guGuestXRes,
291
Rmask, Gmask, Bmask, Amask);
295
/* Create a software surface for which SDL allocates the RAM */
296
gSurfVRAM = SDL_CreateRGBSurface(SDL_SWSURFACE, guGuestXRes, guGuestYRes, guGuestBpp,
297
Rmask, Gmask, Bmask, Amask);
302
RTPrintf("Failed to allocate surface %ldx%ld@%ld\n",
303
guGuestXRes, guGuestYRes, guGuestBpp);
307
RTPrintf(" gScreen=%dx%d@%d (surface: %s)\n",
308
gScreen->w, gScreen->h, gScreen->format->BitsPerPixel,
309
(gScreen->flags & SDL_HWSURFACE) == 0 ? "software" : "hardware");
311
SDL_Rect rect = { 0, 0, (Uint16)guGuestXRes, (Uint16)guGuestYRes };
312
checkSDL("SDL_FillRect",
313
SDL_FillRect(gSurfVRAM, &rect,
314
SDL_MapRGB(gSurfVRAM->format, 0x5F, 0x6F, 0x1F)));
320
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
321
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
322
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
323
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &d);
324
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &o);
325
RTPrintf(" OpenGL ctxt red=%d, green=%d, blue=%d, depth=%d, dbl=%d", r, g, b, d, o);
327
glEnable(GL_TEXTURE_2D);
329
glDisable(GL_DEPTH_TEST);
330
glDepthMask(GL_FALSE);
331
glGenTextures(1, &gTexture);
332
glBindTexture(GL_TEXTURE_2D, gTexture);
333
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
334
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
335
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
336
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
338
for (guTextureWidth = 32; guTextureWidth < newWidth; guTextureWidth <<= 1);
339
for (guTextureHeight = 32; guTextureHeight < newHeight; guTextureHeight <<= 1);
340
RTPrintf(", tex %ldx%ld\n", guTextureWidth, guTextureHeight);
344
case 16: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5, guTextureWidth, guTextureHeight, 0,
345
GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
347
case 24: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, guTextureWidth, guTextureHeight, 0,
348
GL_BGR, GL_UNSIGNED_BYTE, 0);
350
case 32: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, guTextureWidth, guTextureHeight, 0,
351
GL_BGRA, GL_UNSIGNED_BYTE, 0);
353
default: RTPrintf("guGuestBpp=%d?\n", guGuestBpp);
357
glViewport(0, 0, newWidth, newHeight);
358
glMatrixMode(GL_PROJECTION);
360
glOrtho(0.0, newWidth, newHeight, 0.0, -1.0, 1.0);
370
glDeleteTextures(1, &gTexture);
375
static void benchExecute()
377
SDL_Rect rect = { 0, 0, (Uint16)guGuestXRes, (Uint16)guGuestYRes };
381
for (unsigned i=0; i<guLoop; i++)
388
checkSDL("SDL_BlitSurface", SDL_BlitSurface(gSurfVRAM, &rect, gScreen, &rect));
389
if ((gScreen->flags & SDL_HWSURFACE) == 0)
390
SDL_UpdateRect(gScreen, rect.x, rect.y, rect.w, rect.h);
396
glBindTexture(GL_TEXTURE_2D, gTexture);
397
glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
398
glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
399
glPixelStorei(GL_UNPACK_ROW_LENGTH, gSurfVRAM->pitch / gSurfVRAM->format->BytesPerPixel);
400
switch (gSurfVRAM->format->BitsPerPixel)
402
case 16: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect.w, rect.h,
403
GL_RGB, GL_UNSIGNED_SHORT_5_6_5, gSurfVRAM->pixels);
405
case 24: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect.w, rect.h,
406
GL_BGR, GL_UNSIGNED_BYTE, gSurfVRAM->pixels);
408
case 32: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect.w, rect.h,
409
GL_BGRA, GL_UNSIGNED_BYTE, gSurfVRAM->pixels);
411
default: RTPrintf("BitsPerPixel=%d?\n", gSurfVRAM->format->BitsPerPixel);
414
GLfloat tx = (GLfloat)((float)rect.w) / guTextureWidth;
415
GLfloat ty = (GLfloat)((float)rect.h) / guTextureHeight;
417
glColor4f(1.0, 1.0, 1.0, 1.0);
418
glTexCoord2f(0.0, 0.0); glVertex2i(rect.x, rect.y );
419
glTexCoord2f(0.0, ty); glVertex2i(rect.x, rect.y + rect.h);
420
glTexCoord2f(tx, ty); glVertex2i(rect.x + rect.w, rect.y + rect.h);
421
glTexCoord2f(tx, 0.0); glVertex2i(rect.x + rect.w, rect.y );
428
int64_t ms = RTTimeSpecGetMilli(&t2) - RTTimeSpecGetMilli(&t1);
429
printf(" %.1fms/frame\n", (double)ms / guLoop);
432
static int checkSDL(const char *fn, int rc)
435
RTPrintf(""ESC_BOLD"%s() failed:"ESC_NORM" '%s'\n", fn, SDL_GetError());
440
static void checkEvents(void)
443
while (SDL_PollEvent(&event))
448
RTPrintf("\nKey pressed, exiting ...\n");