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
//! Functions for writing various `glium` and `glutin` context info to a file
//! handle.
//!
//! # `glutin`
//!
//! The main datatypes in `glutin` are the `glutin::GlWindow` and
//! `glutin::HeadlessContext`, both of which implement the `glutin::GlContext`
//! trait:
//!
//! - **`glutin::GlWindow`**
//!   -- a `glutin::Context` and an associated `glutin::Window` which are
//!   constructed from `glutin::ContextBuilder`s and `glutin::WindowBuilder`s,
//!   respectively
//!
//! - **`glutin::HeadlessContext`**
//!   -- simply implements the `glutin::GlContext` trait; constructed from
//!   `glutin::HeadlessRendererBuilder`s
//!
//! - **`glutin::Context`**
//!   -- an OpenGL context associated with a single `Window` or shared between
//!   multiple windows; implements `glutin::GlContext` and is constructed by
//!   `glutin::ContextBuilder`
//!
//! - **`glutin::Window`**
//!   -- represents a window with associated `EventsLoop`; can be built
//!   directly from a new `glutin::EventsLoop` or by passing it to
//!   `glutin::WindowBuilder::build`, or by passing the `glutin::WindowBuilder`
//!   itself (which configures a `glutin::WindowAttributes` struct) to
//!   `glutin::GlWindow::new`
//!
//! - **`glutin::EventsLoop`**
//!   -- retrieves events from the system and from the *windows* that were
//!   registered to the events loop
//!
//!
//! Traits:
//!
//! - **`glutin::GlContext`**
//!   -- represents an OpenGL context that can be shared between multiple
//!   windows; a new `Context` that is not yet shared can be *only* be acquired
//!   by passing a `glutin::ContextBuilder` (which configures a
//!   `glutin::GlAttributes` struct) to `glutin::GlWindow::new`
//!
//!
//! ```text
//! [Traits ]                         GlContext
//!                                      /\
//!                                      ||
//! [Structs]  GlAttributes       ==> HeadlessContext
//!            GlAttributes       ==> Context
//!            + WindowAttributes ==> GlWindow
//!
//!
//! GlContext --> PixelFormat
//!           --> Api
//!           --> is_current(), make_current()
//!           --> get_proc_address()
//!           --> resize()
//!           --> swap_buffers()
//! ```
//!
//! `glutin::PixelFormat` -- of a `GlContext` is the pixel format of the main
//! framebuffer.
//!
//! `glutin::Api` -- indicates whether `OpenGl`, `OpenGlEs`, or `WebGl` is
//! being used
//!
//! `is_current`, `make_current` -- queries and sets whether this context is
//! the current one in this thread
//!
//! `get_proc_address` -- required method for getting addresses of OpenGL
//! functions used internally
//!
//! `resize` -- required for contexts on some platforms (macOS, Wayland) to be
//! *manually updated* when their *window or surface* is resized, i.e. it
//! `resize` should be called for each `Resized` window event that is received
//!
//! `swap_buffers` -- according to the documentation for `glutin` 0.12.0
//! (<https://docs.rs/glutin/0.12.0/glutin/trait.GlContext.html#tymethod.swap_buffers>),
//! drivers may choose to override vsync settings to it is not possible to know
//! in advance whether `swap_buffers()` will block or not.
//!
//!
//! # `glium`
//!
//! Glium extends the `glutin::GlWindow` and `glutin::HeadlessContext` datatypes
//! with the `glium::Display` and `glium::HeadlessRenderer` datatypes, both of
//! which dereference to a `glium::backend::Context` which is an implementor of
//! `glium::backend::Facade`:
//!
//! - **`glium::Display`**
//!   -- (re-export of `glium::backend::glutin::Display`) a combination of a
//!   `glutin::GlWindow` and a `glium::backend::Context`
//!
//! - **`glium::HeadlessRenderer`**
//!   -- (re-export of `glium::backend::glutin::headless::Headless`) a
//!   `glium::backend::Context` without an associated window
//!
//! Traits:
//!
//! - **`glium::backend::Facade`**
//!   -- a trait providing safe access for glium functions, implemented by
//!   `glium::backend::Context` and indirectly (via deref to a context) for
//!   `glium::Display` and `glium::Headlessrenderer`
//!
//! ```text
//! [Traits ]                      Facade    CapabilitiesSource   Surface
//!                                  /\  \           /\             /\
//!                                  ||   \------\\\\||             ||
//! [Structs]     GlWindow     ==> Display  ====> Context =======> Frame
//!            HeadlessContext ==> Headless ====>  \\\\==========> DefaultFramebuffer
//!                                                 \\\==========> EmptyFramebuffer,
//!                                                  \\==========> SimpleFramebuffer,
//!                                                   \==========> MultiOutputFramebuffer
//! ```

