1 /*
2     SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
3 
4     Work sponsored by the LiMux project of the city of Munich:
5     SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
6 
7     SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #ifndef OKULAR_THREADEDGENERATOR_P_H
11 #define OKULAR_THREADEDGENERATOR_P_H
12 
13 #include "area.h"
14 
15 #include <QImage>
16 #include <QMutex>
17 #include <QSet>
18 #include <QThread>
19 
20 class QEventLoop;
21 
22 #include "generator.h"
23 #include "page.h"
24 
25 namespace Okular
26 {
27 class DocumentObserver;
28 class DocumentPrivate;
29 class FontInfo;
30 class Generator;
31 class Page;
32 class PixmapGenerationThread;
33 class PixmapRequest;
34 class TextPage;
35 class TextPageGenerationThread;
36 class TilesManager;
37 
38 class GeneratorPrivate
39 {
40 public:
41     GeneratorPrivate();
42 
43     virtual ~GeneratorPrivate();
44 
45     Q_DECLARE_PUBLIC(Generator)
46     Generator *q_ptr;
47 
48     PixmapGenerationThread *pixmapGenerationThread();
49     TextPageGenerationThread *textPageGenerationThread();
50 
51     void pixmapGenerationFinished();
52     void textpageGenerationFinished();
53 
54     QMutex *threadsLock();
55 
56     virtual QVariant metaData(const QString &key, const QVariant &option) const;
57     virtual QImage image(PixmapRequest *);
58 
59     DocumentPrivate *m_document;
60     // NOTE: the following should be a QSet< GeneratorFeature >,
61     // but it is not to avoid #include'ing generator.h
62     QSet<int> m_features;
63     PixmapGenerationThread *mPixmapGenerationThread;
64     TextPageGenerationThread *mTextPageGenerationThread;
65     mutable QMutex m_mutex;
66     QMutex m_threadsMutex;
67     bool mPixmapReady : 1;
68     bool mTextPageReady : 1;
69     bool m_closing : 1;
70     QEventLoop *m_closingLoop;
71     QSizeF m_dpi;
72 };
73 
74 class PixmapRequestPrivate
75 {
76 public:
77     void swap();
78     TilesManager *tilesManager() const;
79 
80     static PixmapRequestPrivate *get(const PixmapRequest *req);
81 
82     DocumentObserver *mObserver;
83     int mPageNumber;
84     int mWidth;
85     int mHeight;
86     int mPriority;
87     int mFeatures;
88     bool mForce : 1;
89     bool mTile : 1;
90     bool mPartialUpdatesWanted : 1;
91     Page *mPage;
92     NormalizedRect mNormalizedRect;
93     QAtomicInt mShouldAbortRender;
94     QImage mResultImage;
95 };
96 
97 class TextRequestPrivate
98 {
99 public:
100     static TextRequestPrivate *get(const TextRequest *req);
101 
102     Page *mPage;
103     QAtomicInt mShouldAbortExtraction;
104 };
105 
106 class PixmapGenerationThread : public QThread
107 {
108     Q_OBJECT
109 
110 public:
111     explicit PixmapGenerationThread(Generator *generator);
112 
113     void startGeneration(PixmapRequest *request, bool calcBoundingBox);
114 
115     void endGeneration();
116 
117     PixmapRequest *request() const;
118 
119     QImage image() const;
120     bool calcBoundingBox() const;
121     NormalizedRect boundingBox() const;
122 
123 protected:
124     void run() override;
125 
126 private:
127     Generator *mGenerator;
128     PixmapRequest *mRequest;
129     NormalizedRect mBoundingBox;
130     bool mCalcBoundingBox : 1;
131 };
132 
133 class TextPageGenerationThread : public QThread
134 {
135     Q_OBJECT
136 
137 public:
138     explicit TextPageGenerationThread(Generator *generator);
139 
140     void endGeneration();
141 
142     void setPage(Page *page);
143     Page *page() const;
144 
145     TextPage *textPage() const;
146 
147     void abortExtraction();
148     bool shouldAbortExtraction() const;
149 
150 public slots:
151     void startGeneration();
152 
153 protected:
154     void run() override;
155 
156 private:
157     Generator *mGenerator;
158     TextPage *mTextPage;
159     TextRequest mTextRequest;
160 };
161 
162 class FontExtractionThread : public QThread
163 {
164     Q_OBJECT
165 
166 public:
167     FontExtractionThread(Generator *generator, int pages);
168 
169     void startExtraction(bool async);
170     void stopExtraction();
171 
172 Q_SIGNALS:
173     void gotFont(const Okular::FontInfo &);
174     void progress(int page);
175 
176 protected:
177     void run() override;
178 
179 private:
180     Generator *mGenerator;
181     int mNumOfPages;
182     bool mGoOn;
183 };
184 
185 }
186 
187 Q_DECLARE_METATYPE(Okular::Page *)
188 
189 #endif
190