File: previewloader.cc

package info (click to toggle)
rawtherapee 5.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124,328 kB
  • sloc: cpp: 271,715; ansic: 27,904; sh: 1,109; python: 521; cs: 155; xml: 57; makefile: 15
file content (209 lines) | stat: -rw-r--r-- 6,089 bytes parent folder | download | duplicates (2)
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
/*
 *  This file is part of RawTherapee.
 *
 *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
 *
 *  RawTherapee is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  RawTherapee is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with RawTherapee.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <memory>
#include <set>
#include "cachemanager.h"
#include "filebrowserentry.h"
#include "previewloader.h"
#include "guiutils.h"
#include "threadutils.h"

#ifdef _OPENMP
#include <omp.h>
#endif

#define DEBUG(format,args...)
//#define DEBUG(format,args...) printf("PreviewLoader::%s: " format "\n", __FUNCTION__, ## args)

class PreviewLoader::Impl :
    public rtengine::NonCopyable
{
public:
    struct Job {
        Job(int dir_id, const Glib::ustring& dir_entry, PreviewLoaderListener* listener):
            dir_id_(dir_id),
            dir_entry_(dir_entry),
            listener_(listener)
        {}

        Job():
            dir_id_(0),
            listener_(nullptr)
        {}

        int dir_id_;
        Glib::ustring dir_entry_;
        PreviewLoaderListener* listener_;
    };
    /* Issue 2406
        struct OutputJob
        {
            bool complete;
            int dir_id;
            PreviewLoaderListener* listener;
            FileBrowserEntry* fdn;
        };
    */
    struct JobCompare {
        bool operator()(const Job& lhs, const Job& rhs) const
        {
            if ( lhs.dir_id_ == rhs.dir_id_ ) {
                return lhs.dir_entry_ < rhs.dir_entry_;
            }

            return lhs.dir_id_ < rhs.dir_id_;
        }
    };

    typedef std::set<Job, JobCompare> JobSet;

    Impl(): nConcurrentThreads(0)
    {
#ifdef _OPENMP
        int threadCount = omp_get_num_procs();
#else
        int threadCount = 2;
#endif

        threadPool_.reset(new Glib::ThreadPool(threadCount, 0));
    }

    std::unique_ptr<Glib::ThreadPool> threadPool_;
    MyMutex mutex_;
    JobSet jobs_;
    gint nConcurrentThreads;
// Issue 2406   std::vector<OutputJob *> output_;

    void processNextJob()
    {
        Job j;
// Issue 2406       OutputJob *oj;
        {
            MyMutex::MyLock lock(mutex_);

            // nothing to do; could be jobs have been removed
            if ( jobs_.empty() ) {
                DEBUG("processing: nothing to do");
                return;
            }

            // copy and remove front job
            j = *jobs_.begin();
            jobs_.erase(jobs_.begin());
            DEBUG("processing %s", j.dir_entry_.c_str());
            DEBUG("%d job(s) remaining", jobs_.size());
            /* Issue 2406
                        oj = new OutputJob();
                        oj->complete = false;
                        oj->dir_id = j.dir_id_;
                        oj->listener = j.listener_;
                        oj->fdn = 0;
                        output_.push_back(oj);
            */
        }

        g_atomic_int_inc (&nConcurrentThreads);  // to detect when last thread in pool has run out

        // unlock and do processing; will relock on block exit, then call listener
        // if something got
// Issue 2406       FileBrowserEntry* fdn = 0;
        try {
            Thumbnail* tmb = nullptr;
            {
                if (Glib::file_test(j.dir_entry_, Glib::FILE_TEST_EXISTS)) {
                    tmb = cacheMgr->getEntry(j.dir_entry_);
                }
            }

            if ( tmb ) {
                DEBUG("Preview Ready\n");
                j.listener_->previewReady(j.dir_id_, new FileBrowserEntry(tmb, j.dir_entry_));
// Issue 2406               fdn = new FileBrowserEntry(tmb,j.dir_entry_);
            }

        } catch (Glib::Error &e) {} catch(...) {}

        /* Issue 2406
                {
                    // the purpose of the output_ vector is to deliver the previewReady() calls in the same
                    // order as we got the jobs from the jobs_ queue.
                    MyMutex::MyLock lock(mutex_);
                    oj->fdn = fdn;
                    oj->complete = true;
                    while (output_.size() > 0 && output_.front()->complete) {
                        oj = output_.front();
                        if (oj->fdn) {
                            oj->listener->previewReady(oj->dir_id,oj->fdn);
                        }
                        output_.erase(output_.begin());
                        delete oj;
                    }
                }
        */
        bool last = g_atomic_int_dec_and_test (&nConcurrentThreads);

        // signal at end
        if (last && jobs_.empty()) {
            j.listener_->previewsFinished(j.dir_id_);
        }
    }
};

PreviewLoader::PreviewLoader():
    impl_(new Impl())
{
}

PreviewLoader::~PreviewLoader() {
    delete impl_;
}

PreviewLoader* PreviewLoader::getInstance()
{
    static PreviewLoader instance_;
    return &instance_;
}

void PreviewLoader::add(int dir_id, const Glib::ustring& dir_entry, PreviewLoaderListener* l)
{
    // somebody listening?
    if ( l != nullptr ) {
        {
            MyMutex::MyLock lock(impl_->mutex_);

            // create a new job and append to queue
            DEBUG("saving job %s", dir_entry.c_str());
            impl_->jobs_.insert(Impl::Job(dir_id, dir_entry, l));
        }

        // queue a run request
        DEBUG("adding run request %s", dir_entry.c_str());
        impl_->threadPool_->push(sigc::mem_fun(*impl_, &PreviewLoader::Impl::processNextJob));
    }
}

void PreviewLoader::removeAllJobs()
{
    DEBUG("stop %d", impl_->nConcurrentThreads);
    MyMutex::MyLock lock(impl_->mutex_);
    impl_->jobs_.clear();
}