Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #ifndef _MATRIX_FILE_CACHE_H_ Chris@148: #define _MATRIX_FILE_CACHE_H_ Chris@148: Chris@148: #include "base/ResizeableBitset.h" Chris@148: Chris@148: #include "FileReadThread.h" Chris@148: Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: Chris@148: class MatrixFile : public QObject Chris@148: { Chris@148: Q_OBJECT Chris@148: Chris@148: public: Chris@148: enum Mode { ReadOnly, ReadWrite }; Chris@148: Chris@148: /** Chris@148: * Construct a MatrixFile object reading from and/or writing to Chris@148: * the matrix file with the given base name in the application's Chris@148: * temporary directory. Chris@148: * Chris@148: * If mode is ReadOnly, the file must exist and be readable. Chris@148: * Chris@148: * If mode is ReadWrite and the file does not exist, it will be Chris@148: * created. If mode is ReadWrite and the file does exist, the Chris@148: * existing file will be used and the mode will be reset to Chris@148: * ReadOnly. Call getMode() to check whether this has occurred Chris@148: * after construction. Chris@148: * Chris@148: * cellSize specifies the size in bytes of the object type stored Chris@148: * in the matrix. For example, use cellSize = sizeof(float) for a Chris@148: * matrix of floats. The MatrixFile object doesn't care about the Chris@148: * objects themselves, it just deals with raw data of a given size. Chris@148: * Chris@148: * If eagerCache is true, blocks from the file will be cached for Chris@148: * read. If eagerCache is false, only columns that have been set Chris@148: * by calling setColumnAt on this MatrixFile (i.e. columns for Chris@148: * which haveSetColumnAt returns true) will be cached. Chris@148: */ Chris@148: MatrixFile(QString fileBase, Mode mode, size_t cellSize, bool eagerCache); Chris@148: virtual ~MatrixFile(); Chris@148: Chris@148: Mode getMode() const { return m_mode; } Chris@148: Chris@148: size_t getWidth() const { return m_width; } Chris@148: size_t getHeight() const { return m_height; } Chris@148: size_t getCellSize() const { return m_cellSize; } Chris@148: Chris@148: void resize(size_t width, size_t height); Chris@148: void reset(); Chris@148: Chris@148: bool haveSetColumnAt(size_t x) const { return m_columnBitset->get(x); } Chris@148: void getColumnAt(size_t x, void *data); Chris@148: void setColumnAt(size_t x, const void *data); Chris@148: Chris@148: void suspend(); Chris@148: Chris@148: protected: Chris@148: int m_fd; Chris@148: Mode m_mode; Chris@148: int m_flags; Chris@148: mode_t m_fmode; Chris@148: size_t m_cellSize; Chris@148: size_t m_width; Chris@148: size_t m_height; Chris@148: size_t m_headerSize; Chris@148: QString m_fileName; Chris@148: size_t m_defaultCacheWidth; Chris@148: size_t m_prevX; Chris@148: Chris@148: struct Cache { Chris@148: size_t x; Chris@148: size_t width; Chris@148: char *data; Chris@148: }; Chris@148: Chris@148: Cache m_cache; Chris@148: bool m_eagerCache; Chris@148: Chris@148: bool getFromCache(size_t x, size_t ystart, size_t ycount, void *data); Chris@148: void primeCache(size_t x, bool left); Chris@148: Chris@148: void resume(); Chris@148: Chris@148: bool seekTo(size_t x, size_t y); Chris@148: Chris@148: static FileReadThread *m_readThread; Chris@148: int m_requestToken; Chris@148: Chris@148: size_t m_requestingX; Chris@148: size_t m_requestingWidth; Chris@148: char *m_spareData; Chris@148: Chris@148: static std::map m_refcount; Chris@148: static QMutex m_refcountMutex; Chris@148: QMutex m_fdMutex; Chris@148: QMutex m_cacheMutex; Chris@148: Chris@148: typedef std::map ResizeableBitsetMap; Chris@148: static ResizeableBitsetMap m_columnBitsets; Chris@148: static QMutex m_columnBitsetWriteMutex; Chris@148: ResizeableBitset *m_columnBitset; Chris@148: }; Chris@148: Chris@148: #endif Chris@148: