Factor out rendering / texture juggling from surface handling. Defuct.

This commit is contained in:
Christian Speckner 2019-12-08 20:58:58 +01:00
parent 3a90828892
commit 1c798bd567
11 changed files with 350 additions and 78 deletions

55
.vscode/settings.json vendored
View File

@ -14,60 +14,15 @@
"C_Cpp.intelliSenseEngine": "Default",
"files.insertFinalNewline": true,
"files.associations": {
"__split_buffer": "cpp",
"__tree": "cpp",
"atomic": "cpp",
"deque": "cpp",
"ios": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"vector": "cpp",
"sstream": "cpp",
"__bit_reference": "cpp",
"__functional_base": "cpp",
"algorithm": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"functional": "cpp",
"iterator": "cpp",
"limits": "cpp",
"array": "cpp",
"istream": "cpp",
"locale": "cpp",
"memory": "cpp",
"ratio": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"stdexcept": "cpp",
"fstream": "cpp",
"__locale": "cpp",
"__string": "cpp",
"__config": "cpp",
"__nullptr": "cpp",
"cstddef": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"new": "cpp",
"typeinfo": "cpp",
"__mutex_base": "cpp",
"mutex": "cpp",
"condition_variable": "cpp",
"*.ins": "cpp",
"cstring": "cpp",
"iostream": "cpp",
"cstdint": "cpp",
"ostream": "cpp",
"__memory": "cpp",
"iosfwd": "cpp",
"__hash_table": "cpp",
"array": "cpp",
"queue": "cpp",
"unordered_map": "cpp",
"istream": "cpp",
"thread": "cpp",
"tuple": "cpp",
"utility": "cpp",
"streambuf": "cpp"
"*.tcc": "cpp",
"functional": "cpp"
}
}

View File

