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_GENERIC_PAGE_CACHE_HXX
25 #define SD_SLIDESORTER_GENERIC_PAGE_CACHE_HXX
26 
27 #include "SlideSorter.hxx"
28 #include "SlsRequestQueue.hxx"
29 #include <boost/scoped_ptr.hpp>
30 
31 namespace sd { namespace slidesorter { namespace cache {
32 
33 class BitmapCache;
34 class QueueProcessor;
35 
36 /** This basically is the implementation class for the PageCache class.
37 */
38 class GenericPageCache
39 {
40 public:
41     /** The page chache is created with a reference to the SlideSorter and
42         thus has access to both view and model.  This allows the cache to
43         fill itself with requests for all pages or just the visible ones.
44         @param rPreviewSize
45             The size of the previews is expected in pixel values.
46         @param bDoSuperSampling
47             When <TRUE/> the previews are rendered larger and then scaled
48             down to the requested size to improve image quality.
49     */
50     GenericPageCache (
51         const Size& rPreviewSize,
52         const bool bDoSuperSampling,
53         const SharedCacheContext& rpCacheContext);
54 
55     ~GenericPageCache (void);
56 
57     /** Change the size of the preview bitmaps.  This may be caused by a
58         resize of the slide sorter window or a change of the number of
59         columns.
60     */
61     void ChangePreviewSize (
62         const Size& rPreviewSize,
63         const bool bDoSuperSampling);
64 
65     /** Request a preview bitmap for the specified page object in the
66         specified size.  The returned bitmap may be a preview of the preview,
67         i.e. either a scaled (up or down) version of a previous preview (of
68         the wrong size) or an empty bitmap.  In this case a request for the
69         generation of a new preview is created and inserted into the request
70         queue.  When the preview is available the page shape will be told to
71         paint itself again.  When it then calls this method again if
72         receives the correctly sized preview bitmap.
73         @param rRequestData
74             This data is used to determine the preview.
75         @param bResize
76             When <TRUE/> then when the available bitmap has not the
77             requested size, it is scaled before it is returned.  When
78             <FALSE/> then the bitmap is returned in the wrong size and it is
79             the task of the caller to scale it.
80         @return
81             Returns a bitmap that is either empty, contains a scaled (up or
82             down) version or is the requested bitmap.
83     */
84     Bitmap GetPreviewBitmap (
85         const CacheKey aKey,
86         const bool bResize);
87     Bitmap GetMarkedPreviewBitmap (
88         const CacheKey aKey,
89         const bool bResize);
90     void SetMarkedPreviewBitmap (
91         const CacheKey aKey,
92         const Bitmap& rMarkedBitmap);
93 
94     /** When the requested preview bitmap does not yet exist or is not
95         up-to-date then the rendering of one is scheduled.  Otherwise this
96         method does nothing.
97         @param rRequestData
98             This data is used to determine the preview.
99         @param bMayBeUpToDate
100             This flag helps the method to determine whether an existing
101             preview that matches the request is up to date.  If the caller
102             knows that it is not then by passing <FALSE/> he tells us that we
103             do not have to check the up-to-date flag a second time.  If
104             unsure use <TRUE/>.
105     */
106     void RequestPreviewBitmap (
107         const CacheKey aKey,
108         const bool bMayBeUpToDate = true);
109 
110     /** Tell the cache to replace the bitmap associated with the given
111         request data with a new one that reflects recent changes in the
112         content of the page object.
113         @return
114             When the key is kown then return <TRUE/>.
115     */
116     bool InvalidatePreviewBitmap (const CacheKey aKey);
117 
118     /** Call this method when a view-object-contact object is being deleted
119         and does not need (a) its current bitmap in the cache and (b) a
120         requested a new bitmap.
121     */
122     void ReleasePreviewBitmap (const CacheKey aKey);
123 
124     /** Call this method when all preview bitmaps have to be generated anew.
125         This is the case when the size of the page objects on the screen has
126         changed or when the model has changed.
127     */
128     void InvalidateCache (const bool bUpdateCache);
129 
130     /** With the precious flag you can control whether a bitmap can be
131         removed from the cache or reduced in size to make room for other
132         bitmaps or is so precious that it will not be touched.  A typical
133         use is to set the precious flag for the visible pages.
134     */
135     void SetPreciousFlag (const CacheKey aKey, const bool bIsPrecious);
136 
137     void Pause (void);
138     void Resume (void);
139 
140 private:
141     ::boost::shared_ptr<BitmapCache> mpBitmapCache;
142 
143     RequestQueue maRequestQueue;
144 
145     ::boost::scoped_ptr<QueueProcessor> mpQueueProcessor;
146 
147     SharedCacheContext mpCacheContext;
148 
149     /** The current size of preview bitmaps.
150     */
151     Size maPreviewSize;
152 
153     bool mbDoSuperSampling;
154 
155     /** Both bitmap cache and queue processor are created on demand by this
156         method.
157     */
158     void ProvideCacheAndProcessor (void);
159 };
160 
161 
162 } } } // end of namespace ::sd::slidesorter::cache
163 
164 #endif
165