VirtualBox

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

Last change on this file since 19798 was 19798, checked in by vboxsync, 16 years ago

removed long obsolete methods IFramebuffer::solidFill() and IFramebuffer::copyScreenBits()

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