File indexing completed on 2024-12-22 04:10:32

0001 /*
0002  *  SPDX-FileCopyrightText: 2006,2010 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KIS_TILED_RANDOM_ACCESSOR_H
0007 #define KIS_TILED_RANDOM_ACCESSOR_H
0008 
0009 #include <QRect>
0010 
0011 #include <kis_shared.h>
0012 
0013 #include "kis_tiled_data_manager.h"
0014 #include "kis_random_accessor_ng.h"
0015 #include "kis_iterator_complete_listener.h"
0016 
0017 
0018 class KisRandomAccessor2 : public KisRandomAccessorNG
0019 {
0020 
0021     struct KisTileInfo {
0022         KisTileSP tile;
0023         KisTileSP oldtile;
0024         quint8* data;
0025         const quint8* oldData;
0026         qint32 area_x1, area_y1, area_x2, area_y2;
0027     };
0028 
0029 public:
0030 
0031     KisRandomAccessor2(KisTiledDataManager *ktm, qint32 offsetX, qint32 offsetY, bool writable, KisIteratorCompleteListener *completeListener);
0032     KisRandomAccessor2(const KisTiledRandomAccessor& lhs);
0033     ~KisRandomAccessor2() override;
0034 
0035 
0036 private:
0037     inline void lockTile(KisTileSP &tile) {
0038         if (m_writable)
0039             tile->lockForWrite();
0040         else
0041             tile->lockForRead();
0042     }
0043 
0044     inline void lockOldTile(KisTileSP &tile) {
0045         // Doesn't depend on access type
0046         tile->lockForRead();
0047     }
0048 
0049     inline void unlockTile(KisTileSP &tile) {
0050         if (m_writable) {
0051             tile->unlockForWrite();
0052         } else {
0053             tile->unlockForRead();
0054         }
0055     }
0056 
0057     inline void unlockOldTile(KisTileSP &tile) {
0058         tile->unlockForRead();
0059     }
0060 
0061     inline quint32 xToCol(quint32 x) const {
0062         return m_ktm ? m_ktm->xToCol(x) : 0;
0063     }
0064     inline quint32 yToRow(quint32 y) const {
0065         return m_ktm ? m_ktm->yToRow(y) : 0;
0066     }
0067 
0068     KisTileInfo* fetchTileData(qint32 col, qint32 row);
0069 
0070 public:
0071     /// Move to a given x,y position, fetch tiles and data
0072     void moveTo(qint32 x, qint32 y) override;
0073     quint8* rawData() override;
0074     const quint8* oldRawData() const override;
0075     const quint8* rawDataConst() const override;
0076     qint32 numContiguousColumns(qint32 x) const override;
0077     qint32 numContiguousRows(qint32 y) const override;
0078     qint32 rowStride(qint32 x, qint32 y) const override;
0079     qint32 x() const override;
0080     qint32 y() const override;
0081 
0082 private:
0083     KisTiledDataManager *m_ktm;
0084     KisTileInfo** m_tilesCache;
0085     quint32 m_tilesCacheSize;
0086     qint32 m_pixelSize;
0087     quint8* m_data;
0088     const quint8* m_oldData;
0089     bool m_writable;
0090     int m_lastX, m_lastY;
0091     qint32 m_offsetX, m_offsetY;
0092     KisIteratorCompleteListener *m_completeListener;
0093     static const quint32 CACHESIZE; // Define the number of tiles we keep in cache
0094 
0095 };
0096 
0097 #endif