Beatmup
recycle_bin.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "recycle_bin.h"
20 #include "../parallelism.h"
21 #include "../debug.h"
22 
23 using namespace Beatmup;
24 using namespace GL;
25 
26 
27 namespace Internal {
28  /**
29  A task to empty the recycle bin
30  */
31  class Recycler : public AbstractTask {
32  private:
33  std::vector<RecycleBin::Item*>& items;
34 
35  public:
36  Recycler(std::vector<RecycleBin::Item*>& items) : items(items) {}
37 
38 
40  return TaskDeviceRequirement::GPU_OR_CPU;
41  }
42 
43 
44  bool process(TaskThread& thread) {
45  return true;
46  }
47 
48 
49  bool processOnGPU(GraphicPipeline& gpu, TaskThread& thread) {
50  for (auto& item : items) {
51  RecycleBin::Item* deleting = item;
52  item = nullptr;
53  if (deleting)
54  delete deleting;
55  }
56  items.clear();
57  return true;
58  }
59  };
60 }
61 
62 
65 }
66 
67 
69  delete recycler;
70 }
71 
72 
74  if (item) {
75  lock();
76  items.push_back(item);
77  unlock();
78  }
79 }
80 
81 
82 void RecycleBin::put(std::initializer_list<Item*> items) {
83  lock();
84  for (auto item : items)
85  if (item)
86  this->items.push_back(item);
87  unlock();
88 }
89 
90 
92  lock();
93  if (items.size() > 0)
95  unlock();
96 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
TaskDeviceRequirement
Specifies which device (CPU and/or GPU) is used to run the task.
Definition: parallelism.h:95
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
float performTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Performs a given task.
Definition: context.cpp:240
A wrapper for a GPU resource.
Definition: recycle_bin.h:39
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73
std::vector< Item * > items
Definition: recycle_bin.h:48
void emptyBin()
Empty the bin destroying all the items in a GPU-aware thread.
Definition: recycle_bin.cpp:91
AbstractTask * recycler
Definition: recycle_bin.h:50
RecycleBin(Context &ctx)
Definition: recycle_bin.cpp:63
Internal low-level GPU control API.
Definition: pipeline.h:33
Thread executing tasks.
Definition: parallelism.h:154
A task to empty the recycle bin.
Definition: recycle_bin.cpp:31
bool process(TaskThread &thread)
Executes the task on CPU within a given thread.
Definition: recycle_bin.cpp:44
bool processOnGPU(GraphicPipeline &gpu, TaskThread &thread)
Executes the task on GPU.
Definition: recycle_bin.cpp:49
Recycler(std::vector< RecycleBin::Item * > &items)
Definition: recycle_bin.cpp:36
std::vector< RecycleBin::Item * > & items
Definition: recycle_bin.cpp:33
TaskDeviceRequirement getUsedDevices() const
Communicates devices (CPU and/or GPU) the task is run on.
Definition: recycle_bin.cpp:39
Beatmup internal declarations.
Definition: basic_types.h:78
Beatmup::Context * ctx