Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tile_data.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2009 Dmitry Kazakov <dimula73@gmail.com>
3 * SPDX-FileCopyrightText: 2018 Andrey Kamakin <a.kamakin@icloud.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8
9#include "kis_tile_data.h"
10#include "kis_tile_data_store.h"
11
12#include <kis_debug.h>
13
14#include <boost/pool/singleton_pool.hpp>
16
17// BPP == bytes per pixel
18#define TILE_SIZE_4BPP (4 * __TILE_DATA_WIDTH * __TILE_DATA_HEIGHT)
19#define TILE_SIZE_8BPP (8 * __TILE_DATA_WIDTH * __TILE_DATA_HEIGHT)
20
21typedef boost::singleton_pool<KisTileData, TILE_SIZE_4BPP, boost::default_user_allocator_new_delete, boost::details::pool::default_mutex, 256, 4096> BoostPool4BPP;
22typedef boost::singleton_pool<KisTileData, TILE_SIZE_8BPP, boost::default_user_allocator_new_delete, boost::details::pool::default_mutex, 128, 2048> BoostPool8BPP;
23
26
28
33
35{
36 QWriteLocker l(&m_cacheLock);
37 quint8 *ptr = 0;
38
39 while (m_4Pool.pop(ptr)) {
40 BoostPool4BPP::free(ptr);
41 }
42
43 while (m_8Pool.pop(ptr)) {
44 BoostPool8BPP::free(ptr);
45 }
46
47 while (m_16Pool.pop(ptr)) {
48 free(ptr);
49 }
50}
51
52
53KisTileData::KisTileData(qint32 pixelSize, const quint8 *defPixel, KisTileDataStore *store, bool checkFreeMemory)
54 : m_state(NORMAL),
55 m_mementoFlag(0),
56 m_age(0),
57 m_usersCount(0),
58 m_refCount(0),
59 m_pixelSize(pixelSize),
60 m_store(store)
61{
62 if (checkFreeMemory) {
64 }
66
67 fillWithPixel(defPixel);
68}
69
70
82KisTileData::KisTileData(const KisTileData& rhs, bool checkFreeMemory)
83 : m_state(NORMAL),
84 m_mementoFlag(0),
85 m_age(0),
86 m_usersCount(0),
87 m_refCount(0),
88 m_pixelSize(rhs.m_pixelSize),
89 m_store(rhs.m_store)
90{
91 if (checkFreeMemory) {
93 }
95
96 memcpy(m_data, rhs.data(), m_pixelSize * WIDTH * HEIGHT);
97}
98
99
104
105void KisTileData::fillWithPixel(const quint8 *defPixel)
106{
107 quint8 *it = m_data;
108
109 for (int i = 0; i < WIDTH * HEIGHT; i++, it += m_pixelSize) {
110 memcpy(it, defPixel, m_pixelSize);
111 }
112}
113
115{
116 if (m_data) {
118 m_data = 0;
119 }
120
121 KisTileData *clone = 0;
122 while (m_clonesStack.pop(clone)) {
123 delete clone;
124 }
125
126 Q_ASSERT(m_clonesStack.isEmpty());
127}
128
130{
131 Q_ASSERT(!m_data);
133}
134
135quint8* KisTileData::allocateData(const qint32 pixelSize)
136{
137 quint8 *ptr = 0;
138
139 if (!m_cache.pop(pixelSize, ptr)) {
140 switch (pixelSize) {
141 case 4:
142 ptr = (quint8*)BoostPool4BPP::malloc();
143 break;
144 case 8:
145 ptr = (quint8*)BoostPool8BPP::malloc();
146 break;
147 default:
148 ptr = (quint8*) malloc(pixelSize * WIDTH * HEIGHT);
149 break;
150 }
151 }
152
153 return ptr;
154}
155
156void KisTileData::freeData(quint8* ptr, const qint32 pixelSize)
157{
158 if (!m_cache.push(pixelSize, ptr)) {
159 switch (pixelSize) {
160 case 4:
161 BoostPool4BPP::free(ptr);
162 break;
163 case 8:
164 BoostPool8BPP::free(ptr);
165 break;
166 default:
167 free(ptr);
168 break;
169 }
170 }
171}
172
173//#define DEBUG_POOL_RELEASE
174
175#ifdef DEBUG_POOL_RELEASE
176#include <unistd.h>
177#endif /* DEBUG_POOL_RELEASE */
178
180{
181 const int maxMigratedTiles = 100;
182
183 if (KisTileDataStore::instance()->numTilesInMemory() < maxMigratedTiles) {
184
185 QVector<KisTileData*> dataObjects;
186 QVector<QByteArray> memoryChunks;
187 bool failedToLock = false;
188
190
191 while (iter->hasNext()) {
192 KisTileData *item = iter->next();
193
194 // first release all the clones
195 KisTileData *clone = 0;
196 while (item->m_clonesStack.pop(clone)) {
197 delete clone;
198 }
199
200 // check if the tile data has actually been pooled
201 if (item->m_pixelSize != 4 &&
202 item->m_pixelSize != 8 &&
203 item->m_pixelSize != 16) {
204
205 continue;
206 }
207
208 // check if the tile has been swapped out
209 if (item->m_data) {
210 const bool locked = item->m_swapLock.tryLockForWrite();
211 if (!locked) {
212 failedToLock = true;
213 break;
214 }
215
216 const int chunkSize = item->m_pixelSize * WIDTH * HEIGHT;
217 dataObjects << item;
218 memoryChunks << QByteArray((const char*)item->m_data, chunkSize);
219 }
220
221 }
222
223 if (!failedToLock) {
224 // purge the pools memory
225 m_cache.clear();
226 BoostPool4BPP::purge_memory();
227 BoostPool8BPP::purge_memory();
228
229 auto it = dataObjects.begin();
230 auto chunkIt = memoryChunks.constBegin();
231
232 for (; it != dataObjects.end(); ++it, ++chunkIt) {
233 KisTileData *item = *it;
234 const int chunkSize = item->m_pixelSize * WIDTH * HEIGHT;
235
236 item->m_data = allocateData(item->m_pixelSize);
237 memcpy(item->m_data, chunkIt->data(), chunkSize);
238
239 item->m_swapLock.unlock();
240 }
241 } else {
242 Q_FOREACH (KisTileData *item, dataObjects) {
243 item->m_swapLock.unlock();
244 }
245
246 warnKrita << "WARNING: Failed to lock the tiles while trying to release the pooled memory";
247 }
248
250
251#ifdef DEBUG_POOL_RELEASE
252 dbgKrita << "After purging unused memory:";
253
254 char command[256];
255 sprintf(command, "cat /proc/%d/status | grep -i vm", (int)getpid());
256 printf("--- %s ---\n", command);
257 (void)system(command);
258#endif /* DEBUG_POOL_RELEASE */
259
260 } else {
261 dbgKrita << "DEBUG: releasing of the pooled memory has been cancelled:"
262 << "there are still"
264 << "tiles in memory";
265 }
266}
qint32 numTilesInMemory() const
void endIteration(KisTileDataStoreIterator *iterator)
KisTileDataStoreIterator * beginIteration()
static KisTileDataStore * instance()
QReadWriteLock m_swapLock
KisTileData(qint32 pixelSize, const quint8 *defPixel, KisTileDataStore *store, bool checkFreeMemory=true)
quint32 pixelSize() const
KisTileData * clone()
quint8 * data() const
void fillWithPixel(const quint8 *defPixel)
static void freeData(quint8 *ptr, const qint32 pixelSize)
void allocateMemory()
KisTileDataStore * m_store
static void releaseInternalPools()
KisTileDataCache m_clonesStack
static SimpleCache m_cache
static const qint32 HEIGHT
static const qint32 WIDTH
static quint8 * allocateData(const qint32 pixelSize)
void releaseMemory()
bool pop(int pixelSize, quint8 *&ptr)
KisLocklessStack< quint8 * > m_16Pool
bool push(int pixelSize, quint8 *&ptr)
QReadWriteLock m_cacheLock
KisLocklessStack< quint8 * > m_4Pool
KisLocklessStack< quint8 * > m_8Pool
#define dbgKrita
Definition kis_debug.h:45
#define warnKrita
Definition kis_debug.h:87
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
boost::singleton_pool< KisTileData, TILE_SIZE_4BPP, boost::default_user_allocator_new_delete, boost::details::pool::default_mutex, 256, 4096 > BoostPool4BPP
boost::singleton_pool< KisTileData, TILE_SIZE_8BPP, boost::default_user_allocator_new_delete, boost::details::pool::default_mutex, 128, 2048 > BoostPool8BPP
#define __TILE_DATA_WIDTH
#define __TILE_DATA_HEIGHT