pixel_buffer_lock.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 
39 namespace clan
40 {
43 
46  template<typename Type>
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 
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 
154 
167 
172 
181 
183 
185 }
PixelBufferLock< Vec4f > PixelBufferLock4f
Definition: pixel_buffer_lock.h:176
Definition: clanapp.h:35
Type & get_pixel(int x, int y)
Definition: pixel_buffer_lock.h:85
PixelBufferLock< HalfFloat > PixelBufferLock1hf
Definition: pixel_buffer_lock.h:168
void lock()
Lock the system pixel_buffer.
Definition: pixel_buffer_lock.h:103
PixelBufferLock< Vec2b > PixelBufferLock2b
Definition: pixel_buffer_lock.h:156
PixelBufferLock< Vec3ub > PixelBufferLock3ub
Definition: pixel_buffer_lock.h:144
PixelBufferLock< signed char > PixelBufferLock1b
Definition: pixel_buffer_lock.h:155
int get_height() const
Definition: pixel_buffer_lock.h:87
PixelBufferLock< signed int > PixelBufferLock1i
Definition: pixel_buffer_lock.h:163
PixelBufferLock< Vec3us > PixelBufferLock3us
Definition: pixel_buffer_lock.h:148
int get_lock_count() const
Returns the amounts of recursive pixel_buffer locks performed by this section.
Definition: pixel_buffer_lock.h:78
PixelBufferLock< Vec4i > PixelBufferLock4i
Definition: pixel_buffer_lock.h:166
Pixel data container.
Definition: pixel_buffer.h:67
PixelBufferLock< Vec3i > PixelBufferLock3i
Definition: pixel_buffer_lock.h:165
int get_pitch() const
Returns the pitch (in bytes per scanline).
int get_height() const
Retrieves the actual height of the buffer.
PixelBufferLock< Vec4ui > PixelBufferLock4ui
Definition: pixel_buffer_lock.h:153
void unlock()
Unlock pixel_buffer.
Definition: pixel_buffer_lock.h:119
PixelBufferLock< Vec3hf > PixelBufferLock3hf
Definition: pixel_buffer_lock.h:170
PixelBuffer locking helper.
Definition: pixel_buffer_lock.h:47
PixelBufferLock< Vec2ub > PixelBufferLock2ub
Definition: pixel_buffer_lock.h:143
PixelBufferLock< Vec4ub > PixelBufferLock4ub
Definition: pixel_buffer_lock.h:145
void * get_data()
Returns a pointer to the beginning of the pixel buffer.
PixelBufferLock< Vec2s > PixelBufferLock2s
Definition: pixel_buffer_lock.h:160
~PixelBufferLock()
Definition: pixel_buffer_lock.h:70
void lock(GraphicContext &gc, BufferAccess access)
Lock the gpu pixel_buffer.
Definition: pixel_buffer_lock.h:91
PixelBufferLock< signed short > PixelBufferLock1s
Definition: pixel_buffer_lock.h:159
Top-level exception class.
Definition: exception.h:41
PixelBufferLock< Vec2f > PixelBufferLock2f
Definition: pixel_buffer_lock.h:174
PixelBufferLock< Vec3ui > PixelBufferLock3ui
Definition: pixel_buffer_lock.h:152
bool is_null() const
Returns true if this object is invalid.
Definition: pixel_buffer.h:107
void unlock()
Unmaps element buffer.
PixelBufferLock< Vec2d > PixelBufferLock2d
Definition: pixel_buffer_lock.h:178
PixelBufferLock< Vec2ui > PixelBufferLock2ui
Definition: pixel_buffer_lock.h:151
PixelBufferLock< Vec3b > PixelBufferLock3b
Definition: pixel_buffer_lock.h:157
int get_width() const
Definition: pixel_buffer_lock.h:86
PixelBufferLock< Vec2hf > PixelBufferLock2hf
Definition: pixel_buffer_lock.h:169
PixelBufferLock< Vec4d > PixelBufferLock4d
Definition: pixel_buffer_lock.h:180
PixelBufferLock< unsigned char > PixelBufferLock1ub
Definition: pixel_buffer_lock.h:142
PixelBufferLock< unsigned int > PixelBufferLock1ui
Definition: pixel_buffer_lock.h:150
PixelBufferLock< Vec3s > PixelBufferLock3s
Definition: pixel_buffer_lock.h:161
PixelBufferLock(PixelBuffer &pixel_buffer, bool lock_pixelbuffer=true)
Constructs a system pixel buffer lock.
Definition: pixel_buffer_lock.h:61
PixelBufferLock< Vec3f > PixelBufferLock3f
Definition: pixel_buffer_lock.h:175
Type * get_row(int y)
Definition: pixel_buffer_lock.h:84
PixelBufferLock< Vec2us > PixelBufferLock2us
Definition: pixel_buffer_lock.h:147
PixelBufferLock< Vec4s > PixelBufferLock4s
Definition: pixel_buffer_lock.h:162
bool is_gpu() const
Returns true if this pixel buffer is a GPU based one.
Interface to drawing graphics.
Definition: graphic_context.h:256
PixelBufferLock(GraphicContext &gc, PixelBuffer &pixel_buffer, BufferAccess access, bool lock_pixelbuffer=true)
Constructs a gpu pixel buffer lock.
Definition: pixel_buffer_lock.h:51
PixelBufferLock< Vec4b > PixelBufferLock4b
Definition: pixel_buffer_lock.h:158
PixelBufferLock< Vec3d > PixelBufferLock3d
Definition: pixel_buffer_lock.h:179
PixelBufferLock< double > PixelBufferLock1d
Definition: pixel_buffer_lock.h:177
int get_pitch() const
Definition: pixel_buffer_lock.h:88
BufferAccess
Array Buffer access enum.
Definition: buffer_usage.h:52
PixelBufferLock< Vec4us > PixelBufferLock4us
Definition: pixel_buffer_lock.h:149
PixelBufferLock< Vec4hf > PixelBufferLock4hf
Definition: pixel_buffer_lock.h:171
int get_width() const
Retrieves the actual width of the buffer.
void lock(GraphicContext &gc, BufferAccess access)
Maps buffer into system memory.
PixelBufferLock< unsigned short > PixelBufferLock1us
Definition: pixel_buffer_lock.h:146
Type * get_data()
Definition: pixel_buffer_lock.h:83
PixelBufferLock< float > PixelBufferLock1f
Definition: pixel_buffer_lock.h:173
PixelBufferLock< unsigned char > PixelBufferLockAny
Definition: pixel_buffer_lock.h:182
PixelBufferLock< Vec2i > PixelBufferLock2i
Definition: pixel_buffer_lock.h:164