@ -18,21 +18,17 @@
#include "FBSurfaceSDL2.hxx"
#include "ThreadDebugging.hxx"
#include "sdl_blitter/BilinearBlitter.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FBSurfaceSDL2::FBSurfaceSDL2(FrameBufferSDL2& buffer,
uInt32 width, uInt32 height, const uInt32* data)
: myFB(buffer),
mySurface(nullptr),
myTexture(nullptr),
mySecondaryTexture(nullptr),
mySurfaceIsDirty(true),
myIsVisible(true),
myTexAccess(SDL_TEXTUREACCESS_STREAMING),
myInterpolate(false),
myBlendEnabled(false),
myBlendAlpha(255)
myIsStatic(false)
{
myBlitter = make_unique<BilinearBlitter>(buffer);
createSurface(width, height, data);
}
@ -93,6 +89,8 @@ void FBSurfaceSDL2::setSrcPos(uInt32 x, uInt32 y)
{
mySrcR.x = x; mySrcR.y = y;
mySrcGUIR.moveTo(x, y);
reinitializeBlitter();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -100,6 +98,8 @@ void FBSurfaceSDL2::setSrcSize(uInt32 w, uInt32 h)
{
mySrcR.w = w; mySrcR.h = h;
mySrcGUIR.setWidth(w); mySrcGUIR.setHeight(h);
reinitializeBlitter();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -107,6 +107,8 @@ void FBSurfaceSDL2::setDstPos(uInt32 x, uInt32 y)
{
myDstR.x = x; myDstR.y = y;
myDstGUIR.moveTo(x, y);
reinitializeBlitter();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -114,6 +116,8 @@ void FBSurfaceSDL2::setDstSize(uInt32 w, uInt32 h)
{
myDstR.w = w; myDstR.h = h;
myDstGUIR.setWidth(w); myDstGUIR.setHeight(h);
reinitializeBlitter();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -132,10 +136,11 @@ void FBSurfaceSDL2::translateCoords(Int32& x, Int32& y) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FBSurfaceSDL2::render()
{
ASSERT_MAIN_THREAD;
// ASSERT_MAIN_THREAD;
if(myIsVisible)
{
/*
SDL_Texture* texture = myTexture;
if(myTexAccess == SDL_TEXTUREACCESS_STREAMING) {
@ -146,6 +151,10 @@ bool FBSurfaceSDL2::render()
SDL_RenderCopy(myFB.myRenderer, texture, &mySrcR, &myDstR);
*/
myBlitter->blit(*mySurface);
return true;
}
return false;
@ -161,7 +170,7 @@ void FBSurfaceSDL2::invalidate()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::free()
{
{/*
ASSERT_MAIN_THREAD;
SDL_Texture* textures[] = {myTexture, mySecondaryTexture};
@ -171,11 +180,15 @@ void FBSurfaceSDL2::free()
SDL_DestroyTexture(myTexture);
myTexture = nullptr;
}
*/
myBlitter->free();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::reload()
{
/*
ASSERT_MAIN_THREAD;
// Re-create texture; the underlying SDL_Surface is fine as-is
@ -202,6 +215,9 @@ void FBSurfaceSDL2::reload()
SDL_SetTextureAlphaMod(texture, myBlendAlpha);
}
}
*/
reinitializeBlitter();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -209,11 +225,14 @@ void FBSurfaceSDL2::resize(uInt32 width, uInt32 height)
{
ASSERT_MAIN_THREAD;
/*
// We will only resize when necessary, and not using static textures
if((myTexAccess == SDL_TEXTUREACCESS_STATIC) || (mySurface &&
int(width) <= mySurface->w && int(height) <= mySurface->h))
return; // don't need to resize at all
*/
if(mySurface)
SDL_FreeSurface(mySurface);
free();
@ -248,21 +267,28 @@ void FBSurfaceSDL2::createSurface(uInt32 width, uInt32 height,
if(data)
{
myTexAccess = SDL_TEXTUREACCESS_STATIC;
myStaticPitch = mySurface->w * 4; // we need pitch in 'bytes'
myStaticData = make_unique<uInt32[]>(mySurface->w * mySurface->h);
SDL_memcpy(myStaticData.get(), data, mySurface->w * mySurface->h * 4);
myIsStatic = true;
SDL_memcpy(mySurface->pixels, data, mySurface->w * mySurface->h * 4);
}
applyAttributes(false);
// applyAttributes(false);
// To generate texture
reload();
// reload();
reinitializeBlitter();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::reinitializeBlitter()
{
myBlitter->reinitialize(mySrcR, myDstR, myAttributes, myIsStatic ? mySurface : nullptr);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FBSurfaceSDL2::applyAttributes(bool immediate)
{
reinitializeBlitter();
/*
myInterpolate = myAttributes.smoothing;
myBlendEnabled = myAttributes.blending;
myBlendAlpha = uInt8(myAttributes.blendalpha * 2.55);
@ -273,4 +299,5 @@ void FBSurfaceSDL2::applyAttributes(bool immediate)
free();
reload();
}
*/
}

View File

@ -21,6 +21,7 @@
#include "bspf.hxx"
#include "FBSurface.hxx"
#include "FrameBufferSDL2.hxx"
#include "sdl_blitter/Blitter.hxx"
/**
An FBSurface suitable for the SDL2 Render2D API, making use of hardware
@ -40,7 +41,7 @@ class FBSurfaceSDL2 : public FBSurface
//
void fillRect(uInt32 x, uInt32 y, uInt32 w, uInt32 h, ColorId color) override;
// With hardware surfaces, it's faster to just update the entire surface
void setDirty() override { mySurfaceIsDirty = true; }
void setDirty() override {}
uInt32 width() const override;
uInt32 height() const override;
@ -66,6 +67,8 @@ class FBSurfaceSDL2 : public FBSurface
private:
void createSurface(uInt32 width, uInt32 height, const uInt32* data);
void reinitializeBlitter();
// Following constructors and assignment operators not supported
FBSurfaceSDL2() = delete;
FBSurfaceSDL2(const FBSurfaceSDL2&) = delete;
@ -76,21 +79,13 @@ class FBSurfaceSDL2 : public FBSurface
private:
FrameBufferSDL2& myFB;
unique_ptr<Blitter> myBlitter;
SDL_Surface* mySurface;
SDL_Texture* myTexture;
SDL_Texture* mySecondaryTexture;
SDL_Rect mySrcR, myDstR;
bool mySurfaceIsDirty;
bool myIsVisible;
SDL_TextureAccess myTexAccess; // Is pixel data constant or can it change?
bool myInterpolate; // Scaling is smoothed or blocky
bool myBlendEnabled; // Blending is enabled
uInt8 myBlendAlpha; // Alpha to use in blending mode
unique_ptr<uInt32[]> myStaticData; // The data to use when the buffer contents are static
uInt32 myStaticPitch; // The number of bytes in a row of static data
bool myIsStatic;
Common::Rect mySrcGUIR, myDstGUIR;
};

View File

@ -427,3 +427,15 @@ void FrameBufferSDL2::clear()
SDL_RenderClear(myRenderer);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SDL_Renderer* FrameBufferSDL2::renderer()
{
return myRenderer;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const SDL_PixelFormat& FrameBufferSDL2::pixelFormat()
{
return *myPixelFormat;
}

View File

@ -116,6 +116,10 @@ class FrameBufferSDL2 : public FrameBuffer
*/
void clear() override;
SDL_Renderer* renderer();
const SDL_PixelFormat& pixelFormat();
protected:
//////////////////////////////////////////////////////////////////////
// The following are derived from protected methods in FrameBuffer.hxx

View File

@ -25,7 +25,8 @@ MODULE_OBJS := \
src/common/FpsMeter.o \
src/common/ThreadDebugging.o \
src/common/StaggeredLogger.o \
src/common/repository/KeyValueRepositoryConfigfile.o
src/common/repository/KeyValueRepositoryConfigfile.o \
src/common/sdl_blitter/BilinearBlitter.o
MODULE_DIRS += \
src/common

View File

@ -0,0 +1,140 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include "BilinearBlitter.hxx"
#include "ThreadDebugging.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BilinearBlitter::BilinearBlitter(FrameBufferSDL2& fb) :
myTexture(nullptr),
mySecondaryTexture(nullptr),
myTexturesAreAllocated(false),
myRecreateTextures(false),
myStaticData(nullptr),
myFB(fb)
{}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BilinearBlitter::~BilinearBlitter()
{
free();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void BilinearBlitter::reinitialize(
SDL_Rect srcRect,
SDL_Rect destRect,
FBSurface::Attributes attributes,
SDL_Surface* staticData
)
{
myRecreateTextures = !(
mySrcRect.w == srcRect.w &&
mySrcRect.h == srcRect.h &&
myDstRect.w == destRect.w &&
myDstRect.h == destRect.h &&
attributes == myAttributes &&
myStaticData == staticData
);
myStaticData = staticData;
mySrcRect = srcRect;
myDstRect = destRect;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void BilinearBlitter::free()
{
if (!myTexturesAreAllocated) {
return;
}
ASSERT_MAIN_THREAD;
SDL_Texture* textures[] = {myTexture, mySecondaryTexture};
for (SDL_Texture* texture: textures) {
if (!texture) continue;
SDL_DestroyTexture(texture);
}
myTexturesAreAllocated = false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void BilinearBlitter::blit(SDL_Surface& surface)
{
ASSERT_MAIN_THREAD;
recreateTexturesIfNecessary();
SDL_Texture* texture = myTexture;
if(myStaticData == nullptr) {
SDL_UpdateTexture(myTexture, &mySrcRect, surface.pixels, surface.pitch);
myTexture = mySecondaryTexture;
mySecondaryTexture = texture;
}
SDL_RenderCopy(myFB.renderer(), texture, &mySrcRect, &myDstRect);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void BilinearBlitter::recreateTexturesIfNecessary()
{
if (myTexturesAreAllocated && !myRecreateTextures) {
return;
}
ASSERT_MAIN_THREAD;
if (myTexturesAreAllocated) {
free();
}
SDL_TextureAccess texAccess = myStaticData == nullptr ? SDL_TEXTUREACCESS_STREAMING : SDL_TEXTUREACCESS_STATIC;
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, myAttributes.smoothing ? "1" : "0");
myTexture = SDL_CreateTexture(myFB.renderer(), myFB.pixelFormat().format,
texAccess, mySrcRect.w, mySrcRect.h);
if (myStaticData == nullptr) {
mySecondaryTexture = SDL_CreateTexture(myFB.renderer(), myFB.pixelFormat().format,
texAccess, mySrcRect.w, mySrcRect.h);
} else {
mySecondaryTexture = nullptr;
SDL_UpdateTexture(myTexture, nullptr, myStaticData->pixels, myStaticData->pitch);
}
if (myAttributes.blending) {
uInt8 blendAlpha = uInt8(myAttributes.blendalpha * 2.55);
SDL_Texture* textures[] = {myTexture, mySecondaryTexture};
for (SDL_Texture* texture: textures) {
if (!texture) continue;
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
SDL_SetTextureAlphaMod(texture, blendAlpha);
}
}
myRecreateTextures = false;
myTexturesAreAllocated = true;
}

View File

@ -0,0 +1,73 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#ifndef BILINEAR_BLITTER_HXX
#define BILINEAR_BLITTER_HXX
#include "Blitter.hxx"
#include "FrameBufferSDL2.hxx"
#include "SDL_lib.hxx"
class BilinearBlitter : public Blitter {
public:
BilinearBlitter(FrameBufferSDL2& fb);
virtual ~BilinearBlitter();
virtual void reinitialize(
SDL_Rect srcRect,
SDL_Rect destRect,
FBSurface::Attributes attributes,
SDL_Surface* staticData = nullptr
) override;
virtual void blit(SDL_Surface& surface) override;
virtual void free() override;
private:
SDL_Texture* myTexture;
SDL_Texture* mySecondaryTexture;
SDL_Rect mySrcRect, myDstRect;
FBSurface::Attributes myAttributes;
bool myTexturesAreAllocated;
bool myRecreateTextures;
SDL_Surface* myStaticData;
FrameBufferSDL2& myFB;
private:
void recreateTexturesIfNecessary();
private:
BilinearBlitter(const BilinearBlitter&) = delete;
BilinearBlitter(BilinearBlitter&&) = delete;
BilinearBlitter& operator=(const BilinearBlitter&) = delete;
BilinearBlitter& operator=(BilinearBlitter&&) = delete;
};
#endif // BILINEAR_BLITTER_HXX

View File

@ -0,0 +1,57 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#ifndef BLITTER_HXX
#define BLITTER_HXX
#include "bspf.hxx"
#include "SDL_lib.hxx"
#include "FBSurface.hxx"
class Blitter {
public:
virtual ~Blitter() = default;
virtual void reinitialize(
SDL_Rect srcRect,
SDL_Rect destRect,
FBSurface::Attributes attributes,
SDL_Surface* staticData = nullptr
) = 0;
virtual void blit(SDL_Surface& surface) = 0;
virtual void free() = 0;
protected:
Blitter() = default;
private:
Blitter(const Blitter&) = delete;
Blitter(Blitter&&) = delete;
Blitter& operator=(const Blitter&) = delete;
Blitter& operator=(Blitter&&) = delete;
};
#endif // BLITTER_HXX

View File

@ -451,3 +451,9 @@ bool FBSurface::checkBounds(const uInt32 x, const uInt32 y) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const uInt32* FBSurface::myPalette = nullptr;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool operator==(const FBSurface::Attributes& a1, const FBSurface::Attributes& a2)
{
return a1.blendalpha == a2.blendalpha && a1.blending == a2.blending && a1.smoothing && a2.smoothing;
}

View File

@ -389,4 +389,6 @@ class FBSurface
FBSurface& operator=(FBSurface&&) = delete;
};
bool operator==(const FBSurface::Attributes& a1, const FBSurface::Attributes& a2);
#endif