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
// surfman/surfman/src/platform/generic/multi/device.rs
//
//! A device abstraction that allows the choice of backends dynamically.

use super::connection::Connection;
use super::context::{Context, ContextDescriptor, NativeContext};
use super::surface::{NativeWidget, Surface, SurfaceTexture};
use crate::connection::Connection as ConnectionInterface;
use crate::context::ContextAttributes;
use crate::device::Device as DeviceInterface;
use crate::gl::types::{GLenum, GLuint};
use crate::{ContextID, Error, GLApi, SurfaceAccess, SurfaceInfo, SurfaceType};
use euclid::default::Size2D;

use std::os::raw::c_void;

/// Represents a hardware display adapter that can be used for rendering (including the CPU).
///
/// Adapters can be sent between threads. To render with an adapter, open a thread-local `Device`.
pub enum Adapter<Def, Alt>
where
    Def: DeviceInterface,
    Alt: DeviceInterface,
{
    /// The default adapter type.
    Default(<Def::Connection as ConnectionInterface>::Adapter),
    /// The alternate adapter type.
    Alternate(<Alt::Connection as ConnectionInterface>::Adapter),
}

impl<Def, Alt> Clone for Adapter<Def, Alt>
where
    Def: DeviceInterface,
    Alt: DeviceInterface,
    <Def::Connection as ConnectionInterface>::Adapter: Clone,
    <Alt::Connection as ConnectionInterface>::Adapter: Clone,
{
    fn clone(&self) -> Self {
        match self {
            Adapter::Default(ref adapter) => Adapter::Default(adapter.clone()),
            Adapter::Alternate(ref adapter) => Adapter::Alternate(adapter.clone()),
        }
    }
}

/// A thread-local handle to a device.
///
/// Devices contain most of the relevant surface management methods.
pub enum Device<Def, Alt>
where
    Def: DeviceInterface,
    Alt: DeviceInterface,
{
    /// The default device type.
    Default(Def),
    /// The alternate device type.
    Alternate(Alt),
}

/// Represents a native platform-specific device.
pub enum NativeDevice<Def, Alt>
where
    Def: DeviceInterface,
    Alt: DeviceInterface,
{
    /// The default native device type.
    Default(<Def::Connection as ConnectionInterface>::NativeDevice),
    /// The alternate native device type.
    Alternate(<Alt::Connection as ConnectionInterface>::NativeDevice),
}

impl<Def, Alt> Device<Def, Alt>
where
    Def: DeviceInterface,
    Alt: DeviceInterface,
    Def::Connection: ConnectionInterface,
    Alt::Connection: ConnectionInterface,
{
    /// Returns the native device underlying this device.
    pub fn native_device(&self) -> NativeDevice<Def, Alt> {
        match *self {
            Device::Default(ref device) => NativeDevice::Default(device.native_device()),
            Device::Alternate(ref device) => NativeDevice::Alternate(device.native_device()),
        }
    }

    /// Returns the display server connection that this device was created with.
    pub fn connection(&self) -> Connection<Def, Alt> {
        match *self {
            Device::Default(ref device) => Connection::Default(device.connection()),
            Device::Alternate(ref device) => Connection::Alternate(device.connection()),
        }
    }

    /// Returns the adapter that this device was created with.
    pub fn adapter(&self) -> Adapter<Def, Alt> {
        match *self {
            Device::Default(ref device) => Adapter::Default(device.adapter()),
            Device::Alternate(ref device) => Adapter::Alternate(device.adapter()),
        }
    }

    /// Returns the OpenGL API flavor that this device supports (OpenGL or OpenGL ES).
    pub fn gl_api(&self) -> GLApi {
        match *self {
            Device::Default(ref device) => device.gl_api(),
            Device::Alternate(ref device) => device.gl_api(),
        }
    }
}

