Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_paint_device_cache.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef __KIS_PAINT_DEVICE_CACHE_H
8#define __KIS_PAINT_DEVICE_CACHE_H
9
10#include "kis_lock_free_cache.h"
11#include <QReadWriteLock>
12#include <QReadLocker>
13#include <QWriteLocker>
14#include <QImage>
15
16#include <kis_paint_device.h>
17
18using ThumbnailCacheKey = std::tuple<QSize, qreal, KisThumbnailBoundsMode>;
19
20size_t qHash(const ThumbnailCacheKey &key) {
21 const auto &[size, oversample, mode] = key;
22
23 size_t result = size.width() * size.height() * qRound(oversample * 1024);
25 result = ~result;
26 }
27 return result;
28}
29
30
32{
33public:
35 : m_paintDevice(paintDevice),
36 m_exactBoundsCache(paintDevice),
37 m_nonDefaultPixelAreaCache(paintDevice),
38 m_regionCache(paintDevice),
40 {
41 }
42
51
52 void setupCache() {
53 invalidate();
54 }
55
63
67
69 QRect bounds;
71
72 if (!result) {
80 }
81
82 return bounds;
83 }
84
88
92
93 QImage createThumbnail(qint32 w, qint32 h, KisThumbnailBoundsMode boundsMode, qreal oversample, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) {
94 QImage thumbnail;
95
96 if (h == 0 || w == 0) {
97 return thumbnail;
98 }
99
100 auto key = std::make_tuple(QSize(w, h), oversample, boundsMode);
101
102 {
103 QReadLocker readLocker(&m_thumbnailsLock);
104 if (m_thumbnailsValid) {
105 if (m_thumbnails.contains(key)) {
106 thumbnail = m_thumbnails[key];
107 }
108 }
109 else {
110 readLocker.unlock();
111 QWriteLocker writeLocker(&m_thumbnailsLock);
112 m_thumbnails.clear();
113 m_thumbnailsValid = true;
114 }
115 }
116
117 if (thumbnail.isNull()) {
119 thumbnail = m_paintDevice->createThumbnailUncached(w, h, bounds, oversample, renderingIntent, conversionFlags);
120
121 QWriteLocker writeLocker(&m_thumbnailsLock);
122 m_thumbnails[key] = thumbnail;
123 m_thumbnailsValid = true;
124 }
125
126 return thumbnail;
127 }
128
129 int sequenceNumber() const {
130 return m_sequenceNumber;
131 }
132
133private:
135
137 ExactBoundsCache(KisPaintDevice *paintDevice) : m_paintDevice(paintDevice) {}
138
139 QRect calculateNewValue() const override {
140 return m_paintDevice->calculateExactBounds(false);
141 }
142 private:
144 };
145
147 NonDefaultPixelCache(KisPaintDevice *paintDevice) : m_paintDevice(paintDevice) {}
148
149 QRect calculateNewValue() const override {
151 }
152 private:
154 };
155
157 RegionCache(KisPaintDevice *paintDevice) : m_paintDevice(paintDevice) {}
158
159 KisRegion calculateNewValue() const override {
160 return m_paintDevice->dataManager()->region();
161 }
162 private:
164 };
165
169
170 QReadWriteLock m_thumbnailsLock;
171 bool m_thumbnailsValid {false};
172
173 QHash<ThumbnailCacheKey, QImage> m_thumbnails;
174
176};
177
178#endif /* __KIS_PAINT_DEVICE_CACHE_H */
const qreal oversample
KisRegion region() const
virtual bool wrapAroundMode() const =0
bool tryGetValue(T &result, Mode mode) const
NonDefaultPixelCache m_nonDefaultPixelAreaCache
QImage createThumbnail(qint32 w, qint32 h, KisThumbnailBoundsMode boundsMode, qreal oversample, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags)
QHash< ThumbnailCacheKey, QImage > m_thumbnails
KisPaintDeviceCache(KisPaintDevice *paintDevice)
ExactBoundsCache m_exactBoundsCache
KisPaintDeviceCache(const KisPaintDeviceCache &rhs)
QRect exactBounds() const
QRect extent() const
KisDataManagerSP dataManager() const
KisDefaultBoundsBaseSP defaultBounds() const
QRect calculateExactBounds(bool nonDefaultOnly) const
QImage createThumbnailUncached(qint32 maxw, qint32 maxh, QRect rect, qreal oversample=1, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags())
#define bounds(x, a, b)
std::tuple< QSize, qreal, KisThumbnailBoundsMode > ThumbnailCacheKey
size_t qHash(const ThumbnailCacheKey &key)
KisThumbnailBoundsMode
Definition kis_types.h:336
ExactBoundsCache(KisPaintDevice *paintDevice)
RegionCache(KisPaintDevice *paintDevice)
KisRegion calculateNewValue() const override