1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored By: Alexandros Frantzis <alexandros.frantzis@canonical.com>
*/
#include "gl_pixel_buffer.h"
#include "mir/graphics/buffer.h"
#include "mir/renderer/gl/context.h"
#include "mir/renderer/gl/texture_source.h"
#include <stdexcept>
#include <boost/throw_exception.hpp>
#include MIR_SERVER_GL_H
#include MIR_SERVER_GLEXT_H
namespace mg = mir::graphics;
namespace ms = mir::scene;
namespace geom = mir::geometry;
namespace
{
bool is_big_endian()
{
uint32_t n = 1;
return (*reinterpret_cast<char*>(&n) != 1);
}
inline uint32_t abgr_to_argb(uint32_t p)
{
return ((p << 16) & 0x00ff0000) | /* Move R to new position */
((p) & 0x0000ff00) | /* G remains at same position */
((p >> 16) & 0x000000ff) | /* Move B to new position */
((p) & 0xff000000); /* A remains at same position */
}
}
ms::GLPixelBuffer::GLPixelBuffer(std::unique_ptr<renderer::gl::Context> gl_context)
: gl_context{std::move(gl_context)},
tex{0}, fbo{0}, gl_pixel_format{0}, pixels_need_y_flip{false}
{
/*
* TODO: Handle systems that are big-endian, and therefore GL_BGRA doesn't
* give the 0xAARRGGBB pixel format we need.
*/
if (is_big_endian())
{
BOOST_THROW_EXCEPTION(std::runtime_error(
"GLPixelBuffer doesn't support big endian architectures"));
}
}
ms::GLPixelBuffer::~GLPixelBuffer() noexcept
{
/*
* This may be called from a different thread
* than the one that called prepare
*/
if (tex != 0 || fbo != 0)
gl_context->make_current();
if (tex != 0)
glDeleteTextures(1, &tex);
if (fbo != 0)
glDeleteFramebuffers(1, &fbo);
}
void ms::GLPixelBuffer::prepare()
{
gl_context->make_current();
if (tex == 0)
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glActiveTexture(GL_TEXTURE0);
if (fbo == 0)
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
}
void ms::GLPixelBuffer::fill_from(graphics::Buffer& buffer)
{
auto width = buffer.size().width.as_uint32_t();
auto height = buffer.size().height.as_uint32_t();
pixels.resize(width * height * 4);
prepare();
auto const texture_source =
dynamic_cast<mir::renderer::gl::TextureSource*>(
buffer.native_buffer_base());
if (!texture_source)
BOOST_THROW_EXCEPTION(std::logic_error("Buffer does not support GL rendering"));
texture_source->gl_bind_to_texture();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
/* First try to get pixels as BGRA */
glGetError();
gl_pixel_format = GL_BGRA_EXT;
glReadPixels(0, 0, width, height, gl_pixel_format, GL_UNSIGNED_BYTE, pixels.data());
/* If getting pixels as BGRA failed, fall back to RGBA */
if (glGetError() != GL_NO_ERROR)
{
gl_pixel_format = GL_RGBA;
glReadPixels(0, 0, width, height, gl_pixel_format, GL_UNSIGNED_BYTE, pixels.data());
}
size_ = buffer.size();
pixels_need_y_flip = true;
}
void const* ms::GLPixelBuffer::as_argb_8888()
{
if (pixels_need_y_flip)
{
auto const stride_val = stride().as_uint32_t();
auto const height = size_.height.as_uint32_t();
std::vector<char> tmp(stride_val);
for (unsigned int i = 0; i < height / 2; i++)
{
/* Store line i */
tmp.assign(&pixels[i * stride_val], &pixels[(i + 1) * stride_val]);
/* Copy line height - i - 1 to line i */
copy_and_convert_pixel_line(&pixels[(height - i - 1) * stride_val],
&pixels[i * stride_val]);
/* Copy stored line (i) to height - i - 1 */
copy_and_convert_pixel_line(tmp.data(),
&pixels[(height - i - 1) * stride_val]);
}
/* Process middle line if there is one */
if (height % 2 == 1)
{
copy_and_convert_pixel_line(&pixels[(height / 2) * stride_val],
&pixels[(height / 2) * stride_val]);
}
pixels_need_y_flip = false;
}
return pixels.data();
}
geom::Size ms::GLPixelBuffer::size() const
{
return size_;
}
geom::Stride ms::GLPixelBuffer::stride() const
{
return geom::Stride{size_.width.as_uint32_t() * sizeof(uint32_t)};
}
void ms::GLPixelBuffer::copy_and_convert_pixel_line(char* src, char* dst)
{
if (gl_pixel_format == GL_RGBA)
{
/* Convert from abgr_8888 to argb_8888 while copying */
auto pixels_src = reinterpret_cast<uint32_t*>(src);
auto pixels_dst = reinterpret_cast<uint32_t*>(dst);
auto const width = size_.width.as_uint32_t();
for (uint32_t n = 0; n < width; n++)
pixels_dst[n] = abgr_to_argb(pixels_src[n]);
}
else if (src != dst)
{
std::copy(src, src + stride().as_uint32_t(), dst);
}
}
|