cognitive-renderer-gl 0.0.2

GL rendering for `cognitive`
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

//! This module contains GL renderer which allows drawing frame scenes with GL.

// -------------------------------------------------------------------------------------------------

use std;
use std::time::Instant;

use gl;
use egl;

use cognitive_graphics::{egl_tools, gl_tools};
use cognitive_graphics::attributes::{DmabufAttributes, EglAttributes};
use qualia::{SurfaceViewer, SurfaceContext, Illusion, Size, PixelFormat, SurfaceId};
use qualia::{Buffer, DataSource, Image, MemoryView, Pixmap};

use cache_gl::CacheGl;

// -------------------------------------------------------------------------------------------------

/// Vertex shader source code for OpenGL ES 2.0 (GLSL ES 100)
const VERTEX_SHADER_100: &'static str = include_str!("vertex.100.glsl");

/// Fragment shader source code for OpenGL ES 2.0 (GLSL ES 100)
const FRAGMENT_SHADER_100: &'static str = include_str!("fragment.100.glsl");

/// Vertex shader source code for OpenGL ES 3.0 (GLSL ES 300)
const VERTEX_SHADER_300: &'static str = include_str!("vertex.300.glsl");

/// Fragment shader source code for OpenGL ES 3.0 (GLSL ES 300)
const FRAGMENT_SHADER_300: &'static str = include_str!("fragment.300.glsl");

// -------------------------------------------------------------------------------------------------

/// GL renderer.
pub struct RendererGl {
    egl: egl_tools::EglBucket,
    size: Size,
    cache: CacheGl,

    // GL rendering
    program: gl::types::GLuint,
    loc_vertices: gl::types::GLint,
    loc_texcoords: gl::types::GLint,
    loc_texture: gl::types::GLint,
    loc_screen_size: gl::types::GLint,
    vbo_vertices: gl::types::GLuint,
    vbo_texcoords: gl::types::GLuint,

    // Pointers to extension functions
    image_target_texture: Option<egl_tools::ImageTargetTexture2DOesFn>,
}

// -------------------------------------------------------------------------------------------------

impl RendererGl {
    /// `RendererGl` constructor.
    pub fn new(egl: egl_tools::EglBucket, size: Size) -> Self {
        RendererGl {
            egl: egl,
            size: size,
            cache: CacheGl::new(),
            program: gl::types::GLuint::default(),
            loc_vertices: gl::types::GLint::default(),
            loc_texcoords: gl::types::GLint::default(),
            loc_texture: gl::types::GLint::default(),
            loc_screen_size: gl::types::GLint::default(),
            vbo_vertices: gl::types::GLuint::default(),
            vbo_texcoords: gl::types::GLuint::default(),
            image_target_texture: None,
        }
    }

    /// Initialize renderer.
    ///  - prepare shaders and program,
    ///  - bind locations,
    ///  - generate buffers,
    ///  - configure textures,
    pub fn initialize(&mut self) -> Result<(), Illusion> {
        gl::load_with(|s| egl::get_proc_address(s) as *const std::os::raw::c_void);

        let _context = self.egl.make_current()?;

        // Get GLSL version
        let (vshader_src, fshader_src) = match gl_tools::get_shading_lang_version() {
            gl_tools::GlslVersion::Glsl100 => {
                (VERTEX_SHADER_100.to_owned(), FRAGMENT_SHADER_100.to_owned())
            }
            gl_tools::GlslVersion::Glsl300 => {
                (VERTEX_SHADER_300.to_owned(), FRAGMENT_SHADER_300.to_owned())
            }
            gl_tools::GlslVersion::Unknown => {
                return Err(Illusion::General(format!("Could not figure out GLSL version")));
            }
        };

        // Compile shades, link program and get locations
        self.program = gl_tools::prepare_shader_program(vshader_src, fshader_src)?;
        self.loc_vertices = gl_tools::get_attrib_location(self.program, "vertices".to_owned())?;
        self.loc_texcoords = gl_tools::get_attrib_location(self.program, "texcoords".to_owned())?;
        self.loc_texture = gl_tools::get_uniform_location(self.program, "texture".to_owned())?;
        self.loc_screen_size = gl_tools::get_uniform_location(self.program,
                                                              "screen_size".to_owned())?;

        // Generate vertex buffer object
        unsafe {
            gl::GenBuffers(1, &mut self.vbo_vertices);
            gl::GenBuffers(1, &mut self.vbo_texcoords);
        }

        // Get needed extension functions
        self.image_target_texture = egl_tools::get_proc_addr_of_image_target_texture_2d_oes();

        Ok(())
    }