use ::{std, glium, glium::glutin};

static INDENT : std::sync::atomic::AtomicU8
  = std::sync::atomic::AtomicU8::new (0);

///////////////////////////////////////////////////////////////////////////////
//  glutin                                                                   //
///////////////////////////////////////////////////////////////////////////////

pub fn glutin_info_write <W : std::io::Write>
  (writer : &mut W) -> Result <(), std::io::Error>
{
  let mut indent = indent_string();
  try!{ writeln!(writer, "{}# glutin info #", indent) }
  // minimum core profile gl request
  try!{
    writeln!(writer, "{}{:<36} {:?}",
      indent, "minimum core profile gl request:", glutin::GL_CORE)
  }

  // default gl attributes
  let default_gl_attributes = glutin::GlAttributes::<u32>::default();
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "default gl attributes:", "GlAttributes {") };
  indent = indent_bump (4);
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "sharing:",     default_gl_attributes.sharing) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "version:",     default_gl_attributes.version) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "profile:",     default_gl_attributes.profile) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "debug:",       default_gl_attributes.debug) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "robustness:",  default_gl_attributes.robustness) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "vsync:",       default_gl_attributes.vsync) }
  indent = indent_unbump (4);
  try!{ writeln!(writer, "{}}}", indent) }

  // default window attriutes
  let default_window_attributes = glutin::WindowAttributes::default();
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "default window attributes:", "WindowAttributes {") };
  indent = indent_bump (4);
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "dimensions:",      default_window_attributes.dimensions) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "min_dimensions:",  default_window_attributes.min_dimensions) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "max_dimensions:",  default_window_attributes.max_dimensions) }
  if let Some(ref fullscreen_monitor) = default_window_attributes.fullscreen {
    try!{ writeln!(writer, "{}{:<36} {}",
      indent, "fullscreen monitor:", "MonitorId {") }
    indent = indent_bump (4);
    try!{ writeln!(writer, "{}{:<36} {:?}",
      indent, "name:",        fullscreen_monitor.get_name()) }
    try!{ writeln!(writer, "{}{:<36} {:?}",
      indent, "dimensions:",  fullscreen_monitor.get_dimensions()) }
    try!{ writeln!(writer, "{}{:<36} {:?}",
      indent, "position:",    fullscreen_monitor.get_position()) }
    indent = indent_unbump (4);
    try!{ writeln!(writer, "{}}}", indent) }
  } else {
    try!{ writeln!(writer, "{}{:<36} {}",
      indent, "fullscreen monitor:", "None") }
  }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "title:",       default_window_attributes.title) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "visible:",     default_window_attributes.visible) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "transparent:", default_window_attributes.transparent) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "decorations:", default_window_attributes.decorations) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "multitouch:",  default_window_attributes.multitouch) }
  indent = indent_unbump (4);
  try!{ writeln!(writer, "{}}}", indent) }
  // end default window attributes

  // default pixel format requirements
  let pretty_pixel_format_requirements
    = pretty_indent (&glutin::PixelFormatRequirements::default());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent,
    "default pixel format requirements:",
    pretty_pixel_format_requirements) }

  // print events loop info
  indent_bump (4);
  let events_loop = glutin::EventsLoop::new();
  try!{ glutin_events_loop_info_write (&events_loop, writer) }
  indent_unbump (4);

  try!{ writeln!(writer, "{}# end glutin info #", indent) }
  Ok (())
}

pub fn glutin_gl_window_info_write <W : std::io::Write>
  (gl_window : &glutin::GlWindow, writer : &mut W) -> Result <(), std::io::Error>
{
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glutin gl window info #", indent) }
  indent_bump (4);
  try!{ glutin_window_info_write (gl_window.window(), writer) }
  try!{ glutin_gl_context_info_write (gl_window, writer) }
  indent_unbump (4);
  try!{ writeln!(writer, "{}# end glutin gl context info #", indent) }
  Ok (())
}

