Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "SlsGenericPageCache.hxx"
31 : :
32 : : #include "SlsQueueProcessor.hxx"
33 : : #include "SlsRequestPriorityClass.hxx"
34 : : #include "SlsRequestFactory.hxx"
35 : : #include "cache/SlsPageCacheManager.hxx"
36 : : #include "model/SlideSorterModel.hxx"
37 : : #include "model/SlsPageDescriptor.hxx"
38 : : #include "controller/SlideSorterController.hxx"
39 : :
40 : :
41 : : namespace sd { namespace slidesorter { namespace cache {
42 : :
43 : 130 : GenericPageCache::GenericPageCache (
44 : : const Size& rPreviewSize,
45 : : const bool bDoSuperSampling,
46 : : const SharedCacheContext& rpCacheContext)
47 : : : mpBitmapCache(),
48 : : maRequestQueue(rpCacheContext),
49 : : mpQueueProcessor(),
50 : : mpCacheContext(rpCacheContext),
51 : : maPreviewSize(rPreviewSize),
52 [ + - ][ + - ]: 130 : mbDoSuperSampling(bDoSuperSampling)
53 : : {
54 : : // A large size may indicate an error of the caller. After all we
55 : : // are creating previews.
56 : : DBG_ASSERT (maPreviewSize.Width()<1000 && maPreviewSize.Height()<1000,
57 : : "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
58 : : "This may indicate an error.");
59 : 130 : }
60 : :
61 : :
62 : :
63 : :
64 [ + - ][ + - ]: 130 : GenericPageCache::~GenericPageCache (void)
[ + - ]
65 : : {
66 [ + + ]: 130 : if (mpQueueProcessor.get() != NULL)
67 [ + - ]: 126 : mpQueueProcessor->Stop();
68 [ + - ]: 130 : maRequestQueue.Clear();
69 [ + + ]: 130 : if (mpQueueProcessor.get() != NULL)
70 [ + - ]: 126 : mpQueueProcessor->Terminate();
71 [ + - ]: 130 : mpQueueProcessor.reset();
72 : :
73 [ + + ]: 130 : if (mpBitmapCache.get() != NULL)
74 [ + - ][ + - ]: 126 : PageCacheManager::Instance()->ReleaseCache(mpBitmapCache);
[ + - ]
75 [ + - ]: 130 : mpBitmapCache.reset();
76 : 130 : }
77 : :
78 : :
79 : :
80 : :
81 : 3610 : void GenericPageCache::ProvideCacheAndProcessor (void)
82 : : {
83 [ + + ]: 3610 : if (mpBitmapCache.get() == NULL)
84 : : mpBitmapCache = PageCacheManager::Instance()->GetCache(
85 : 126 : mpCacheContext->GetModel(),
86 [ + - ][ + - ]: 126 : maPreviewSize);
[ + - ][ + - ]
[ + - ]
87 : :
88 [ + + ]: 3610 : if (mpQueueProcessor.get() == NULL)
89 : : mpQueueProcessor.reset(new QueueProcessor(
90 : : maRequestQueue,
91 : : mpBitmapCache,
92 : : maPreviewSize,
93 : : mbDoSuperSampling,
94 [ + - ]: 126 : mpCacheContext));
95 : 3610 : }
96 : :
97 : :
98 : :
99 : :
100 : 133 : void GenericPageCache::ChangePreviewSize (
101 : : const Size& rPreviewSize,
102 : : const bool bDoSuperSampling)
103 : : {
104 [ - + ][ # # ]: 133 : if (rPreviewSize!=maPreviewSize || bDoSuperSampling!=mbDoSuperSampling)
[ + - ]
105 : : {
106 : : // A large size may indicate an error of the caller. After all we
107 : : // are creating previews.
108 : : DBG_ASSERT (maPreviewSize.Width()<1000 && maPreviewSize.Height()<1000,
109 : : "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
110 : : "This may indicate an error.");
111 : :
112 [ + + ]: 133 : if (mpBitmapCache.get() != NULL)
113 : : {
114 : : mpBitmapCache = PageCacheManager::Instance()->ChangeSize(
115 [ + - ][ + - ]: 10 : mpBitmapCache, maPreviewSize, rPreviewSize);
[ + - ]
116 [ + - ]: 10 : if (mpQueueProcessor.get() != NULL)
117 : : {
118 : 10 : mpQueueProcessor->SetPreviewSize(rPreviewSize, bDoSuperSampling);
119 : 10 : mpQueueProcessor->SetBitmapCache(mpBitmapCache);
120 : : }
121 : : }
122 : 133 : maPreviewSize = rPreviewSize;
123 : 133 : mbDoSuperSampling = bDoSuperSampling;
124 : : }
125 : 133 : }
126 : :
127 : :
128 : :
129 : :
130 : 677 : Bitmap GenericPageCache::GetPreviewBitmap (
131 : : const CacheKey aKey,
132 : : const bool bResize)
133 : : {
134 : : OSL_ASSERT(aKey != NULL);
135 : :
136 [ + - ]: 677 : Bitmap aPreview;
137 : 677 : bool bMayBeUpToDate = true;
138 [ + - ]: 677 : ProvideCacheAndProcessor();
139 [ + - ]: 677 : const SdrPage* pPage = mpCacheContext->GetPage(aKey);
140 [ + - ][ + + ]: 677 : if (mpBitmapCache->HasBitmap(pPage))
141 : : {
142 [ + - ][ + - ]: 381 : aPreview = mpBitmapCache->GetBitmap(pPage);
[ + - ]
143 [ + - ]: 381 : const Size aBitmapSize (aPreview.GetSizePixel());
144 [ + + ]: 381 : if (aBitmapSize != maPreviewSize)
145 : : {
146 : : // Scale the bitmap to the desired size when that is possible,
147 : : // i.e. the bitmap is not empty.
148 [ - + ][ # # ]: 7 : if (bResize && aBitmapSize.Width()>0 && aBitmapSize.Height()>0)
[ # # ][ - + ]
149 : : {
150 [ # # ]: 0 : aPreview.Scale(maPreviewSize, BMP_SCALE_FAST);
151 : : }
152 : 7 : bMayBeUpToDate = false;
153 : : }
154 : : else
155 : 381 : bMayBeUpToDate = true;
156 : : }
157 : : else
158 : 296 : bMayBeUpToDate = false;
159 : :
160 : : // Request the creation of a correctly sized preview bitmap. We do this
161 : : // even when the size of the bitmap in the cache is correct because its
162 : : // content may be not up-to-date anymore.
163 [ + - ]: 677 : RequestPreviewBitmap(aKey, bMayBeUpToDate);
164 : :
165 : 677 : return aPreview;
166 : : }
167 : :
168 : :
169 : :
170 : :
171 : 0 : Bitmap GenericPageCache::GetMarkedPreviewBitmap (
172 : : const CacheKey aKey,
173 : : const bool bResize)
174 : : {
175 : : OSL_ASSERT(aKey != NULL);
176 : :
177 [ # # ]: 0 : ProvideCacheAndProcessor();
178 [ # # ]: 0 : const SdrPage* pPage = mpCacheContext->GetPage(aKey);
179 [ # # ]: 0 : Bitmap aMarkedPreview (mpBitmapCache->GetMarkedBitmap(pPage));
180 [ # # ]: 0 : const Size aBitmapSize (aMarkedPreview.GetSizePixel());
181 [ # # ][ # # ]: 0 : if (bResize && aBitmapSize != maPreviewSize)
[ # # ]
182 : : {
183 : : // Scale the bitmap to the desired size when that is possible,
184 : : // i.e. the bitmap is not empty.
185 [ # # ][ # # ]: 0 : if (aBitmapSize.Width()>0 && aBitmapSize.Height()>0)
[ # # ]
186 : : {
187 [ # # ]: 0 : aMarkedPreview.Scale(maPreviewSize, BMP_SCALE_FAST);
188 : : }
189 : : }
190 : :
191 : 0 : return aMarkedPreview;
192 : : }
193 : :
194 : :
195 : :
196 : :
197 : 0 : void GenericPageCache::SetMarkedPreviewBitmap (
198 : : const CacheKey aKey,
199 : : const Bitmap& rMarkedBitmap)
200 : : {
201 : : OSL_ASSERT(aKey != NULL);
202 : :
203 [ # # ]: 0 : ProvideCacheAndProcessor();
204 [ # # ]: 0 : const SdrPage* pPage = mpCacheContext->GetPage(aKey);
205 [ # # ]: 0 : mpBitmapCache->SetMarkedBitmap(pPage, rMarkedBitmap);
206 : 0 : }
207 : :
208 : :
209 : :
210 : :
211 : 1520 : void GenericPageCache::RequestPreviewBitmap (
212 : : const CacheKey aKey,
213 : : const bool bMayBeUpToDate)
214 : : {
215 : : OSL_ASSERT(aKey != NULL);
216 : :
217 [ + - ]: 1520 : const SdrPage* pPage = mpCacheContext->GetPage(aKey);
218 : :
219 [ + - ]: 1520 : ProvideCacheAndProcessor();
220 : :
221 : : // Determine if the available bitmap is up to date.
222 : 1520 : bool bIsUpToDate = false;
223 [ + + ]: 1520 : if (bMayBeUpToDate)
224 [ + - ]: 1217 : bIsUpToDate = mpBitmapCache->BitmapIsUpToDate (pPage);
225 [ + + ]: 1520 : if (bIsUpToDate)
226 : : {
227 [ + - ]: 278 : const Bitmap aPreview (mpBitmapCache->GetBitmap(pPage));
228 [ + - ][ + - ]: 278 : if (aPreview.IsEmpty() || aPreview.GetSizePixel()!=maPreviewSize)
[ - + ][ + - ]
[ - + # # ]
229 [ + - ]: 278 : bIsUpToDate = false;
230 : : }
231 : :
232 [ + + ]: 1520 : if ( ! bIsUpToDate)
233 : : {
234 : : // No, the bitmap is not up-to-date. Request a new one.
235 : 1242 : RequestPriorityClass ePriorityClass (NOT_VISIBLE);
236 [ + - ][ + + ]: 1242 : if (mpCacheContext->IsVisible(aKey))
237 : : {
238 [ + - ][ + + ]: 1099 : if (mpBitmapCache->HasBitmap(pPage))
239 : 753 : ePriorityClass = VISIBLE_OUTDATED_PREVIEW;
240 : : else
241 : 346 : ePriorityClass = VISIBLE_NO_PREVIEW;
242 : : }
243 [ + - ]: 1242 : maRequestQueue.AddRequest(aKey, ePriorityClass);
244 [ + - ]: 1242 : mpQueueProcessor->Start(ePriorityClass);
245 : : }
246 : 1520 : }
247 : :
248 : :
249 : :
250 : :
251 : 10 : bool GenericPageCache::InvalidatePreviewBitmap (const CacheKey aKey)
252 : : {
253 : : // Invalidate the page in all caches that reference it, not just this one.
254 : : ::boost::shared_ptr<cache::PageCacheManager> pCacheManager (
255 [ + - ]: 10 : cache::PageCacheManager::Instance());
256 [ + - ]: 10 : if (pCacheManager)
257 : : return pCacheManager->InvalidatePreviewBitmap(
258 : 10 : mpCacheContext->GetModel(),
259 [ + - ][ + - ]: 10 : aKey);
260 [ # # ]: 0 : else if (mpBitmapCache.get() != NULL)
261 [ # # ][ # # ]: 0 : return mpBitmapCache->InvalidateBitmap(mpCacheContext->GetPage(aKey));
262 : : else
263 [ + - ]: 10 : return false;
264 : : }
265 : :
266 : 0 : void GenericPageCache::InvalidateCache (const bool bUpdateCache)
267 : : {
268 [ # # ]: 0 : if (mpBitmapCache)
269 : : {
270 : : // When the cache is being invalidated then it makes no sense to
271 : : // continue creating preview bitmaps. However, this may be
272 : : // re-started below.
273 : 0 : mpQueueProcessor->Stop();
274 : 0 : maRequestQueue.Clear();
275 : :
276 : : // Mark the previews in the cache as not being up-to-date anymore.
277 : : // Depending on the given bUpdateCache flag we start to create new
278 : : // preview bitmaps.
279 : 0 : mpBitmapCache->InvalidateCache();
280 [ # # ]: 0 : if (bUpdateCache)
281 [ # # ]: 0 : RequestFactory()(maRequestQueue, mpCacheContext);
282 : : }
283 : 0 : }
284 : :
285 : :
286 : :
287 : :
288 : 1413 : void GenericPageCache::SetPreciousFlag (
289 : : const CacheKey aKey,
290 : : const bool bIsPrecious)
291 : : {
292 : 1413 : ProvideCacheAndProcessor();
293 : :
294 : : // Change the request priority class according to the new precious flag.
295 [ + + ]: 1413 : if (bIsPrecious)
296 : : {
297 [ + - ][ + + ]: 1408 : if (mpBitmapCache->HasBitmap(mpCacheContext->GetPage(aKey)))
298 : 813 : maRequestQueue.ChangeClass(aKey,VISIBLE_OUTDATED_PREVIEW);
299 : : else
300 : 595 : maRequestQueue.ChangeClass(aKey,VISIBLE_NO_PREVIEW);
301 : : }
302 : : else
303 : : {
304 [ - + ]: 5 : if (mpBitmapCache->IsFull())
305 : : {
306 : : // When the bitmap cache is full then requests for slides that
307 : : // are not visible are removed.
308 : 0 : maRequestQueue.RemoveRequest(aKey);
309 : : }
310 : : else
311 : 5 : maRequestQueue.ChangeClass(aKey,NOT_VISIBLE);
312 : : }
313 : :
314 [ + - ]: 1413 : mpBitmapCache->SetPrecious(mpCacheContext->GetPage(aKey), bIsPrecious);
315 : 1413 : }
316 : :
317 : :
318 : :
319 : :
320 : 0 : void GenericPageCache::Pause (void)
321 : : {
322 : 0 : ProvideCacheAndProcessor();
323 [ # # ]: 0 : if (mpQueueProcessor.get() != NULL)
324 : 0 : mpQueueProcessor->Pause();
325 : 0 : }
326 : :
327 : :
328 : :
329 : :
330 : 0 : void GenericPageCache::Resume (void)
331 : : {
332 : 0 : ProvideCacheAndProcessor();
333 [ # # ]: 0 : if (mpQueueProcessor.get() != NULL)
334 : 0 : mpQueueProcessor->Resume();
335 : 0 : }
336 : :
337 : :
338 : :
339 : : } } } // end of namespace ::sd::slidesorter::cache
340 : :
341 : :
342 : :
343 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|