    /// Draw passed frame scene.
    pub fn draw(&mut self,
                layunder: &Vec<SurfaceContext>,
                surfaces: &Vec<SurfaceContext>,
                layover: &Vec<SurfaceContext>,
                viewer: &SurfaceViewer)
                -> Result<(), Illusion> {
        let _context = self.egl.make_current()?;
        self.prepare_view();
        self.draw_surfaces(layunder, viewer);
        self.draw_surfaces(surfaces, viewer);
        self.draw_surfaces(layover, viewer);
        self.release_view();
        Ok(())
    }

    /// Swap buffers.
    pub fn swap_buffers(&mut self) -> Result<(), Illusion> {
        let context = self.egl.make_current()?;
        context.swap_buffers()?;
        Ok(())
    }

    /// Reads pixels for whole screen and returns image data as `Buffer`.
    pub fn take_screenshot(&self) -> Result<Buffer, Illusion> {
        let _context = self.egl.make_current()?;

        let format = PixelFormat::ARGB8888;
        let stride = format.get_size() * self.size.width;
        let size = stride * self.size.height;
        let mut dst: Vec<u8> = Vec::with_capacity(size);
        unsafe { dst.set_len(size) };

        unsafe {
            gl::ReadBuffer(gl::BACK);
            gl::ReadPixels(0,
                           0,
                           self.size.width as i32,
                           self.size.height as i32,
                           gl::RGBA,
                           gl::UNSIGNED_BYTE,
                           dst.as_mut_ptr() as *mut std::os::raw::c_void);
        }

        // GL returns data starting from bottom. We have to reverse the order.
        let mut data = Vec::new();
        for chunk in dst.chunks(stride).rev() {
            data.extend(chunk);
        }

        Ok(Buffer::new(format, self.size.width, self.size.height, stride, data))
    }
}

// -------------------------------------------------------------------------------------------------

/// Drawing helpers.
impl RendererGl {
    /// Prepare view for drawing.
    fn prepare_view(&self) {
        unsafe {
            gl::ClearColor(0.0, 0.3, 0.5, 1.0);
            gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);

            gl::Enable(gl::BLEND);
            gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);

