1// Aseprite
2// Copyright (C) 2019-2022 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_THUMBNAIL_GENERATOR_H_INCLUDED
9#define APP_THUMBNAIL_GENERATOR_H_INCLUDED
10#pragma once
11
12#include "base/concurrent_queue.h"
13
14#include <memory>
15#include <mutex>
16#include <vector>
17
18namespace base {
19 class thread;
20}
21
22namespace app {
23 class FileOp;
24 class IFileItem;
25
26 class ThumbnailGenerator {
27 ThumbnailGenerator();
28 public:
29 static ThumbnailGenerator* instance();
30
31 // Generate a thumbnail for the given file-item. It must be called
32 // from the GUI thread.
33 void generateThumbnail(IFileItem* fileitem);
34
35 // Checks the status of workers. If there are workers that already
36 // done its job, we've to destroy them. This function must be called
37 // from the GUI thread (because a thread is joint to it).
38 // Returns true if there are workers generating thumbnails.
39 bool checkWorkers();
40
41 // Stops all workers generating thumbnails. This is an non-blocking
42 // operation. The cancelation of all workers is done in a background
43 // thread.
44 void stopAllWorkers();
45
46 private:
47 void startWorker();
48
49 class Worker;
50 using WorkerPtr = std::unique_ptr<Worker>;
51 using WorkerList = std::vector<WorkerPtr>;
52
53 struct Item {
54 IFileItem* fileitem;
55 FileOp* fop;
56 Item() : fileitem(nullptr), fop(nullptr) { }
57 Item(const Item& item) : fileitem(item.fileitem), fop(item.fop) { }
58 Item(IFileItem* fileitem, FileOp* fop)
59 : fileitem(fileitem), fop(fop) {
60 }
61 };
62
63 int m_maxWorkers;
64 WorkerList m_workers;
65 std::mutex m_workersAccess;
66 base::concurrent_queue<Item> m_remainingItems;
67 };
68
69} // namespace app
70
71#endif
72