merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / slidesorter / cache / SlsGenericPageCache.cxx
blobfe7089430c4dac55b25416a8cd13e2bbde6a26b3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsGenericPageCache.cxx,v $
11 * $Revision: 1.5 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "precompiled_sd.hxx"
36 #include "SlsGenericPageCache.hxx"
38 #include "SlsQueueProcessor.hxx"
39 #include "SlsRequestPriorityClass.hxx"
40 #include "SlsRequestFactory.hxx"
41 #include "cache/SlsPageCacheManager.hxx"
42 #include "model/SlideSorterModel.hxx"
43 #include "model/SlsPageDescriptor.hxx"
44 #include "view/SlsPageObjectViewObjectContact.hxx"
45 #include "controller/SlideSorterController.hxx"
47 namespace sd { namespace slidesorter { namespace cache {
49 GenericPageCache::GenericPageCache (
50 const Size& rPreviewSize,
51 const SharedCacheContext& rpCacheContext)
52 : mpBitmapCache(),
53 maRequestQueue(rpCacheContext),
54 mpQueueProcessor(),
55 mpCacheContext(rpCacheContext),
56 maPreviewSize(rPreviewSize)
63 GenericPageCache::~GenericPageCache (void)
65 OSL_TRACE("terminating queue processor %p", mpQueueProcessor.get());
66 if (mpQueueProcessor.get() != NULL)
67 mpQueueProcessor->Stop();
68 maRequestQueue.Clear();
69 if (mpQueueProcessor.get() != NULL)
70 mpQueueProcessor->Terminate();
71 mpQueueProcessor.reset();
72 OSL_TRACE("queue processor stopped and terminated");
74 if (mpBitmapCache.get() != NULL)
75 PageCacheManager::Instance()->ReleaseCache(mpBitmapCache);
76 mpBitmapCache.reset();
82 void GenericPageCache::ProvideCacheAndProcessor (void)
84 if (mpBitmapCache.get() == NULL)
85 mpBitmapCache = PageCacheManager::Instance()->GetCache(
86 mpCacheContext->GetModel(),
87 maPreviewSize);
89 if (mpQueueProcessor.get() == NULL)
90 mpQueueProcessor.reset(new QueueProcessor(
91 maRequestQueue,
92 mpBitmapCache,
93 maPreviewSize,
94 mpCacheContext));
100 void GenericPageCache::ChangePreviewSize (const Size& rPreviewSize)
102 if (rPreviewSize != maPreviewSize)
104 if (mpBitmapCache.get() != NULL)
106 mpBitmapCache = PageCacheManager::Instance()->ChangeSize(
107 mpBitmapCache, maPreviewSize, rPreviewSize);
108 if (mpQueueProcessor.get() != NULL)
110 mpQueueProcessor->SetPreviewSize(rPreviewSize);
111 mpQueueProcessor->SetBitmapCache(mpBitmapCache);
114 maPreviewSize = rPreviewSize;
121 BitmapEx GenericPageCache::GetPreviewBitmap (
122 CacheKey aKey,
123 const Size& rSize)
125 OSL_ASSERT(aKey != NULL);
127 BitmapEx aPreview;
128 bool bMayBeUpToDate = true;
129 ProvideCacheAndProcessor();
130 const SdrPage* pPage = mpCacheContext->GetPage(aKey);
131 if (mpBitmapCache->HasBitmap(pPage))
133 ::boost::shared_ptr<BitmapEx> pPreview(mpBitmapCache->GetBitmap(pPage));
134 OSL_ASSERT(pPreview.get() != NULL);
135 aPreview = *pPreview;
136 Size aBitmapSize (aPreview.GetSizePixel());
137 if (aBitmapSize != rSize)
139 // The bitmap has the wrong size.
140 DBG_ASSERT (rSize.Width() < 1000,
141 "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
142 "This may indicate an error.");
144 // Scale the bitmap to the desired size when that is possible,
145 // i.e. the bitmap is not empty.
146 if (aBitmapSize.Width()>0 && aBitmapSize.Height()>0)
147 aPreview.Scale (rSize, BMP_SCALE_FAST);
149 bMayBeUpToDate = true;
151 else
152 bMayBeUpToDate = false;
154 // Request the creation of a correctly sized preview bitmap. We do this
155 // even when the size of the bitmap in the cache is correct because its
156 // content may be not up-to-date anymore.
157 RequestPreviewBitmap(aKey, rSize, bMayBeUpToDate);
159 return aPreview;
165 void GenericPageCache::RequestPreviewBitmap (
166 CacheKey aKey,
167 const Size& rSize,
168 bool bMayBeUpToDate)
170 OSL_ASSERT(aKey != NULL);
172 const SdrPage* pPage = mpCacheContext->GetPage(aKey);
174 ProvideCacheAndProcessor();
176 // Determine if the available bitmap is up to date.
177 bool bIsUpToDate = false;
178 if (bMayBeUpToDate)
179 bIsUpToDate = mpBitmapCache->BitmapIsUpToDate (pPage);
180 if (bIsUpToDate)
182 ::boost::shared_ptr<BitmapEx> pPreview (mpBitmapCache->GetBitmap(pPage));
183 if (pPreview.get()==NULL || pPreview->GetSizePixel()!=rSize)
184 bIsUpToDate = false;
187 if ( ! bIsUpToDate)
189 // No, the bitmap is not up-to-date. Request a new one.
190 RequestPriorityClass ePriorityClass (NOT_VISIBLE);
191 if (mpCacheContext->IsVisible(aKey))
193 if (mpBitmapCache->HasBitmap(pPage))
194 ePriorityClass = VISIBLE_OUTDATED_PREVIEW;
195 else
196 ePriorityClass = VISIBLE_NO_PREVIEW;
198 maRequestQueue.AddRequest(aKey, ePriorityClass);
199 mpQueueProcessor->Start(ePriorityClass);
206 void GenericPageCache::InvalidatePreviewBitmap (CacheKey aKey)
208 if (mpBitmapCache.get() != NULL)
209 mpBitmapCache->InvalidateBitmap(mpCacheContext->GetPage(aKey));
215 void GenericPageCache::ReleasePreviewBitmap (CacheKey aKey)
217 if (mpBitmapCache.get() != NULL)
219 // Suspend the queue processing temporarily to avoid the reinsertion
220 // of the request that is to be deleted.
221 mpQueueProcessor->Stop();
223 maRequestQueue.RemoveRequest(aKey);
224 mpQueueProcessor->RemoveRequest(aKey);
226 // Resume the queue processing.
227 if ( ! maRequestQueue.IsEmpty())
231 mpQueueProcessor->Start(maRequestQueue.GetFrontPriorityClass());
233 catch (::com::sun::star::uno::RuntimeException)
239 // We do not relase the preview bitmap that is associated with the page
240 // of the given request data because this method is called when the
241 // request data, typically a view-object-contact object, is destroyed.
242 // The page object usually lives longer than that and thus the preview
243 // bitmap may be used later on.
249 void GenericPageCache::InvalidateCache (bool bUpdateCache)
251 if (mpBitmapCache.get() != NULL)
253 // When the cache is being invalidated then it makes no sense to
254 // continue creating preview bitmaps. However, this may be
255 // re-started below.
256 mpQueueProcessor->Stop();
257 maRequestQueue.Clear();
259 // Mark the previews in the cache as not being up-to-date anymore.
260 // Depending on the given bUpdateCache flag we start to create new
261 // preview bitmaps.
262 mpBitmapCache->InvalidateCache();
263 if (bUpdateCache)
264 RequestFactory()(maRequestQueue, mpCacheContext);
271 void GenericPageCache::SetPreciousFlag (CacheKey aKey, bool bIsPrecious)
273 ProvideCacheAndProcessor();
275 // Change the request priority class according to the new precious flag.
276 if (bIsPrecious)
278 if (mpBitmapCache->HasBitmap(mpCacheContext->GetPage(aKey)))
279 maRequestQueue.ChangeClass(aKey,VISIBLE_OUTDATED_PREVIEW);
280 else
281 maRequestQueue.ChangeClass(aKey,VISIBLE_NO_PREVIEW);
283 else
285 if (mpBitmapCache->IsFull())
287 // When the bitmap cache is full then requests for slides that
288 // are not visible are removed.
289 maRequestQueue.RemoveRequest(aKey);
291 else
292 maRequestQueue.ChangeClass(aKey,NOT_VISIBLE);
295 mpBitmapCache->SetPrecious(mpCacheContext->GetPage(aKey), bIsPrecious);
301 bool GenericPageCache::IsEmpty (void) const
303 if (mpBitmapCache.get() != NULL)
304 return mpBitmapCache->IsEmpty();
305 else
306 return true;
312 void GenericPageCache::Pause (void)
314 ProvideCacheAndProcessor();
315 if (mpQueueProcessor.get() != NULL)
316 mpQueueProcessor->Pause();
322 void GenericPageCache::Resume (void)
324 ProvideCacheAndProcessor();
325 if (mpQueueProcessor.get() != NULL)
326 mpQueueProcessor->Resume();
331 } } } // end of namespace ::sd::slidesorter::cache