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