Commit 6adbd839 authored by Patrick Julien's avatar Patrick Julien

new tile stuff

svn path=/trunk/koffice/; revision=174691
parent 09621eb5
INCLUDES = -I$(srcdir)/../helper -I$(srcdir)/../../ui -I$(srcdir)/.. -I$(srcdir)/../../tools $(KOFFICE_INCLUDES) -I$(interfacedir) \
$(KOPAINTER_INCLUDES) $(LIBMAGICK_CPPFLAGS) $(all_includes)
noinst_LTLIBRARIES = libkistile.la
libkistile_la_SOURCES = kistile.cpp kistilemgr.cpp kistilemediator.cpp kispixeldata.cpp
libkistile_la_METASOURCES = AUTO
libkistile_la_LDFLAGS = $(KDE_RPATH) $(LIBMAGICK_RPATH)
libkistile_la_LIBADD = $(LIBMAGICK_LIBS)
KDE_CXXFLAGS = $(USE_EXCEPTIONS)
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "kispixeldata.h"
KisPixelData::~KisPixelData()
{
if (owner)
delete[] data;
}
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined KISPIXELDATA_H_
#define KISPIXELDATA_H_
#include "kistile.h"
#include "kistilemgr.h"
struct KisPixelData : public KShared {
virtual ~KisPixelData();
KisTileMgrSP mgr;
KisTileSP tile;
Q_INT32 mode;
Q_INT32 x1;
Q_INT32 y1;
Q_INT32 x2;
Q_INT32 y2;
Q_UINT16 *data;
bool owner;
Q_INT32 width;
Q_INT32 height;
Q_INT32 stride;
Q_INT32 depth;
};
typedef KSharedPtr<KisPixelData> KisPixelDataSP;
#endif // KISPIXELDATA_H_
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <qtl.h>
#include "kis_global.h"
#include "kistilecache.h"
#include "kistile.h"
#include "kistileswap.h"
KisTile::KisTile(Q_INT32 depth, KisTileCacheInterface *cache, KisTileSwapInterface *swap)
{
init(depth, cache, swap);
}
KisTile::KisTile(KisTile& rhs) : super(rhs)
{
if (this != &rhs) {
init(rhs.m_depth, rhs.m_cache, rhs.m_swap);
m_width = rhs.m_width;
m_height = rhs.m_height;
m_valid = rhs.valid();
allocate();
m_hints = rhs.m_hints;
if (rhs.m_data) {
memcpy(m_data, rhs.m_data, size());
} else {
rhs.lock();
memcpy(m_data, rhs.m_data, size());
rhs.release();
}
}
}
KisTile::~KisTile()
{
if (m_data) {
delete[] m_data;
m_data = 0;
}
m_swap -> remove(m_swapNo);
}
QMutex *KisTile::mutex()
{
return &m_mutex;
}
void KisTile::lock()
{
#if 0
m_mutex.lock();
// TODO No swap implementation right now, tile always in core
m_mutex.unlock();
#endif
}
void KisTile::lockAsync()
{
}
void KisTile::release()
{
#if 0
// TODO No swap implementation right now, tile always in core
m_mutex.unlock();
#endif
}
void KisTile::allocate()
{
if (m_data == 0)
m_data = new Q_UINT16[size()];
}
Q_UINT16 *KisTile::data(Q_INT32 xoff, Q_INT32 yoff) const
{
Q_INT32 offset = yoff * m_width + xoff;
return m_data + offset * m_depth;
}
Q_INT32 KisTile::width() const
{
return m_width;
}
void KisTile::width(Q_INT32 w)
{
m_width = w;
}
Q_INT32 KisTile::height() const
{
return m_height;
}
void KisTile::height(Q_INT32 h)
{
m_height = h;
}
Q_INT32 KisTile::depth() const
{
return m_depth;
}
Q_INT32 KisTile::size() const
{
return m_width * m_height * m_depth;
}
bool KisTile::valid() const
{
return m_valid;
}
void KisTile::valid(bool valid)
{
m_mutex.lock();
m_valid = valid;
if (m_valid) {
// ?!?
} else {
if (m_cache)
m_cache -> flush(this);
if (m_data) {
delete[] m_data;
m_data = 0;
}
if (m_swap)
m_swap -> swapDel(this);
}
m_mutex.unlock();
}
bool KisTile::dirty() const
{
return m_dirty;
}
void KisTile::dirty(bool val)
{
m_dirty = val;
}
Q_INT32 KisTile::rowHint(Q_INT32 row) const
{
if (m_hints.empty())
return unknown;
if (row >= height() || row < 0)
return outofrange;
return m_hints[row];
}
void KisTile::setRowHint(Q_INT32 row, KisTile::drawingHints hint)
{
initRowHints();
if (row < height() || row >= 0)
m_hints[row] = hint;
}
void KisTile::init(Q_INT32 depth, KisTileCacheInterface *cache, KisTileSwapInterface *swap)
{
m_dirty = false;
m_valid = false;
m_width = TILE_WIDTH;
m_height = TILE_HEIGHT;
m_depth = depth;
m_data = 0;
m_datacnt = 0;
m_swap = swap;
m_swapNo = SWAP_IN_CORE;
m_cache = cache;
m_nref = 0;
m_nshare = 0;
m_nwrite = 0;
}
void KisTile::initRowHints()
{
if (m_hints.empty()) {
Q_INT32 height = m_height;
m_hints.resize(height);
qFill(m_hints.begin(), m_hints.end(), unknown);
}
}
Q_INT32 KisTile::refCount() const
{
return m_nref;
}
void KisTile::ref()
{
m_nref++;
}
Q_INT32 KisTile::shareCount() const
{
return m_nshare;
}
void KisTile::shareRef()
{
m_nshare++;
}
void KisTile::shareRelease()
{
m_nshare--;
}
Q_INT32 KisTile::writeCount() const
{
return m_nwrite;
}
void KisTile::writeRef()
{
m_nwrite++;
}
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined KISTILE_H_
#define KISTILE_H_
#include <qcstring.h>
#include <qglobal.h>
#include <qmutex.h>
#include <qvaluevector.h>
#include <ksharedptr.h>
class KisTileCacheInterface;
class KisTileSwapInterface;
/**
* Provides abstraction to a tile. A tile contains
* a part of a layer. Layers form an image.
*/
class KisTile : public KShared {
typedef KShared super;
public:
enum drawingHints {
broken,
opaque,
transparent,
mixed,
outofrange,
undef,
unknown
};
public:
KisTile(Q_INT32 depth, KisTileCacheInterface *cache, KisTileSwapInterface *swap);
KisTile(KisTile& rhs);
virtual ~KisTile();
public:
QMutex *mutex();
void lock();
void lockAsync();
void release();
void allocate();
Q_UINT16 *data(Q_INT32 xoff = 0, Q_INT32 yoff = 0) const;
Q_INT32 width() const;
void width(Q_INT32 w);
Q_INT32 height() const;
void height(Q_INT32 h);
Q_INT32 depth() const;
Q_INT32 size() const;
bool dirty() const;
void dirty(bool val);
bool valid() const;
void valid(bool valid);
Q_INT32 rowHint(Q_INT32 row) const;
void setRowHint(Q_INT32 row, drawingHints hint);
Q_INT32 refCount() const;
void ref();
Q_INT32 shareCount() const;
void shareRef();
void shareRelease();
Q_INT32 writeCount() const;
void writeRef();
private:
KisTile& operator=(const KisTile&);
void init(Q_INT32 depth, KisTileCacheInterface *cache, KisTileSwapInterface *swap);
void initRowHints();
private:
bool m_dirty;
bool m_valid;
Q_UINT16 *m_data;
Q_INT32 m_datacnt;
Q_INT32 m_width;
Q_INT32 m_height;
Q_INT32 m_depth;
QMutex m_mutex;
QValueVector<drawingHints> m_hints;
KisTileSwapInterface *m_swap;
KisTileCacheInterface *m_cache;
Q_INT32 m_swapNo;
Q_INT32 m_nref;
Q_INT32 m_nshare;
Q_INT32 m_nwrite;
};
typedef KSharedPtr<KisTile> KisTileSP;
typedef QValueVector<KisTileSP> vKisTileSP;
typedef vKisTileSP::iterator vKisTileSP_it;
typedef vKisTileSP::const_iterator vKisTileSPLst_cit;
#endif // KISTILE_H_
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined KISTILECACHE_H_
#define KISTILECACHE_H_
#include <qglobal.h>
#include <ksharedptr.h>
#include "kistile.h"
class KisTileCacheInterface : public KShared {
typedef KShared super;
public:
KisTileCacheInterface();
virtual ~KisTileCacheInterface();
public:
virtual void sizeHint(Q_INT32 nelements) = 0;
virtual void flush(KisTileSP tile) = 0;
virtual void insert(KisTileSP tile) = 0;
};
inline
KisTileCacheInterface::KisTileCacheInterface()
{
}
inline
KisTileCacheInterface::~KisTileCacheInterface()
{
}
#endif // KISTILECACHE_H_
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <qpair.h>
#include <qmap.h>
#include <qtl.h>
#include <qvaluelist.h>
#include "kisscopedlock.h"
#include "kistilemediator.h"
class KisTileMediatorSingleton {
typedef QPair<KisTileMgrSP, Q_INT32> KisTileLink;
typedef QValueList<KisTileLink> vKisTileLink;
typedef vKisTileLink::iterator vKisTileLink_it;
typedef QMap<KisTileSP, vKisTileLink> KisTileLinkMap;
typedef KisTileLinkMap::iterator KisTileLinkMap_it;
public:
KisTileMediatorSingleton();
~KisTileMediatorSingleton();
public:
void attach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum);
void detach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum);
void detachAll(KisTileMgrSP mgr);
Q_INT32 tileNum(KisTileSP tile, KisTileMgrSP mgr);
private:
KisTileLinkMap m_links;
};
QMutex KisTileMediator::m_mutex;
KisTileMediatorSingleton *KisTileMediator::m_instance = 0;
Q_INT32 KisTileMediator::m_ref = 0;
KisTileMediator::KisTileMediator()
{
KisScopedLock l(&KisTileMediator::m_mutex);
if (KisTileMediator::m_ref == 0)
KisTileMediator::m_instance = new KisTileMediatorSingleton;
KisTileMediator::m_ref++;
}
KisTileMediator::~KisTileMediator()
{
KisScopedLock l(&KisTileMediator::m_mutex);
KisTileMediator::m_ref--;
if (KisTileMediator::m_ref == 0) {
delete KisTileMediator::m_instance;
KisTileMediator::m_instance = 0;
}
}
void KisTileMediator::attach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum)
{
KisScopedLock l(&KisTileMediator::m_mutex);
KisTileMediator::m_instance -> attach(tile, mgr, tilenum);
}
void KisTileMediator::detach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum)
{
KisScopedLock l(&KisTileMediator::m_mutex);
KisTileMediator::m_instance -> detach(tile, mgr, tilenum);
}
Q_INT32 KisTileMediator::tileNum(KisTileSP tile, KisTileMgrSP mgr)
{
KisScopedLock l(&KisTileMediator::m_mutex);
KisTileMediator::m_instance -> tileNum(tile, mgr);
}
void KisTileMediator::detachAll(KisTileMgrSP mgr)
{
KisScopedLock l(&KisTileMediator::m_mutex);
KisTileMediator::m_instance -> detachAll(mgr);
}
KisTileMediatorSingleton::KisTileMediatorSingleton()
{
}
KisTileMediatorSingleton::~KisTileMediatorSingleton()
{
}
void KisTileMediatorSingleton::attach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum)
{
if (m_links.count(tile) == 0)
m_links[tile] = vKisTileLink();
m_links[tile].push_back(qMakePair(mgr, tilenum));
tile -> shareRef();
}
void KisTileMediatorSingleton::detach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum)
{
if (m_links.count(tile)) {
vKisTileLink& l = m_links[tile];
for (vKisTileLink_it it = l.begin(); it != l.end(); it++) {
const KisTileLink& link = *it;
if (link.first == mgr && link.second == tilenum) {
tile -> shareRelease();
it = l.erase(it);
}
}
if (l.empty())
m_links.erase(tile);
}
}
void KisTileMediatorSingleton::detachAll(KisTileMgrSP mgr)
{
for (KisTileLinkMap_it it = m_links.begin(); it != m_links.end(); it++) {
KisTileSP tile = it.key();
vKisTileLink& l = it.data();
for (vKisTileLink_it lit = l.begin(); lit != l.end(); lit++) {
const KisTileLink& link = *lit;
if (link.first == mgr) {
tile -> shareRelease();
lit = l.erase(lit);
}
}
if (l.empty())
m_links.erase(it);
}
}
Q_INT32 KisTileMediatorSingleton::tileNum(KisTileSP tile, KisTileMgrSP mgr)
{
if (!m_links.count(tile))
return TILE_NOT_ATTACHED;
vKisTileLink& l = m_links[tile];
for (vKisTileLink_it it = l.begin(); it != l.end(); it++) {
const KisTileLink& link = *it;
if (link.first == mgr)
return link.second;
}
return TILE_NOT_ATTACHED;
}
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined KISTILEMEDIATOR_H_
#define KISTILEMEDIATOR_H_
#include <qmutex.h>
#include "kistile.h"
#include "kistilemgr.h"
#define TILE_NOT_ATTACHED -1
class KisTileMediatorSingleton;
class KisTileMediator {
public:
KisTileMediator();
~KisTileMediator();
void attach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum);
void detach(KisTileSP tile, KisTileMgrSP mgr, Q_INT32 tilenum);
void detachAll(KisTileMgrSP mgr);
Q_INT32 tileNum(KisTileSP tile, KisTileMgrSP mgr);
private:
KisTileMediator(const KisTileMediator&);
KisTileMediator& operator=(const KisTileMediator&);
private:
static QMutex m_mutex;
static KisTileMediatorSingleton *m_instance;
static Q_INT32 m_ref;
};
#endif // KISTILEMEDIATOR_H_
This diff is collapsed.
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software