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