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
// Copyright (c) 2017, Marty Mills <daggerbot@gmail.com>
// This software is available under the terms of the zlib license.
// See COPYING.md for more information.

use dwindow::Visual;

use error::Result;
use provider::{ConfigProvider, ConfigsProvider, GetProvider};

/// RGB pixel format description.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct RgbFormat {
    pub red_size: u8,
    pub green_size: u8,
    pub blue_size: u8,
    pub alpha_size: u8,
    pub accum_red_size: u8,
    pub accum_green_size: u8,
    pub accum_blue_size: u8,
    pub accum_alpha_size: u8,
}

impl RgbFormat {
    pub fn satisfies (&self, spec: &RgbFormat) -> bool {
        self.red_size >= spec.red_size
        && self.green_size >= spec.green_size
        && self.blue_size >= spec.blue_size
        && self.alpha_size >= spec.alpha_size
        && self.accum_red_size >= spec.accum_red_size
        && self.accum_blue_size >= spec.accum_blue_size
        && self.accum_alpha_size >= spec.accum_alpha_size
    }

    pub fn total_bits (&self) -> u32 {
        self.red_size as u32
        + self.green_size as u32
        + self.blue_size as u32
        + self.alpha_size as u32
        + self.accum_red_size as u32
        + self.accum_green_size as u32
        + self.accum_blue_size as u32
        + self.accum_alpha_size as u32
    }
}

/// Pixel format description.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PixelFormat {
    Index(u8),
    Rgb(RgbFormat),
}

impl PixelFormat {
    pub fn satisfies (&self, spec: &PixelFormat) -> bool {
        match (*self, *spec) {
            (PixelFormat::Index(bits), PixelFormat::Index(spec_bits)) => bits >= spec_bits,
            (PixelFormat::Rgb(ref f), PixelFormat::Rgb(ref spec)) => f.satisfies(spec),
            _ => false
        }
    }

    pub fn total_bits (&self) -> u32 {
        match *self {
            PixelFormat::Index(n) => n as u32,
            PixelFormat::Rgb(f) => f.total_bits(),
        }
    }
}

impl Default for PixelFormat {
    fn default () -> PixelFormat { PixelFormat::Rgb(RgbFormat::default()) }
}

/// Description of a surface configuration.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ConfigInfo {
    pub support_gl: bool,
    pub support_gles: bool,
    pub draw_to_window: bool,
    pub draw_to_pixmap: bool,
    pub pixel_format: PixelFormat,
    pub depth_size: u8,
    pub stencil_size: u8,
    pub double_buffer: bool,
    pub stereoscopic: bool,
}

impl ConfigInfo {
    pub fn satisfies (&self, spec: &ConfigInfo) -> bool {
        (self.support_gl || !spec.support_gl)
        && (self.support_gles || !spec.support_gles)
        && (self.draw_to_window || !spec.draw_to_window)
        && (self.draw_to_pixmap || !spec.draw_to_pixmap)
        && self.pixel_format.satisfies(&spec.pixel_format)
        && self.depth_size >= spec.depth_size
        && self.stencil_size >= spec.stencil_size
        && (self.double_buffer || !spec.double_buffer)
        && self.stereoscopic == spec.stereoscopic
    }

    pub fn total_bits (&self) -> u32 {
        self.pixel_format.total_bits()
        + self.depth_size as u32
        + self.stencil_size as u32
    }
}

/// `Config` provider trait.
pub trait IConfigProvider : Sized + Clone + Eq {
    fn info (&self) -> Result<ConfigInfo>;
    fn visual (&self) -> &Visual;
}

/// OpenGL surface configuration.
#[derive(Clone, Eq, PartialEq)]
pub struct Config {
    provider: ConfigProvider,
    info: ConfigInfo,
}

impl Config {
    pub fn info (&self) -> ConfigInfo { self.info }
    pub fn visual (&self) -> &Visual { self.provider.visual() }
}

impl AsRef<Config> for Config {
    fn as_ref (&self) -> &Config { self }
}

impl AsRef<Visual> for Config {
    fn as_ref (&self) -> &Visual { self.provider.visual() }
}

impl GetProvider for Config {
    type Provider = ConfigProvider;
    fn provider (&self) -> &ConfigProvider { &self.provider }
}

/// `Configs` provider trait.
pub trait IConfigsProvider {
    type Config : IConfigProvider;
    fn next (&mut self) -> Result<Option<Self::Config>>;
}

/// Iterator over available `Config`s.
pub struct Configs {
    provider: ConfigsProvider,
}

impl Configs {
    pub fn choose (mut self, spec: &ConfigInfo) -> Result<Config> {
        let mut best_config;
        let mut best_bits;

        // Get first matching config
        loop {
            let config = match self.next() {
                None => { return Err(err!(NoMatch)); },
                Some(Ok(c)) => c,
                Some(Err(err)) => { return Err(err); },
            };
            let info = config.info();

            if info == *spec {
                return Ok(config);
            } else if info.satisfies(spec) {
                best_config = config;
                best_bits = info.total_bits();
                break;
            }
        }

        // Try to find a better match
        for result in self {
            let config = result?;
            let info = config.info();

            if info == *spec {
                return Ok(config);
            } else if info.satisfies(spec) {
                let bits = info.total_bits();

                if bits < best_bits {
                    best_config = config;
                    best_bits = bits;
                }
            }
        }

        Ok(best_config)
    }

    #[doc(hidden)]
    pub fn from_provider (provider: ConfigsProvider) -> Configs {
        Configs { provider }
    }
}

impl Iterator for Configs {
    type Item = Result<Config>;

    fn next (&mut self) -> Option<Result<Config>> {
        match self.provider.next() {
            Ok(None) => None,
            Ok(Some(provider)) => {
                let info = match provider.info() {
                    Ok(info) => info,
                    Err(err) => { return Some(Err(err)); },
                };
                Some(Ok(Config { provider, info }))
            },
            Err(err) => Some(Err(err)),
        }
    }
}