pub fn glutin_gl_context_info_write <C, W>
  (gl_context : &C, writer : &mut W) -> Result <(), std::io::Error>
where
  C : glutin::GlContext,
  W : std::io::Write
{
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glutin gl context info #", indent) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "is current:", gl_context.is_current()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "api:",        gl_context.get_api()) }
  let pretty_pixel_format = pretty_indent (&gl_context.get_pixel_format());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "pixel format:", pretty_pixel_format) }
  try!{ writeln!(writer, "{}# end glutin gl context info #", indent) }
  Ok(())
}

pub fn glutin_window_info_write <W : std::io::Write>
  (window : &glutin::Window, writer : &mut W) -> Result <(), std::io::Error>
{
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glutin window info #", indent) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "position:",      window.get_position()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "inner size:",    window.get_inner_size()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "outer size:",    window.get_outer_size()) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "hidpi factor:",  window.hidpi_factor()) }
  try!{ writeln!(writer, "{}# end glutin window info #", indent) }
  Ok (())
}

pub fn glutin_events_loop_info_write <W : std::io::Write>
  (events_loop : &glutin::EventsLoop, writer : &mut W
) -> Result <(), std::io::Error> {
  let mut indent = indent_string();

  try!{ writeln!(writer, "{}# glutin events loop info #", indent) }

  // NB: winit 12.0-14.1 causes a crash here not able to find primary monitor
  // available monitor ids; a minimal example has been added in
  // examples/crash.rs
  try!{ writeln!(writer, "{}available monitors:", indent) }
  indent = indent_bump (4);
  for i in events_loop.get_available_monitors() {
    try!{ writeln!(writer, "{}MonitorId {{", indent) }
    indent = indent_bump (4);
    try!{ writeln!(writer, "{}{:<36} {:?}",
      indent, "name:",       i.get_name()) }
    try!{ writeln!(writer, "{}{:<36} {:?}",
      indent, "dimensions:", i.get_dimensions()) }
    try!{ writeln!(writer, "{}{:<36} {:?}",
      indent, "position:", i.get_position()) }
    indent = indent_unbump (4);
    try!{ writeln!(writer, "{}}}", indent) }
  }
  indent = indent_unbump (4);

  // primary monitor id
  let primary_monitor = events_loop.get_primary_monitor();
  try!{ writeln!(writer, "{}primary monitor:", indent) }
  indent = indent_bump (4);
  try!{ writeln!(writer, "{}MonitorId {{", indent) }
  indent = indent_bump (4);
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "name:",       primary_monitor.get_name()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "dimensions:", primary_monitor.get_dimensions()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "position:",   primary_monitor.get_position()) }
  indent = indent_unbump (4);
  try!{ writeln!(writer, "{}}}", indent) }
  indent = indent_unbump (4);

  try!{ writeln!(writer, "{}# end glutin events loop info #", indent) }
  Ok (())
}

///////////////////////////////////////////////////////////////////////////////
//  glium                                                                    //
///////////////////////////////////////////////////////////////////////////////

pub fn glium_info_write <W : std::io::Write>
  (writer : &mut W) -> Result <(), std::io::Error>
{
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glium info #", indent) }

  // supported glsl version for minimum gl core profile request
  let version = match glutin::GL_CORE {
    glutin::GlRequest::Latest
      => unimplemented!(),
    glutin::GlRequest::Specific (glutin::Api::OpenGl, (m,n))
      => glium::Version (glium::Api::Gl, m, n),
    glutin::GlRequest::Specific (glutin::Api::OpenGlEs, (m,n))
      => glium::Version (glium::Api::GlEs, m, n),
    glutin::GlRequest::Specific (glutin::Api::WebGl, _)
      => unimplemented!(),
    glutin::GlRequest::GlThenGles {..}
      => unimplemented!()
  };
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "minimum supported glsl version:",
    glium::get_supported_glsl_version (&version)) }

  // default blend
  let pretty_default_blend = pretty_indent (&glium::Blend::default());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "default blend:", pretty_default_blend) }
  // default depth
  let pretty_default_depth = pretty_indent (&glium::Depth::default());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "default depth:", pretty_default_depth) }
  // default draw parameters
  let pretty_default_draw_parameters
    = pretty_indent (&glium::DrawParameters::default());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "default draw parameters:", pretty_default_draw_parameters) }
  // default uniforms sample behavior
  let pretty_default_uniform_sampler_behavior
    = pretty_indent (&glium::uniforms::SamplerBehavior::default());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "default uniform sampler behavior:",
    pretty_default_uniform_sampler_behavior) }

  Ok(())
}

