Force software decompression if OPENMW_DECOMPRESS_TEXTURES is set

pull/456/head
Nikolay Kasyanov 6 years ago
parent 359f87ab9f
commit 75d79e98b9

@ -25,15 +25,14 @@
USE_GRAPHICSWINDOW() USE_GRAPHICSWINDOW()
#endif #endif
#if OSG_VERSION_GREATER_OR_EQUAL(3, 5, 8) || defined(__APPLE__) namespace CursorDecompression
#define OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION 1
#else
#define OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION 0
#endif
#if !OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION
namespace
{ {
// macOS builds use the OSG fork that includes DXTC commit
#if OSG_VERSION_GREATER_OR_EQUAL(3, 5, 8) || defined(__APPLE__)
static const bool DXTCSupported = true;
#else
static const bool DXTCSupported = false;
#endif
class MyGraphicsContext { class MyGraphicsContext {
public: public:
@ -90,10 +89,8 @@ namespace
osg::ref_ptr<osg::GraphicsContext> _gc; osg::ref_ptr<osg::GraphicsContext> _gc;
}; };
osg::ref_ptr<osg::Image> decompress (osg::ref_ptr<osg::Image> source, float rotDegrees) SDLUtil::SurfaceUniquePtr hardwareDecompress (osg::ref_ptr<osg::Image> source, float rotDegrees)
{ {
// TODO: use software decompression once S3TC patent expires
int width = source->s(); int width = source->s();
int height = source->t(); int height = source->t();
@ -152,11 +149,55 @@ namespace
source->releaseGLObjects(); source->releaseGLObjects();
texture->releaseGLObjects(); texture->releaseGLObjects();
return resultImage; return SDLUtil::imageToSurface(resultImage, true);
}
SDLUtil::SurfaceUniquePtr softwareDecompress (osg::ref_ptr<osg::Image> source, float rotDegrees)
{
int width = source->s();
int height = source->t();
bool useAlpha = source->isImageTranslucent();
osg::ref_ptr<osg::Image> decompressedImage = new osg::Image;
decompressedImage->setFileName(source->getFileName());
decompressedImage->allocateImage(width, height, 1, useAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE);
for (int s=0; s<width; ++s)
for (int t=0; t<height; ++t)
decompressedImage->setColor(source->getColor(s,t,0), s,t,0);
Uint32 redMask = 0x000000ff;
Uint32 greenMask = 0x0000ff00;
Uint32 blueMask = 0x00ff0000;
Uint32 alphaMask = useAlpha ? 0xff000000 : 0;
SDL_Surface *cursorSurface = SDL_CreateRGBSurfaceFrom(decompressedImage->data(),
width,
height,
decompressedImage->getPixelSizeInBits(),
decompressedImage->getRowSizeInBytes(),
redMask,
greenMask,
blueMask,
alphaMask);
SDL_Surface *targetSurface = SDL_CreateRGBSurface(0, width, height, 32, redMask, greenMask, blueMask, alphaMask);
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(targetSurface);
SDL_RenderClear(renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
SDL_Texture *cursorTexture = SDL_CreateTextureFromSurface(renderer, cursorSurface);
SDL_RenderCopyEx(renderer, cursorTexture, NULL, NULL, -rotDegrees, NULL, SDL_FLIP_VERTICAL);
SDL_DestroyTexture(cursorTexture);
SDL_FreeSurface(cursorSurface);
SDL_DestroyRenderer(renderer);
return SDLUtil::SurfaceUniquePtr(targetSurface, SDL_FreeSurface);
} }
} }
#endif
namespace SDLUtil namespace SDLUtil
{ {
@ -220,77 +261,32 @@ namespace SDLUtil
#endif #endif
} }
#if OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION
SurfaceUniquePtr decompress(osg::Image* source, int rotDegrees)
{
int width = source->s();
int height = source->t();
bool useAlpha = source->isImageTranslucent();
osg::ref_ptr<osg::Image> decompressedImage = new osg::Image;
decompressedImage->setFileName(source->getFileName());
decompressedImage->allocateImage(width, height, 1, useAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE);
for (int s=0; s<width; ++s)
for (int t=0; t<height; ++t)
decompressedImage->setColor(source->getColor(s,t,0), s,t,0);
Uint32 redMask = 0x000000ff;
Uint32 greenMask = 0x0000ff00;
Uint32 blueMask = 0x00ff0000;
Uint32 alphaMask = useAlpha ? 0xff000000 : 0;
SDL_Surface *cursorSurface = SDL_CreateRGBSurfaceFrom(decompressedImage->data(),
width,
height,
decompressedImage->getPixelSizeInBits(),
decompressedImage->getRowSizeInBytes(),
redMask,
greenMask,
blueMask,
alphaMask);
SDL_Surface *targetSurface = SDL_CreateRGBSurface(0, width, height, 32, redMask, greenMask, blueMask, alphaMask);
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(targetSurface);
SDL_RenderClear(renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
SDL_Texture *cursorTexture = SDL_CreateTextureFromSurface(renderer, cursorSurface);
SDL_RenderCopyEx(renderer, cursorTexture, NULL, NULL, -rotDegrees, NULL, SDL_FLIP_VERTICAL);
SDL_DestroyTexture(cursorTexture);
SDL_FreeSurface(cursorSurface);
SDL_DestroyRenderer(renderer);
return SurfaceUniquePtr(targetSurface, SDL_FreeSurface);
}
#endif
void SDLCursorManager::_createCursorFromResource(const std::string& name, int rotDegrees, osg::Image* image, Uint8 hotspot_x, Uint8 hotspot_y) void SDLCursorManager::_createCursorFromResource(const std::string& name, int rotDegrees, osg::Image* image, Uint8 hotspot_x, Uint8 hotspot_y)
{ {
if (mCursorMap.find(name) != mCursorMap.end()) if (mCursorMap.find(name) != mCursorMap.end())
return; return;
#if !OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION static bool forceSoftwareDecompression = (getenv("OPENMW_DECOMPRESS_TEXTURES") != 0);
osg::ref_ptr<osg::Image> decompressed;
SurfaceUniquePtr (*decompressionFunction)(osg::ref_ptr<osg::Image>, float);
if (forceSoftwareDecompression || CursorDecompression::DXTCSupported) {
decompressionFunction = CursorDecompression::softwareDecompress;
} else {
decompressionFunction = CursorDecompression::hardwareDecompress;
}
try { try {
decompressed = decompress(image, static_cast<float>(rotDegrees)); auto surface = decompressionFunction(image, static_cast<float>(rotDegrees));
//set the cursor and store it for later
SDL_Cursor* curs = SDL_CreateColorCursor(surface.get(), hotspot_x, hotspot_y);
mCursorMap.insert(CursorMap::value_type(std::string(name), curs));
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
std::cerr <<"Using default cursor."<<std::endl; std::cerr <<"Using default cursor."<<std::endl;
return; return;
} }
auto surf = SDLUtil::imageToSurface(decompressed, true);
#else
auto surf = decompress(image, rotDegrees);
#endif
//set the cursor and store it for later
SDL_Cursor* curs = SDL_CreateColorCursor(surf.get(), hotspot_x, hotspot_y);
mCursorMap.insert(CursorMap::value_type(std::string(name), curs));
} }
} }

Loading…
Cancel
Save