Skip to content

Commit

Permalink
SDL: List supported 32bpp pixel formats when using SDL2
Browse files Browse the repository at this point in the history
_hwscreen is always initialized to 16bpp so the supported 32bpp
pixel formats would never be put into the list of supported pixel
formats, making it useless for engines to query for usable 32bpp
pixel formats.

This patch changes things so that the native desktop pixel format
is at the top of the supported formats list, and all pixel formats
<= the default desktop pixel format will now show up in the list
of supported formats. ("Supported" is somewhat of a misnomer here
since there is no hardware querying beyond checking the default
desktop pixel format. SDL generally accepts a wide variety of pixel
formats and tries to convert them to whatever the hardware
supports.)
  • Loading branch information
csnover committed Sep 12, 2017
1 parent e1c33a6 commit 2228ae2
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions backends/graphics/surfacesdl/surfacesdl-graphics.cpp
Expand Up @@ -464,11 +464,64 @@ Common::List<Graphics::PixelFormat> SurfaceSdlGraphicsManager::getSupportedForma
return _supportedFormats;
}

void SurfaceSdlGraphicsManager::detectSupportedFormats() {
#if SDL_VERSION_ATLEAST(2, 0, 0)
static void maskToBitCount(uint32 mask, uint8 &numBits, uint8 &shift) {
numBits = 0;
shift = 32;
for (int i = 0; i < 32; ++i) {
if (mask & 1) {
if (i < shift) {
shift = i;
}
++numBits;
}

mask >>= 1;
}
}
#endif

// Clear old list
void SurfaceSdlGraphicsManager::detectSupportedFormats() {
_supportedFormats.clear();

Graphics::PixelFormat format = Graphics::PixelFormat::createFormatCLUT8();

#if SDL_VERSION_ATLEAST(2, 0, 0)
{
SDL_Window *window = _window->getSDLWindow();
if (window == nullptr) {
error("Could not find ScummVM window for retrieving default display mode");
}

const int displayIndex = SDL_GetWindowDisplayIndex(window);
if (displayIndex < 0) {
error("Could not find ScummVM window display index");
}

SDL_DisplayMode defaultMode;
if (SDL_GetDesktopDisplayMode(displayIndex, &defaultMode) != 0) {
error("Could not get default system display mode");
}

int bpp;
uint32 rMask, gMask, bMask, aMask;
if (SDL_PixelFormatEnumToMasks(defaultMode.format, &bpp, &rMask, &gMask, &bMask, &aMask) != SDL_TRUE) {
error("Could not convert system pixel format %s to masks", SDL_GetPixelFormatName(defaultMode.format));
}

const uint8 bytesPerPixel = SDL_BYTESPERPIXEL(defaultMode.format);
uint8 rBits, rShift, gBits, gShift, bBits, bShift, aBits, aShift;
maskToBitCount(rMask, rBits, rShift);
maskToBitCount(gMask, gBits, gShift);
maskToBitCount(bMask, bBits, bShift);
maskToBitCount(aMask, aBits, aShift);

format = Graphics::PixelFormat(bytesPerPixel, rBits, gBits, bBits, aBits, rShift, gShift, bShift, aShift);

_supportedFormats.push_back(format);
}
#endif

// Some tables with standard formats that we always list
// as "supported". If frontend code tries to use one of
// these, we will perform the necessary format
Expand Down Expand Up @@ -507,21 +560,23 @@ void SurfaceSdlGraphicsManager::detectSupportedFormats() {
Graphics::PixelFormat(2, 4, 4, 4, 4, 4, 8, 12, 0)
};

Graphics::PixelFormat format = Graphics::PixelFormat::createFormatCLUT8();
if (_hwscreen) {
// Get our currently set hardware format
format = Graphics::PixelFormat(_hwscreen->format->BytesPerPixel,
Graphics::PixelFormat hwFormat(_hwscreen->format->BytesPerPixel,
8 - _hwscreen->format->Rloss, 8 - _hwscreen->format->Gloss,
8 - _hwscreen->format->Bloss, 8 - _hwscreen->format->Aloss,
_hwscreen->format->Rshift, _hwscreen->format->Gshift,
_hwscreen->format->Bshift, _hwscreen->format->Ashift);

// Workaround to SDL not providing an accurate Aloss value on Mac OS X.
if (_hwscreen->format->Amask == 0)
format.aLoss = 8;
hwFormat.aLoss = 8;

// Push it first, as the prefered format.
_supportedFormats.push_back(format);
_supportedFormats.push_back(hwFormat);

#if !SDL_VERSION_ATLEAST(2, 0, 0)
format = hwFormat;
#endif
}

// TODO: prioritize matching alpha masks
Expand Down

0 comments on commit 2228ae2

Please sign in to comment.