LibreOffice Module sd (master) 1
SlsGenericPageCache.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21
22#include "SlsQueueProcessor.hxx"
24#include "SlsRequestFactory.hxx"
25#include "SlsBitmapCache.hxx"
27#include <tools/debug.hxx>
28
29namespace sd::slidesorter::cache {
30
32 const Size& rPreviewSize,
33 const bool bDoSuperSampling,
34 const SharedCacheContext& rpCacheContext)
35 : maRequestQueue(rpCacheContext),
36 mpCacheContext(rpCacheContext),
37 maPreviewSize(rPreviewSize),
38 mbDoSuperSampling(bDoSuperSampling)
39{
40 // A large size may indicate an error of the caller. After all we
41 // are creating previews.
43 "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
44 "This may indicate an error.");
45}
46
48{
49 if (mpQueueProcessor != nullptr)
50 mpQueueProcessor->Stop();
52 mpQueueProcessor.reset();
53
54 if (mpBitmapCache != nullptr)
56 mpBitmapCache.reset();
57}
58
60{
61 if (mpBitmapCache == nullptr)
63 mpCacheContext->GetModel(),
65
66 if (mpQueueProcessor == nullptr)
73}
74
76 const Size& rPreviewSize,
77 const bool bDoSuperSampling)
78{
79 if (rPreviewSize==maPreviewSize && bDoSuperSampling==mbDoSuperSampling)
80 return;
81
82 // A large size may indicate an error of the caller. After all we
83 // are creating previews.
85 "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
86 "This may indicate an error.");
87
88 if (mpBitmapCache != nullptr)
89 {
91 mpBitmapCache, maPreviewSize, rPreviewSize);
92 if (mpQueueProcessor != nullptr)
93 {
94 mpQueueProcessor->SetPreviewSize(rPreviewSize, bDoSuperSampling);
95 mpQueueProcessor->SetBitmapCache(mpBitmapCache);
96 }
97 }
98 maPreviewSize = rPreviewSize;
99 mbDoSuperSampling = bDoSuperSampling;
100}
101
103 const CacheKey aKey,
104 const bool bResize)
105{
106 assert(aKey != nullptr);
107
108 BitmapEx aPreview;
109 bool bMayBeUpToDate = true;
111 const SdrPage* pPage = mpCacheContext->GetPage(aKey);
112 if (mpBitmapCache->HasBitmap(pPage))
113 {
114 aPreview = mpBitmapCache->GetBitmap(pPage);
115 const Size aBitmapSize (aPreview.GetSizePixel());
116 if (aBitmapSize != maPreviewSize)
117 {
118 // Scale the bitmap to the desired size when that is possible,
119 // i.e. the bitmap is not empty.
120 if (bResize && !aBitmapSize.IsEmpty())
121 {
122 aPreview.Scale(maPreviewSize);
123 }
124 bMayBeUpToDate = false;
125 }
126 else
127 bMayBeUpToDate = true;
128 }
129 else
130 bMayBeUpToDate = false;
131
132 // Request the creation of a correctly sized preview bitmap. We do this
133 // even when the size of the bitmap in the cache is correct because its
134 // content may be not up-to-date anymore.
135 RequestPreviewBitmap(aKey, bMayBeUpToDate);
136
137 return aPreview;
138}
139
141 const CacheKey aKey)
142{
143 assert(aKey != nullptr);
144
146 const SdrPage* pPage = mpCacheContext->GetPage(aKey);
147 BitmapEx aMarkedPreview (mpBitmapCache->GetMarkedBitmap(pPage));
148
149 return aMarkedPreview;
150}
151
153 const CacheKey aKey,
154 const BitmapEx& rMarkedBitmap)
155{
156 assert(aKey != nullptr);
157
159 const SdrPage* pPage = mpCacheContext->GetPage(aKey);
160 mpBitmapCache->SetMarkedBitmap(pPage, rMarkedBitmap);
161}
162
164 const CacheKey aKey,
165 const bool bMayBeUpToDate)
166{
167 assert(aKey != nullptr);
168
169 const SdrPage* pPage = mpCacheContext->GetPage(aKey);
170
172
173 // Determine if the available bitmap is up to date.
174 bool bIsUpToDate = false;
175 if (bMayBeUpToDate)
176 bIsUpToDate = mpBitmapCache->BitmapIsUpToDate (pPage);
177 if (bIsUpToDate)
178 {
179 const BitmapEx aPreview (mpBitmapCache->GetBitmap(pPage));
180 if (aPreview.IsEmpty() || aPreview.GetSizePixel()!=maPreviewSize)
181 bIsUpToDate = false;
182 }
183
184 if ( bIsUpToDate)
185 return;
186
187 // No, the bitmap is not up-to-date. Request a new one.
188 RequestPriorityClass ePriorityClass (NOT_VISIBLE);
189 if (mpCacheContext->IsVisible(aKey))
190 {
191 if (mpBitmapCache->HasBitmap(pPage))
192 ePriorityClass = VISIBLE_OUTDATED_PREVIEW;
193 else
194 ePriorityClass = VISIBLE_NO_PREVIEW;
195 }
196 maRequestQueue.AddRequest(aKey, ePriorityClass);
197 mpQueueProcessor->Start(ePriorityClass);
198}
199
201{
202 // Invalidate the page in all caches that reference it, not just this one.
203 std::shared_ptr<cache::PageCacheManager> pCacheManager (
205 if (pCacheManager)
206 return pCacheManager->InvalidatePreviewBitmap(
207 mpCacheContext->GetModel(),
208 aKey);
209 else if (mpBitmapCache != nullptr)
210 return mpBitmapCache->InvalidateBitmap(mpCacheContext->GetPage(aKey));
211 else
212 return false;
213}
214
216{
217 if (!mpBitmapCache)
218 return;
219
220 // When the cache is being invalidated then it makes no sense to
221 // continue creating preview bitmaps. However, this may be
222 // re-started below.
223 mpQueueProcessor->Stop();
225
226 // Mark the previews in the cache as not being up-to-date anymore.
227 // Depending on the given bUpdateCache flag we start to create new
228 // preview bitmaps.
229 mpBitmapCache->InvalidateCache();
231}
232
234 const CacheKey aKey,
235 const bool bIsPrecious)
236{
238
239 // Change the request priority class according to the new precious flag.
240 if (bIsPrecious)
241 {
242 if (mpBitmapCache->HasBitmap(mpCacheContext->GetPage(aKey)))
244 else
246 }
247 else
248 {
249 if (mpBitmapCache->IsFull())
250 {
251 // When the bitmap cache is full then requests for slides that
252 // are not visible are removed.
254 }
255 else
257 }
258
259 mpBitmapCache->SetPrecious(mpCacheContext->GetPage(aKey), bIsPrecious);
260}
261
263{
265 if (mpQueueProcessor != nullptr)
266 mpQueueProcessor->Pause();
267}
268
270{
272 if (mpQueueProcessor != nullptr)
273 mpQueueProcessor->Resume();
274}
275
276} // end of namespace ::sd::slidesorter::cache
277
278/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
bool IsEmpty() const
const Size & GetSizePixel() const
bool IsEmpty() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void RequestPreviewBitmap(const CacheKey aKey, const bool bMayBeUpToDate)
When the requested preview bitmap does not yet exist or is not up-to-date then the rendering of one i...
void ProvideCacheAndProcessor()
Both bitmap cache and queue processor are created on demand by this method.
void SetPreciousFlag(const CacheKey aKey, const bool bIsPrecious)
With the precious flag you can control whether a bitmap can be removed from the cache or reduced in s...
void ChangePreviewSize(const Size &rPreviewSize, const bool bDoSuperSampling)
Change the size of the preview bitmaps.
BitmapEx GetMarkedPreviewBitmap(const CacheKey aKey)
BitmapEx GetPreviewBitmap(const CacheKey aKey, const bool bResize)
Request a preview bitmap for the specified page object in the specified size.
GenericPageCache(const Size &rPreviewSize, const bool bDoSuperSampling, const SharedCacheContext &rpCacheContext)
The page cache is created with a reference to the SlideSorter and thus has access to both view and mo...
std::unique_ptr< QueueProcessor > mpQueueProcessor
void InvalidateCache()
Call this method when all preview bitmaps have to be generated anew.
Size maPreviewSize
The current size of preview bitmaps.
std::shared_ptr< BitmapCache > mpBitmapCache
bool InvalidatePreviewBitmap(const CacheKey aKey)
Tell the cache to replace the bitmap associated with the given request data with a new one that refle...
void SetMarkedPreviewBitmap(const CacheKey aKey, const BitmapEx &rMarkedBitmap)
static std::shared_ptr< PageCacheManager > Instance()
Return the one instance of the PageCacheManager class.
This queue processor is timer based, i.e.
bool RemoveRequest(CacheKey aKey)
Remove the specified request from the queue.
void AddRequest(CacheKey aKey, RequestPriorityClass eRequestClass)
Insert a request with highest or lowest priority in its priority class.
void Clear()
Remove all requests from the queue.
void ChangeClass(CacheKey aKey, RequestPriorityClass eNewRequestClass)
Change the priority class of the specified request.
#define DBG_ASSERT(sCon, aError)
RequestPriorityClass
Each request for a preview creation has a priority.
std::shared_ptr< CacheContext > SharedCacheContext