VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxSDL/Framebuffer.cpp@ 35273

Last change on this file since 35273 was 34513, checked in by vboxsync, 14 years ago

Frontends/VBoxSDL: fix SDL uninit code, in certain failure cases it can be called without being completely initialized and that could result in a crash

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.8 KB
Line 
1/** @file
2 *
3 * VBox frontends: VBoxSDL (simple frontend based on SDL):
4 * Implementation of VBoxSDLFB (SDL framebuffer) class
5 */
6
7/*
8 * Copyright (C) 2006-2010 Oracle Corporation
9 *
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 (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <VBox/com/com.h>
20#include <VBox/com/string.h>
21#include <VBox/com/Guid.h>
22#include <VBox/com/ErrorInfo.h>
23#include <VBox/com/EventQueue.h>
24#include <VBox/com/VirtualBox.h>
25
26#include <iprt/stream.h>
27#include <iprt/env.h>
28
29#ifdef RT_OS_OS2
30# undef RT_MAX
31// from <iprt/cdefs.h>
32# define RT_MAX(Value1, Value2) ((Value1) >= (Value2) ? (Value1) : (Value2))
33#endif
34
35using namespace com;
36
37#define LOG_GROUP LOG_GROUP_GUI
38#include <VBox/err.h>
39#include <VBox/log.h>
40
41#include "VBoxSDL.h"
42#include "Framebuffer.h"
43#include "Ico64x01.h"
44
45#if defined(RT_OS_WINDOWS) || defined(RT_OS_LINUX)
46#include <SDL_syswm.h> /* for SDL_GetWMInfo() */
47#endif
48
49#if defined(VBOX_WITH_XPCOM)
50NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VBoxSDLFB, IFramebuffer)
51NS_DECL_CLASSINFO(VBoxSDLFB)
52NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VBoxSDLFBOverlay, IFramebufferOverlay)
53NS_DECL_CLASSINFO(VBoxSDLFBOverlay)
54#endif
55
56#ifdef VBOX_SECURELABEL
57/* function pointers */
58extern "C"
59{
60DECLSPEC int (SDLCALL *pTTF_Init)(void);
61DECLSPEC TTF_Font* (SDLCALL *pTTF_OpenFont)(const char *file, int ptsize);
62DECLSPEC SDL_Surface* (SDLCALL *pTTF_RenderUTF8_Solid)(TTF_Font *font, const char *text, SDL_Color fg);
63DECLSPEC SDL_Surface* (SDLCALL *pTTF_RenderUTF8_Blended)(TTF_Font *font, const char *text, SDL_Color fg);
64DECLSPEC void (SDLCALL *pTTF_CloseFont)(TTF_Font *font);
65DECLSPEC void (SDLCALL *pTTF_Quit)(void);
66}
67#endif /* VBOX_SECURELABEL */
68
69static bool gfSdlInitialized = false; /**< if SDL was initialized */
70static SDL_Surface *gWMIcon = NULL; /**< the application icon */
71static RTNATIVETHREAD gSdlNativeThread = NIL_RTNATIVETHREAD; /**< the SDL thread */
72
73//
74// Constructor / destructor
75//
76
77/**
78 * SDL framebuffer constructor. It is called from the main
79 * (i.e. SDL) thread. Therefore it is safe to use SDL calls
80 * here.
81 * @param fFullscreen flag whether we start in fullscreen mode
82 * @param fResizable flag whether the SDL window should be resizable
83 * @param fShowSDLConfig flag whether we print out SDL settings
84 * @param fKeepHostRes flag whether we switch the host screen resolution
85 * when switching to fullscreen or not
86 * @param iFixedWidth fixed SDL width (-1 means not set)
87 * @param iFixedHeight fixed SDL height (-1 means not set)
88 */
89VBoxSDLFB::VBoxSDLFB(uint32_t uScreenId,
90 bool fFullscreen, bool fResizable, bool fShowSDLConfig,
91 bool fKeepHostRes, uint32_t u32FixedWidth,
92 uint32_t u32FixedHeight, uint32_t u32FixedBPP)
93{
94 int rc;
95 LogFlow(("VBoxSDLFB::VBoxSDLFB\n"));
96
97#if defined (RT_OS_WINDOWS)
98 refcnt = 0;
99#endif
100
101 mScreenId = uScreenId;
102 mScreen = NULL;
103#ifdef VBOX_WITH_SDL13
104 mWindow = 0;
105 mTexture = 0;
106#endif
107 mSurfVRAM = NULL;
108 mfInitialized = false;
109 mfFullscreen = fFullscreen;
110 mfKeepHostRes = fKeepHostRes;
111 mTopOffset = 0;
112 mfResizable = fResizable;
113 mfShowSDLConfig = fShowSDLConfig;
114 mFixedSDLWidth = u32FixedWidth;
115 mFixedSDLHeight = u32FixedHeight;
116 mFixedSDLBPP = u32FixedBPP;
117 mCenterXOffset = 0;
118 mCenterYOffset = 0;
119 /* Start with standard screen dimensions. */
120 mGuestXRes = 640;
121 mGuestYRes = 480;
122 mPixelFormat = FramebufferPixelFormat_Opaque;
123 mUsesGuestVRAM = FALSE;
124 mPtrVRAM = NULL;
125 mBitsPerPixel = 0;
126 mBytesPerLine = 0;
127 mfSameSizeRequested = false;
128#ifdef VBOX_SECURELABEL
129 mLabelFont = NULL;
130 mLabelHeight = 0;
131 mLabelOffs = 0;
132#endif
133
134 rc = RTCritSectInit(&mUpdateLock);
135 AssertMsg(rc == VINF_SUCCESS, ("Error from RTCritSectInit!\n"));
136
137 resizeGuest();
138 Assert(mScreen);
139 mfInitialized = true;
140}
141
142VBoxSDLFB::~VBoxSDLFB()
143{
144 LogFlow(("VBoxSDLFB::~VBoxSDLFB\n"));
145 if (mSurfVRAM)
146 {
147 SDL_FreeSurface(mSurfVRAM);
148 mSurfVRAM = NULL;
149 }
150 mScreen = NULL;
151
152#ifdef VBOX_SECURELABEL
153 if (mLabelFont)
154 pTTF_CloseFont(mLabelFont);
155 if (pTTF_Quit)
156 pTTF_Quit();
157#endif
158
159 RTCritSectDelete(&mUpdateLock);
160}
161
162bool VBoxSDLFB::init(bool fShowSDLConfig)
163{
164 LogFlow(("VBoxSDLFB::init\n"));
165
166 /* memorize the thread that inited us, that's the SDL thread */
167 gSdlNativeThread = RTThreadNativeSelf();
168
169#ifdef RT_OS_WINDOWS
170 /* default to DirectX if nothing else set */
171 if (!RTEnvGet("SDL_VIDEODRIVER"))
172 {
173 _putenv("SDL_VIDEODRIVER=directx");
174// _putenv("SDL_VIDEODRIVER=windib");
175 }
176#endif
177#ifdef VBOXSDL_WITH_X11
178 /* On some X servers the mouse is stuck inside the bottom right corner.
179 * See http://wiki.clug.org.za/wiki/QEMU_mouse_not_working */
180 RTEnvSet("SDL_VIDEO_X11_DGAMOUSE", "0");
181#endif
182 int rc = SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
183 if (rc != 0)
184 {
185 RTPrintf("SDL Error: '%s'\n", SDL_GetError());
186 return false;
187 }
188 gfSdlInitialized = true;
189
190 const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
191 Assert(videoInfo);
192 if (videoInfo)
193 {
194 /* output what SDL is capable of */
195 if (fShowSDLConfig)
196 RTPrintf("SDL capabilities:\n"
197 " Hardware surface support: %s\n"
198 " Window manager available: %s\n"
199 " Screen to screen blits accelerated: %s\n"
200 " Screen to screen colorkey blits accelerated: %s\n"
201 " Screen to screen alpha blits accelerated: %s\n"
202 " Memory to screen blits accelerated: %s\n"
203 " Memory to screen colorkey blits accelerated: %s\n"
204 " Memory to screen alpha blits accelerated: %s\n"
205 " Color fills accelerated: %s\n"
206 " Video memory in kilobytes: %d\n"
207 " Optimal bpp mode: %d\n"
208 "SDL video driver: %s\n",
209 videoInfo->hw_available ? "yes" : "no",
210 videoInfo->wm_available ? "yes" : "no",
211 videoInfo->blit_hw ? "yes" : "no",
212 videoInfo->blit_hw_CC ? "yes" : "no",
213 videoInfo->blit_hw_A ? "yes" : "no",
214 videoInfo->blit_sw ? "yes" : "no",
215 videoInfo->blit_sw_CC ? "yes" : "no",
216 videoInfo->blit_sw_A ? "yes" : "no",
217 videoInfo->blit_fill ? "yes" : "no",
218 videoInfo->video_mem,
219 videoInfo->vfmt->BitsPerPixel,
220 RTEnvGet("SDL_VIDEODRIVER"));
221 }
222
223 if (12320 == g_cbIco64x01)
224 {
225 gWMIcon = SDL_AllocSurface(SDL_SWSURFACE, 64, 64, 24, 0xff, 0xff00, 0xff0000, 0);
226 /** @todo make it as simple as possible. No PNM interpreter here... */
227 if (gWMIcon)
228 {
229 memcpy(gWMIcon->pixels, g_abIco64x01+32, g_cbIco64x01-32);
230 SDL_WM_SetIcon(gWMIcon, NULL);
231 }
232 }
233
234 return true;
235}
236
237/**
238 * Terminate SDL
239 *
240 * @remarks must be called from the SDL thread!
241 */
242void VBoxSDLFB::uninit()
243{
244 if (gfSdlInitialized)
245 {
246 AssertMsg(gSdlNativeThread == RTThreadNativeSelf(), ("Wrong thread! SDL is not threadsafe!\n"));
247 SDL_QuitSubSystem(SDL_INIT_VIDEO);
248 if (gWMIcon)
249 {
250 SDL_FreeSurface(gWMIcon);
251 gWMIcon = NULL;
252 }
253 }
254}
255
256/**
257 * Returns the current framebuffer width in pixels.
258 *
259 * @returns COM status code
260 * @param width Address of result buffer.
261 */
262STDMETHODIMP VBoxSDLFB::COMGETTER(Width)(ULONG *width)
263{
264 LogFlow(("VBoxSDLFB::GetWidth\n"));
265 if (!width)
266 return E_INVALIDARG;
267 *width = mGuestXRes;
268 return S_OK;
269}
270
271/**
272 * Returns the current framebuffer height in pixels.
273 *
274 * @returns COM status code
275 * @param height Address of result buffer.
276 */
277STDMETHODIMP VBoxSDLFB::COMGETTER(Height)(ULONG *height)
278{
279 LogFlow(("VBoxSDLFB::GetHeight\n"));
280 if (!height)
281 return E_INVALIDARG;
282 *height = mGuestYRes;
283 return S_OK;
284}
285
286/**
287 * Lock the framebuffer (make its address immutable).
288 *
289 * @returns COM status code
290 */
291STDMETHODIMP VBoxSDLFB::Lock()
292{
293 LogFlow(("VBoxSDLFB::Lock\n"));
294 RTCritSectEnter(&mUpdateLock);
295 return S_OK;
296}
297
298/**
299 * Unlock the framebuffer.
300 *
301 * @returns COM status code
302 */
303STDMETHODIMP VBoxSDLFB::Unlock()
304{
305 LogFlow(("VBoxSDLFB::Unlock\n"));
306 RTCritSectLeave(&mUpdateLock);
307 return S_OK;
308}
309
310/**
311 * Return the framebuffer start address.
312 *
313 * @returns COM status code.
314 * @param address Pointer to result variable.
315 */
316STDMETHODIMP VBoxSDLFB::COMGETTER(Address)(BYTE **address)
317{
318 LogFlow(("VBoxSDLFB::GetAddress\n"));
319 if (!address)
320 return E_INVALIDARG;
321
322 if (!mSurfVRAM)
323 {
324 /* That's actually rather bad. */
325 AssertMsgFailed(("mSurfVRAM is NULL!\n"));
326 return E_FAIL;
327 }
328
329 *address = (BYTE *) mSurfVRAM->pixels;
330 LogFlow(("VBoxSDL::GetAddress returning %p\n", *address));
331 return S_OK;
332}
333
334/**
335 * Return the current framebuffer color depth.
336 *
337 * @returns COM status code
338 * @param bitsPerPixel Address of result variable
339 */
340STDMETHODIMP VBoxSDLFB::COMGETTER(BitsPerPixel)(ULONG *bitsPerPixel)
341{
342 LogFlow(("VBoxSDLFB::GetBitsPerPixel\n"));
343 if (!bitsPerPixel)
344 return E_INVALIDARG;
345 /* get the information directly from the surface in use */
346 Assert(mSurfVRAM);
347 *bitsPerPixel = (ULONG)(mSurfVRAM ? mSurfVRAM->format->BitsPerPixel : 0);
348 return S_OK;
349}
350
351/**
352 * Return the current framebuffer line size in bytes.
353 *
354 * @returns COM status code.
355 * @param lineSize Address of result variable.
356 */
357STDMETHODIMP VBoxSDLFB::COMGETTER(BytesPerLine)(ULONG *bytesPerLine)
358{
359 LogFlow(("VBoxSDLFB::GetBytesPerLine\n"));
360 if (!bytesPerLine)
361 return E_INVALIDARG;
362 /* get the information directly from the surface */
363 Assert(mSurfVRAM);
364 *bytesPerLine = (ULONG)(mSurfVRAM ? mSurfVRAM->pitch : 0);
365 return S_OK;
366}
367
368STDMETHODIMP VBoxSDLFB::COMGETTER(PixelFormat) (ULONG *pixelFormat)
369{
370 if (!pixelFormat)
371 return E_POINTER;
372 *pixelFormat = mPixelFormat;
373 return S_OK;
374}
375
376STDMETHODIMP VBoxSDLFB::COMGETTER(UsesGuestVRAM) (BOOL *usesGuestVRAM)
377{
378 if (!usesGuestVRAM)
379 return E_POINTER;
380 *usesGuestVRAM = mUsesGuestVRAM;
381 return S_OK;
382}
383
384/**
385 * Returns by how many pixels the guest should shrink its
386 * video mode height values.
387 *
388 * @returns COM status code.
389 * @param heightReduction Address of result variable.
390 */
391STDMETHODIMP VBoxSDLFB::COMGETTER(HeightReduction)(ULONG *heightReduction)
392{
393 if (!heightReduction)
394 return E_POINTER;
395#ifdef VBOX_SECURELABEL
396 *heightReduction = mLabelHeight;
397#else
398 *heightReduction = 0;
399#endif
400 return S_OK;
401}
402
403/**
404 * Returns a pointer to an alpha-blended overlay used for displaying status
405 * icons above the framebuffer.
406 *
407 * @returns COM status code.
408 * @param aOverlay The overlay framebuffer.
409 */
410STDMETHODIMP VBoxSDLFB::COMGETTER(Overlay)(IFramebufferOverlay **aOverlay)
411{
412 if (!aOverlay)
413 return E_POINTER;
414 /* Not yet implemented */
415 *aOverlay = 0;
416 return S_OK;
417}
418
419/**
420 * Returns handle of window where framebuffer context is being drawn
421 *
422 * @returns COM status code.
423 * @param winId Handle of associated window.
424 */
425STDMETHODIMP VBoxSDLFB::COMGETTER(WinId)(int64_t *winId)
426{
427 if (!winId)
428 return E_POINTER;
429 *winId = mWinId;
430 return S_OK;
431}
432
433/**
434 * Notify framebuffer of an update.
435 *
436 * @returns COM status code
437 * @param x Update region upper left corner x value.
438 * @param y Update region upper left corner y value.
439 * @param w Update region width in pixels.
440 * @param h Update region height in pixels.
441 * @param finished Address of output flag whether the update
442 * could be fully processed in this call (which
443 * has to return immediately) or VBox should wait
444 * for a call to the update complete API before
445 * continuing with display updates.
446 */
447STDMETHODIMP VBoxSDLFB::NotifyUpdate(ULONG x, ULONG y,
448 ULONG w, ULONG h)
449{
450 /*
451 * The input values are in guest screen coordinates.
452 */
453 LogFlow(("VBoxSDLFB::NotifyUpdate: x = %d, y = %d, w = %d, h = %d\n",
454 x, y, w, h));
455
456#ifdef VBOXSDL_WITH_X11
457 /*
458 * SDL does not allow us to make this call from any other thread than
459 * the main SDL thread (which initialized the video mode). So we have
460 * to send an event to the main SDL thread and process it there. For
461 * sake of simplicity, we encode all information in the event parameters.
462 */
463 SDL_Event event;
464 event.type = SDL_USEREVENT;
465 event.user.code = mScreenId;
466 event.user.type = SDL_USER_EVENT_UPDATERECT;
467 // 16 bit is enough for coordinates
468 event.user.data1 = (void*)(x << 16 | y);
469 event.user.data2 = (void*)(w << 16 | h);
470 PushNotifyUpdateEvent(&event);
471#else /* !VBOXSDL_WITH_X11 */
472 update(x, y, w, h, true /* fGuestRelative */);
473#endif /* !VBOXSDL_WITH_X11 */
474
475 return S_OK;
476}
477
478/**
479 * Request a display resize from the framebuffer.
480 *
481 * @returns COM status code.
482 * @param pixelFormat The requested pixel format.
483 * @param vram Pointer to the guest VRAM buffer (can be NULL).
484 * @param bitsPerPixel Color depth in bits.
485 * @param bytesPerLine Size of a scanline in bytes.
486 * @param w New display width in pixels.
487 * @param h New display height in pixels.
488 * @param finished Address of output flag whether the update
489 * could be fully processed in this call (which
490 * has to return immediately) or VBox should wait
491 * for all call to the resize complete API before
492 * continuing with display updates.
493 */
494STDMETHODIMP VBoxSDLFB::RequestResize(ULONG aScreenId, ULONG pixelFormat, BYTE *vram,
495 ULONG bitsPerPixel, ULONG bytesPerLine,
496 ULONG w, ULONG h, BOOL *finished)
497{
498 LogFlowFunc (("w=%d, h=%d, pixelFormat=0x%08lX, vram=%p, "
499 "bpp=%d, bpl=%d\n",
500 w, h, pixelFormat, vram, bitsPerPixel, bytesPerLine));
501
502 /*
503 * SDL does not allow us to make this call from any other thread than
504 * the main thread (the one which initialized the video mode). So we
505 * have to send an event to the main SDL thread and tell VBox to wait.
506 */
507 if (!finished)
508 {
509 AssertMsgFailed(("RequestResize requires the finished flag!\n"));
510 return E_FAIL;
511 }
512
513 /*
514 * Optimize the case when the guest has changed only the VRAM ptr
515 * and the framebuffer uses the guest VRAM as the source bitmap.
516 */
517 if ( mGuestXRes == w
518 && mGuestYRes == h
519 && mPixelFormat == pixelFormat
520 && mBitsPerPixel == bitsPerPixel
521 && mBytesPerLine == bytesPerLine
522 && mUsesGuestVRAM
523 )
524 {
525 mfSameSizeRequested = true;
526 }
527 else
528 {
529 mfSameSizeRequested = false;
530 }
531
532 mGuestXRes = w;
533 mGuestYRes = h;
534 mPixelFormat = pixelFormat;
535 mPtrVRAM = vram;
536 mBitsPerPixel = bitsPerPixel;
537 mBytesPerLine = bytesPerLine;
538 mUsesGuestVRAM = FALSE; /* yet */
539
540 SDL_Event event;
541 event.type = SDL_USEREVENT;
542 event.user.type = SDL_USER_EVENT_RESIZE;
543 event.user.code = mScreenId;
544
545 /* Try multiple times if necessary */
546 PushSDLEventForSure(&event);
547
548 /* we want this request to be processed quickly, so yield the CPU */
549 RTThreadYield();
550
551 *finished = false;
552
553 return S_OK;
554}
555
556/**
557 * Returns whether we like the given video mode.
558 *
559 * @returns COM status code
560 * @param width video mode width in pixels
561 * @param height video mode height in pixels
562 * @param bpp video mode bit depth in bits per pixel
563 * @param supported pointer to result variable
564 */
565STDMETHODIMP VBoxSDLFB::VideoModeSupported(ULONG width, ULONG height, ULONG bpp, BOOL *supported)
566{
567 if (!supported)
568 return E_POINTER;
569
570 /* are constraints set? */
571 if ( ( (mMaxScreenWidth != ~(uint32_t)0)
572 && (width > mMaxScreenWidth))
573 || ( (mMaxScreenHeight != ~(uint32_t)0)
574 && (height > mMaxScreenHeight)))
575 {
576 /* nope, we don't want that (but still don't freak out if it is set) */
577#ifdef DEBUG
578 printf("VBoxSDL::VideoModeSupported: we refused mode %dx%dx%d\n", width, height, bpp);
579#endif
580 *supported = false;
581 }
582 else
583 {
584 /* anything will do */
585 *supported = true;
586 }
587 return S_OK;
588}
589
590STDMETHODIMP VBoxSDLFB::GetVisibleRegion(BYTE *aRectangles, ULONG aCount,
591 ULONG *aCountCopied)
592{
593 PRTRECT rects = (PRTRECT)aRectangles;
594
595 if (!rects)
596 return E_POINTER;
597
598 /// @todo
599
600 NOREF(aCount);
601 NOREF(aCountCopied);
602
603 return S_OK;
604}
605
606STDMETHODIMP VBoxSDLFB::SetVisibleRegion(BYTE *aRectangles, ULONG aCount)
607{
608 PRTRECT rects = (PRTRECT)aRectangles;
609
610 if (!rects)
611 return E_POINTER;
612
613 /// @todo
614
615 NOREF(aCount);
616
617 return S_OK;
618}
619
620STDMETHODIMP VBoxSDLFB::ProcessVHWACommand(BYTE *pCommand)
621{
622 return E_NOTIMPL;
623}
624//
625// Internal public methods
626//
627
628/**
629 * Method that does the actual resize of the guest framebuffer and
630 * then changes the SDL framebuffer setup.
631 */
632void VBoxSDLFB::resizeGuest()
633{
634 LogFlowFunc (("mGuestXRes: %d, mGuestYRes: %d\n", mGuestXRes, mGuestYRes));
635 AssertMsg(gSdlNativeThread == RTThreadNativeSelf(),
636 ("Wrong thread! SDL is not threadsafe!\n"));
637
638 uint32_t Rmask, Gmask, Bmask, Amask = 0;
639
640 mUsesGuestVRAM = FALSE;
641
642 /* pixel characteristics. if we don't support the format directly, we will
643 * fallback to the indirect 32bpp buffer (mUsesGuestVRAM will remain
644 * FALSE) */
645 if (mPixelFormat == FramebufferPixelFormat_FOURCC_RGB)
646 {
647 switch (mBitsPerPixel)
648 {
649 case 16:
650 case 24:
651 case 32:
652 mUsesGuestVRAM = TRUE;
653 break;
654 default:
655 /* the fallback buffer is always 32bpp */
656 mBitsPerPixel = 32;
657 mBytesPerLine = mGuestXRes * 4;
658 break;
659 }
660 }
661 else
662 {
663 /* the fallback buffer is always RGB, 32bpp */
664 mPixelFormat = FramebufferPixelFormat_FOURCC_RGB;
665 mBitsPerPixel = 32;
666 mBytesPerLine = mGuestXRes * 4;
667 }
668
669 switch (mBitsPerPixel)
670 {
671 case 16: Rmask = 0x0000F800; Gmask = 0x000007E0; Bmask = 0x0000001F; break;
672 default: Rmask = 0x00FF0000; Gmask = 0x0000FF00; Bmask = 0x000000FF; break;
673 }
674
675 /* first free the current surface */
676 if (mSurfVRAM)
677 {
678 SDL_FreeSurface(mSurfVRAM);
679 mSurfVRAM = NULL;
680 }
681
682 /* is the guest in a linear framebuffer mode we support? */
683 if (mUsesGuestVRAM)
684 {
685 /* Create a source surface from guest VRAM. */
686 mSurfVRAM = SDL_CreateRGBSurfaceFrom(mPtrVRAM, mGuestXRes, mGuestYRes, mBitsPerPixel,
687 mBytesPerLine, Rmask, Gmask, Bmask, Amask);
688 LogRel(("mSurfVRAM from guest %d x %d\n", mGuestXRes, mGuestYRes));
689 }
690 else
691 {
692 /* Create a software surface for which SDL allocates the RAM */
693 mSurfVRAM = SDL_CreateRGBSurface(SDL_SWSURFACE, mGuestXRes, mGuestYRes, mBitsPerPixel,
694 Rmask, Gmask, Bmask, Amask);
695 LogRel(("mSurfVRAM from SDL %d x %d\n", mGuestXRes, mGuestYRes));
696 }
697 LogFlow(("VBoxSDL:: created VRAM surface %p\n", mSurfVRAM));
698
699 if (mfSameSizeRequested && mUsesGuestVRAM)
700 {
701 /*
702 * Same size has been requested and the framebuffer still uses the guest VRAM.
703 * Reset the condition and return.
704 */
705 mfSameSizeRequested = false;
706 LogFlow(("VBoxSDL:: the same resolution requested, skipping the resize.\n"));
707 return;
708 }
709
710 /* now adjust the SDL resolution */
711 resizeSDL();
712}
713
714/**
715 * Sets SDL video mode. This is independent from guest video
716 * mode changes.
717 *
718 * @remarks Must be called from the SDL thread!
719 */
720void VBoxSDLFB::resizeSDL(void)
721{
722 LogFlow(("VBoxSDL:resizeSDL\n"));
723
724 /*
725 * We request a hardware surface from SDL so that we can perform
726 * accelerated system memory to VRAM blits. The way video handling
727 * works it that on the one hand we have the screen surface from SDL
728 * and on the other hand we have a software surface that we create
729 * using guest VRAM memory for linear modes and using SDL allocated
730 * system memory for text and non linear graphics modes. We never
731 * directly write to the screen surface but always use SDL blitting
732 * functions to blit from our system memory surface to the VRAM.
733 * Therefore, SDL can take advantage of hardware acceleration.
734 */
735 int sdlFlags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
736#ifndef RT_OS_OS2 /* doesn't seem to work for some reason... */
737 if (mfResizable)
738 sdlFlags |= SDL_RESIZABLE;
739#endif
740 if (mfFullscreen)
741 sdlFlags |= SDL_FULLSCREEN;
742
743 /*
744 * Now we have to check whether there are video mode restrictions
745 */
746 SDL_Rect **modes;
747 /* Get available fullscreen/hardware modes */
748 modes = SDL_ListModes(NULL, sdlFlags);
749 Assert(modes != NULL);
750 /* -1 means that any mode is possible (usually non fullscreen) */
751 if (modes != (SDL_Rect **)-1)
752 {
753 /*
754 * according to the SDL documentation, the API guarantees that
755 * the modes are sorted from larger to smaller, so we just
756 * take the first entry as the maximum.
757 */
758 mMaxScreenWidth = modes[0]->w;
759 mMaxScreenHeight = modes[0]->h;
760 }
761 else
762 {
763 /* no restriction */
764 mMaxScreenWidth = ~(uint32_t)0;
765 mMaxScreenHeight = ~(uint32_t)0;
766 }
767
768 uint32_t newWidth;
769 uint32_t newHeight;
770
771 /* reset the centering offsets */
772 mCenterXOffset = 0;
773 mCenterYOffset = 0;
774
775 /* we either have a fixed SDL resolution or we take the guest's */
776 if (mFixedSDLWidth != ~(uint32_t)0)
777 {
778 newWidth = mFixedSDLWidth;
779 newHeight = mFixedSDLHeight;
780 }
781 else
782 {
783 newWidth = RT_MIN(mGuestXRes, mMaxScreenWidth);
784#ifdef VBOX_SECURELABEL
785 newHeight = RT_MIN(mGuestYRes + mLabelHeight, mMaxScreenHeight);
786#else
787 newHeight = RT_MIN(mGuestYRes, mMaxScreenHeight);
788#endif
789 }
790
791 /* we don't have any extra space by default */
792 mTopOffset = 0;
793
794#if defined(VBOX_WITH_SDL13)
795 int sdlWindowFlags = SDL_WINDOW_SHOWN;
796 if (mfResizable)
797 sdlWindowFlags |= SDL_WINDOW_RESIZABLE;
798 if (!mWindow)
799 {
800 SDL_DisplayMode desktop_mode;
801 int x = 40 + mScreenId * 20;
802 int y = 40 + mScreenId * 15;
803
804 SDL_GetDesktopDisplayMode(&desktop_mode);
805 /* create new window */
806
807 char szTitle[64];
808 RTStrPrintf(szTitle, sizeof(szTitle), "SDL window %d", mScreenId);
809 mWindow = SDL_CreateWindow(szTitle, x, y,
810 newWidth, newHeight, sdlWindowFlags);
811 if (SDL_CreateRenderer(mWindow, -1,
812 SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0)
813 AssertReleaseFailed();
814
815 SDL_GetRendererInfo(&mRenderInfo);
816
817 mTexture = SDL_CreateTexture(desktop_mode.format,
818 SDL_TEXTUREACCESS_STREAMING, newWidth, newHeight);
819 if (!mTexture)
820 AssertReleaseFailed();
821 }
822 else
823 {
824 int w, h;
825 uint32_t format;
826 int access;
827
828 /* resize current window */
829 SDL_GetWindowSize(mWindow, &w, &h);
830
831 if (w != (int)newWidth || h != (int)newHeight)
832 SDL_SetWindowSize(mWindow, newWidth, newHeight);
833
834 SDL_QueryTexture(mTexture, &format, &access, &w, &h);
835 SDL_SelectRenderer(mWindow);
836 SDL_DestroyTexture(mTexture);
837 mTexture = SDL_CreateTexture(format, access, newWidth, newHeight);
838 if (!mTexture)
839 AssertReleaseFailed();
840 }
841
842 void *pixels;
843 int pitch;
844 int w, h, bpp;
845 uint32_t Rmask, Gmask, Bmask, Amask;
846 uint32_t format;
847
848 if (SDL_QueryTexture(mTexture, &format, NULL, &w, &h) < 0)
849 AssertReleaseFailed();
850
851 if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask))
852 AssertReleaseFailed();
853
854 if (SDL_QueryTexturePixels(mTexture, &pixels, &pitch) == 0)
855 {
856 mScreen = SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch,
857 Rmask, Gmask, Bmask, Amask);
858 }
859 else
860 {
861 mScreen = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
862 AssertReleaseFailed();
863 }
864
865 SDL_SetClipRect(mScreen, NULL);
866
867#else
868 /*
869 * Now set the screen resolution and get the surface pointer
870 * @todo BPP is not supported!
871 */
872 mScreen = SDL_SetVideoMode(newWidth, newHeight, 0, sdlFlags);
873
874 /*
875 * Set the Window ID. Currently used for OpenGL accelerated guests.
876 */
877# if defined (RT_OS_WINDOWS)
878 SDL_SysWMinfo info;
879 SDL_VERSION(&info.version);
880 if (SDL_GetWMInfo(&info))
881 mWinId = (LONG64) info.window;
882# elif defined (RT_OS_LINUX)
883 SDL_SysWMinfo info;
884 SDL_VERSION(&info.version);
885 if (SDL_GetWMInfo(&info))
886 mWinId = (LONG64) info.info.x11.wmwindow;
887# else
888 /* XXX ignore this for other architectures */
889# endif
890#endif
891#ifdef VBOX_SECURELABEL
892 /*
893 * For non fixed SDL resolution, the above call tried to add the label height
894 * to the guest height. If it worked, we have an offset. If it didn't the below
895 * code will try again with the original guest resolution.
896 */
897 if (mFixedSDLWidth == ~(uint32_t)0)
898 {
899 /* if it didn't work, then we have to go for the original resolution and paint over the guest */
900 if (!mScreen)
901 {
902 mScreen = SDL_SetVideoMode(newWidth, newHeight - mLabelHeight, 0, sdlFlags);
903 }
904 else
905 {
906 /* we now have some extra space */
907 mTopOffset = mLabelHeight;
908 }
909 }
910 else
911 {
912 /* in case the guest resolution is small enough, we do have a top offset */
913 if (mFixedSDLHeight - mGuestYRes >= mLabelHeight)
914 mTopOffset = mLabelHeight;
915
916 /* we also might have to center the guest picture */
917 if (mFixedSDLWidth > mGuestXRes)
918 mCenterXOffset = (mFixedSDLWidth - mGuestXRes) / 2;
919 if (mFixedSDLHeight > mGuestYRes + mLabelHeight)
920 mCenterYOffset = (mFixedSDLHeight - (mGuestYRes + mLabelHeight)) / 2;
921 }
922#endif
923 AssertMsg(mScreen, ("Error: SDL_SetVideoMode failed!\n"));
924 if (mScreen)
925 {
926#ifdef VBOX_WIN32_UI
927 /* inform the UI code */
928 resizeUI(mScreen->w, mScreen->h);
929#endif
930 if (mfShowSDLConfig)
931 RTPrintf("Resized to %dx%d, screen surface type: %s\n", mScreen->w, mScreen->h,
932 ((mScreen->flags & SDL_HWSURFACE) == 0) ? "software" : "hardware");
933 }
934 repaint();
935}
936
937/**
938 * Update specified framebuffer area. The coordinates can either be
939 * relative to the guest framebuffer or relative to the screen.
940 *
941 * @remarks Must be called from the SDL thread on Linux!
942 * @param x left column
943 * @param y top row
944 * @param w width in pixels
945 * @param h height in pixels
946 * @param fGuestRelative flag whether the above values are guest relative or screen relative;
947 */
948void VBoxSDLFB::update(int x, int y, int w, int h, bool fGuestRelative)
949{
950#ifdef VBOXSDL_WITH_X11
951 AssertMsg(gSdlNativeThread == RTThreadNativeSelf(), ("Wrong thread! SDL is not threadsafe!\n"));
952#endif
953 Assert(mScreen);
954 Assert(mSurfVRAM);
955 if (!mScreen || !mSurfVRAM)
956 return;
957
958 /* the source and destination rectangles */
959 SDL_Rect srcRect;
960 SDL_Rect dstRect;
961
962 /* this is how many pixels we have to cut off from the height for this specific blit */
963 int yCutoffGuest = 0;
964
965#ifdef VBOX_SECURELABEL
966 bool fPaintLabel = false;
967 /* if we have a label and no space for it, we have to cut off a bit */
968 if (mLabelHeight && !mTopOffset)
969 {
970 if (y < (int)mLabelHeight)
971 yCutoffGuest = mLabelHeight - y;
972 }
973#endif
974
975 /**
976 * If we get a SDL window relative update, we
977 * just perform a full screen update to keep things simple.
978 *
979 * @todo improve
980 */
981 if (!fGuestRelative)
982 {
983#ifdef VBOX_SECURELABEL
984 /* repaint the label if necessary */
985 if (y < (int)mLabelHeight)
986 fPaintLabel = true;
987#endif
988 x = 0;
989 w = mGuestXRes;
990 y = 0;
991 h = mGuestYRes;
992 }
993
994 srcRect.x = x;
995 srcRect.y = y + yCutoffGuest;
996 srcRect.w = w;
997 srcRect.h = RT_MAX(0, h - yCutoffGuest);
998
999 /*
1000 * Destination rectangle is just offset by the label height.
1001 * There are two cases though: label height is added to the
1002 * guest resolution (mTopOffset == mLabelHeight; yCutoffGuest == 0)
1003 * or the label cuts off a portion of the guest screen (mTopOffset == 0;
1004 * yCutoffGuest >= 0)
1005 */
1006 dstRect.x = x + mCenterXOffset;
1007#ifdef VBOX_SECURELABEL
1008 dstRect.y = RT_MAX(mLabelHeight, y + yCutoffGuest + mTopOffset) + mCenterYOffset;
1009#else
1010 dstRect.y = y + yCutoffGuest + mTopOffset + mCenterYOffset;
1011#endif
1012 dstRect.w = w;
1013 dstRect.h = RT_MAX(0, h - yCutoffGuest);
1014
1015 /*
1016 * Now we just blit
1017 */
1018 SDL_BlitSurface(mSurfVRAM, &srcRect, mScreen, &dstRect);
1019 /* hardware surfaces don't need update notifications */
1020#if defined(VBOX_WITH_SDL13)
1021 AssertRelease(mScreen->flags & SDL_PREALLOC);
1022 SDL_SelectRenderer(mWindow);
1023 SDL_DirtyTexture(mTexture, 1, &dstRect);
1024 AssertRelease(mRenderInfo.flags & SDL_RENDERER_PRESENTCOPY);
1025 SDL_RenderCopy(mTexture, &dstRect, &dstRect);
1026 SDL_RenderPresent();
1027#else
1028 if ((mScreen->flags & SDL_HWSURFACE) == 0)
1029 SDL_UpdateRect(mScreen, dstRect.x, dstRect.y, dstRect.w, dstRect.h);
1030#endif
1031
1032#ifdef VBOX_SECURELABEL
1033 if (fPaintLabel)
1034 paintSecureLabel(0, 0, 0, 0, false);
1035#endif
1036}
1037
1038/**
1039 * Repaint the whole framebuffer
1040 *
1041 * @remarks Must be called from the SDL thread!
1042 */
1043void VBoxSDLFB::repaint()
1044{
1045 AssertMsg(gSdlNativeThread == RTThreadNativeSelf(), ("Wrong thread! SDL is not threadsafe!\n"));
1046 LogFlow(("VBoxSDLFB::repaint\n"));
1047 update(0, 0, mScreen->w, mScreen->h, false /* fGuestRelative */);
1048}
1049
1050/**
1051 * Toggle fullscreen mode
1052 *
1053 * @remarks Must be called from the SDL thread!
1054 */
1055void VBoxSDLFB::setFullscreen(bool fFullscreen)
1056{
1057 AssertMsg(gSdlNativeThread == RTThreadNativeSelf(), ("Wrong thread! SDL is not threadsafe!\n"));
1058 LogFlow(("VBoxSDLFB::SetFullscreen: fullscreen: %d\n", fFullscreen));
1059 mfFullscreen = fFullscreen;
1060 /* only change the SDL resolution, do not touch the guest framebuffer */
1061 resizeSDL();
1062}
1063
1064/**
1065 * Return the geometry of the host. This isn't very well tested but it seems
1066 * to work at least on Linux hosts.
1067 */
1068void VBoxSDLFB::getFullscreenGeometry(uint32_t *width, uint32_t *height)
1069{
1070 SDL_Rect **modes;
1071
1072 /* Get available fullscreen/hardware modes */
1073 modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
1074 Assert(modes != NULL);
1075 /* -1 means that any mode is possible (usually non fullscreen) */
1076 if (modes != (SDL_Rect **)-1)
1077 {
1078 /*
1079 * According to the SDL documentation, the API guarantees that the modes
1080 * are sorted from larger to smaller, so we just take the first entry as
1081 * the maximum.
1082 *
1083 * XXX Crude Xinerama hack :-/
1084 */
1085 if ( modes[0]->w > (16*modes[0]->h/9)
1086 && modes[1]
1087 && modes[1]->h == modes[0]->h)
1088 {
1089 *width = modes[1]->w;
1090 *height = modes[1]->h;
1091 }
1092 else
1093 {
1094 *width = modes[0]->w;
1095 *height = modes[0]->w;
1096 }
1097 }
1098}
1099
1100#ifdef VBOX_SECURELABEL
1101/**
1102 * Setup the secure labeling parameters
1103 *
1104 * @returns VBox status code
1105 * @param height height of the secure label area in pixels
1106 * @param font file path fo the TrueType font file
1107 * @param pointsize font size in points
1108 */
1109int VBoxSDLFB::initSecureLabel(uint32_t height, char *font, uint32_t pointsize, uint32_t labeloffs)
1110{
1111 LogFlow(("VBoxSDLFB:initSecureLabel: new offset: %d pixels, new font: %s, new pointsize: %d\n",
1112 height, font, pointsize));
1113 mLabelHeight = height;
1114 mLabelOffs = labeloffs;
1115 Assert(font);
1116 pTTF_Init();
1117 mLabelFont = pTTF_OpenFont(font, pointsize);
1118 if (!mLabelFont)
1119 {
1120 AssertMsgFailed(("Failed to open TTF font file %s\n", font));
1121 return VERR_OPEN_FAILED;
1122 }
1123 mSecureLabelColorFG = 0x0000FF00;
1124 mSecureLabelColorBG = 0x00FFFF00;
1125 repaint();
1126 return VINF_SUCCESS;
1127}
1128
1129/**
1130 * Set the secure label text and repaint the label
1131 *
1132 * @param text UTF-8 string of new label
1133 * @remarks must be called from the SDL thread!
1134 */
1135void VBoxSDLFB::setSecureLabelText(const char *text)
1136{
1137 mSecureLabelText = text;
1138 paintSecureLabel(0, 0, 0, 0, true);
1139}
1140
1141/**
1142 * Sets the secure label background color.
1143 *
1144 * @param colorFG encoded RGB value for text
1145 * @param colorBG encored RGB value for background
1146 * @remarks must be called from the SDL thread!
1147 */
1148void VBoxSDLFB::setSecureLabelColor(uint32_t colorFG, uint32_t colorBG)
1149{
1150 mSecureLabelColorFG = colorFG;
1151 mSecureLabelColorBG = colorBG;
1152 paintSecureLabel(0, 0, 0, 0, true);
1153}
1154
1155/**
1156 * Paint the secure label if required
1157 *
1158 * @param fForce Force the repaint
1159 * @remarks must be called from the SDL thread!
1160 */
1161void VBoxSDLFB::paintSecureLabel(int x, int y, int w, int h, bool fForce)
1162{
1163#ifdef VBOXSDL_WITH_X11
1164 AssertMsg(gSdlNativeThread == RTThreadNativeSelf(), ("Wrong thread! SDL is not threadsafe!\n"));
1165#endif
1166 /* only when the function is present */
1167 if (!pTTF_RenderUTF8_Solid)
1168 return;
1169 /* check if we can skip the paint */
1170 if (!fForce && ((uint32_t)y > mLabelHeight))
1171 {
1172 return;
1173 }
1174 /* first fill the background */
1175 SDL_Rect rect = {0, 0, (Uint16)mScreen->w, (Uint16)mLabelHeight};
1176 SDL_FillRect(mScreen, &rect, SDL_MapRGB(mScreen->format,
1177 (mSecureLabelColorBG & 0x00FF0000) >> 16, /* red */
1178 (mSecureLabelColorBG & 0x0000FF00) >> 8, /* green */
1179 mSecureLabelColorBG & 0x000000FF)); /* blue */
1180
1181 /* now the text */
1182 if ( mLabelFont != NULL
1183 && !mSecureLabelText.isEmpty()
1184 )
1185 {
1186 SDL_Color clrFg = {(mSecureLabelColorFG & 0x00FF0000) >> 16,
1187 (mSecureLabelColorFG & 0x0000FF00) >> 8,
1188 mSecureLabelColorFG & 0x000000FF, 0};
1189 SDL_Surface *sText = (pTTF_RenderUTF8_Blended != NULL)
1190 ? pTTF_RenderUTF8_Blended(mLabelFont, mSecureLabelText.c_str(), clrFg)
1191 : pTTF_RenderUTF8_Solid(mLabelFont, mSecureLabelText.c_str(), clrFg);
1192 rect.x = 10;
1193 rect.y = mLabelOffs;
1194 SDL_BlitSurface(sText, NULL, mScreen, &rect);
1195 SDL_FreeSurface(sText);
1196 }
1197 /* make sure to update the screen */
1198 SDL_UpdateRect(mScreen, 0, 0, mScreen->w, mLabelHeight);
1199}
1200#endif /* VBOX_SECURELABEL */
1201
1202// IFramebufferOverlay
1203///////////////////////////////////////////////////////////////////////////////////
1204
1205/**
1206 * Constructor for the VBoxSDLFBOverlay class (IFramebufferOverlay implementation)
1207 *
1208 * @param x Initial X offset for the overlay
1209 * @param y Initial Y offset for the overlay
1210 * @param width Initial width for the overlay
1211 * @param height Initial height for the overlay
1212 * @param visible Whether the overlay is initially visible
1213 * @param alpha Initial alpha channel value for the overlay
1214 */
1215VBoxSDLFBOverlay::VBoxSDLFBOverlay(ULONG x, ULONG y, ULONG width, ULONG height,
1216 BOOL visible, VBoxSDLFB *aParent) :
1217 mOverlayX(x), mOverlayY(y), mOverlayWidth(width),
1218 mOverlayHeight(height), mOverlayVisible(visible),
1219 mParent(aParent)
1220{}
1221
1222/**
1223 * Destructor for the VBoxSDLFBOverlay class.
1224 */
1225VBoxSDLFBOverlay::~VBoxSDLFBOverlay()
1226{
1227 SDL_FreeSurface(mBlendedBits);
1228 SDL_FreeSurface(mOverlayBits);
1229}
1230
1231/**
1232 * Perform any initialisation of the overlay that can potentially fail
1233 *
1234 * @returns S_OK on success or the reason for the failure
1235 */
1236HRESULT VBoxSDLFBOverlay::init()
1237{
1238 mBlendedBits = SDL_CreateRGBSurface(SDL_ANYFORMAT, mOverlayWidth, mOverlayHeight, 32,
1239 0x00ff0000, 0x0000ff00, 0x000000ff, 0);
1240 AssertMsgReturn(mBlendedBits != NULL, ("Failed to create an SDL surface\n"),
1241 E_OUTOFMEMORY);
1242 mOverlayBits = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, mOverlayWidth,
1243 mOverlayHeight, 32, 0x00ff0000, 0x0000ff00,
1244 0x000000ff, 0xff000000);
1245 AssertMsgReturn(mOverlayBits != NULL, ("Failed to create an SDL surface\n"),
1246 E_OUTOFMEMORY);
1247 return S_OK;
1248}
1249
1250/**
1251 * Returns the current overlay X offset in pixels.
1252 *
1253 * @returns COM status code
1254 * @param x Address of result buffer.
1255 */
1256STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(X)(ULONG *x)
1257{
1258 LogFlow(("VBoxSDLFBOverlay::GetX\n"));
1259 if (!x)
1260 return E_INVALIDARG;
1261 *x = mOverlayX;
1262 return S_OK;
1263}
1264
1265/**
1266 * Returns the current overlay height in pixels.
1267 *
1268 * @returns COM status code
1269 * @param height Address of result buffer.
1270 */
1271STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Y)(ULONG *y)
1272{
1273 LogFlow(("VBoxSDLFBOverlay::GetY\n"));
1274 if (!y)
1275 return E_INVALIDARG;
1276 *y = mOverlayY;
1277 return S_OK;
1278}
1279
1280/**
1281 * Returns the current overlay width in pixels. In fact, this returns the line size.
1282 *
1283 * @returns COM status code
1284 * @param width Address of result buffer.
1285 */
1286STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Width)(ULONG *width)
1287{
1288 LogFlow(("VBoxSDLFBOverlay::GetWidth\n"));
1289 if (!width)
1290 return E_INVALIDARG;
1291 *width = mOverlayBits->pitch;
1292 return S_OK;
1293}
1294
1295/**
1296 * Returns the current overlay line size in pixels.
1297 *
1298 * @returns COM status code
1299 * @param lineSize Address of result buffer.
1300 */
1301STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(BytesPerLine)(ULONG *bytesPerLine)
1302{
1303 LogFlow(("VBoxSDLFBOverlay::GetBytesPerLine\n"));
1304 if (!bytesPerLine)
1305 return E_INVALIDARG;
1306 *bytesPerLine = mOverlayBits->pitch;
1307 return S_OK;
1308}
1309
1310/**
1311 * Returns the current overlay height in pixels.
1312 *
1313 * @returns COM status code
1314 * @param height Address of result buffer.
1315 */
1316STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Height)(ULONG *height)
1317{
1318 LogFlow(("VBoxSDLFBOverlay::GetHeight\n"));
1319 if (!height)
1320 return E_INVALIDARG;
1321 *height = mOverlayHeight;
1322 return S_OK;
1323}
1324
1325/**
1326 * Returns whether the overlay is currently visible.
1327 *
1328 * @returns COM status code
1329 * @param visible Address of result buffer.
1330 */
1331STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Visible)(BOOL *visible)
1332{
1333 LogFlow(("VBoxSDLFBOverlay::GetVisible\n"));
1334 if (!visible)
1335 return E_INVALIDARG;
1336 *visible = mOverlayVisible;
1337 return S_OK;
1338}
1339
1340/**
1341 * Sets whether the overlay is currently visible.
1342 *
1343 * @returns COM status code
1344 * @param visible New value.
1345 */
1346STDMETHODIMP VBoxSDLFBOverlay::COMSETTER(Visible)(BOOL visible)
1347{
1348 LogFlow(("VBoxSDLFBOverlay::SetVisible\n"));
1349 mOverlayVisible = visible;
1350 return S_OK;
1351}
1352
1353/**
1354 * Returns the value of the global alpha channel.
1355 *
1356 * @returns COM status code
1357 * @param alpha Address of result buffer.
1358 */
1359STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Alpha)(ULONG *alpha)
1360{
1361 LogFlow(("VBoxSDLFBOverlay::GetAlpha\n"));
1362 return E_NOTIMPL;
1363}
1364
1365/**
1366 * Sets whether the overlay is currently visible.
1367 *
1368 * @returns COM status code
1369 * @param alpha new value.
1370 */
1371STDMETHODIMP VBoxSDLFBOverlay::COMSETTER(Alpha)(ULONG alpha)
1372{
1373 LogFlow(("VBoxSDLFBOverlay::SetAlpha\n"));
1374 return E_NOTIMPL;
1375}
1376
1377/**
1378 * Returns the address of the framebuffer bits for writing to.
1379 *
1380 * @returns COM status code
1381 * @param alpha Address of result buffer.
1382 */
1383STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Address)(ULONG *address)
1384{
1385 LogFlow(("VBoxSDLFBOverlay::GetAddress\n"));
1386 if (!address)
1387 return E_INVALIDARG;
1388 *address = (uintptr_t) mOverlayBits->pixels;
1389 return S_OK;
1390}
1391
1392/**
1393 * Returns the current colour depth. In fact, this is always 32bpp.
1394 *
1395 * @returns COM status code
1396 * @param bitsPerPixel Address of result buffer.
1397 */
1398STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(BitsPerPixel)(ULONG *bitsPerPixel)
1399{
1400 LogFlow(("VBoxSDLFBOverlay::GetBitsPerPixel\n"));
1401 if (!bitsPerPixel)
1402 return E_INVALIDARG;
1403 *bitsPerPixel = 32;
1404 return S_OK;
1405}
1406
1407/**
1408 * Returns the current pixel format. In fact, this is always RGB.
1409 *
1410 * @returns COM status code
1411 * @param pixelFormat Address of result buffer.
1412 */
1413STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(PixelFormat)(ULONG *pixelFormat)
1414{
1415 LogFlow(("VBoxSDLFBOverlay::GetPixelFormat\n"));
1416 if (!pixelFormat)
1417 return E_INVALIDARG;
1418 *pixelFormat = FramebufferPixelFormat_FOURCC_RGB;
1419 return S_OK;
1420}
1421
1422/**
1423 * Returns whether the guest VRAM is used directly. In fact, this is always FALSE.
1424 *
1425 * @returns COM status code
1426 * @param usesGuestVRAM Address of result buffer.
1427 */
1428STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(UsesGuestVRAM)(BOOL *usesGuestVRAM)
1429{
1430 LogFlow(("VBoxSDLFBOverlay::GetUsesGuestVRAM\n"));
1431 if (!usesGuestVRAM)
1432 return E_INVALIDARG;
1433 *usesGuestVRAM = FALSE;
1434 return S_OK;
1435}
1436
1437/**
1438 * Returns the height reduction. In fact, this is always 0.
1439 *
1440 * @returns COM status code
1441 * @param heightReduction Address of result buffer.
1442 */
1443STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(HeightReduction)(ULONG *heightReduction)
1444{
1445 LogFlow(("VBoxSDLFBOverlay::GetHeightReduction\n"));
1446 if (!heightReduction)
1447 return E_INVALIDARG;
1448 *heightReduction = 0;
1449 return S_OK;
1450}
1451
1452/**
1453 * Returns the overlay for this framebuffer. Obviously, we return NULL here.
1454 *
1455 * @returns COM status code
1456 * @param overlay Address of result buffer.
1457 */
1458STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(Overlay)(IFramebufferOverlay **aOverlay)
1459{
1460 LogFlow(("VBoxSDLFBOverlay::GetOverlay\n"));
1461 if (!aOverlay)
1462 return E_INVALIDARG;
1463 *aOverlay = 0;
1464 return S_OK;
1465}
1466
1467/**
1468 * Returns associated window handle. We return NULL here.
1469 *
1470 * @returns COM status code
1471 * @param winId Address of result buffer.
1472 */
1473STDMETHODIMP VBoxSDLFBOverlay::COMGETTER(WinId)(LONG64 *winId)
1474{
1475 LogFlow(("VBoxSDLFBOverlay::GetWinId\n"));
1476 if (!winId)
1477 return E_INVALIDARG;
1478 *winId = 0;
1479 return S_OK;
1480}
1481
1482
1483/**
1484 * Lock the overlay. This should not be used - lock the parent IFramebuffer instead.
1485 *
1486 * @returns COM status code
1487 */
1488STDMETHODIMP VBoxSDLFBOverlay::Lock()
1489{
1490 LogFlow(("VBoxSDLFBOverlay::Lock\n"));
1491 AssertMsgFailed(("You should not attempt to lock an IFramebufferOverlay object -\n"
1492 "lock the parent IFramebuffer object instead.\n"));
1493 return E_NOTIMPL;
1494}
1495
1496/**
1497 * Unlock the overlay.
1498 *
1499 * @returns COM status code
1500 */
1501STDMETHODIMP VBoxSDLFBOverlay::Unlock()
1502{
1503 LogFlow(("VBoxSDLFBOverlay::Unlock\n"));
1504 AssertMsgFailed(("You should not attempt to lock an IFramebufferOverlay object -\n"
1505 "lock the parent IFramebuffer object instead.\n"));
1506 return E_NOTIMPL;
1507}
1508
1509/**
1510 * Change the X and Y co-ordinates of the overlay area.
1511 *
1512 * @returns COM status code
1513 * @param x New X co-ordinate.
1514 * @param y New Y co-ordinate.
1515 */
1516STDMETHODIMP VBoxSDLFBOverlay::Move(ULONG x, ULONG y)
1517{
1518 mOverlayX = x;
1519 mOverlayY = y;
1520 return S_OK;
1521}
1522
1523/**
1524 * Notify the overlay that a section of the framebuffer has been redrawn.
1525 *
1526 * @returns COM status code
1527 * @param x X co-ordinate of upper left corner of modified area.
1528 * @param y Y co-ordinate of upper left corner of modified area.
1529 * @param w Width of modified area.
1530 * @param h Height of modified area.
1531 * @retval finished Set if the operation has completed.
1532 *
1533 * All we do here is to send a request to the parent to update the affected area,
1534 * translating between our co-ordinate system and the parent's. It would be have
1535 * been better to call the parent directly, but such is life. We leave bounds
1536 * checking to the parent.
1537 */
1538STDMETHODIMP VBoxSDLFBOverlay::NotifyUpdate(ULONG x, ULONG y,
1539 ULONG w, ULONG h)
1540{
1541 return mParent->NotifyUpdate(x + mOverlayX, y + mOverlayY, w, h);
1542}
1543
1544/**
1545 * Change the dimensions of the overlay.
1546 *
1547 * @returns COM status code
1548 * @param pixelFormat Must be FramebufferPixelFormat_PixelFormatRGB32.
1549 * @param vram Must be NULL.
1550 * @param lineSize Ignored.
1551 * @param w New overlay width.
1552 * @param h New overlay height.
1553 * @retval finished Set if the operation has completed.
1554 */
1555STDMETHODIMP VBoxSDLFBOverlay::RequestResize(ULONG aScreenId, ULONG pixelFormat, ULONG vram,
1556 ULONG bitsPerPixel, ULONG bytesPerLine,
1557 ULONG w, ULONG h, BOOL *finished)
1558{
1559 AssertReturn(pixelFormat == FramebufferPixelFormat_FOURCC_RGB, E_INVALIDARG);
1560 AssertReturn(vram == 0, E_INVALIDARG);
1561 AssertReturn(bitsPerPixel == 32, E_INVALIDARG);
1562 mOverlayWidth = w;
1563 mOverlayHeight = h;
1564 SDL_FreeSurface(mOverlayBits);
1565 mBlendedBits = SDL_CreateRGBSurface(SDL_ANYFORMAT, mOverlayWidth, mOverlayHeight, 32,
1566 0x00ff0000, 0x0000ff00, 0x000000ff, 0);
1567 AssertMsgReturn(mBlendedBits != NULL, ("Failed to create an SDL surface\n"),
1568 E_OUTOFMEMORY);
1569 mOverlayBits = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, mOverlayWidth,
1570 mOverlayHeight, 32, 0x00ff0000, 0x0000ff00,
1571 0x000000ff, 0xff000000);
1572 AssertMsgReturn(mOverlayBits != NULL, ("Failed to create an SDL surface\n"),
1573 E_OUTOFMEMORY);
1574 return S_OK;
1575}
1576
1577/**
1578 * Returns whether we like the given video mode.
1579 *
1580 * @returns COM status code
1581 * @param width video mode width in pixels
1582 * @param height video mode height in pixels
1583 * @param bpp video mode bit depth in bits per pixel
1584 * @retval supported pointer to result variable
1585 *
1586 * Basically, we support anything with 32bpp.
1587 */
1588STDMETHODIMP VBoxSDLFBOverlay::VideoModeSupported(ULONG width, ULONG height, ULONG bpp,
1589 BOOL *supported)
1590{
1591 if (!supported)
1592 return E_POINTER;
1593 if (bpp == 32)
1594 *supported = true;
1595 else
1596 *supported = false;
1597 return S_OK;
1598}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette