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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
//! This module implements the functionality to support graphics in the grid.
use std::mem;
use crate::display::SizeInfo;
use crate::renderer::{RenderRect, ShaderVersion};
use alacritty_terminal::graphics::{
ClearSubregion, ColorType, GraphicData, GraphicId, UpdateQueues,
};
use crossfont::Metrics;
use log::trace;
use serde::{Deserialize, Serialize};
use crate::gl::types::*;
use crate::{gl, renderer};
use std::cmp;
use std::collections::{HashMap, HashSet};
use std::ffi::CStr;
mod draw;
mod shader;
/// Max. number of textures stored in the GPU.
const MAX_TEXTURES_COUNT: usize = 1000;
pub use draw::RenderList;
bitflags::bitflags! {
/// Result of the `run_updates` operation.
pub struct UpdateResult: u8 {
const SUCCESS = 1 << 0;
const NEED_RESET_ACTIVE_TEX = 1 << 1;
}
}
/// Type for texture names generated in the GPU.
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
pub struct TextureName(GLuint);
impl Drop for TextureName {
fn drop(&mut self) {
if self.0 != 0 {
trace!("Delete texture {}.", self.0);
unsafe {
gl::DeleteTextures(1, &self.0);
}
}
}
}
/// Texture for a graphic in the grid.
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct GraphicTexture {
/// Texture in the GPU where the graphic pixels are stored.
texture: TextureName,
/// Cell height at the moment graphic was created.
///
/// Used to scale it if the user increases or decreases the font size.
cell_height: f32,
/// Cell width at the moment graphic was created.
cell_width: f32,
/// Width in pixels of the graphic.
width: u16,
/// Height in pixels of the graphic.
height: u16,
}
#[derive(Debug)]
pub struct GraphicsRenderer {
/// Program in the GPU to render graphics.
program: shader::GraphicsShaderProgram,
/// Collection to associate graphic identifiers with their textures.
graphic_textures: HashMap<GraphicId, GraphicTexture>,
/// Indicate if the OpenGL driver has the `clear_texture` extension.
clear_texture_ext: bool,
}
impl GraphicsRenderer {
pub fn new(shader_version: ShaderVersion) -> Result<GraphicsRenderer, renderer::Error> {
let program = shader::GraphicsShaderProgram::new(shader_version)?;
let clear_texture_ext = check_opengl_extensions(&["GL_ARB_clear_texture"]);
Ok(GraphicsRenderer { program, graphic_textures: HashMap::default(), clear_texture_ext })
}
/// Run the required actions to apply changes for the graphics in the grid.
#[inline]
pub fn run_updates(
&mut self,
update_queues: UpdateQueues,
size_info: &SizeInfo,
) -> UpdateResult {
self.remove_graphics(update_queues.remove_queue)
| self.upload_pending_graphics(update_queues.pending, size_info)
| self.clear_subregions(update_queues.clear_subregions)
}
/// Release resources used by removed graphics.
fn remove_graphics(&mut self, removed_ids: Vec<GraphicId>) -> UpdateResult {
let mut textures = Vec::with_capacity(removed_ids.len());
for id in removed_ids {
if let Some(mut graphic_texture) = self.graphic_textures.remove(&id) {
// Reset the inner value of TextureName, so the Drop implementation
// (in debug mode) can verify that the texture was deleted.
textures.push(mem::take(&mut graphic_texture.texture.0));
}
}
trace!("Call glDeleteTextures with {} items", textures.len());
unsafe {
gl::DeleteTextures(textures.len() as GLint, textures.as_ptr());
}
UpdateResult::SUCCESS
}
/// Create new textures in the GPU, and upload the pixels to them.
fn upload_pending_graphics(
&mut self,
graphics: Vec<GraphicData>,
size_info: &SizeInfo,
) -> UpdateResult {
if graphics.is_empty() {
return UpdateResult::SUCCESS;
}
for graphic in graphics {
let mut texture = 0;
unsafe {
gl::GenTextures(1, &mut texture);
trace!("Texture generated: {texture}");
gl::BindTexture(gl::TEXTURE_2D, texture);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAX_LEVEL, 0);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as GLint);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as GLint);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as GLint);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as GLint);
let pixel_format = match graphic.color_type {
ColorType::Rgb => gl::RGB,
ColorType::Rgba => gl::RGBA,
};
gl::TexImage2D(
gl::TEXTURE_2D,
0,
gl::RGBA as GLint,
graphic.width as GLint,
graphic.height as GLint,
0,
pixel_format,
gl::UNSIGNED_BYTE,
graphic.pixels.as_ptr().cast(),
);
gl::BindTexture(gl::TEXTURE_2D, 0);
}
let graphic_texture = GraphicTexture {
texture: TextureName(texture),
cell_height: size_info.cell_height(),
cell_width: size_info.cell_width(),
width: graphic.width as u16,
height: graphic.height as u16,
};
self.graphic_textures.insert(graphic.id, graphic_texture);
}
// If we exceed the textures limit, discard the oldest ones.
while self.graphic_textures.len() > MAX_TEXTURES_COUNT {
match self.graphic_textures.keys().min().copied() {
Some(id) => self.graphic_textures.remove(&id),
None => unreachable!(),
};
}
UpdateResult::NEED_RESET_ACTIVE_TEX
}
/// Update textures in the GPU to clear specific subregions.
pub fn clear_subregions(&mut self, clear_subregions: Vec<ClearSubregion>) -> UpdateResult {
// If the GL_ARB_clear_texture extension is available we can use
// glClearTexSubImage to clear the region without sending any memory.
//
// If the extension is not available, we have to initialize empty memory
// and upload it with glTexSubImage2D.
let mut result = UpdateResult::SUCCESS;
for clear_subregion in clear_subregions {
let entry = match self.graphic_textures.get(&clear_subregion.id) {
Some(entry) => entry,
None => continue,
};
let x_offset = clear_subregion.x as GLint;
let y_offset = clear_subregion.y as GLint;
let max_width = entry.width as GLint - x_offset;
let max_height = entry.height as GLint - y_offset;
let width = cmp::min(entry.cell_width as GLint, max_width);
let height = cmp::min(entry.cell_height as GLint, max_height);
if self.clear_texture_ext {
let empty = [0_u8; 4];
unsafe {
gl::ClearTexSubImage(
entry.texture.0,
0,
x_offset,
y_offset,
0,
width,
height,
1,
gl::RGBA,
gl::UNSIGNED_BYTE,
empty.as_ptr().cast(),
);
}
} else {
let buf_size = width * height * 4;
let empty = vec![0_u8; buf_size as usize];
unsafe {
gl::BindTexture(gl::TEXTURE_2D, entry.texture.0);
gl::TexSubImage2D(
gl::TEXTURE_2D,
0,
x_offset,
y_offset,
width,
height,
gl::RGBA,
gl::UNSIGNED_BYTE,
empty.as_ptr().cast(),
)
}
result = UpdateResult::NEED_RESET_ACTIVE_TEX;
}
}
result
}
/// Draw graphics in the display.
#[inline]
pub fn draw(
&mut self,
render_list: RenderList,
size_info: &SizeInfo,
rects: &mut Vec<RenderRect>,
metrics: &Metrics,
) {
if !render_list.is_empty() {
render_list.draw(self, size_info, rects, metrics);
}
}
}
fn check_opengl_extensions(extensions: &[&str]) -> bool {
// Use a HashSet to track extensions needed to be found. When the set is
// empty, we know that all extensions are available.
let mut needed: HashSet<_> = extensions.iter().collect();
let mut num_exts = 0;
unsafe {
gl::GetIntegerv(gl::NUM_EXTENSIONS, &mut num_exts);
}
for index in 0..num_exts as GLuint {
let pointer = unsafe { gl::GetStringi(gl::EXTENSIONS, index) };
if pointer.is_null() {
log::warn!("Can't get OpenGL extension name at index {index} of {num_exts}");
return false;
}
let extension = unsafe { CStr::from_ptr(pointer.cast()) };
if let Ok(ext) = extension.to_str() {
if needed.remove(&ext) && needed.is_empty() {
return true;
}
}
}
log::debug!("Missing OpenGL extensions: {needed:?}");
false
}
|