            gl::UseProgram(self.program);
            gl::Uniform2i(self.loc_screen_size, self.size.width as i32, self.size.height as i32);
        }
    }

    /// Loads memory buffer as texture. Returns dimensions of the buffer.
    fn load_buffer_as_texture(&mut self,
                              sid: SurfaceId,
                              buffer: &MemoryView,
                              time_stamp: Instant)
                              -> Option<Size> {
        let format = {
            match buffer.get_format() {
                // NOTE: Mixing channels is intentional. In `PixelFormat` one reads it from
                // right to left, and in `gl` from left to right.
                PixelFormat::XBGR8888 => gl::RGBA,
                PixelFormat::ABGR8888 => gl::RGBA,
                PixelFormat::XRGB8888 => gl::BGRA,
                PixelFormat::ARGB8888 => gl::BGRA,
            }
        };

        // Get or generate texture info
        let texinfo = self.cache.get_or_generate_info(sid);
        unsafe { gl::BindTexture(gl::TEXTURE_2D, texinfo.get_texture()) };

        // If buffer was updated recently - load it to GPU memory
        if texinfo.is_younger(time_stamp) {
            unsafe {
                gl::TexImage2D(gl::TEXTURE_2D, // target
                               0, // level, 0 = no mipmap
                               gl::RGBA as gl::types::GLint, // internal format
                               buffer.get_width() as gl::types::GLint, // width
                               buffer.get_height() as gl::types::GLint, // height
                               0, // always 0 in OpenGL ES
                               format, // format
                               gl::UNSIGNED_BYTE, // type
                               buffer.as_ptr() as *const _);
            }
            self.cache.update(sid, None);
        }

        Some(buffer.get_size())
    }

    /// Loads hardware image as texture. Returns dimensions of the image.
    fn load_image_as_texture(&mut self,
                             sid: SurfaceId,
                             attrs: &EglAttributes,
                             time_stamp: Instant)
                             -> Option<Size> {
        // Get or generate texture info
        let texinfo = self.cache.get_or_generate_info(sid);
        unsafe { gl::BindTexture(gl::TEXTURE_2D, texinfo.get_texture()) };

        // If buffer was updated recently - load it to GPU memory
        if texinfo.is_younger(time_stamp) {
            // Destroy image if it was created previously
            if let Some(image) = texinfo.get_image() {
                let _ = egl_tools::destroy_image(self.egl.display, image);
            }

            // Create the image
            if let Some(image_target_texture) = self.image_target_texture {
                let image = egl_tools::create_image(self.egl.display, attrs);
                if let Some(ref img) = image {
                    // Set image as texture target and update cache
                    image_target_texture(gl::TEXTURE_2D, img.as_raw());
                    self.cache.update(sid, image.clone());
                    Some(attrs.get_size())
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            Some(attrs.get_size())
        }
    }

    /// Loads dmabuf as texture. Returns dimensions of the dmabuf.
    fn load_dmabuf_as_texture(&mut self,
                              sid: SurfaceId,
                              attrs: &DmabufAttributes,
                              time_stamp: Instant)
                              -> Option<Size> {
        // Get or generate texture info
        let texinfo = self.cache.get_or_generate_info(sid);
        unsafe { gl::BindTexture(gl::TEXTURE_2D, texinfo.get_texture()) };

        // If buffer was updated recently - load it to GPU memory
        if texinfo.is_younger(time_stamp) {
            // Destroy image if it was created previously
            if let Some(image) = texinfo.get_image() {
                let _ = egl_tools::destroy_image(self.egl.display, image);
            }

            // Create the image
            if let Some(image_target_texture) = self.image_target_texture {
                let image = egl_tools::import_dmabuf(self.egl.display, attrs);
                if let Some(ref img) = image {
                    // Set image as texture target and update cache
                    image_target_texture(gl::TEXTURE_2D, img.as_raw());
                    self.cache.update(sid, image.clone());
                    Some(attrs.get_size())
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            Some(attrs.get_size())
        }
    }

    /// Load textures and prepare vertices.
    fn load_texture_and_prepare_vertices(&mut self,
                                         viewer: &SurfaceViewer,
                                         context: &SurfaceContext,
                                         vertices: &mut [gl::types::GLfloat],
                                         texcoords: &mut [gl::types::GLfloat]) {
        if let Some(ref surface) = viewer.get_surface(context.id) {
            let size = {
                match surface.data_source {
                    DataSource::Shm { ref source, time_stamp } => {
                        self.load_buffer_as_texture(context.id, source, time_stamp)
                    }
                    DataSource::EglImage { ref source, time_stamp } => {
                        self.load_image_as_texture(context.id, source, time_stamp)
                    }
                    DataSource::Dmabuf { ref source, time_stamp } => {
                        self.load_dmabuf_as_texture(context.id, source, time_stamp)
                    }
                    DataSource::None => None,
                }
            };

            if let Some(size) = size {
                let left = (context.pos.x - surface.offset.x) as gl::types::GLfloat;
                let top = (context.pos.y - surface.offset.y) as gl::types::GLfloat;
                let right = left + size.width as gl::types::GLfloat;
                let bottom = top + size.height as gl::types::GLfloat;

                vertices[0] = left;
                vertices[1] = top;
                vertices[2] = right;
                vertices[3] = top;
                vertices[4] = left;
                vertices[5] = bottom;
                vertices[6] = right;
                vertices[7] = top;
                vertices[8] = right;
                vertices[9] = bottom;
                vertices[10] = left;
                vertices[11] = bottom;

                // TODO: Use element buffer.
                texcoords[0] = 0.0;
                texcoords[1] = 0.0;
                texcoords[2] = 1.0;
                texcoords[3] = 0.0;
                texcoords[4] = 0.0;
                texcoords[5] = 1.0;
                texcoords[6] = 1.0;
                texcoords[7] = 0.0;
                texcoords[8] = 1.0;
                texcoords[9] = 1.0;
                texcoords[10] = 0.0;
                texcoords[11] = 1.0;
            } else {
                log_warn3!("Renderer: No buffer for surface {}", context.id);
            }
        } else {
            log_warn3!("Renderer: No info for surface {}", context.id);
        }
    }

    /// Draws surfaces.
    fn draw_surfaces(&mut self, surfaces: &Vec<SurfaceContext>, viewer: &SurfaceViewer) {
        if surfaces.len() == 0 {
            return;
        }

        // Prepare vertices positions and upload textures
        let vertices_len = 12 * surfaces.len();
        let vertices_size = vertices_len * std::mem::size_of::<gl::types::GLfloat>();
        let mut vertices = vec![0.0; vertices_len];
        let mut texcoords = vec![0.0; vertices_len];

        for i in 0..surfaces.len() {
            // Activate the target texture
            unsafe { gl::ActiveTexture(gl::TEXTURE0 + i as u32) };

            // Bind data to texture and prepare vertices
            self.load_texture_and_prepare_vertices(viewer,
                                                   &surfaces[i],
                                                   &mut vertices[12 * i..12 * i + 12],
                                                   &mut texcoords[12 * i..12 * i + 12]);
        }

        unsafe {
            // Upload positions to vertex buffer object
            gl::BindBuffer(gl::ARRAY_BUFFER, self.vbo_vertices);
            gl::EnableVertexAttribArray(self.loc_vertices as gl::types::GLuint);
            gl::VertexAttribPointer(self.loc_vertices as gl::types::GLuint,
                                    2,
                                    gl::FLOAT,
                                    gl::FALSE,
                                    2 *
                                    std::mem::size_of::<gl::types::GLfloat>() as gl::types::GLint,
                                    std::ptr::null());
            gl::BufferData(gl::ARRAY_BUFFER,
                           vertices_size as isize,
                           vertices.as_ptr() as *const _,
                           gl::DYNAMIC_DRAW);

            // Upload positions to vertex buffer object
            gl::BindBuffer(gl::ARRAY_BUFFER, self.vbo_texcoords);
            gl::EnableVertexAttribArray(self.loc_texcoords as gl::types::GLuint);
            gl::VertexAttribPointer(self.loc_texcoords as gl::types::GLuint,
                                    2,
                                    gl::FLOAT,
                                    gl::FALSE,
                                    2 *
                                    std::mem::size_of::<gl::types::GLfloat>() as gl::types::GLint,
                                    std::ptr::null());
            gl::BufferData(gl::ARRAY_BUFFER,
                           vertices_size as isize,
                           texcoords.as_ptr() as *const _,
                           gl::DYNAMIC_DRAW);

            // Redraw everything
            for i in 0..surfaces.len() as i32 {
                gl::Uniform1i(self.loc_texture, i);
                gl::DrawArrays(gl::TRIANGLES, 6 * i, 6);
            }

            // Release resources
            gl::DisableVertexAttribArray(self.loc_texcoords as gl::types::GLuint);
            gl::DisableVertexAttribArray(self.loc_vertices as gl::types::GLuint);
        }
    }

    /// Unbind framebuffer and program.
    fn release_view(&self) {
        unsafe {
            gl::BindFramebuffer(gl::DRAW_FRAMEBUFFER, 0);
            gl::UseProgram(0);
        }
    }
}

// -------------------------------------------------------------------------------------------------