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: SlsBitmapCache.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SD_SLIDESORTER_BITMAP_CACHE_HXX
32 #define SD_SLIDESORTER_BITMAP_CACHE_HXX
36 #include <vcl/bitmapex.hxx>
37 #include <osl/mutex.hxx>
39 #include <boost/shared_ptr.hpp>
42 namespace sd
{ namespace slidesorter
{ namespace cache
{
44 class BitmapReplacement
;
46 class BitmapCompressor
;
48 /** This low level cache is the actual bitmap container. It supports a
49 precious flag for every preview bitmap and keeps track of total sizes
50 for all previews with as well as those without the flag. The precious
51 flag is used by compaction algorithms to determine which previews may be
52 compressed or even discarded and which have to remain in their original
53 form. The precious flag is usually set for the visible previews.
58 /** The key for looking up preview bitmaps is a pointer to an SdrPage
59 object. The prior use of PageObjectViewObjectContact objects (which
60 ultimatly use them) turned out to be less suitable because their
61 life time is shorter then that of the page objects. Frequent
62 destruction and re-creation of the preview bitmaps was the result.
64 typedef const SdrPage
* CacheKey
;
66 class CacheBitmapContainer
;
67 typedef ::std::vector
<CacheKey
> CacheIndex
;
69 /** Create a new cache for bitmap objects.
70 @param nMaximalNormalCacheSize
71 When a size larger then zero is given then that size is used.
72 Otherwise the default value from the configuration is used.
73 When that does not exist either then a internal default value is
76 BitmapCache (const sal_Int32 nMaximalNormalCacheSize
= 0);
78 /** The destructor clears the cache and relases all bitmaps still in it.
82 /** Remove all preview bitmaps from the cache. After this call the
87 /** Returns <TRUE/> when there is no preview bitmap in the cache.
89 bool IsEmpty (void) const;
91 /** Return <TRUE/> when the cache is full, i.e. the cache compactor had
94 bool IsFull (void) const;
96 /** Return the memory size that is occupied by all non-precious bitmaps
99 sal_Int32
GetSize (void);
101 /** Return <TRUE/> when a preview bitmap exists for the given key.
103 bool HasBitmap (const CacheKey
& rKey
);
105 /** Return <TRUE/> when a preview bitmap exists for the given key and
106 when it is up-to-date.
108 bool BitmapIsUpToDate (const CacheKey
& rKey
);
110 /** Return the preview bitmap for the given contact object.
112 ::boost::shared_ptr
<BitmapEx
> GetBitmap (const CacheKey
& rKey
);
114 /** Release the reference to the preview bitmap that is associated with
117 void ReleaseBitmap (const CacheKey
& rKey
);
119 /** Mark the specified preview bitmap as not being up-to-date anymore.
121 void InvalidateBitmap (const CacheKey
& rKey
);
123 /** Mark all preview bitmaps as not being up-to-date anymore.
125 void InvalidateCache (void);
127 /** Add or replace a bitmap for the given key.
130 const CacheKey
& rKey
,
131 const ::boost::shared_ptr
<BitmapEx
>& rpPreview
,
134 /** Return whether the specified preview bitmap has been marked as
137 bool IsPrecious (const CacheKey
& rKey
);
139 /** Mark the specified preview bitmap as precious, i.e. that it must not
140 be compressed or otherwise removed from the cache.
142 void SetPrecious (const CacheKey
& rKey
, bool bIsPrecious
);
144 /** Calculate the cache size. This should rarely be necessary because
145 the cache size is tracked with each modification of preview
148 void ReCalculateTotalCacheSize (void);
150 /** Use the previews in the given cache to initialize missing previews.
152 void Recycle (const BitmapCache
& rCache
);
154 /** Return a list of sorted cache keys that represent an index into (a
155 part of) the cache. The entries of the index are sorted according
156 to last access times with the least recently access time first.
157 @param bIncludePrecious
158 When this flag is <TRUE/> entries with the precious flag set are
159 included in the index. When the flag is <FALSE/> these entries
161 @param bIncludeNoPreview
162 When this flag is <TRUE/> entries with that have no preview
163 bitmaps are included in the index. When the flag is <FALSE/> these entries
166 ::std::auto_ptr
<CacheIndex
> GetCacheIndex (
167 bool bIncludePrecious
,
168 bool bIncludeNoPreview
) const;
170 /** Compress the specified preview bitmap with the given bitmap
171 compressor. A reference to the compressor is stored for later
175 const CacheKey
& rKey
,
176 const ::boost::shared_ptr
<BitmapCompressor
>& rpCompressor
);
179 mutable ::osl::Mutex maMutex
;
181 ::std::auto_ptr
<CacheBitmapContainer
> mpBitmapContainer
;
183 /** Total size of bytes that are occupied by bitmaps in the cache for
184 whom the slides are currently not inside the visible area.
186 sal_Int32 mnNormalCacheSize
;
188 /** Total size of bytes that are occupied by bitmaps in the cache for
189 whom the slides are currently visible.
191 sal_Int32 mnPreciousCacheSize
;
193 /** At the moment the access time is not an actual time or date value
194 but a counter that is increased with every access. It thus defines
195 the same ordering as a true time.
197 sal_Int32 mnCurrentAccessTime
;
199 /** The maximal cache size for the off-screen preview bitmaps. When
200 mnNormalCacheSize grows larger than this value then the
201 mpCacheCompactor member is used to reduce the cache size.
203 sal_Int32 mnMaximalNormalCacheSize
;
205 /** The cache compactor is used to reduce the number of bytes used by
206 off-screen preview bitmaps.
208 ::std::auto_ptr
<CacheCompactor
> mpCacheCompactor
;
210 /** This flag stores if the cache is or recently was full, i.e. the
211 cache compactor has or had to be run in order to reduce the cache
212 size to the allowed value.
216 /** Update mnNormalCacheSize or mnPreciousCacheSize according to the
217 precious flag of the specified preview bitmap and the specified
220 enum CacheOperation
{ ADD
, REMOVE
};
221 void UpdateCacheSize (const CacheEntry
& rKey
, CacheOperation eOperation
);
226 } } } // end of namespace ::sd::slidesorter::cache