LibreOffice Module sd (master) 1
SlsQueueProcessor.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include "SlsQueueProcessor.hxx"
21#include "SlsRequestQueue.hxx"
22#include "SlsBitmapCache.hxx"
23
24#include <sdpage.hxx>
26#include <utility>
28
29namespace sd::slidesorter::cache {
30
31//===== QueueProcessor ======================================================
32
34 RequestQueue& rQueue,
35 std::shared_ptr<BitmapCache> pCache,
36 const Size& rPreviewSize,
37 const bool bDoSuperSampling,
38 SharedCacheContext pCacheContext)
39 : maTimer("sd::QueueProcessor maTimer"),
40 maPreviewSize(rPreviewSize),
41 mbDoSuperSampling(bDoSuperSampling),
42 mpCacheContext(std::move(pCacheContext)),
43 mrQueue(rQueue),
44 mpCache(std::move(pCache)),
45 mbIsPaused(false)
46{
47 maTimer.SetInvokeHandler (LINK(this,QueueProcessor,ProcessRequestHdl));
49}
50
52{
53}
54
55void QueueProcessor::Start (int nPriorityClass)
56{
57 if (mbIsPaused)
58 return;
59 if ( ! maTimer.IsActive())
60 {
61 if (nPriorityClass == 0)
63 else
64 maTimer.SetTimeout (100);
65 maTimer.Start();
66 }
67}
68
70{
71 if (maTimer.IsActive())
72 maTimer.Stop();
73}
74
76{
77 mbIsPaused = true;
78}
79
81{
82 mbIsPaused = false;
83 if ( ! mrQueue.IsEmpty())
85}
86
88 const Size& rPreviewSize,
89 const bool bDoSuperSampling)
90{
91 maPreviewSize = rPreviewSize;
92 mbDoSuperSampling = bDoSuperSampling;
93}
94
95IMPL_LINK_NOARG(QueueProcessor, ProcessRequestHdl, Timer *, void)
96{
97 ProcessRequests();
98}
99
101{
102 assert(mpCacheContext);
103
104 // Never process more than one request at a time in order to prevent the
105 // lock up of the edit view.
106 if ( ! mrQueue.IsEmpty()
107 && ! mbIsPaused
108 && mpCacheContext->IsIdle())
109 {
110 CacheKey aKey = nullptr;
111 RequestPriorityClass ePriorityClass (NOT_VISIBLE);
112 {
113 ::osl::MutexGuard aGuard (mrQueue.GetMutex());
114
115 if ( ! mrQueue.IsEmpty())
116 {
117 // Get the request with the highest priority from the queue.
118 ePriorityClass = mrQueue.GetFrontPriorityClass();
119 aKey = mrQueue.GetFront();
121 }
122 }
123
124 if (aKey != nullptr)
125 ProcessOneRequest(aKey, ePriorityClass);
126 }
127
128 // Schedule the processing of the next element(s).
129 {
130 ::osl::MutexGuard aGuard (mrQueue.GetMutex());
131 if ( ! mrQueue.IsEmpty())
133 else
134 {
135 comphelper::ProfileZone aZone("QueueProcessor finished processing all elements");
136 }
137 }
138}
139
141 CacheKey aKey,
142 const RequestPriorityClass ePriorityClass)
143{
144 try
145 {
146 std::scoped_lock aGuard (maMutex);
147
148 // Create a new preview bitmap and store it in the cache.
149 if (mpCache != nullptr && mpCacheContext)
150 {
151 const SdPage* pSdPage = dynamic_cast<const SdPage*>(mpCacheContext->GetPage(aKey));
152 if (pSdPage != nullptr)
153 {
154 const BitmapEx aPreview (
156 mpCache->SetBitmap (pSdPage, aPreview, ePriorityClass!=NOT_VISIBLE);
157
158 // Initiate a repaint of the new preview.
159 mpCacheContext->NotifyPreviewCreation(aKey);
160 }
161 }
162 }
163 catch (css::uno::Exception &)
164 {
165 TOOLS_WARN_EXCEPTION( "sd", "QueueProcessor");
166 }
167}
168
170 const std::shared_ptr<BitmapCache>& rpCache)
171{
172 mpCache = rpCache;
173}
174
175} // end of namespace ::sd::slidesorter::cache
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsActive() const
void Stop()
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
BitmapEx CreateBitmap(const SdPage &rPage, const Size &rPixelSize, const bool bDoSuperSampling)
This queue processor is timer based, i.e.
void SetBitmapCache(const std::shared_ptr< BitmapCache > &rpCache)
Use this method when the page cache is (maybe) using a different BitmapCache.
std::mutex maMutex
This mutex is used to guard the queue processor.
void ProcessOneRequest(CacheKey aKey, const RequestPriorityClass ePriorityClass)
std::shared_ptr< BitmapCache > mpCache
QueueProcessor(RequestQueue &rQueue, std::shared_ptr< BitmapCache > pCache, const Size &rPreviewSize, const bool bDoSuperSampling, SharedCacheContext pCacheContext)
void Start(int nPriorityClass)
Start the processor.
void SetPreviewSize(const Size &rSize, const bool bDoSuperSampling)
The request queue stores requests that are described by the Request sorted according to priority clas...
bool IsEmpty()
Returns <TRUE> when there is no element in the queue.
::osl::Mutex & GetMutex()
Return the mutex that guards the access to the priority queue.
RequestPriorityClass GetFrontPriorityClass()
void PopFront()
Really a synonym for RemoveRequest(GetFront());.
CacheKey GetFront()
Get the request with the highest priority int the highest priority class.
#define TOOLS_WARN_EXCEPTION(area, stream)
IMPL_LINK_NOARG(CacheCompactor, CompactionCallback, Timer *, void)
RequestPriorityClass
Each request for a preview creation has a priority.
std::shared_ptr< CacheContext > SharedCacheContext