FS#84 - Rewrite the OpenGL context handling

FS#66 - Free the global rendering context
Added thread-local storage classes

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1116 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
laurentgom 2009-05-31 11:38:54 +00:00
parent 3e23e46a64
commit 23bf546b6a
44 changed files with 1263 additions and 405 deletions

View file

@ -36,6 +36,8 @@
#include <SFML/System/Randomizer.hpp>
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Thread.hpp>
#include <SFML/System/ThreadLocal.hpp>
#include <SFML/System/ThreadLocalPtr.hpp>
#include <SFML/System/Unicode.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/Vector3.hpp>

View file

@ -0,0 +1,95 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_THREADLOCAL_HPP
#define SFML_THREADLOCAL_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <cstdlib>
namespace sf
{
namespace priv
{
class ThreadLocalImpl;
}
////////////////////////////////////////////////////////////
/// Wrapper to handle variables that are local to threads.
/// This means that the same ThreadLocalStorage variable will
/// hold a different value for each different thread.
/// For a more strongly-typed interface, use the ThreadLocal
/// template class.
////////////////////////////////////////////////////////////
class SFML_API ThreadLocal : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Default constructor
///
/// \param Value : Optional value to initalize the variable (NULL by default)
///
////////////////////////////////////////////////////////////
ThreadLocal(void* Value = NULL);
////////////////////////////////////////////////////////////
/// Destructor
///
////////////////////////////////////////////////////////////
~ThreadLocal();
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
///
/// \param Value : Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void SetValue(void* Value);
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
///
/// \return Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void* GetValue() const;
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
priv::ThreadLocalImpl* myImpl; ///< Pointer to the OS specific implementation
};
} // namespace sf
#endif // SFML_THREADLOCAL_HPP

View file

@ -0,0 +1,102 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_THREADLOCALPTR_HPP
#define SFML_THREADLOCALPTR_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/ThreadLocal.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
/// Type-safe wrapper for thread local pointer variables
////////////////////////////////////////////////////////////
template <typename T>
class ThreadLocalPtr : private ThreadLocal
{
public :
////////////////////////////////////////////////////////////
/// Default constructor
///
/// \param Value : Optional value to initalize the variable (NULL by default)
///
////////////////////////////////////////////////////////////
ThreadLocalPtr(T* Value = NULL);
////////////////////////////////////////////////////////////
/// Operator * overload to return a reference to the variable
///
/// \return Reference to the thread-local value of the variable
///
////////////////////////////////////////////////////////////
T& operator *() const;
////////////////////////////////////////////////////////////
/// Operator -> overload to return a pointer to the variable
///
/// \return Pointer to the thread-local value of the variable
///
////////////////////////////////////////////////////////////
T* operator ->() const;
////////////////////////////////////////////////////////////
/// Implicit cast operator to T*
///
/// \return Value of the pointer for this thread
///
////////////////////////////////////////////////////////////
operator T*() const;
////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param Value : New pointer value to assign for this thread
///
/// \return Reference to this
///
////////////////////////////////////////////////////////////
ThreadLocalPtr<T>& operator =(T* Value);
////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param Other : Other thread-local pointer value to assign
///
/// \return Reference to this
///
////////////////////////////////////////////////////////////
ThreadLocalPtr<T>& operator =(const ThreadLocalPtr<T>& Other);
};
} // namespace sf
#include <SFML/System/ThreadLocalPtr.inl>
#endif // SFML_THREADLOCALPTR_HPP

View file

@ -0,0 +1,89 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
namespace sf
{
////////////////////////////////////////////////////////////
/// Default constructor
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>::ThreadLocalPtr(T* Value) :
ThreadLocal(Value)
{
}
////////////////////////////////////////////////////////////
/// Operator * overload to return a reference to the variable
////////////////////////////////////////////////////////////
template <typename T>
T& ThreadLocalPtr<T>::operator *() const
{
return *static_cast<T*>(GetValue());
}
////////////////////////////////////////////////////////////
/// Operator -> overload to return a pointer to the variable
////////////////////////////////////////////////////////////
template <typename T>
T* ThreadLocalPtr<T>::operator ->() const
{
return static_cast<T*>(GetValue());
}
////////////////////////////////////////////////////////////
/// Implicit cast operator to T*
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>::operator T*() const
{
return static_cast<T*>(GetValue());
}
////////////////////////////////////////////////////////////
/// Assignment operator
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>& ThreadLocalPtr<T>::operator =(T* Value)
{
SetValue(Value);
return *this;
}
////////////////////////////////////////////////////////////
/// Assignment operator
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>& ThreadLocalPtr<T>::operator =(const ThreadLocalPtr<T>& Other)
{
SetValue(Other.GetValue());
return *this;
}
} // namespace sf

View file

