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
/* 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 https://mozilla.org/MPL/2.0/. */

use std::rc::Rc;

use gleam::gl::{self, Gl};
use image::RgbaImage;
use log::{trace, warn};
use servo_geometry::FramebufferUintLength;

pub struct RenderTargetInfo {
    gl: Rc<dyn Gl>,
    framebuffer_ids: Vec<gl::GLuint>,
    renderbuffer_ids: Vec<gl::GLuint>,
    texture_ids: Vec<gl::GLuint>,
}

impl RenderTargetInfo {
    pub fn new(
        gl: Rc<dyn Gl>,
        width: FramebufferUintLength,
        height: FramebufferUintLength,
    ) -> Self {
        let framebuffer_ids = gl.gen_framebuffers(1);
        gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer_ids[0]);
        trace!("Configuring fbo {}", framebuffer_ids[0]);

        let texture_ids = gl.gen_textures(1);
        gl.bind_texture(gl::TEXTURE_2D, texture_ids[0]);
        gl.tex_image_2d(
            gl::TEXTURE_2D,
            0,
            gl::RGBA as gl::GLint,
            width.get() as gl::GLsizei,
            height.get() as gl::GLsizei,
            0,
            gl::RGBA,
            gl::UNSIGNED_BYTE,
            None,
        );
        gl.tex_parameter_i(
            gl::TEXTURE_2D,
            gl::TEXTURE_MAG_FILTER,
            gl::NEAREST as gl::GLint,
        );
        gl.tex_parameter_i(
            gl::TEXTURE_2D,
            gl::TEXTURE_MIN_FILTER,
            gl::NEAREST as gl::GLint,
        );

        gl.framebuffer_texture_2d(
            gl::FRAMEBUFFER,
            gl::COLOR_ATTACHMENT0,
            gl::TEXTURE_2D,
            texture_ids[0],
            0,
        );

        gl.bind_texture(gl::TEXTURE_2D, 0);

        let renderbuffer_ids = gl.gen_renderbuffers(1);
        let depth_rb = renderbuffer_ids[0];
        gl.bind_renderbuffer(gl::RENDERBUFFER, depth_rb);
        gl.renderbuffer_storage(
            gl::RENDERBUFFER,
            gl::DEPTH_COMPONENT24,
            width.get() as gl::GLsizei,
            height.get() as gl::GLsizei,
        );
        gl.framebuffer_renderbuffer(
            gl::FRAMEBUFFER,
            gl::DEPTH_ATTACHMENT,
            gl::RENDERBUFFER,
            depth_rb,
        );

        Self {
            gl,
            framebuffer_ids,
            renderbuffer_ids,
            texture_ids,
        }
    }

    pub fn framebuffer_id(&self) -> gl::GLuint {
        *self.framebuffer_ids.first().expect("Guaranteed by new")
    }

    pub fn bind(&self) {
        trace!("Binding FBO {}", self.framebuffer_id());
        self.gl
            .bind_framebuffer(gl::FRAMEBUFFER, self.framebuffer_id());
    }

    pub fn unbind(&self) {
        trace!("Unbinding FBO {}", self.framebuffer_id());
        self.gl.bind_framebuffer(gl::FRAMEBUFFER, 0);
    }

    pub fn read_back_from_gpu(
        self,
        x: i32,
        y: i32,
        width: FramebufferUintLength,
        height: FramebufferUintLength,
    ) -> RgbaImage {
        let width = width.get() as usize;
        let height = height.get() as usize;
        // For some reason, OSMesa fails to render on the 3rd
        // attempt in headless mode, under some conditions.
        // I think this can only be some kind of synchronization
        // bug in OSMesa, but explicitly un-binding any vertex
        // array here seems to work around that bug.
        // See https://github.com/servo/servo/issues/18606.
        self.gl.bind_vertex_array(0);

        let mut pixels = self.gl.read_pixels(
            x,
            y,
            width as gl::GLsizei,
            height as gl::GLsizei,
            gl::RGBA,
            gl::UNSIGNED_BYTE,
        );
        let gl_error = self.gl.get_error();
        if gl_error != gl::NO_ERROR {
            warn!("GL error code 0x{gl_error:x} set after read_pixels");
        }

        // flip image vertically (texture is upside down)
        let orig_pixels = pixels.clone();
        let stride = width * 4;
        for y in 0..height {
            let dst_start = y * stride;
            let src_start = (height - y - 1) * stride;
            let src_slice = &orig_pixels[src_start..src_start + stride];
            pixels[dst_start..dst_start + stride].clone_from_slice(&src_slice[..stride]);
        }

        RgbaImage::from_raw(width as u32, height as u32, pixels).expect("Flipping image failed!")
    }
}

impl Drop for RenderTargetInfo {
    fn drop(&mut self) {
        trace!("Dropping FBO {}", self.framebuffer_id());
        self.unbind();
        self.gl.delete_textures(&self.texture_ids);
        self.gl.delete_renderbuffers(&self.renderbuffer_ids);
        self.gl.delete_framebuffers(&self.framebuffer_ids);
    }
}

#[cfg(test)]
mod test {
    use gleam::gl;
    use image::Rgba;
    use servo_geometry::FramebufferUintLength;
    use surfman::{Connection, ContextAttributeFlags, ContextAttributes, Error, GLApi, GLVersion};

    use super::RenderTargetInfo;

    #[test]
    #[allow(unsafe_code)]
    fn test_read_pixels() -> Result<(), Error> {
        let connection = Connection::new()?;
        let adapter = connection.create_software_adapter()?;
        let mut device = connection.create_device(&adapter)?;
        let context_descriptor = device.create_context_descriptor(&ContextAttributes {
            version: GLVersion::new(3, 0),
            flags: ContextAttributeFlags::empty(),
        })?;
        let mut context = device.create_context(&context_descriptor, None)?;

        let gl = match connection.gl_api() {
            GLApi::GL => unsafe { gl::GlFns::load_with(|s| device.get_proc_address(&context, s)) },
            GLApi::GLES => unsafe {
                gl::GlesFns::load_with(|s| device.get_proc_address(&context, s))
            },
        };

        device.make_context_current(&context)?;

        {
            const WIDTH: FramebufferUintLength = FramebufferUintLength::new(16);
            const HEIGHT: FramebufferUintLength = FramebufferUintLength::new(16);
            let render_target = RenderTargetInfo::new(gl, WIDTH, HEIGHT);
            render_target.bind();
            render_target
                .gl
                .clear_color(12.0 / 255.0, 34.0 / 255.0, 56.0 / 255.0, 78.0 / 255.0);
            render_target.gl.clear(gl::COLOR_BUFFER_BIT);

            let img = render_target.read_back_from_gpu(0, 0, WIDTH, HEIGHT);
            assert_eq!(img.width(), WIDTH.get());
            assert_eq!(img.height(), HEIGHT.get());

            let expected_pixel: Rgba<u8> = Rgba([12, 34, 56, 78]);
            assert!(img.pixels().all(|&p| p == expected_pixel));
        }

        device.destroy_context(&mut context)?;

        Ok(())
    }
}