pixel_buffer_lock.h Source File - ClanLib SDK
Description: pixel_buffer_lock.h Source File

pixel_buffer_lock.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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#pragma once
30
31#include <memory>
32#include "../../Core/System/exception.h"
33#include "../../Core/Math/vec2.h"
34#include "../../Core/Math/vec3.h"
35#include "../../Core/Math/vec4.h"
36#include "../../Core/Math/half_float_vector.h"
37#include "pixel_buffer.h"
38
39namespace clan
40{
43
46 template<typename Type>
47 class PixelBufferLock
48 {
49 public:
51 PixelBufferLock(GraphicContext &gc, PixelBuffer &pixel_buffer, BufferAccess access, bool lock_pixelbuffer = true)
52 : pixel_buffer(pixel_buffer), lock_count(0), pitch(0), data(nullptr)
53 {
54 width = pixel_buffer.get_width();
55 height = pixel_buffer.get_height();
56 if (lock_pixelbuffer)
57 lock(gc, access);
58 }
59
61 PixelBufferLock(PixelBuffer &pixel_buffer, bool lock_pixelbuffer = true)
62 : pixel_buffer(pixel_buffer), lock_count(0), pitch(0), data(nullptr)
63 {
64 width = pixel_buffer.get_width();
65 height = pixel_buffer.get_height();
66 if (lock_pixelbuffer)
67 lock();
68 }
69
70 ~PixelBufferLock()
71 {
72 if (lock_count > 0 && !(pixel_buffer.is_null()))
73 pixel_buffer.unlock();
74 lock_count = 0;
75 }
76
78 int get_lock_count() const
79 {
80 return lock_count;
81 }
82
83 Type *get_data() { return reinterpret_cast<Type*>(data); }
84 Type *get_row(int y) { return reinterpret_cast<Type*>(data + pitch * y); }
85 Type &get_pixel(int x, int y) { return *(reinterpret_cast<Type*>(data + pitch * y) + x); }
86 int get_width() const { return width; }
87 int get_height() const { return height; }
88 int get_pitch() const { return pitch; }
89
91 void lock(GraphicContext &gc, BufferAccess access)
92 {
93 if (!pixel_buffer.is_null())
94 {
95 pixel_buffer.lock(gc, access);
96 data = static_cast<unsigned char*>(pixel_buffer.get_data());
97 pitch = pixel_buffer.get_pitch();
98 }
99 lock_count++;
100 }
101
103 void lock()
104 {
105 if (!pixel_buffer.is_null())
106 {
107 if (pixel_buffer.is_gpu())
108 throw Exception("Incorrect PixelBufferLock constructor called with a GPU pixelbuffer");
109
110 // lock() does not do anything on system pixel buffers, so we do not call it
111
112 data = static_cast<unsigned char*>(pixel_buffer.get_data());
113 pitch = pixel_buffer.get_pitch();
114 }
115 lock_count++;
116 }
117
119 void unlock()
120 {
121 if (lock_count <= 0)
122 return;
123
124 if (!pixel_buffer.is_null())
125 {
126 pixel_buffer.unlock();
127 pitch = 0;
128 data = 0;
129 }
130 lock_count--;
131 }
132
133 private:
134 PixelBuffer pixel_buffer;
135 int lock_count;
136 int width;
137 int height;
138 int pitch;
139 unsigned char *data;
140 };
141
142 typedef PixelBufferLock<unsigned char> PixelBufferLock1ub;
143 typedef PixelBufferLock<Vec2ub> PixelBufferLock2ub;
144 typedef PixelBufferLock<Vec3ub> PixelBufferLock3ub;
145 typedef PixelBufferLock<Vec4ub> PixelBufferLock4ub;
146 typedef PixelBufferLock<unsigned short> PixelBufferLock1us;
147 typedef PixelBufferLock<Vec2us> PixelBufferLock2us;
148 typedef PixelBufferLock<Vec3us> PixelBufferLock3us;
149 typedef PixelBufferLock<Vec4us> PixelBufferLock4us;
150 typedef PixelBufferLock<unsigned int> PixelBufferLock1ui;
151 typedef PixelBufferLock<Vec2ui> PixelBufferLock2ui;
152 typedef PixelBufferLock<Vec3ui> PixelBufferLock3ui;
153 typedef PixelBufferLock<Vec4ui> PixelBufferLock4ui;
154
155 typedef PixelBufferLock<signed char> PixelBufferLock1b;
156 typedef PixelBufferLock<Vec2b> PixelBufferLock2b;
157 typedef PixelBufferLock<Vec3b> PixelBufferLock3b;
158 typedef PixelBufferLock<Vec4b> PixelBufferLock4b;
159 typedef PixelBufferLock<signed short> PixelBufferLock1s;
160 typedef PixelBufferLock<Vec2s> PixelBufferLock2s;
161 typedef PixelBufferLock<Vec3s> PixelBufferLock3s;
162 typedef PixelBufferLock<Vec4s> PixelBufferLock4s;
163 typedef PixelBufferLock<signed int> PixelBufferLock1i;
164 typedef PixelBufferLock<Vec2i> PixelBufferLock2i;
165 typedef PixelBufferLock<Vec3i> PixelBufferLock3i;
166 typedef PixelBufferLock<Vec4i> PixelBufferLock4i;
167
168 typedef PixelBufferLock<HalfFloat> PixelBufferLock1hf;
169 typedef PixelBufferLock<Vec2hf> PixelBufferLock2hf;
170 typedef PixelBufferLock<Vec3hf> PixelBufferLock3hf;
171 typedef PixelBufferLock<Vec4hf> PixelBufferLock4hf;
172
173 typedef PixelBufferLock<float> PixelBufferLock1f;
174 typedef PixelBufferLock<Vec2f> PixelBufferLock2f;
175 typedef PixelBufferLock<Vec3f> PixelBufferLock3f;
176 typedef PixelBufferLock<Vec4f> PixelBufferLock4f;
177 typedef PixelBufferLock<double> PixelBufferLock1d;
178 typedef PixelBufferLock<Vec2d> PixelBufferLock2d;
179 typedef PixelBufferLock<Vec3d> PixelBufferLock3d;
180 typedef PixelBufferLock<Vec4d> PixelBufferLock4d;
181
182 typedef PixelBufferLock<unsigned char> PixelBufferLockAny;
183
185}
clan::Exception
Top-level exception class.
Definition exception.h:42
clan::GraphicContext
Interface to drawing graphics.
Definition graphic_context.h:257
clan::PixelBufferLock
PixelBuffer locking helper.
Definition pixel_buffer_lock.h:48
clan::PixelBufferLock::get_row
Type * get_row(int y)
Definition pixel_buffer_lock.h:84
clan::PixelBufferLock::lock
void lock()
Lock the system pixel_buffer.
Definition pixel_buffer_lock.h:103
clan::PixelBufferLock::get_pitch
int get_pitch() const
Definition pixel_buffer_lock.h:88
clan::PixelBufferLock::~PixelBufferLock
~PixelBufferLock()
Definition pixel_buffer_lock.h:70
clan::PixelBufferLock::unlock
void unlock()
Unlock pixel_buffer.
Definition pixel_buffer_lock.h:119
clan::PixelBufferLock::get_width
int get_width() const
Definition pixel_buffer_lock.h:86
clan::PixelBufferLock::get_pixel
Type & get_pixel(int x, int y)
Definition pixel_buffer_lock.h:85
clan::PixelBufferLock::lock
void lock(GraphicContext &gc, BufferAccess access)
Lock the gpu pixel_buffer.
Definition pixel_buffer_lock.h:91
clan::PixelBufferLock::get_data
Type * get_data()
Definition pixel_buffer_lock.h:83
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:51
clan::PixelBufferLock::get_height
int get_height() const
Definition pixel_buffer_lock.h:87
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:78
clan::PixelBufferLock::PixelBufferLock
PixelBufferLock(PixelBuffer &pixel_buffer, bool lock_pixelbuffer=true)
Constructs a system pixel buffer lock.
Definition pixel_buffer_lock.h:61
clan::PixelBuffer
Pixel data container.
Definition pixel_buffer.h:68
clan::PixelBuffer::get_height
int get_height() const
Retrieves the actual height of the buffer.
clan::PixelBuffer::unlock
void unlock()
Unmaps element buffer.
clan::PixelBuffer::is_null
bool is_null() const
Returns true if this object is invalid.
Definition pixel_buffer.h:107
clan::PixelBuffer::lock
void lock(GraphicContext &gc, BufferAccess access)
Maps buffer into system memory.
clan::PixelBuffer::get_width
int get_width() const
Retrieves the actual width of the buffer.
clan::PixelBuffer::get_data
void * get_data()
Returns a pointer to the beginning of the pixel buffer.
clan::PixelBuffer::is_gpu
bool is_gpu() const
Returns true if this pixel buffer is a GPU based one.
clan::PixelBuffer::get_pitch
int get_pitch() const
Returns the pitch (in bytes per scanline).
clan::PixelBufferLock3ub
PixelBufferLock< Vec3ub > PixelBufferLock3ub
Definition pixel_buffer_lock.h:144
clan::PixelBufferLock4us
PixelBufferLock< Vec4us > PixelBufferLock4us
Definition pixel_buffer_lock.h:149
clan::PixelBufferLock4d
PixelBufferLock< Vec4d > PixelBufferLock4d
Definition pixel_buffer_lock.h:180
clan::PixelBufferLock1hf
PixelBufferLock< HalfFloat > PixelBufferLock1hf
Definition pixel_buffer_lock.h:168
clan::PixelBufferLock1i
PixelBufferLock< signed int > PixelBufferLock1i
Definition pixel_buffer_lock.h:163
clan::PixelBufferLock4hf
PixelBufferLock< Vec4hf > PixelBufferLock4hf
Definition pixel_buffer_lock.h:171
clan::PixelBufferLock4f
PixelBufferLock< Vec4f > PixelBufferLock4f
Definition pixel_buffer_lock.h:176
clan::PixelBufferLock4ub
PixelBufferLock< Vec4ub > PixelBufferLock4ub
Definition pixel_buffer_lock.h:145
clan::PixelBufferLock2b
PixelBufferLock< Vec2b > PixelBufferLock2b
Definition pixel_buffer_lock.h:156
clan::PixelBufferLock3s
PixelBufferLock< Vec3s > PixelBufferLock3s
Definition pixel_buffer_lock.h:161
clan::PixelBufferLock1ui
PixelBufferLock< unsigned int > PixelBufferLock1ui
Definition pixel_buffer_lock.h:150
clan::PixelBufferLock1us
PixelBufferLock< unsigned short > PixelBufferLock1us
Definition pixel_buffer_lock.h:146
clan::PixelBufferLock3us
PixelBufferLock< Vec3us > PixelBufferLock3us
Definition pixel_buffer_lock.h:148
clan::BufferAccess
BufferAccess
Array Buffer access enum.
Definition buffer_usage.h:53
clan::PixelBufferLock1d
PixelBufferLock< double > PixelBufferLock1d
Definition pixel_buffer_lock.h:177
clan::PixelBufferLock2ui
PixelBufferLock< Vec2ui > PixelBufferLock2ui
Definition pixel_buffer_lock.h:151
clan::PixelBufferLock4ui
PixelBufferLock< Vec4ui > PixelBufferLock4ui
Definition pixel_buffer_lock.h:153
clan::PixelBufferLock2f
PixelBufferLock< Vec2f > PixelBufferLock2f
Definition pixel_buffer_lock.h:174
clan::PixelBufferLock4i
PixelBufferLock< Vec4i > PixelBufferLock4i
Definition pixel_buffer_lock.h:166
clan::PixelBufferLock2d
PixelBufferLock< Vec2d > PixelBufferLock2d
Definition pixel_buffer_lock.h:178
clan::PixelBufferLock3hf
PixelBufferLock< Vec3hf > PixelBufferLock3hf
Definition pixel_buffer_lock.h:170
clan::PixelBufferLock3b
PixelBufferLock< Vec3b > PixelBufferLock3b
Definition pixel_buffer_lock.h:157
clan::PixelBufferLock1s
PixelBufferLock< signed short > PixelBufferLock1s
Definition pixel_buffer_lock.h:159
clan::PixelBufferLock3d
PixelBufferLock< Vec3d > PixelBufferLock3d
Definition pixel_buffer_lock.h:179
clan::PixelBufferLock1f
PixelBufferLock< float > PixelBufferLock1f
Definition pixel_buffer_lock.h:173
clan::PixelBufferLock2ub
PixelBufferLock< Vec2ub > PixelBufferLock2ub
Definition pixel_buffer_lock.h:143
clan::PixelBufferLock2us
PixelBufferLock< Vec2us > PixelBufferLock2us
Definition pixel_buffer_lock.h:147
clan::PixelBufferLock2i
PixelBufferLock< Vec2i > PixelBufferLock2i
Definition pixel_buffer_lock.h:164
clan::PixelBufferLock2s
PixelBufferLock< Vec2s > PixelBufferLock2s
Definition pixel_buffer_lock.h:160
clan::PixelBufferLock1b
PixelBufferLock< signed char > PixelBufferLock1b
Definition pixel_buffer_lock.h:155
clan::PixelBufferLock3ui
PixelBufferLock< Vec3ui > PixelBufferLock3ui
Definition pixel_buffer_lock.h:152
clan::PixelBufferLock4b
PixelBufferLock< Vec4b > PixelBufferLock4b
Definition pixel_buffer_lock.h:158
clan::PixelBufferLock4s
PixelBufferLock< Vec4s > PixelBufferLock4s
Definition pixel_buffer_lock.h:162
clan::PixelBufferLock2hf
PixelBufferLock< Vec2hf > PixelBufferLock2hf
Definition pixel_buffer_lock.h:169
clan::PixelBufferLock3i
PixelBufferLock< Vec3i > PixelBufferLock3i
Definition pixel_buffer_lock.h:165
clan::PixelBufferLock1ub
PixelBufferLock< unsigned char > PixelBufferLock1ub
Definition pixel_buffer_lock.h:142
clan::PixelBufferLock3f
PixelBufferLock< Vec3f > PixelBufferLock3f
Definition pixel_buffer_lock.h:175
clan::PixelBufferLockAny
PixelBufferLock< unsigned char > PixelBufferLockAny
Definition pixel_buffer_lock.h:182
clan
Definition clanapp.h:36
clan::Key::y
@ y
clan::Key::x
@ x