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>
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 
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 
169 
182 
187 
196 
198 
199 }
200 
PixelBufferLock< HalfFloat > PixelBufferLock1hf
Definition: pixel_buffer_lock.h:183
PixelBufferLock< Vec2b > PixelBufferLock2b
Definition: pixel_buffer_lock.h:171
void unlock()
Unlock pixel_buffer.
Definition: pixel_buffer_lock.h:130
int get_width() const
Definition: pixel_buffer_lock.h:93
int get_width() const
Retrieves the actual width of the buffer.
PixelBufferLock< Vec2i > PixelBufferLock2i
Definition: pixel_buffer_lock.h:179
PixelBufferLock< Vec4f > PixelBufferLock4f
Definition: pixel_buffer_lock.h:191
PixelBufferLock< Vec3ub > PixelBufferLock3ub
Definition: pixel_buffer_lock.h:159
PixelBufferLock< signed char > PixelBufferLock1b
Definition: pixel_buffer_lock.h:170
PixelBufferLock< Vec4ui > PixelBufferLock4ui
Definition: pixel_buffer_lock.h:168
PixelBufferLock< signed int > PixelBufferLock1i
Definition: pixel_buffer_lock.h:178
PixelBufferLock< Vec3hf > PixelBufferLock3hf
Definition: pixel_buffer_lock.h:185
~PixelBufferLock()
Definition: pixel_buffer_lock.h:73
PixelBufferLock< Vec3us > PixelBufferLock3us
Definition: pixel_buffer_lock.h:163
PixelBufferLock< Vec2ub > PixelBufferLock2ub
Definition: pixel_buffer_lock.h:158
void * get_data()
Returns a pointer to the beginning of the pixel buffer.
PixelBufferLock< Vec4i > PixelBufferLock4i
Definition: pixel_buffer_lock.h:181
int get_pitch() const
Definition: pixel_buffer_lock.h:95
PixelBufferLock< Vec3i > PixelBufferLock3i
Definition: pixel_buffer_lock.h:180
Type & get_pixel(int x, int y)
Definition: pixel_buffer_lock.h:92
PixelBufferLock< Vec4ub > PixelBufferLock4ub
Definition: pixel_buffer_lock.h:160
PixelBufferLock< Vec2f > PixelBufferLock2f
Definition: pixel_buffer_lock.h:189
void lock(GraphicContext &gc, BufferAccess access)
Lock the gpu pixel_buffer.
Definition: pixel_buffer_lock.h:102
int get_pitch() const
Returns the pitch (in bytes per scanline).
int get_height() const
Definition: pixel_buffer_lock.h:94
PixelBufferLock< Vec2s > PixelBufferLock2s
Definition: pixel_buffer_lock.h:175
Type * get_row(int y)
Definition: pixel_buffer_lock.h:91
PixelBufferLock< signed short > PixelBufferLock1s
Definition: pixel_buffer_lock.h:174
PixelBufferLock< Vec2d > PixelBufferLock2d
Definition: pixel_buffer_lock.h:193
PixelBufferLock< Vec2ui > PixelBufferLock2ui
Definition: pixel_buffer_lock.h:166
PixelBufferLock< Vec3ui > PixelBufferLock3ui
Definition: pixel_buffer_lock.h:167
void unlock()
Unmaps element buffer.
Interface to drawing graphics.
Definition: graphic_context.h:258
PixelBufferLock< Vec4d > PixelBufferLock4d
Definition: pixel_buffer_lock.h:195
PixelBufferLock< unsigned int > PixelBufferLock1ui
Definition: pixel_buffer_lock.h:165
PixelBufferLock< Vec3b > PixelBufferLock3b
Definition: pixel_buffer_lock.h:172
void lock(GraphicContext &gc, BufferAccess access)
Maps buffer into system memory.
PixelBufferLock< Vec3s > PixelBufferLock3s
Definition: pixel_buffer_lock.h:176
PixelBufferLock< Vec2hf > PixelBufferLock2hf
Definition: pixel_buffer_lock.h:184
PixelBufferLock< Vec2us > PixelBufferLock2us
Definition: pixel_buffer_lock.h:162
PixelBufferLock< Vec4s > PixelBufferLock4s
Definition: pixel_buffer_lock.h:177
PixelBufferLock< unsigned char > PixelBufferLock1ub
Definition: pixel_buffer_lock.h:157
PixelBufferLock< Vec4b > PixelBufferLock4b
Definition: pixel_buffer_lock.h:173
bool is_null() const
Returns true if this object is invalid.
Definition: pixel_buffer.h:116
Definition: clanapp.h:36
PixelBufferLock< double > PixelBufferLock1d
Definition: pixel_buffer_lock.h:192
PixelBufferLock< Vec3f > PixelBufferLock3f
Definition: pixel_buffer_lock.h:190
PixelBuffer locking helper.
Definition: pixel_buffer_lock.h:49
Pixel data container.
Definition: pixel_buffer.h:69
PixelBufferLock< Vec4us > PixelBufferLock4us
Definition: pixel_buffer_lock.h:164
PixelBufferLock< Vec3d > PixelBufferLock3d
Definition: pixel_buffer_lock.h:194
int get_lock_count() const
Returns the amounts of recursive pixel_buffer locks performed by this section.
Definition: pixel_buffer_lock.h:85
void lock()
Lock the system pixel_buffer.
Definition: pixel_buffer_lock.h:114
Top-level exception class.
Definition: exception.h:43
PixelBufferLock< unsigned short > PixelBufferLock1us
Definition: pixel_buffer_lock.h:161
Type * get_data()
Definition: pixel_buffer_lock.h:90
BufferAccess
Array Buffer access enum.
Definition: buffer_usage.h:55
PixelBufferLock(PixelBuffer &pixel_buffer, bool lock_pixelbuffer=true)
Constructs a system pixel buffer lock.
Definition: pixel_buffer_lock.h:64
bool is_gpu() const
Returns true if this pixel buffer is a GPU based one.
PixelBufferLock< Vec4hf > PixelBufferLock4hf
Definition: pixel_buffer_lock.h:186
PixelBufferLock< unsigned char > PixelBufferLockAny
Definition: pixel_buffer_lock.h:197
int get_height() const
Retrieves the actual height of the buffer.
PixelBufferLock< float > PixelBufferLock1f
Definition: pixel_buffer_lock.h:188
PixelBufferLock(GraphicContext &gc, PixelBuffer &pixel_buffer, BufferAccess access, bool lock_pixelbuffer=true)
Constructs a gpu pixel buffer lock.
Definition: pixel_buffer_lock.h:54