[98176] | 1 | /* $Id: Helper.cpp 98176 2023-01-21 22:09:36Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[98176] | 3 | * VBoxFB - Helper routines.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
| 28 | #include "VBoxFB.h"
|
---|
| 29 | #include "Helper.h"
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Globals
|
---|
| 33 | */
|
---|
[98176] | 34 | videoMode g_videoModes[MAX_VIDEOMODES] = {{0}};
|
---|
| 35 | uint32_t g_numVideoModes = 0;
|
---|
[1] | 36 |
|
---|
| 37 | /**
|
---|
| 38 | * callback handler for populating the supported video modes
|
---|
| 39 | *
|
---|
| 40 | * @returns callback success indicator
|
---|
| 41 | * @param width width in pixels of the current video mode
|
---|
| 42 | * @param height height in pixels of the current video mode
|
---|
| 43 | * @param bpp bits per pixel of the current video mode
|
---|
| 44 | * @param callbackdata user data pointer
|
---|
| 45 | */
|
---|
| 46 | DFBEnumerationResult enumVideoModesHandler(int width, int height, int bpp, void *callbackdata)
|
---|
| 47 | {
|
---|
[98176] | 48 | RT_NOREF(callbackdata);
|
---|
| 49 | if (g_numVideoModes >= MAX_VIDEOMODES)
|
---|
[1] | 50 | {
|
---|
| 51 | return DFENUM_CANCEL;
|
---|
| 52 | }
|
---|
| 53 | // don't take palette based modes
|
---|
| 54 | if (bpp >= 16)
|
---|
| 55 | {
|
---|
| 56 | // don't take modes we already have (I have seen many cases where
|
---|
| 57 | // DirectFB returns the same modes several times)
|
---|
| 58 | int32_t existingMode = getBestVideoMode(width, height, bpp);
|
---|
[98176] | 59 | if ( existingMode == -1
|
---|
| 60 | || g_videoModes[existingMode].width != (uint32_t)width
|
---|
| 61 | || g_videoModes[existingMode].height != (uint32_t)height
|
---|
| 62 | || g_videoModes[existingMode].bpp != (uint32_t)bpp)
|
---|
[1] | 63 | {
|
---|
[98176] | 64 | g_videoModes[g_numVideoModes].width = (uint32_t)width;
|
---|
| 65 | g_videoModes[g_numVideoModes].height = (uint32_t)height;
|
---|
| 66 | g_videoModes[g_numVideoModes].bpp = (uint32_t)bpp;
|
---|
| 67 | g_numVideoModes++;
|
---|
[1] | 68 | }
|
---|
| 69 | }
|
---|
| 70 | return DFENUM_OK;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * Returns the best fitting video mode for the given characteristics.
|
---|
| 75 | *
|
---|
| 76 | * @returns index of the best video mode, -1 if no suitable mode found
|
---|
| 77 | * @param width requested width
|
---|
| 78 | * @param height requested height
|
---|
| 79 | * @param bpp requested bit depth
|
---|
| 80 | */
|
---|
| 81 | int32_t getBestVideoMode(uint32_t width, uint32_t height, uint32_t bpp)
|
---|
| 82 | {
|
---|
| 83 | int32_t bestMode = -1;
|
---|
| 84 |
|
---|
[98176] | 85 | for (uint32_t i = 0; i < g_numVideoModes; i++)
|
---|
[1] | 86 | {
|
---|
| 87 | // is this mode compatible?
|
---|
[98176] | 88 | if (g_videoModes[i].width >= width && g_videoModes[i].height >= height && g_videoModes[i].bpp >= bpp)
|
---|
[1] | 89 | {
|
---|
| 90 | // first suitable mode?
|
---|
| 91 | if (bestMode == -1)
|
---|
| 92 | bestMode = i;
|
---|
[98176] | 93 | else
|
---|
[1] | 94 | {
|
---|
| 95 | // is it better than the one we got before?
|
---|
[98176] | 96 | if ( g_videoModes[i].width < g_videoModes[bestMode].width
|
---|
| 97 | || g_videoModes[i].height < g_videoModes[bestMode].height
|
---|
| 98 | || g_videoModes[i].bpp < g_videoModes[bestMode].bpp)
|
---|
[1] | 99 | {
|
---|
| 100 | bestMode = i;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | return bestMode;
|
---|
| 106 | }
|
---|