2007-06-11 11:50:49 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file factory.hpp Factory to 'query' all available blitters. */
|
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
#ifndef BLITTER_FACTORY_HPP
|
|
|
|
#define BLITTER_FACTORY_HPP
|
2007-06-11 11:50:49 +00:00
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "base.hpp"
|
2007-09-13 14:44:49 +00:00
|
|
|
#include "../debug.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "../string_func.h"
|
2007-06-11 11:50:49 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base factory, keeping track of all blitters.
|
|
|
|
*/
|
|
|
|
class BlitterFactoryBase {
|
|
|
|
private:
|
|
|
|
char *name;
|
|
|
|
typedef std::map<std::string, BlitterFactoryBase *> Blitters;
|
|
|
|
|
|
|
|
static Blitters &GetBlitters()
|
|
|
|
{
|
|
|
|
static Blitters &s_blitters = *new Blitters();
|
|
|
|
return s_blitters;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Blitter **GetActiveBlitter()
|
|
|
|
{
|
|
|
|
static Blitter *s_blitter = NULL;
|
|
|
|
return &s_blitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Register a blitter internally, based on his name.
|
|
|
|
* @param name the name of the blitter.
|
|
|
|
* @note an assert() will be trigger if 2 blitters with the same name try to register.
|
|
|
|
*/
|
|
|
|
void RegisterBlitter(const char *name)
|
|
|
|
{
|
|
|
|
/* Don't register nameless Blitters */
|
|
|
|
if (name == NULL) return;
|
|
|
|
|
|
|
|
this->name = strdup(name);
|
2008-06-01 15:08:14 +00:00
|
|
|
|
|
|
|
std::pair<Blitters::iterator, bool> P = GetBlitters().insert(Blitters::value_type(name, this));
|
2007-06-11 11:50:49 +00:00
|
|
|
assert(P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlitterFactoryBase() :
|
|
|
|
name(NULL)
|
|
|
|
{}
|
|
|
|
|
2008-05-18 12:47:08 +00:00
|
|
|
virtual ~BlitterFactoryBase()
|
|
|
|
{
|
|
|
|
if (this->name == NULL) return;
|
|
|
|
GetBlitters().erase(this->name);
|
|
|
|
if (GetBlitters().empty()) delete &GetBlitters();
|
|
|
|
free(this->name);
|
|
|
|
}
|
2007-06-11 11:50:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the requested blitter and return his class.
|
|
|
|
* @param name the blitter to select.
|
|
|
|
* @post Sets the blitter so GetCurrentBlitter() returns it too.
|
|
|
|
*/
|
|
|
|
static Blitter *SelectBlitter(const char *name)
|
|
|
|
{
|
2007-09-13 12:28:53 +00:00
|
|
|
const char *default_blitter = "8bpp-optimized";
|
|
|
|
|
2007-06-11 11:50:49 +00:00
|
|
|
if (GetBlitters().size() == 0) return NULL;
|
2007-09-13 12:28:53 +00:00
|
|
|
const char *bname = (StrEmpty(name)) ? default_blitter : name;
|
2007-06-11 11:50:49 +00:00
|
|
|
|
|
|
|
Blitters::iterator it = GetBlitters().begin();
|
|
|
|
for (; it != GetBlitters().end(); it++) {
|
|
|
|
BlitterFactoryBase *b = (*it).second;
|
2007-09-13 12:28:53 +00:00
|
|
|
if (strcasecmp(bname, b->name) == 0) {
|
2007-06-11 11:50:49 +00:00
|
|
|
Blitter *newb = b->CreateInstance();
|
2007-06-27 17:19:05 +00:00
|
|
|
delete *GetActiveBlitter();
|
2007-06-11 11:50:49 +00:00
|
|
|
*GetActiveBlitter() = newb;
|
2007-09-13 14:44:49 +00:00
|
|
|
|
|
|
|
DEBUG(driver, 1, "Successfully %s blitter '%s'",StrEmpty(name) ? "probed" : "loaded", bname);
|
2007-06-11 11:50:49 +00:00
|
|
|
return newb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current active blitter (always set by calling SelectBlitter).
|
|
|
|
*/
|
|
|
|
static Blitter *GetCurrentBlitter()
|
|
|
|
{
|
|
|
|
return *GetActiveBlitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *GetBlittersInfo(char *p, const char *last)
|
|
|
|
{
|
|
|
|
p += snprintf(p, last - p, "List of blitters:\n");
|
|
|
|
Blitters::iterator it = GetBlitters().begin();
|
|
|
|
for (; it != GetBlitters().end(); it++) {
|
|
|
|
BlitterFactoryBase *b = (*it).second;
|
|
|
|
p += snprintf(p, last - p, "%18s: %s\n", b->name, b->GetDescription());
|
|
|
|
}
|
|
|
|
p += snprintf(p, last - p, "\n");
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a nice description of the blitter-class.
|
|
|
|
*/
|
|
|
|
virtual const char *GetDescription() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance of this Blitter-class.
|
|
|
|
*/
|
|
|
|
virtual Blitter *CreateInstance() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A template factory, so ->GetName() works correctly. This because else some compiler will complain.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class BlitterFactory: public BlitterFactoryBase {
|
|
|
|
public:
|
|
|
|
BlitterFactory() { this->RegisterBlitter(((T *)this)->GetName()); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the long, human readable, name for the Blitter-class.
|
|
|
|
*/
|
|
|
|
const char *GetName();
|
|
|
|
};
|
|
|
|
|
2008-01-13 21:41:24 +00:00
|
|
|
extern char _ini_blitter[32];
|
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
#endif /* BLITTER_FACTORY_HPP */
|