Beatmup
content_lock.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, 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 #pragma once
20 #include "basic_types.h"
21 #include <initializer_list>
22 #include <map>
23 
24 namespace Beatmup {
25  class GraphicPipeline;
26  class AbstractBitmap;
27 
28  /**
29  * Makes sure the bitmap content is accessible within an image processing task.
30  * To enable direct access to pixels, a specific operation of locking the bitmap content is typically required. When done, the bitmap content needs to be unlocked.
31  * Depending on the access type (reading/writing) an the processing target (CPU or GPU), a memory allocation and/or data transfer may be required.
32  * BitmapContentLock performs the data transfer if necessary. It also handles multiple locking of the same bitmap by reference counting.
33  */
36 
37  private:
38  typedef struct {
39  bool read, write;
40  bool cpu, gpu;
41  bool isDataLocked; //!< if `true`, the bitmap pixel data is locked in RAM
42  int refs; //!< number of times the bitmap is locked
44 
45  std::map<AbstractBitmap*, LockDescriptor> bitmaps;
46 
47  public:
50 
51  /**
52  Locks content of a bitmap for reading using a specific processing target device.
53  If the lock is already acquired, only increases the counter.
54  If the bitmap was previously locked for a different target device, an exception is thrown.
55  \param gpu A graphic pipeline instance. Used to transfer the pixel data between CPU and GPU if needed.
56  \param bitmap The bitmap to lock
57  \param target Target processing device to make the pixel content readable for (CPU or GPU)
58  */
60 
61  /**
62  Locks content of a bitmap for writing using a specific processing target device.
63  If the lock is already acquired, only increases the counter.
64  If the bitmap was previously locked for reading, an exception is thrown.
65  However, it is allowed to lock for different devices, or to lock for writing first and reading later.
66  \param gpu A graphic pipeline instance
67  \param bitmap The bitmap to lock
68  \param target Target processing device to make the pixel content readable for (CPU or GPU)
69  */
71 
72  /**
73  Drops a lock to the bitmap.
74  If no other locks own the content, the bitmap is unlocked.
75  */
77 
78  /**
79  Unlocks all the locked bitmaps unconditionally.
80  */
81  void unlockAll();
82 
83  template <const ProcessingTarget target>
84  inline void lock(GraphicPipeline* gpu, AbstractBitmap* input, AbstractBitmap* output) {
85  writeLock(gpu, output, target);
86  readLock(gpu, input, target);
87  }
88 
89  inline void lock(GraphicPipeline* gpu, ProcessingTarget target, AbstractBitmap* input, AbstractBitmap* output) {
90  writeLock(gpu, output, target);
91  readLock(gpu, input, target);
92  }
93 
94  template <const ProcessingTarget target>
95  inline void lock(GraphicPipeline* gpu, std::initializer_list<AbstractBitmap*> read, std::initializer_list<AbstractBitmap*> write) {
96  for (auto bmp : write)
97  writeLock(gpu, bmp, target);
98  for (auto bmp : read)
99  readLock(gpu, bmp, target);
100  }
101 
102  template<typename ... Args>
103  inline void unlock(AbstractBitmap* first, Args ... others) {
104  unlock(first);
105  unlock(others...);
106  }
107  };
108 }
A very basic class for any image.
Makes sure the bitmap content is accessible within an image processing task.
Definition: content_lock.h:34
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.
void lock(GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
Definition: content_lock.h:84
BitmapContentLock(const BitmapContentLock &)=delete
std::map< AbstractBitmap *, LockDescriptor > bitmaps
Definition: content_lock.h:45
void readLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for reading using a specific processing target device.
void unlock(AbstractBitmap *first, Args ... others)
Definition: content_lock.h:103
void lock(GraphicPipeline *gpu, ProcessingTarget target, AbstractBitmap *input, AbstractBitmap *output)
Definition: content_lock.h:89
void lock(GraphicPipeline *gpu, std::initializer_list< AbstractBitmap * > read, std::initializer_list< AbstractBitmap * > write)
Definition: content_lock.h:95
void writeLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for writing using a specific processing target device.
void unlockAll()
Unlocks all the locked bitmaps unconditionally.
Internal low-level GPU control API.
Definition: pipeline.h:33
void write(Bitmap &bitmap, Args &&... args)
Calls a Func< WriterClass >::process(access, params) that writes to a bitmap of any kind,...
Definition: processing.h:90
void read(Bitmap &bitmap, Args &&... args)
Calls a Func< ReaderClass >::process(access, params), where.
Definition: processing.h:49
ProcessingTarget
Definition: basic_types.h:55
bool isDataLocked
if true, the bitmap pixel data is locked in RAM
Definition: content_lock.h:41
int refs
number of times the bitmap is locked
Definition: content_lock.h:42
Beatmup::InternalBitmap * bitmap