impl<Def, Alt> DeviceInterface for Device<Def, Alt>
where
    Def: DeviceInterface,
    Alt: DeviceInterface,
    Def::Connection: ConnectionInterface<Device = Def>,
    Alt::Connection: ConnectionInterface<Device = Alt>,
{
    type Connection = Connection<Def, Alt>;
    type Context = Context<Def, Alt>;
    type ContextDescriptor = ContextDescriptor<Def, Alt>;
    type NativeContext = NativeContext<Def, Alt>;
    type Surface = Surface<Def, Alt>;
    type SurfaceTexture = SurfaceTexture<Def, Alt>;

    // device.rs

    #[inline]
    fn native_device(&self) -> NativeDevice<Def, Alt> {
        Device::native_device(self)
    }

    #[inline]
    fn connection(&self) -> Connection<Def, Alt> {
        Device::connection(self)
    }

    #[inline]
    fn adapter(&self) -> Adapter<Def, Alt> {
        Device::adapter(self)
    }

    #[inline]
    fn gl_api(&self) -> GLApi {
        Device::gl_api(self)
    }

    // context.rs

    #[inline]
    fn create_context_descriptor(
        &self,
        attributes: &ContextAttributes,
    ) -> Result<Self::ContextDescriptor, Error> {
        Device::create_context_descriptor(self, attributes)
    }

    #[inline]
    fn create_context(
        &mut self,
        descriptor: &ContextDescriptor<Def, Alt>,
        share_with: Option<&Context<Def, Alt>>,
    ) -> Result<Context<Def, Alt>, Error> {
        Device::create_context(self, descriptor, share_with)
    }

    #[inline]
    unsafe fn create_context_from_native_context(
        &self,
        native_context: Self::NativeContext,
    ) -> Result<Context<Def, Alt>, Error> {
        Device::create_context_from_native_context(self, native_context)
    }

    #[inline]
    fn destroy_context(&self, context: &mut Context<Def, Alt>) -> Result<(), Error> {
        Device::destroy_context(self, context)
    }

    #[inline]
    fn native_context(&self, context: &Context<Def, Alt>) -> Self::NativeContext {
        Device::native_context(self, context)
    }

    #[inline]
    fn context_descriptor(&self, context: &Context<Def, Alt>) -> Self::ContextDescriptor {
        Device::context_descriptor(self, context)
    }

    #[inline]
    fn make_context_current(&self, context: &Context<Def, Alt>) -> Result<(), Error> {
        Device::make_context_current(self, context)
    }

    #[inline]
    fn make_no_context_current(&self) -> Result<(), Error> {
        Device::make_no_context_current(self)
    }

    #[inline]
    fn context_descriptor_attributes(
        &self,
        context_descriptor: &ContextDescriptor<Def, Alt>,
    ) -> ContextAttributes {
        Device::context_descriptor_attributes(self, context_descriptor)
    }

    #[inline]
    fn get_proc_address(&self, context: &Context<Def, Alt>, symbol_name: &str) -> *const c_void {
        Device::get_proc_address(self, context, symbol_name)
    }

    #[inline]
    fn bind_surface_to_context(
        &self,
        context: &mut Context<Def, Alt>,
        surface: Surface<Def, Alt>,
    ) -> Result<(), (Error, Surface<Def, Alt>)> {
        Device::bind_surface_to_context(self, context, surface)
    }

    #[inline]
    fn unbind_surface_from_context(
        &self,
        context: &mut Context<Def, Alt>,
    ) -> Result<Option<Surface<Def, Alt>>, Error> {
        Device::unbind_surface_from_context(self, context)
    }

    #[inline]
    fn context_id(&self, context: &Context<Def, Alt>) -> ContextID {
        Device::context_id(self, context)
    }

    #[inline]
    fn context_surface_info(
        &self,
        context: &Context<Def, Alt>,
    ) -> Result<Option<SurfaceInfo>, Error> {
        Device::context_surface_info(self, context)
    }

    // surface.rs

    #[inline]
    fn create_surface(
        &mut self,
        context: &Context<Def, Alt>,
        surface_access: SurfaceAccess,
        surface_type: SurfaceType<NativeWidget<Def, Alt>>,
    ) -> Result<Surface<Def, Alt>, Error> {
        Device::create_surface(self, context, surface_access, surface_type)
    }

    #[inline]
    fn create_surface_texture(
        &self,
        context: &mut Context<Def, Alt>,
        surface: Surface<Def, Alt>,
    ) -> Result<SurfaceTexture<Def, Alt>, (Error, Surface<Def, Alt>)> {
        Device::create_surface_texture(self, context, surface)
    }

    #[inline]
    fn destroy_surface(
        &self,
        context: &mut Context<Def, Alt>,
        surface: &mut Surface<Def, Alt>,
    ) -> Result<(), Error> {
        Device::destroy_surface(self, context, surface)
    }

    #[inline]
    fn destroy_surface_texture(
        &self,
        context: &mut Context<Def, Alt>,
        surface_texture: SurfaceTexture<Def, Alt>,
    ) -> Result<Surface<Def, Alt>, (Error, SurfaceTexture<Def, Alt>)> {
        Device::destroy_surface_texture(self, context, surface_texture)
    }

    #[inline]
    fn surface_gl_texture_target(&self) -> GLenum {
        Device::surface_gl_texture_target(self)
    }

    #[inline]
    fn present_surface(
        &self,
        context: &Context<Def, Alt>,
        surface: &mut Surface<Def, Alt>,
    ) -> Result<(), Error> {
        Device::present_surface(self, context, surface)
    }

    #[inline]
    fn resize_surface(
        &self,
        context: &Context<Def, Alt>,
        surface: &mut Surface<Def, Alt>,
        size: Size2D<i32>,
    ) -> Result<(), Error> {
        Device::resize_surface(self, context, surface, size)
    }

    #[inline]
    fn surface_info(&self, surface: &Surface<Def, Alt>) -> SurfaceInfo {
        Device::surface_info(self, surface)
    }

    #[inline]
    fn surface_texture_object(&self, surface_texture: &SurfaceTexture<Def, Alt>) -> GLuint {
        Device::surface_texture_object(self, surface_texture)
    }
}