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

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __KIS_MEMORY_WINDOW_H
0008 #define __KIS_MEMORY_WINDOW_H
0009 
0010 #include <QTemporaryFile>
0011 
0012 #include "kis_chunk_allocator.h"
0013 
0014 
0015 #define DEFAULT_WINDOW_SIZE (16*MiB)
0016 
0017 class KRITAIMAGE_EXPORT KisMemoryWindow
0018 {
0019 public:
0020     /**
0021      * @param swapDir If the dir doesn't exist, it'll be created, if it's empty QDir::tempPath will be used.
0022      * @param writeWindowSize write window size.
0023      */
0024     KisMemoryWindow(const QString &swapDir, quint64 writeWindowSize = DEFAULT_WINDOW_SIZE);
0025     ~KisMemoryWindow();
0026 
0027     inline quint8* getReadChunkPtr(KisChunk readChunk) {
0028         return getReadChunkPtr(readChunk.data());
0029     }
0030 
0031     inline quint8* getWriteChunkPtr(KisChunk writeChunk) {
0032         return getWriteChunkPtr(writeChunk.data());
0033     }
0034 
0035     quint8* getReadChunkPtr(const KisChunkData &readChunk);
0036     quint8* getWriteChunkPtr(const KisChunkData &writeChunk);
0037 
0038 private:
0039     struct MappingWindow {
0040         MappingWindow(quint64 _defaultSize)
0041             : chunk(0,0),
0042               window(0),
0043               defaultSize(_defaultSize)
0044         {
0045         }
0046 
0047         quint8* calculatePointer(const KisChunkData &other) const {
0048             return window + other.m_begin - chunk.m_begin;
0049         }
0050 
0051         KisChunkData chunk;
0052         quint8 *window;
0053         const quint64 defaultSize;
0054     };
0055 
0056 
0057 private:
0058     bool adjustWindow(const KisChunkData &requestedChunk,
0059                       MappingWindow *adjustingWindow,
0060                       MappingWindow *otherWindow);
0061 
0062 private:
0063     QTemporaryFile m_file;
0064 
0065     bool m_valid;
0066     MappingWindow m_readWindowEx;
0067     MappingWindow m_writeWindowEx;
0068 };
0069 
0070 #endif /* __KIS_MEMORY_WINDOW_H */
0071