1c45d927aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3c45d927aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c45d927aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c45d927aSAndrew Rist  * distributed with this work for additional information
6c45d927aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c45d927aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c45d927aSAndrew Rist  * "License"); you may not use this file except in compliance
9c45d927aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10c45d927aSAndrew Rist  *
11c45d927aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12c45d927aSAndrew Rist  *
13c45d927aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c45d927aSAndrew Rist  * software distributed under the License is distributed on an
15c45d927aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c45d927aSAndrew Rist  * KIND, either express or implied.  See the License for the
17c45d927aSAndrew Rist  * specific language governing permissions and limitations
18c45d927aSAndrew Rist  * under the License.
19c45d927aSAndrew Rist  *
20c45d927aSAndrew Rist  *************************************************************/
21c45d927aSAndrew Rist 
22c45d927aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SD_SLIDESORTER_BITMAP_CACHE_HXX
25cdf0e10cSrcweir #define SD_SLIDESORTER_BITMAP_CACHE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir class SdrPage;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <vcl/bitmapex.hxx>
30cdf0e10cSrcweir #include <osl/mutex.hxx>
31cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
32cdf0e10cSrcweir #include <boost/scoped_ptr.hpp>
33cdf0e10cSrcweir #include <hash_map>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace sd { namespace slidesorter { namespace cache {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir class BitmapReplacement;
38cdf0e10cSrcweir class CacheCompactor;
39cdf0e10cSrcweir class BitmapCompressor;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /** This low level cache is the actual bitmap container.  It supports a
42cdf0e10cSrcweir     precious flag for every preview bitmap and keeps track of total sizes
43cdf0e10cSrcweir     for all previews with/without this flag.  The precious flag is used by
44cdf0e10cSrcweir     compaction algorithms to determine which previews may be compressed or
45cdf0e10cSrcweir     even discarded and which have to remain in their original form.  The
46cdf0e10cSrcweir     precious flag is usually set for the visible previews.
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     Additionally to the actual preview there is an optional marked preview.
49cdf0e10cSrcweir     This is used for slides excluded from the slide show which have a preview
50cdf0e10cSrcweir     that shows a mark (some sort of bitmap overlay) to that effect.
51cdf0e10cSrcweir */
52cdf0e10cSrcweir class BitmapCache
53cdf0e10cSrcweir {
54cdf0e10cSrcweir public:
55cdf0e10cSrcweir     /** The key for looking up preview bitmaps is a pointer to an SdrPage
56cdf0e10cSrcweir         object.  The prior use of PageObjectViewObjectContact objects (which
57cdf0e10cSrcweir         ultimatly use them) turned out to be less suitable because their
58cdf0e10cSrcweir         life time is shorter then that of the page objects.  Frequent
59cdf0e10cSrcweir         destruction and re-creation of the preview bitmaps was the result.
60cdf0e10cSrcweir     */
61cdf0e10cSrcweir     typedef const SdrPage* CacheKey;
62cdf0e10cSrcweir     class CacheEntry;
63cdf0e10cSrcweir     class CacheBitmapContainer;
64cdf0e10cSrcweir     typedef ::std::vector<CacheKey> CacheIndex;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     /** Create a new cache for bitmap objects.
67cdf0e10cSrcweir         @param nMaximalNormalCacheSize
68cdf0e10cSrcweir             When a size larger then zero is given then that size is used.
69cdf0e10cSrcweir             Otherwise the default value from the configuration is used.
70cdf0e10cSrcweir             When that does not exist either then a internal default value is
71cdf0e10cSrcweir             used.
72cdf0e10cSrcweir     */
73cdf0e10cSrcweir     BitmapCache (const sal_Int32 nMaximalNormalCacheSize = 0);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     /** The destructor clears the cache and relases all bitmaps still in it.
76cdf0e10cSrcweir     */
77cdf0e10cSrcweir     ~BitmapCache (void);
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /** Remove all preview bitmaps from the cache.  After this call the
80cdf0e10cSrcweir         cache is empty.
81cdf0e10cSrcweir     */
82cdf0e10cSrcweir     void Clear (void);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     /** Return <TRUE/> when the cache is full, i.e. the cache compactor had
85cdf0e10cSrcweir         to be run.
86cdf0e10cSrcweir     */
87cdf0e10cSrcweir     bool IsFull (void) const;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /** Return the memory size that is occupied by all non-precious bitmaps
90cdf0e10cSrcweir         in the cache.
91cdf0e10cSrcweir     */
92cdf0e10cSrcweir     sal_Int32 GetSize (void);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     /** Return <TRUE/> when a preview bitmap exists for the given key.
95cdf0e10cSrcweir     */
96cdf0e10cSrcweir     bool HasBitmap (const CacheKey& rKey);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     /** Return <TRUE/> when a preview bitmap exists for the given key and
99cdf0e10cSrcweir         when it is up-to-date.
100cdf0e10cSrcweir     */
101cdf0e10cSrcweir     bool BitmapIsUpToDate (const CacheKey& rKey);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     /** Return the preview bitmap for the given contact object.
104cdf0e10cSrcweir     */
105cdf0e10cSrcweir     Bitmap GetBitmap (const CacheKey& rKey);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     /** Return the marked preview bitmap for the given contact object.
108cdf0e10cSrcweir     */
109cdf0e10cSrcweir     Bitmap GetMarkedBitmap (const CacheKey& rKey);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     /** Release the reference to the preview bitmap that is associated with
112cdf0e10cSrcweir         the given key.
113cdf0e10cSrcweir     */
114cdf0e10cSrcweir     void ReleaseBitmap (const CacheKey& rKey);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     /** Mark the specified preview bitmap as not being up-to-date
117cdf0e10cSrcweir         anymore.
118cdf0e10cSrcweir         @return
119cdf0e10cSrcweir             When the key references a page in the cache then
120cdf0e10cSrcweir             return <TRUE/>.  When the key is not known then <FALSE/>
121cdf0e10cSrcweir             is returned.
122cdf0e10cSrcweir     */
123cdf0e10cSrcweir     bool InvalidateBitmap (const CacheKey& rKey);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     /** Mark all preview bitmaps as not being up-to-date anymore.
126cdf0e10cSrcweir     */
127cdf0e10cSrcweir     void InvalidateCache (void);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     /** Add or replace a bitmap for the given key.
130cdf0e10cSrcweir     */
131cdf0e10cSrcweir     void SetBitmap (
132cdf0e10cSrcweir         const CacheKey& rKey,
133cdf0e10cSrcweir         const Bitmap& rPreview,
134cdf0e10cSrcweir         bool bIsPrecious);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     /** Add or replace a marked bitmap for the given key.
137cdf0e10cSrcweir     */
138cdf0e10cSrcweir     void SetMarkedBitmap (
139cdf0e10cSrcweir         const CacheKey& rKey,
140cdf0e10cSrcweir         const Bitmap& rPreview);
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     /** Mark the specified preview bitmap as precious, i.e. that it must not
143cdf0e10cSrcweir         be compressed or otherwise removed from the cache.
144cdf0e10cSrcweir     */
145cdf0e10cSrcweir     void SetPrecious (const CacheKey& rKey, bool bIsPrecious);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     /** Calculate the cache size.  This should rarely be necessary because
148cdf0e10cSrcweir         the cache size is tracked with each modification of preview
149cdf0e10cSrcweir         bitmaps.
150cdf0e10cSrcweir     */
151cdf0e10cSrcweir     void ReCalculateTotalCacheSize (void);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     /** Use the previews in the given cache to initialize missing previews.
154cdf0e10cSrcweir     */
155cdf0e10cSrcweir     void Recycle (const BitmapCache& rCache);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     /** Return a list of sorted cache keys that represent an index into (a
158cdf0e10cSrcweir         part of) the cache.  The entries of the index are sorted according
159cdf0e10cSrcweir         to last access times with the least recently access time first.
160cdf0e10cSrcweir         @param bIncludePrecious
161cdf0e10cSrcweir             When this flag is <TRUE/> entries with the precious flag set are
162cdf0e10cSrcweir             included in the index.  When the flag is <FALSE/> these entries
163*86e1cf34SPedro Giffuni             are omitted.
164cdf0e10cSrcweir         @param bIncludeNoPreview
165cdf0e10cSrcweir             When this flag is <TRUE/> entries with that have no preview
166cdf0e10cSrcweir             bitmaps are included in the index.  When the flag is <FALSE/> these entries
167*86e1cf34SPedro Giffuni             are omitted.
168cdf0e10cSrcweir     */
169cdf0e10cSrcweir     ::std::auto_ptr<CacheIndex> GetCacheIndex (
170cdf0e10cSrcweir         bool bIncludePrecious,
171cdf0e10cSrcweir         bool bIncludeNoPreview) const;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     /** Compress the specified preview bitmap with the given bitmap
174cdf0e10cSrcweir         compressor.  A reference to the compressor is stored for later
175cdf0e10cSrcweir         decompression.
176cdf0e10cSrcweir     */
177cdf0e10cSrcweir     void Compress (
178cdf0e10cSrcweir         const CacheKey& rKey,
179cdf0e10cSrcweir         const ::boost::shared_ptr<BitmapCompressor>& rpCompressor);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir private:
182cdf0e10cSrcweir     mutable ::osl::Mutex maMutex;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     ::boost::scoped_ptr<CacheBitmapContainer> mpBitmapContainer;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     /** Total size of bytes that are occupied by bitmaps in the cache for
187cdf0e10cSrcweir         whom the slides are currently not inside the visible area.
188cdf0e10cSrcweir     */
189cdf0e10cSrcweir     sal_Int32 mnNormalCacheSize;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     /** Total size of bytes that are occupied by bitmaps in the cache for
192cdf0e10cSrcweir         whom the slides are currently visible.
193cdf0e10cSrcweir     */
194cdf0e10cSrcweir     sal_Int32 mnPreciousCacheSize;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     /** At the moment the access time is not an actual time or date value
197cdf0e10cSrcweir         but a counter that is increased with every access.  It thus defines
198cdf0e10cSrcweir         the same ordering as a true time.
199cdf0e10cSrcweir     */
200cdf0e10cSrcweir     sal_Int32 mnCurrentAccessTime;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     /** The maximal cache size for the off-screen preview bitmaps.  When
203cdf0e10cSrcweir         mnNormalCacheSize grows larger than this value then the
204cdf0e10cSrcweir         mpCacheCompactor member is used to reduce the cache size.
205cdf0e10cSrcweir     */
206cdf0e10cSrcweir     sal_Int32 mnMaximalNormalCacheSize;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     /** The cache compactor is used to reduce the number of bytes used by
209cdf0e10cSrcweir         off-screen preview bitmaps.
210cdf0e10cSrcweir     */
211cdf0e10cSrcweir     ::std::auto_ptr<CacheCompactor> mpCacheCompactor;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     /** This flag stores if the cache is or recently was full, i.e. the
214cdf0e10cSrcweir         cache compactor has or had to be run in order to reduce the cache
215cdf0e10cSrcweir         size to the allowed value.
216cdf0e10cSrcweir     */
217cdf0e10cSrcweir     bool mbIsFull;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     /** Update mnNormalCacheSize or mnPreciousCacheSize according to the
220cdf0e10cSrcweir         precious flag of the specified preview bitmap and the specified
221cdf0e10cSrcweir         operation.
222cdf0e10cSrcweir     */
223cdf0e10cSrcweir     enum CacheOperation { ADD, REMOVE };
224cdf0e10cSrcweir     void UpdateCacheSize (const CacheEntry& rKey, CacheOperation eOperation);
225cdf0e10cSrcweir };
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 
229cdf0e10cSrcweir } } } // end of namespace ::sd::slidesorter::cache
230cdf0e10cSrcweir 
231cdf0e10cSrcweir #endif
232