summaryrefslogtreecommitdiffstats
path: root/sd/source/ui/slidesorter/cache/SlsGenericPageCache.cxx
blob: 6275754fa65e0d826776325cbd07b602724a7c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "SlsGenericPageCache.hxx"

#include "SlsQueueProcessor.hxx"
#include "SlsRequestPriorityClass.hxx"
#include "SlsRequestFactory.hxx"
#include "SlsBitmapCache.hxx"
#include <cache/SlsPageCacheManager.hxx>
#include <tools/debug.hxx>

namespace sd::slidesorter::cache {

GenericPageCache::GenericPageCache (
    const Size& rPreviewSize,
    const bool bDoSuperSampling,
    const SharedCacheContext& rpCacheContext)
    : maRequestQueue(rpCacheContext),
      mpCacheContext(rpCacheContext),
      maPreviewSize(rPreviewSize),
      mbDoSuperSampling(bDoSuperSampling)
{
    // A large size may indicate an error of the caller.  After all we
    // are creating previews.
        DBG_ASSERT (maPreviewSize.Width()<1000 && maPreviewSize.Height()<1000,
        "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
        "This may indicate an error.");
}

GenericPageCache::~GenericPageCache()
{
    if (mpQueueProcessor != nullptr)
        mpQueueProcessor->Stop();
    maRequestQueue.Clear();
    mpQueueProcessor.reset();

    if (mpBitmapCache != nullptr)
        PageCacheManager::Instance()->ReleaseCache(mpBitmapCache);
    mpBitmapCache.reset();
}

void GenericPageCache::ProvideCacheAndProcessor()
{
    if (mpBitmapCache == nullptr)
        mpBitmapCache = PageCacheManager::Instance()->GetCache(
            mpCacheContext->GetModel(),
            maPreviewSize);

    if (mpQueueProcessor == nullptr)
        mpQueueProcessor.reset(new QueueProcessor(
            maRequestQueue,
            mpBitmapCache,
            maPreviewSize,
            mbDoSuperSampling,
            mpCacheContext));
}

void GenericPageCache::ChangePreviewSize (
    const Size& rPreviewSize,
    const bool bDoSuperSampling)
{
    if (rPreviewSize==maPreviewSize && bDoSuperSampling==mbDoSuperSampling)
        return;

    // A large size may indicate an error of the caller.  After all we
    // are creating previews.
    DBG_ASSERT (maPreviewSize.Width()<1000 && maPreviewSize.Height()<1000,
        "GenericPageCache<>::GetPreviewBitmap(): bitmap requested with large width. "
        "This may indicate an error.");

    if (mpBitmapCache != nullptr)
    {
        mpBitmapCache = PageCacheManager::Instance()->ChangeSize(
            mpBitmapCache, maPreviewSize, rPreviewSize);
        if (mpQueueProcessor != nullptr)
        {
            mpQueueProcessor->SetPreviewSize(rPreviewSize, bDoSuperSampling);
            mpQueueProcessor->SetBitmapCache(mpBitmapCache);
        }
    }
    maPreviewSize = rPreviewSize;
    mbDoSuperSampling = bDoSuperSampling;
}

BitmapEx GenericPageCache::GetPreviewBitmap (
    const CacheKey aKey,
    const bool bResize)
{
    assert(aKey != nullptr);

    BitmapEx aPreview;
    bool bMayBeUpToDate = true;
    ProvideCacheAndProcessor();
    const SdrPage* pPage = mpCacheContext->GetPage(aKey);
    if (mpBitmapCache->HasBitmap(pPage))
    {
        aPreview = mpBitmapCache->GetBitmap(pPage);
        const Size aBitmapSize (aPreview.GetSizePixel());
        if (aBitmapSize != maPreviewSize)
        {
            // Scale the bitmap to the desired size when that is possible,
            // i.e. the bitmap is not empty.
            if (bResize && !aBitmapSize.IsEmpty())
            {
                aPreview.Scale(maPreviewSize);
            }
            bMayBeUpToDate = false;
        }
        else
            bMayBeUpToDate = true;
    }
    else
        bMayBeUpToDate = false;

    // Request the creation of a correctly sized preview bitmap.  We do this
    // even when the size of the bitmap in the cache is correct because its
    // content may be not up-to-date anymore.
    RequestPreviewBitmap(aKey, bMayBeUpToDate);

    return aPreview;
}

BitmapEx GenericPageCache::GetMarkedPreviewBitmap (
    const CacheKey aKey)
{
    assert(aKey != nullptr);

    ProvideCacheAndProcessor();
    const SdrPage* pPage = mpCacheContext->GetPage(aKey);
    BitmapEx aMarkedPreview (mpBitmapCache->GetMarkedBitmap(pPage));

    return aMarkedPreview;
}

void GenericPageCache::SetMarkedPreviewBitmap (
    const CacheKey aKey,
    const BitmapEx& rMarkedBitmap)
{
    assert(aKey != nullptr);

    ProvideCacheAndProcessor();
    const SdrPage* pPage = mpCacheContext->GetPage(aKey);
    mpBitmapCache->SetMarkedBitmap(pPage, rMarkedBitmap);
}

void GenericPageCache::RequestPreviewBitmap (
    const CacheKey aKey,
    const bool bMayBeUpToDate)
{
    assert(aKey != nullptr);

    const SdrPage* pPage = mpCacheContext->GetPage(aKey);

    ProvideCacheAndProcessor();

    // Determine if the available bitmap is up to date.
    bool bIsUpToDate = false;
    if (bMayBeUpToDate)
        bIsUpToDate = mpBitmapCache->BitmapIsUpToDate (pPage);
    if (bIsUpToDate)
    {
        const BitmapEx aPreview (mpBitmapCache->GetBitmap(pPage));
        if (aPreview.IsEmpty() || aPreview.GetSizePixel()!=maPreviewSize)
              bIsUpToDate = false;
    }

    if (  bIsUpToDate)
        return;

    // No, the bitmap is not up-to-date.  Request a new one.
    RequestPriorityClass ePriorityClass (NOT_VISIBLE);
    if (mpCacheContext->IsVisible(aKey))
    {
        if (mpBitmapCache->HasBitmap(pPage))
            ePriorityClass = VISIBLE_OUTDATED_PREVIEW;
        else
            ePriorityClass = VISIBLE_NO_PREVIEW;
    }
    maRequestQueue.AddRequest(aKey, ePriorityClass);
    mpQueueProcessor->Start(ePriorityClass);
}

bool GenericPageCache::InvalidatePreviewBitmap (const CacheKey aKey)
{
    // Invalidate the page in all caches that reference it, not just this one.
    std::shared_ptr<cache::PageCacheManager> pCacheManager (
        cache::PageCacheManager::Instance());
    if (pCacheManager)
        return pCacheManager->InvalidatePreviewBitmap(
            mpCacheContext->GetModel(),
            aKey);
    else if (mpBitmapCache != nullptr)
        return mpBitmapCache->InvalidateBitmap(mpCacheContext->GetPage(aKey));
    else
        return false;
}

void GenericPageCache::InvalidateCache ()
{
    if (!mpBitmapCache)
        return;

    // When the cache is being invalidated then it makes no sense to
    // continue creating preview bitmaps.  However, this may be
    // re-started below.
    mpQueueProcessor->Stop();
    maRequestQueue.Clear();

    // Mark the previews in the cache as not being up-to-date anymore.
    // Depending on the given bUpdateCache flag we start to create new
    // preview bitmaps.
    mpBitmapCache->InvalidateCache();
    RequestFactory()(maRequestQueue, mpCacheContext);
}

void GenericPageCache::SetPreciousFlag (
    const CacheKey aKey,
    const bool bIsPrecious)
{
    ProvideCacheAndProcessor();

    // Change the request priority class according to the new precious flag.
    if (bIsPrecious)
    {
        if (mpBitmapCache->HasBitmap(mpCacheContext->GetPage(aKey)))
            maRequestQueue.ChangeClass(aKey,VISIBLE_OUTDATED_PREVIEW);
        else
            maRequestQueue.ChangeClass(aKey,VISIBLE_NO_PREVIEW);
    }
    else
    {
        if (mpBitmapCache->IsFull())
        {
            // When the bitmap cache is full then requests for slides that
            // are not visible are removed.
            maRequestQueue.RemoveRequest(aKey);
        }
        else
            maRequestQueue.ChangeClass(aKey,NOT_VISIBLE);
    }

    mpBitmapCache->SetPrecious(mpCacheContext->GetPage(aKey), bIsPrecious);
}

void GenericPageCache::Pause()
{
    ProvideCacheAndProcessor();
    if (mpQueueProcessor != nullptr)
        mpQueueProcessor->Pause();
}

void GenericPageCache::Resume()
{
    ProvideCacheAndProcessor();
    if (mpQueueProcessor != nullptr)
        mpQueueProcessor->Resume();
}

} // end of namespace ::sd::slidesorter::cache

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */