offscreen_gl_context 0.25.1

Creation and manipulation of HW accelerated offscreen rendering contexts in multiple platforms. Originally intended for the Servo project's WebGL implementation.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use euclid::Size2D;
use sparkle::gl;

use crate::GLContext;
#[cfg(all(target_os = "linux", feature = "test_egl_in_linux"))]
use crate::platform::with_egl::NativeGLContext;
#[cfg(feature="test_osmesa")]
use crate::platform::with_osmesa::OSMesaContext as NativeGLContext;
#[cfg(not(any(feature = "test_egl_in_linux", feature = "test_osmesa")))]
use crate::NativeGLContext;
use crate::NativeGLContextMethods;
use crate::GLContextAttributes;
use crate::GLVersion;
use crate::ColorAttachmentType;
use std::thread;
use std::sync::mpsc;

fn test_gl_context<T: NativeGLContextMethods>(context: &GLContext<T>) {
    context.make_current().unwrap();

    context.gl().clear_color(1.0, 0.0, 0.0, 1.0);
    context.gl().clear(gl::COLOR_BUFFER_BIT);

    let size = context.draw_buffer_size().unwrap();

    let pixels = context.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);

    assert!(pixels.len() == (size.width * size.height * 4) as usize);
    test_pixels(&pixels);
}

fn test_pixels_eq(pixels: &[u8], to: &[u8]) {
    assert!(to.len() == 4);
    for pixel in pixels.chunks(4) {
        assert_eq!(pixel, to);
    }

}

fn test_pixels(pixels: &[u8]) {
    test_pixels_eq(pixels, &[255, 0, 0, 255]);
}

#[cfg(not(feature = "test_osmesa"))]
fn test_unbinding(api_version: GLVersion) {
    let ctx = GLContext::<NativeGLContext>::new(Size2D::new(256, 256),
                                                GLContextAttributes::default(),
                                                ColorAttachmentType::Renderbuffer,
                                                gl::GlType::Gl,
                                                api_version,
                                                None).unwrap();

    assert!(NativeGLContext::current_handle().is_some());

    ctx.unbind().unwrap();
    assert!(NativeGLContext::current_handle().is_none());
}

#[test]
#[cfg(not(feature = "test_osmesa"))]
fn test_unbinding_gl2() {
    test_unbinding(GLVersion::Major(2));
}

#[test]
#[cfg(not(feature = "test_osmesa"))]
fn test_unbinding_gl3() {
    test_unbinding(GLVersion::Major(3));
}

fn test_renderbuffer_color_attachment(api_version: GLVersion) {
    test_gl_context(&GLContext::<NativeGLContext>::new(Size2D::new(256, 256),
                                                       GLContextAttributes::default(),
                                                       ColorAttachmentType::Renderbuffer,
                                                       gl::GlType::Gl,
                                                       api_version,
                                                       None).unwrap());
}

#[test]
fn test_renderbuffer_color_attachment_gl2() {
    test_renderbuffer_color_attachment(GLVersion::Major(2));
}

#[test]
fn test_renderbuffer_color_attachment_gl3() {
    test_renderbuffer_color_attachment(GLVersion::Major(3));
}

fn test_texture_color_attachment(api_version: GLVersion) {
    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    GLContextAttributes::default(),
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    test_gl_context(&context);

    // Get the bound texture and check we're painting on it
    let texture_id = context.borrow_draw_buffer().unwrap().get_bound_texture_id().unwrap();
    assert!(texture_id != 0);

    assert!(context.gl().get_error() == gl::NO_ERROR);

    // Actually we just check that writing to the framebuffer works, and that there's a texture
    // attached to it. Doing a getTexImage should be a good idea, but it's not available on gles,
    // so what we should do is rebinding to another FBO.
    //
    // This is done in the `test_sharing` test though, so if that passes we know everything
    // works and we're just happy.
    let vec = context.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
    test_pixels(&vec);
}

#[test]
fn test_texture_color_attachment_gl2() {
    test_texture_color_attachment(GLVersion::Major(2));
}

#[test]
fn test_texture_color_attachment_gl3() {
    test_texture_color_attachment(GLVersion::Major(3));
}

fn test_sharing(api_version: GLVersion) {
    let size = Size2D::new(256, 256);
    let primary = GLContext::<NativeGLContext>::new(size,
                                                    GLContextAttributes::default(),
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();

    let primary_texture_id = primary.borrow_draw_buffer().unwrap().get_bound_texture_id().unwrap();
    assert!(primary_texture_id != 0);

    let secondary = GLContext::<NativeGLContext>::new(size,
                                                      GLContextAttributes::default(),
                                                      ColorAttachmentType::Texture,
                                                      gl::GlType::Gl,
                                                      api_version,
                                                      Some(&primary.handle())).unwrap();

    // Paint the second context red
    test_gl_context(&secondary);

    // Now the secondary context is bound, get the texture id, switch contexts, and check the
    // texture is there.
    let secondary_texture_id = secondary.borrow_draw_buffer().unwrap().get_bound_texture_id().unwrap();
    assert!(secondary_texture_id != 0);

    primary.make_current().unwrap();
    assert!(primary.gl().is_texture(secondary_texture_id));

    // Clearing and re-binding to a framebuffer instead of using getTexImage
    // since it's not available in GLES2
    primary.gl().clear_color(0.0, 0.0, 0.0, 1.0);
    primary.gl().clear(gl::COLOR_BUFFER_BIT);

    let vec = primary.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
    test_pixels_eq(&vec, &[0, 0, 0, 255]);

    primary.gl().bind_texture(gl::TEXTURE_2D, secondary_texture_id);

    primary.gl().framebuffer_texture_2d(gl::FRAMEBUFFER,
                                        gl::COLOR_ATTACHMENT0,
                                        gl::TEXTURE_2D,
                                        secondary_texture_id, 0);

    let vec = primary.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
    assert!(primary.gl().get_error() == gl::NO_ERROR);

    test_pixels(&vec);
}

#[test]
fn test_sharing_gl2() {
    test_sharing(GLVersion::Major(2));
}

#[test]
fn test_sharing_gl3() {
    test_sharing(GLVersion::Major(3));
}

fn test_multithread_render(api_version: GLVersion) {
    let size = Size2D::new(256, 256);
    let primary = GLContext::<NativeGLContext>::new(size,
                                                    GLContextAttributes::default(),
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    test_gl_context(&primary);
    let (tx, rx) = mpsc::channel();
    let (end_tx, end_rx) = mpsc::channel();
    thread::spawn(move ||{
        //create the context in a different thread
        let secondary = GLContext::<NativeGLContext>::new(size,
                                                          GLContextAttributes::default(),
                                                          ColorAttachmentType::Texture,
                                                          gl::GlType::Gl,
                                                          api_version,
                                                          None).unwrap();
        secondary.make_current().unwrap();
        assert!(secondary.is_current());
        //render green adn test pixels
        secondary.gl().clear_color(0.0, 1.0, 0.0, 1.0);
        secondary.gl().clear(gl::COLOR_BUFFER_BIT);

        let vec = secondary.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
        test_pixels_eq(&vec, &[0, 255, 0, 255]);

        tx.send(()).unwrap();

        // Avoid drop until test ends
        end_rx.recv().unwrap();
    });
    // Wait until thread has drawn the texture
    rx.recv().unwrap();
    // This context must remain to be current in this thread
    assert!(primary.is_current());

    // The colors must remain unchanged
    let vec = primary.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
    test_pixels_eq(&vec, &[255, 0, 0, 255]);

    end_tx.send(()).unwrap();
}

#[test]
fn test_multithread_render_gl2() {
    test_multithread_render(GLVersion::Major(2));
}

#[test]
fn test_multithread_render_gl3() {
    test_multithread_render(GLVersion::Major(3));
}


struct SGLUint(gl::GLuint);
unsafe impl Sync for SGLUint {}
unsafe impl Send for SGLUint {}

fn test_multithread_sharing(api_version: GLVersion) {
    let size = Size2D::new(256, 256);
    let primary = GLContext::<NativeGLContext>::new(size,
                                                    GLContextAttributes::default(),
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    primary.make_current().unwrap();

    let primary_texture_id = primary.borrow_draw_buffer().unwrap().get_bound_texture_id().unwrap();
    assert!(primary_texture_id != 0);

    let (tx, rx) = mpsc::channel();
    let (end_tx, end_rx) = mpsc::channel();
    let primary_handle = primary.handle();

    // Unbind required by some APIs as WGL
    primary.unbind().unwrap();

    thread::spawn(move || {
        // Create the context in a different thread
        let secondary = GLContext::<NativeGLContext>::new(size,
                                                      GLContextAttributes::default(),
                                                      ColorAttachmentType::Texture,
                                                      gl::GlType::Gl,
                                                      api_version,
                                                      Some(&primary_handle)).unwrap();
        // Make the context current on this thread only
        secondary.make_current().unwrap();
        // Paint the second context red
        test_gl_context(&secondary);
        // Send texture_id to main thread
        let texture_id = secondary.borrow_draw_buffer().unwrap().get_bound_texture_id().unwrap();
        assert!(texture_id != 0);
        tx.send(SGLUint(texture_id)).unwrap();
        // Avoid drop until test ends
        end_rx.recv().unwrap();
    });
    // Wait until thread has drawn the texture
    let secondary_texture_id = rx.recv().unwrap().0;

    primary.make_current().unwrap();

    // Clearing and re-binding to a framebuffer instead of using getTexImage
    // since it's not available in GLES2
    primary.gl().clear_color(0.0, 0.0, 0.0, 1.0);
    primary.gl().clear(gl::COLOR_BUFFER_BIT);

    let vec = primary.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
    test_pixels_eq(&vec, &[0, 0, 0, 255]);


    primary.gl().framebuffer_texture_2d(gl::FRAMEBUFFER,
                                        gl::COLOR_ATTACHMENT0,
                                        gl::TEXTURE_2D,
                                        secondary_texture_id,
                                        0);

    let vec = primary.gl().read_pixels(0, 0, size.width, size.height, gl::RGBA, gl::UNSIGNED_BYTE);
    assert!(primary.gl().get_error() == gl::NO_ERROR);

    test_pixels(&vec);
    end_tx.send(()).unwrap();
}

#[test]
fn test_multithread_sharing_gl2() {
    test_multithread_sharing(GLVersion::Major(2));
}

#[test]
fn test_multithread_sharing_gl3() {
    test_multithread_sharing(GLVersion::Major(3));
}

fn test_limits(api_version: GLVersion) {
    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    GLContextAttributes::default(),
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    assert!(context.borrow_limits().max_vertex_attribs != 0);
}

#[test]
fn test_limits_gl2() {
    test_limits(GLVersion::Major(2));
}

#[test]
fn test_limits_gl3() {
    test_limits(GLVersion::Major(3));
}

fn test_no_alpha(api_version: GLVersion) {
    let mut attributes = GLContextAttributes::default();
    attributes.alpha = false;

    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    attributes,
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    assert!(context.borrow_limits().max_vertex_attribs != 0);
}

#[test]
fn test_no_alpha_gl2() {
    test_no_alpha(GLVersion::Major(2));
}

#[test]
fn test_no_alpha_gl3() {
    test_no_alpha(GLVersion::Major(3));
}

fn test_no_depth(api_version: GLVersion) {
    let mut attributes = GLContextAttributes::default();
    attributes.depth = false;

    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    attributes,
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    assert!(context.borrow_limits().max_vertex_attribs != 0);
}

#[test]
fn test_no_depth_gl2() {
    test_no_depth(GLVersion::Major(2));
}

#[test]
fn test_no_depth_gl3() {
    test_no_depth(GLVersion::Major(3));
}

fn test_no_depth_no_alpha(api_version: GLVersion) {
    let mut attributes = GLContextAttributes::default();
    attributes.depth = false;
    attributes.alpha = false;

    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    attributes,
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    assert!(context.borrow_limits().max_vertex_attribs != 0);
}

#[test]
fn test_no_depth_no_alpha_gl2() {
    test_no_depth_no_alpha(GLVersion::Major(2));
}

#[test]
fn test_no_depth_no_alpha_gl3() {
    test_no_depth_no_alpha(GLVersion::Major(3));
}

fn test_no_premul_alpha(api_version: GLVersion) {
    let mut attributes = GLContextAttributes::default();
    attributes.depth = false;
    attributes.alpha = false;
    attributes.premultiplied_alpha = false;

    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    attributes,
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();
    assert!(context.borrow_limits().max_vertex_attribs != 0);
}

#[test]
fn test_no_premul_alpha_gl2() {
    test_no_premul_alpha(GLVersion::Major(2));
}

#[test]
fn test_no_premul_alpha_gl3() {
    test_no_premul_alpha(GLVersion::Major(3));
}

fn test_in_a_row(api_version: GLVersion) {
    let mut attributes = GLContextAttributes::default();
    attributes.depth = false;
    attributes.alpha = false;
    attributes.premultiplied_alpha = false;

    let size = Size2D::new(256, 256);
    let context = GLContext::<NativeGLContext>::new(size,
                                                    attributes.clone(),
                                                    ColorAttachmentType::Texture,
                                                    gl::GlType::Gl,
                                                    api_version,
                                                    None).unwrap();

    let handle = context.handle();

    GLContext::<NativeGLContext>::new(size,
                                      attributes.clone(),
                                      ColorAttachmentType::Texture,
                                      gl::GlType::Gl,
                                      api_version,
                                      Some(&handle)).unwrap();

    GLContext::<NativeGLContext>::new(size,
                                      attributes.clone(),
                                      ColorAttachmentType::Texture,
                                      gl::GlType::Gl,
                                      api_version,
                                      Some(&handle)).unwrap();
}

#[test]
fn test_in_a_row_gl2() {
    test_in_a_row(GLVersion::Major(2));
}

#[test]
fn test_in_a_row_gl3() {
    test_in_a_row(GLVersion::Major(3));
}

fn test_zero_size(api_version: GLVersion) {
    GLContext::<NativeGLContext>::new(Size2D::new(0, 320),
                                      GLContextAttributes::default(),
                                      ColorAttachmentType::Texture,
                                      gl::GlType::Gl,
                                      api_version,
                                      None).unwrap();
}

#[test]
fn test_zero_size_gl2() {
    test_zero_size(GLVersion::Major(2));
}

#[test]
fn test_zero_size_gl3() {
    test_zero_size(GLVersion::Major(3));
}

fn test_both_depth_stencil(api_version: GLVersion) {
    let attributes = GLContextAttributes {
        depth: true,
        stencil: true,
        .. Default::default()
    };

    let size = Size2D::new(256, 256);
    GLContext::<NativeGLContext>::new(size,
                                      attributes,
                                      ColorAttachmentType::Texture,
                                      gl::GlType::Gl,
                                      api_version,
                                      None).unwrap();
}

#[test]
fn test_both_depth_stencil_gl2() {
    test_both_depth_stencil(GLVersion::Major(2));
}

#[test]
fn test_both_depth_stencil_gl3() {
    test_both_depth_stencil(GLVersion::Major(3));
}

fn test_stencil_no_depth(api_version: GLVersion) {
    let attributes = GLContextAttributes {
        depth: false,
        stencil: true,
        .. Default::default()
    };

    let size = Size2D::new(256, 256);
    GLContext::<NativeGLContext>::new(size,
                                      attributes,
                                      ColorAttachmentType::Texture,
                                      gl::GlType::Gl,
                                      api_version,
                                      None).unwrap();
}

#[test]
fn test_stencil_no_depth_gl2() {
    test_stencil_no_depth(GLVersion::Major(2));
}

#[test]
fn test_stencil_no_depth_gl3() {
    test_stencil_no_depth(GLVersion::Major(3));
}