pub fn glium_display_info_write <W : std::io::Write> (
  display : &glium::backend::glutin::Display,
  writer  : &mut W
) -> Result <(), std::io::Error> {
  use glium::backend::Facade;

  let mut indent = indent_string();
  try!{ writeln!(writer, "{}# glium display info #", indent) }
  indent_bump (4);
  try!{ glutin_gl_window_info_write (&display.gl_window(),  writer) }
  try!{ glium_context_info_write    (display.get_context(), writer) }
  indent = indent_unbump (4);
  try!{ writeln!(writer, "{}# glium display info #", indent) }
  Ok (())
}

pub fn glium_context_info_write <W : std::io::Write> (
  context : &glium::backend::Context, writer : &mut W
) -> Result <(), std::io::Error> {
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glium context info #", indent) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "framebuffer dimensions:", context.get_framebuffer_dimensions()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "opengl version:", context.get_opengl_version()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "supported glsl version:", context.get_supported_glsl_version()) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "robust:", context.is_robust()) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "context loss possible:", context.is_context_loss_possible()) }
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "context lost:", context.is_context_lost()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "release behavior:", context.get_release_behavior()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "max anisotropy support:", context.get_max_anisotropy_support()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "max viewport dimensions:", context.get_max_viewport_dimensions()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "free video memory:", context.get_free_video_memory()) }
  indent_bump (4);
  try!{ glium_capabilities_source_info_write (context, writer) }
  indent_unbump (4);
  try!{ writeln!(writer, "{}# end glium context info #", indent) }
  Ok (())
}

pub fn glium_capabilities_source_info_write <C, W>
  (capabilities_source : &C, writer : &mut W) -> Result <(), std::io::Error>
where
  C : glium::CapabilitiesSource,
  W : std::io::Write
{
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glium capabilities source info #", indent) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "version: {:?}", capabilities_source.get_version()) }
  let pretty_extensions
    = pretty_indent (&capabilities_source.get_extensions());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "extensions:",   pretty_extensions) }
  let pretty_capabilities
    = pretty_indent (&capabilities_source.get_capabilities());
  try!{ writeln!(writer, "{}{:<36} {}",
    indent, "capabilities:", pretty_capabilities) }
  try!{ writeln!(writer, "{}# end glium capabilities source info #", indent) }
  Ok(())
}

pub fn glium_surface_info_write <S, W>
  (surface : &S, writer  : &mut W) -> Result <(), std::io::Error>
where
  S : glium::Surface,
  W : std::io::Write
{
  let /*mut*/ indent = indent_string();
  try!{ writeln!(writer, "{}# glium surface info #", indent) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "dimensions:",          surface.get_dimensions()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "depth buffer:",        surface.has_depth_buffer()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "depth buffer bits:",   surface.get_depth_buffer_bits()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "stencil buffer:",      surface.has_stencil_buffer()) }
  try!{ writeln!(writer, "{}{:<36} {:?}",
    indent, "stencil buffer bits:", surface.get_stencil_buffer_bits()) }
  try!{ writeln!(writer, "{}# end glium surface info #", indent) }
  Ok(())
}

//
//  private
//
fn indent_bump (spaces : u8) -> String {
  INDENT.fetch_add (spaces, std::sync::atomic::Ordering::SeqCst);
  indent_string()
}

fn indent_unbump (spaces : u8) -> String {
  INDENT.fetch_sub (spaces, std::sync::atomic::Ordering::SeqCst);
  indent_string()
}

fn indent_string () -> String {
  use std::iter::FromIterator;
  let indent = INDENT.load (std::sync::atomic::Ordering::SeqCst) as usize;
  std::string::String::from_iter (std::iter::repeat (' ').take (indent))
}

fn pretty_indent (d : &std::fmt::Debug) -> String {
  let indent = indent_string();
  let s = format!("{:#?}", d);
  let mut lines = s.lines();
  let first = lines.next().unwrap();
  let mut rest : String
    = lines.map (|line| format!("{}{}\n", indent, line)).collect();
  rest.pop().unwrap();
  [first.to_string(), "\n".to_string(), rest].concat()
}