@ -29,7 +29,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/NonCopyable.hpp>
@ -37,132 +36,70 @@ namespace sf
{
namespace priv
{
class WindowImpl;
class ContextGL;
}
////////////////////////////////////////////////////////////
/// Abstract class representing an OpenGL context
/// Class holding a valid drawing context
///
/// If you need to make OpenGL / graphics calls without
/// having an active window (like in a thread), you can use
/// an instance of this class to get a valid context.
///
/// Having a valid context is necessary for *every* OpenGL call,
/// and for most of the classes from the Graphics package.
///
/// Note that a context is only active in its current thread,
/// if you create a new thread it will have no valid context
/// by default.
///
/// \code
/// void ThreadedFunc(void*)
/// {
/// sf::Context Ctx;
/// // from now on, you have a valid context
///
/// // you can make OpenGL calls
/// glClear(GL_DEPTH_BUFFER_BIT);
///
/// // as well as using objects from the graphics package
/// sf::Image Img;
/// Img.LoadFromFile("image.png");
/// }
/// // the context is automatically deactivated and destroyed
/// // by the sf::Context destructor
/// \endcode
////////////////////////////////////////////////////////////
class SFML_API Context : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Create a new context, not associated to a window
///
/// \return Pointer to the created context
///
////////////////////////////////////////////////////////////
static Context* New();
////////////////////////////////////////////////////////////
/// Create a new context attached to a window
///
/// \param Owner : Pointer to the owner window
/// \param BitsPerPixel : Pixel depth (in bits per pixel)
/// \param Settings : Creation parameters
///
/// \return Pointer to the created context
///
////////////////////////////////////////////////////////////
static Context* New(const priv::WindowImpl* Owner, unsigned int BitsPerPixel, const ContextSettings& Settings);
////////////////////////////////////////////////////////////
/// Check if a context is active on the current thread
///
/// \return True if there's an active context, false otherwise
///
////////////////////////////////////////////////////////////
static bool IsContextActive();
////////////////////////////////////////////////////////////
/// Return the default context
///
/// \return Reference to the default context
///
////////////////////////////////////////////////////////////
static Context& GetDefault();
public :
////////////////////////////////////////////////////////////
/// Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~Context();
////////////////////////////////////////////////////////////
/// Get the settings of the context
///
/// \return Structure containing the settings
///
////////////////////////////////////////////////////////////
const ContextSettings& GetSettings() const;
////////////////////////////////////////////////////////////
/// Activate or deactivate the context as the current target
/// for rendering
///
/// \param Active : True to activate, false to deactivate
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
bool SetActive(bool Active);
////////////////////////////////////////////////////////////
/// Display the contents of the context
///
////////////////////////////////////////////////////////////
virtual void Display() = 0;
////////////////////////////////////////////////////////////
/// Enable / disable vertical synchronization
///
/// \param Enabled : True to enable v-sync, false to deactivate
///
////////////////////////////////////////////////////////////
virtual void UseVerticalSync(bool Enabled) = 0;
protected :
////////////////////////////////////////////////////////////
/// Default constructor
/// Default constructor -- creates and activates the context
///
////////////////////////////////////////////////////////////
Context();
////////////////////////////////////////////////////////////
/// Make this context the current one
///
/// \param Active : True to activate, false to deactivate
///
/// \return True on success, false if any error happened
/// Destructor -- deactivates and destroys the context
///
////////////////////////////////////////////////////////////
virtual bool MakeCurrent(bool Active) = 0;
~Context();
////////////////////////////////////////////////////////////
/// Evaluate a pixel format configuration.
/// This functions can be used by implementations that have
/// several valid formats and want to get the best one
/// Activate or deactivate explicitely the context
///
/// \param BitsPerPixel : Requested pixel depth (bits per pixel)
/// \param Settings : Requested additionnal settings
/// \param ColorBits : Color bits of the configuration to evaluate
/// \param DepthBits : Depth bits of the configuration to evaluate
/// \param StencilBits : Stencil bits of the configuration to evaluate
/// \param Antialiasing : Antialiasing level of the configuration to evaluate
///
/// \return Score of the configuration : the lower the better
/// \param Active True to activate, false to deactivate
///
////////////////////////////////////////////////////////////
static int EvaluateFormat(unsigned int BitsPerPixel, const ContextSettings& Settings, int ColorBits, int DepthBits, int StencilBits, int Antialiasing);
void SetActive(bool Active);
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ContextSettings mySettings; ///< Creation settings of the context
priv::ContextGL* myContext; ///< Internal OpenGL context
};
} // namespace sf

View file

@ -46,10 +46,9 @@ namespace sf
namespace priv
{
class WindowImpl;
class ContextGL;
}
class Context;
////////////////////////////////////////////////////////////
/// Window is a rendering window ; it can create a new window
/// or connect to an existing one
@ -309,7 +308,7 @@ private :
// Member data
////////////////////////////////////////////////////////////
priv::WindowImpl* myWindow; ///< Platform-specific implementation of the window
Context* myContext; ///< Platform-specific implementation of the OpenGL context
priv::ContextGL* myContext; ///< Platform-specific implementation of the OpenGL context
std::queue<Event> myEvents; ///< Queue of received events
Input myInput; ///< Input manager connected to window
Clock myClock; ///< Clock for measuring the elapsed time between frames