pixel_buffer_lock.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Mark Page
27 */
28
29
30 #pragma once
31
32 #include <memory>
33 #include "../../Core/System/exception.h"
34 #include "../../Core/Math/vec2.h"
35 #include "../../Core/Math/vec3.h"
36 #include "../../Core/Math/vec4.h"
37 #include "../../Core/Math/half_float_vector.h"
38 #include "pixel_buffer.h"
39
40 namespace clan
41 {
44
47 template<typename Type>
48 class PixelBufferLock
49 {
52 public:
54 PixelBufferLock(GraphicContext &gc, PixelBuffer &pixel_buffer, BufferAccess access, bool lock_pixelbuffer = true)
55 : pixel_buffer(pixel_buffer), lock_count(0), pitch(0), data(nullptr)
56 {
57 width = pixel_buffer.get_width();
58 height = pixel_buffer.get_height();
59 if (lock_pixelbuffer)
60 lock(gc, access);
61 }
62
64 PixelBufferLock(PixelBuffer &pixel_buffer, bool lock_pixelbuffer = true)
65 : pixel_buffer(pixel_buffer), lock_count(0), pitch(0), data(nullptr)
66 {
67 width = pixel_buffer.get_width();
68 height = pixel_buffer.get_height();
69 if (lock_pixelbuffer)
70 lock();
71 }
72
73 ~PixelBufferLock()
74 {
75 if (lock_count > 0 && !(pixel_buffer.is_null()))
76 pixel_buffer.unlock();
77 lock_count = 0;
78 }
80
83 public:
85 int get_lock_count() const
86 {
87 return lock_count;
88 }
89
90 Type *get_data() { return reinterpret_cast<Type*>(data); }
91 Type *get_row(int y) { return reinterpret_cast<Type*>(data + pitch * y); }
92 Type &get_pixel(int x, int y) { return *(reinterpret_cast<Type*>(data + pitch * y) + x); }
93 int get_width() const { return width; }
94 int get_height() const { return height; }
95 int get_pitch() const { return pitch; }
97
100 public:
102 void lock(GraphicContext &gc, BufferAccess access)
103 {
104 if (!pixel_buffer.is_null())
105 {
106 pixel_buffer.lock(gc, access);
107 data = static_cast<unsigned char*>(pixel_buffer.get_data());
108 pitch = pixel_buffer.get_pitch();
109 }
110 lock_count++;
111 }
112
114 void lock()
115 {
116 if (!pixel_buffer.is_null())
117 {
118 if (pixel_buffer.is_gpu())
119 throw Exception("Incorrect PixelBufferLock constructor called with a GPU pixelbuffer");
120
121 // lock() does not do anything on system pixel buffers, so we do not call it
122
123 data = static_cast<unsigned char*>(pixel_buffer.get_data());
124 pitch = pixel_buffer.get_pitch();
125 }
126 lock_count++;
127 }
128
130 void unlock()
131 {
132 if (lock_count <= 0)
133 return;
134
135 if (!pixel_buffer.is_null())
136 {
137 pixel_buffer.unlock();
138 pitch = 0;
139 data = 0;
140 }
141 lock_count--;
142 }
144
147 private:
148 PixelBuffer pixel_buffer;
149 int lock_count;
150 int width;
151 int height;
152 int pitch;
153 unsigned char *data;
155 };
156
157 typedef PixelBufferLock<unsigned char> PixelBufferLock1ub;
158 typedef PixelBufferLock<Vec2ub> PixelBufferLock2ub;
159 typedef PixelBufferLock<Vec3ub> PixelBufferLock3ub;
160 typedef PixelBufferLock<Vec4ub> PixelBufferLock4ub;
161 typedef PixelBufferLock<unsigned short> PixelBufferLock1us;
162 typedef PixelBufferLock<Vec2us> PixelBufferLock2us;
163 typedef PixelBufferLock<Vec3us> PixelBufferLock3us;
164 typedef PixelBufferLock<Vec4us> PixelBufferLock4us;
165 typedef PixelBufferLock<unsigned int> PixelBufferLock1ui;
166 typedef PixelBufferLock<Vec2ui> PixelBufferLock2ui;
167 typedef PixelBufferLock<Vec3ui> PixelBufferLock3ui;
168 typedef PixelBufferLock<Vec4ui> PixelBufferLock4ui;
169
170 typedef PixelBufferLock<signed char> PixelBufferLock1b;
171 typedef PixelBufferLock<Vec2b> PixelBufferLock2b;
172 typedef PixelBufferLock<Vec3b> PixelBufferLock3b;
173 typedef PixelBufferLock<Vec4b> PixelBufferLock4b;
174 typedef PixelBufferLock<signed short> PixelBufferLock1s;
175 typedef PixelBufferLock<Vec2s> PixelBufferLock2s;
176 typedef PixelBufferLock<Vec3s> PixelBufferLock3s;
177 typedef PixelBufferLock<Vec4s> PixelBufferLock4s;
178 typedef PixelBufferLock<signed int> PixelBufferLock1i;
179 typedef PixelBufferLock<Vec2i> PixelBufferLock2i;
180 typedef PixelBufferLock<Vec3i> PixelBufferLock3i;
181 typedef PixelBufferLock<Vec4i> PixelBufferLock4i;
182
183 typedef PixelBufferLock<HalfFloat> PixelBufferLock1hf;
184 typedef PixelBufferLock<Vec2hf> PixelBufferLock2hf;
185 typedef PixelBufferLock<Vec3hf> PixelBufferLock3hf;
186 typedef PixelBufferLock<Vec4hf> PixelBufferLock4hf;
187
188 typedef PixelBufferLock<float> PixelBufferLock1f;
189 typedef PixelBufferLock<Vec2f> PixelBufferLock2f;
190 typedef PixelBufferLock<Vec3f> PixelBufferLock3f;
191 typedef PixelBufferLock<Vec4f> PixelBufferLock4f;
192 typedef PixelBufferLock<double> PixelBufferLock1d;
193 typedef PixelBufferLock<Vec2d> PixelBufferLock2d;
194 typedef PixelBufferLock<Vec3d> PixelBufferLock3d;
195 typedef PixelBufferLock<Vec4d> PixelBufferLock4d;
196
197 typedef PixelBufferLock<unsigned char> PixelBufferLockAny;
198
199 }
200
clan::PixelBuffer::get_width
int get_width() const
Retrieves the actual width of the buffer.
clan::PixelBufferLock4f
PixelBufferLock< Vec4f > PixelBufferLock4f
Definition: pixel_buffer_lock.h:191
clan
Definition: clanapp.h:35
clan::PixelBufferLock1hf
PixelBufferLock< HalfFloat > PixelBufferLock1hf
Definition: pixel_buffer_lock.h:183
clan::PixelBufferLock2b
PixelBufferLock< Vec2b > PixelBufferLock2b
Definition: pixel_buffer_lock.h:171
clan::PixelBufferLock::get_pitch
int get_pitch() const
Definition: pixel_buffer_lock.h:95
clan::PixelBufferLock3ub
PixelBufferLock< Vec3ub > PixelBufferLock3ub
Definition: pixel_buffer_lock.h:159
clan::PixelBufferLock::unlock
void unlock()
Unlock pixel_buffer.
Definition: pixel_buffer_lock.h:130
clan::PixelBufferLock1b
PixelBufferLock< signed char > PixelBufferLock1b
Definition: pixel_buffer_lock.h:170
clan::PixelBufferLock1i
PixelBufferLock< signed int > PixelBufferLock1i
Definition: pixel_buffer_lock.h:178
clan::PixelBuffer::is_null
bool is_null() const
Returns true if this object is invalid.
Definition: pixel_buffer.h:116
clan::PixelBufferLock3us
PixelBufferLock< Vec3us > PixelBufferLock3us
Definition: pixel_buffer_lock.h:163
clan::PixelBufferLock::get_width
int get_width() const
Definition: pixel_buffer_lock.h:93
clan::PixelBufferLock4i
PixelBufferLock< Vec4i > PixelBufferLock4i
Definition: pixel_buffer_lock.h:181
clan::PixelBuffer
Pixel data container.
Definition: pixel_buffer.h:68
clan::PixelBufferLock3i
PixelBufferLock< Vec3i > PixelBufferLock3i
Definition: pixel_buffer_lock.h:180
clan::Key::x
clan::PixelBufferLock4ui
PixelBufferLock< Vec4ui > PixelBufferLock4ui
Definition: pixel_buffer_lock.h:168
clan::PixelBufferLock3hf
PixelBufferLock< Vec3hf > PixelBufferLock3hf
Definition: pixel_buffer_lock.h:185
clan::PixelBufferLock::~PixelBufferLock
~PixelBufferLock()
Definition: pixel_buffer_lock.h:73
clan::PixelBufferLock
PixelBuffer locking helper.
Definition: pixel_buffer_lock.h:48
clan::PixelBufferLock2ub
PixelBufferLock< Vec2ub > PixelBufferLock2ub
Definition: pixel_buffer_lock.h:158
clan::PixelBuffer::get_data
void * get_data()
Returns a pointer to the beginning of the pixel buffer.
clan::PixelBufferLock4ub
PixelBufferLock< Vec4ub > PixelBufferLock4ub
Definition: pixel_buffer_lock.h:160
clan::PixelBufferLock::get_pixel
Type & get_pixel(int x, int y)
Definition: pixel_buffer_lock.h:92
clan::PixelBufferLock::lock
void lock(GraphicContext &gc, BufferAccess access)
Lock the gpu pixel_buffer.
Definition: pixel_buffer_lock.h:102
clan::PixelBufferLock2s
PixelBufferLock< Vec2s > PixelBufferLock2s
Definition: pixel_buffer_lock.h:175
clan::PixelBufferLock1s
PixelBufferLock< signed short > PixelBufferLock1s
Definition: pixel_buffer_lock.h:174
clan::Exception
Top-level exception class.
Definition: exception.h:42
clan::PixelBufferLock::get_height
int get_height() const
Definition: pixel_buffer_lock.h:94
clan::PixelBufferLock2f
PixelBufferLock< Vec2f > PixelBufferLock2f
Definition: pixel_buffer_lock.h:189
clan::PixelBufferLock3ui
PixelBufferLock< Vec3ui > PixelBufferLock3ui
Definition: pixel_buffer_lock.h:167
clan::PixelBuffer::get_height
int get_height() const
Retrieves the actual height of the buffer.
clan::PixelBufferLock::get_row
Type * get_row(int y)
Definition: pixel_buffer_lock.h:91
clan::PixelBufferLock2d
PixelBufferLock< Vec2d > PixelBufferLock2d
Definition: pixel_buffer_lock.h:193
clan::PixelBufferLock2ui
PixelBufferLock< Vec2ui > PixelBufferLock2ui
Definition: pixel_buffer_lock.h:166
clan::Key::y
clan::PixelBufferLock3b
PixelBufferLock< Vec3b > PixelBufferLock3b
Definition: pixel_buffer_lock.h:172
clan::PixelBuffer::unlock
void unlock()
Unmaps element buffer.
clan::PixelBuffer::lock
void lock(GraphicContext &gc, BufferAccess access)
Maps buffer into system memory.
clan::PixelBufferLock2hf
PixelBufferLock< Vec2hf > PixelBufferLock2hf
Definition: pixel_buffer_lock.h:184
clan::PixelBufferLock4d
PixelBufferLock< Vec4d > PixelBufferLock4d
Definition: pixel_buffer_lock.h:195
clan::PixelBufferLock1ub
PixelBufferLock< unsigned char > PixelBufferLock1ub
Definition: pixel_buffer_lock.h:157
clan::PixelBufferLock1ui
PixelBufferLock< unsigned int > PixelBufferLock1ui
Definition: pixel_buffer_lock.h:165
clan::PixelBufferLock3s
PixelBufferLock< Vec3s > PixelBufferLock3s
Definition: pixel_buffer_lock.h:176
clan::PixelBufferLock3f
PixelBufferLock< Vec3f > PixelBufferLock3f
Definition: pixel_buffer_lock.h:190
clan::PixelBuffer::is_gpu
bool is_gpu() const
Returns true if this pixel buffer is a GPU based one.
clan::PixelBufferLock2us
PixelBufferLock< Vec2us > PixelBufferLock2us
Definition: pixel_buffer_lock.h:162
clan::PixelBufferLock4s
PixelBufferLock< Vec4s > PixelBufferLock4s
Definition: pixel_buffer_lock.h:177
clan::GraphicContext
Interface to drawing graphics.
Definition: graphic_context.h:257
clan::PixelBuffer::get_pitch
int get_pitch() const
Returns the pitch (in bytes per scanline).
clan::PixelBufferLock4b
PixelBufferLock< Vec4b > PixelBufferLock4b
Definition: pixel_buffer_lock.h:173
clan::PixelBufferLock3d
PixelBufferLock< Vec3d > PixelBufferLock3d
Definition: pixel_buffer_lock.h:194
clan::PixelBufferLock1d
PixelBufferLock< double > PixelBufferLock1d
Definition: pixel_buffer_lock.h:192
clan::BufferAccess
BufferAccess
Array Buffer access enum.
Definition: buffer_usage.h:54
clan::PixelBufferLock4us
PixelBufferLock< Vec4us > PixelBufferLock4us
Definition: pixel_buffer_lock.h:164
clan::PixelBufferLock::get_lock_count
int get_lock_count() const
Returns the amounts of recursive pixel_buffer locks performed by this section.
Definition: pixel_buffer_lock.h:85
clan::PixelBufferLock4hf
PixelBufferLock< Vec4hf > PixelBufferLock4hf
Definition: pixel_buffer_lock.h:186
clan::PixelBufferLock::lock
void lock()
Lock the system pixel_buffer.
Definition: pixel_buffer_lock.h:114
clan::PixelBufferLock1us
PixelBufferLock< unsigned short > PixelBufferLock1us
Definition: pixel_buffer_lock.h:161
clan::PixelBufferLock::get_data
Type * get_data()
Definition: pixel_buffer_lock.h:90
clan::PixelBufferLock1f
PixelBufferLock< float > PixelBufferLock1f
Definition: pixel_buffer_lock.h:188
clan::PixelBufferLock::PixelBufferLock
PixelBufferLock(PixelBuffer &pixel_buffer, bool lock_pixelbuffer=true)
Constructs a system pixel buffer lock.
Definition: pixel_buffer_lock.h:64
clan::PixelBufferLock::PixelBufferLock
PixelBufferLock(GraphicContext &gc, PixelBuffer &pixel_buffer, BufferAccess access, bool lock_pixelbuffer=true)
Constructs a gpu pixel buffer lock.
Definition: pixel_buffer_lock.h:54
clan::PixelBufferLockAny
PixelBufferLock< unsigned char > PixelBufferLockAny
Definition: pixel_buffer_lock.h:197
clan::PixelBufferLock2i
PixelBufferLock< Vec2i > PixelBufferLock2i
Definition: pixel_buffer_lock.h:179