Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use euclid::default::{Point2D, Rect, Size2D, Vector2D};
use gl::types::{GLenum, GLint, GLuint};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use surfman::{declare_surfman, SurfaceAccess, SurfaceTexture, SurfaceType};
use surfman::{Adapter, Connection, Context, ContextDescriptor, Device, GLApi, Surface};
use surfman::{declare_surfman, ContextDescriptor, SurfaceAccess, SurfaceTexture, SurfaceType};
use surfman::{Adapter, Connection, Context, Device, GLApi, Surface};

#[cfg(not(target_os = "android"))]
use self::common::FilesystemResourceLoader;
Expand Down
19 changes: 13 additions & 6 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::base::io_surface::adapter::AppleAdapter;
use crate::free_unix::adapter::FreeUnixAdapter;
#[cfg(any(android_platform, ohos_platform))]
use crate::hardware_buffer::adapter::HardwareBufferAdapter;
use crate::macros::enum_conversion;
#[cfg(all(windows_platform, not(feature = "sm-no-wgl")))]
use crate::wgl::adapter::WglAdapter;
use crate::{macros::enum_conversion, Error};

/// A power usage preference for selecting an adapter.
#[derive(Copy, Clone, Debug, Default)]
Expand Down Expand Up @@ -69,17 +69,24 @@ pub enum Adapter {
}

#[cfg(all(windows_platform, feature = "sm-angle"))]
enum_conversion!(Adapter, Angle, AngleAdapter, angle);
enum_conversion!(Adapter, Angle, AngleAdapter, angle, IncompatibleAdapter);
#[cfg(macos_platform)]
enum_conversion!(Adapter, Apple, AppleAdapter, apple);
enum_conversion!(Adapter, Apple, AppleAdapter, apple, IncompatibleAdapter);
#[cfg(free_unix)]
enum_conversion!(Adapter, FreeUnix, FreeUnixAdapter, free_unix);
enum_conversion!(
Adapter,
FreeUnix,
FreeUnixAdapter,
free_unix,
IncompatibleAdapter
);
#[cfg(any(android_platform, ohos_platform))]
enum_conversion!(
Adapter,
HardwareBuffer,
HardwareBufferAdapter,
hardware_buffer
hardware_buffer,
IncompatibleAdapter
);
#[cfg(all(windows_platform, not(feature = "sm-no-wgl")))]
enum_conversion!(Adapter, Wgl, WglAdapter, wgl);
enum_conversion!(Adapter, Wgl, WglAdapter, wgl, IncompatibleAdapter);
2 changes: 1 addition & 1 deletion src/angle/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Wrapper for EGL contexts managed by ANGLE using Direct3D 11 as a backend on Windows.

use super::surface::Surface;
pub use crate::base::egl::context::{ContextDescriptor, NativeContext};
pub use crate::base::egl::context::NativeContext;
use crate::base::egl::surface::ExternalEGLSurfaces;
use crate::context::ContextID;
use crate::egl;
Expand Down
26 changes: 19 additions & 7 deletions src/angle/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::adapter::AngleAdapter;
use crate::angle::connection::Connection;
use crate::angle::context::{Context, ContextDescriptor, NativeContext};
use crate::angle::context::{Context, NativeContext};
use crate::angle::surface::{
Surface, SurfaceDataGuard, SurfaceTexture, Synchronization, Win32Objects,
};
Expand All @@ -20,8 +20,10 @@ use crate::egl::types::{
EGLAttrib, EGLConfig, EGLDeviceEXT, EGLDisplay, EGLNativeWindowType, EGLSurface, EGLint,
};
use crate::surface::Framebuffer;
use crate::EglContextDescriptor;
use crate::{
egl, gl, Adapter, ContextAttributes, Error, GLApi, Gl, SurfaceAccess, SurfaceInfo, SurfaceType,
egl, gl, Adapter, ContextAttributes, ContextDescriptor, Error, GLApi, Gl, SurfaceAccess,
SurfaceInfo, SurfaceType,
};
use euclid::default::Size2D;
use glow::HasContext;
Expand Down Expand Up @@ -244,7 +246,7 @@ impl Device {
attributes: &ContextAttributes,
) -> Result<ContextDescriptor, Error> {
unsafe {
ContextDescriptor::new(
EglContextDescriptor::new(
self.egl_display,
attributes,
&[
Expand All @@ -256,6 +258,7 @@ impl Device {
egl::OPENGL_ES2_BIT as EGLint,
],
)
.map(Into::into)
}
}

Expand All @@ -273,7 +276,7 @@ impl Device {
let egl_context = unsafe {
context::create_context(
self.egl_display,
descriptor,
descriptor.egl()?,
share_with.map_or(egl::NO_CONTEXT, |ctx| ctx.egl_context),
self.gl_api(),
)?
Expand Down Expand Up @@ -370,7 +373,12 @@ impl Device {
/// Returns the descriptor that this context was created with.
pub fn context_descriptor(&self, context: &Context) -> ContextDescriptor {
unsafe {
ContextDescriptor::from_egl_context(&context.gl, self.egl_display, context.egl_context)
EglContextDescriptor::from_egl_context(
&context.gl,
self.egl_display,
context.egl_context,
)
.into()
}
}

Expand Down Expand Up @@ -428,6 +436,9 @@ impl Device {
&self,
context_descriptor: &ContextDescriptor,
) -> ContextAttributes {
let context_descriptor = context_descriptor
.egl()
.expect("Passed incompatible context descriptor");
unsafe { context_descriptor.attributes(self.egl_display) }
}

Expand All @@ -446,7 +457,7 @@ impl Device {
#[inline]
pub(crate) fn context_descriptor_to_egl_config(
&self,
context_descriptor: &ContextDescriptor,
context_descriptor: &EglContextDescriptor,
) -> EGLConfig {
unsafe { context::egl_config_from_id(self.egl_display, context_descriptor.egl_config_id) }
}
Expand Down Expand Up @@ -630,6 +641,7 @@ impl Device {
texture: Option<ComPtr<d3d11::ID3D11Texture2D>>,
) -> Result<Surface, Error> {
let context_descriptor = self.context_descriptor(context);
let context_descriptor = context_descriptor.egl()?.clone();
let egl_config = self.context_descriptor_to_egl_config(&context_descriptor);

unsafe {
Expand Down Expand Up @@ -732,7 +744,7 @@ impl Device {
context: &Context,
egl_native_window: EGLNativeWindowType,
) -> Result<Surface, Error> {
let context_descriptor = self.context_descriptor(context);
let context_descriptor = self.context_descriptor(context).egl()?.clone();
let egl_config = self.context_descriptor_to_egl_config(&context_descriptor);

unsafe {
Expand Down
5 changes: 2 additions & 3 deletions src/angle/surface.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Surface management for Direct3D 11 on Windows using the ANGLE library as a frontend.

use super::context::ContextDescriptor;
use super::device::Device;
use crate::base::egl::device::EGL_FUNCTIONS;
use crate::context::ContextID;
use crate::egl::types::EGLSurface;
use crate::egl::{self};
use crate::{Error, SurfaceID};
use crate::{EglContextDescriptor, Error, SurfaceID};

use euclid::default::Size2D;
use std::fmt::{self, Debug, Formatter};
Expand Down Expand Up @@ -37,7 +36,7 @@ pub struct Surface {
pub(crate) egl_surface: EGLSurface,
pub(crate) size: Size2D<i32>,
pub(crate) context_id: ContextID,
pub(crate) context_descriptor: ContextDescriptor,
pub(crate) context_descriptor: EglContextDescriptor,
pub(crate) win32_objects: Win32Objects,
}

Expand Down
16 changes: 8 additions & 8 deletions src/base/egl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct NativeContext {
///
/// These are local to a device.
#[derive(Clone)]
pub struct ContextDescriptor {
pub struct EglContextDescriptor {
pub(crate) egl_config_id: EGLint,
pub(crate) gl_version: GLVersion,
pub(crate) compatibility_profile: bool,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Drop for EGLBackedContext {
impl EGLBackedContext {
pub(crate) unsafe fn new(
egl_display: EGLDisplay,
descriptor: &ContextDescriptor,
descriptor: &EglContextDescriptor,
share_with: Option<&EGLBackedContext>,
gl_api: GLApi,
) -> Result<EGLBackedContext, Error> {
Expand Down Expand Up @@ -316,12 +316,12 @@ impl NativeContext {
}
}

impl ContextDescriptor {
impl EglContextDescriptor {
pub(crate) unsafe fn new(
egl_display: EGLDisplay,
attributes: &ContextAttributes,
extra_config_attributes: &[EGLint],
) -> Result<ContextDescriptor, Error> {
) -> Result<EglContextDescriptor, Error> {
let flags = attributes.flags;

let alpha_size = if flags.contains(ContextAttributeFlags::ALPHA) {
Expand Down Expand Up @@ -430,7 +430,7 @@ impl ContextDescriptor {
let egl_config_id = get_config_attr(egl_display, egl_config, egl::CONFIG_ID as EGLint);
let gl_version = attributes.version;

Ok(ContextDescriptor {
Ok(EglContextDescriptor {
egl_config_id,
gl_version,
compatibility_profile,
Expand All @@ -442,12 +442,12 @@ impl ContextDescriptor {
gl: &Gl,
egl_display: EGLDisplay,
egl_context: EGLContext,
) -> ContextDescriptor {
) -> EglContextDescriptor {
let egl_config_id = get_context_attr(egl_display, egl_context, egl::CONFIG_ID as EGLint);
let gl_version = GLVersion::current(gl);
let compatibility_profile = context::current_context_uses_compatibility_profile(gl);

ContextDescriptor {
EglContextDescriptor {
egl_config_id,
gl_version,
compatibility_profile,
Expand Down Expand Up @@ -508,7 +508,7 @@ impl ContextDescriptor {

pub(crate) unsafe fn create_context(
egl_display: EGLDisplay,
descriptor: &ContextDescriptor,
descriptor: &EglContextDescriptor,
share_with: EGLContext,
gl_api: GLApi,
) -> Result<EGLContext, Error> {
Expand Down
12 changes: 6 additions & 6 deletions src/cgl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ impl Drop for Context {
/// Options that control OpenGL rendering.
///
/// This corresponds to a "pixel format" object in many APIs. These are thread-safe.
pub struct ContextDescriptor {
pub struct CglContextDescriptor {
pub(crate) cgl_pixel_format: CGLPixelFormatObj,
}

impl Drop for ContextDescriptor {
impl Drop for CglContextDescriptor {
// These have been verified to be thread-safe.
#[inline]
fn drop(&mut self) {
Expand All @@ -65,18 +65,18 @@ impl Drop for ContextDescriptor {
}
}

impl Clone for ContextDescriptor {
impl Clone for CglContextDescriptor {
#[inline]
fn clone(&self) -> ContextDescriptor {
fn clone(&self) -> CglContextDescriptor {
unsafe {
ContextDescriptor {
CglContextDescriptor {
cgl_pixel_format: CGLRetainPixelFormat(self.cgl_pixel_format),
}
}
}
}

unsafe impl Send for ContextDescriptor {}
unsafe impl Send for CglContextDescriptor {}

#[must_use]
pub(crate) struct CurrentContextGuard {
Expand Down
12 changes: 8 additions & 4 deletions src/cgl/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A handle to the device. (This is a no-op, because handles are implicit in Apple's Core OpenGL.)

use super::connection::Connection;
use super::context::CglContextDescriptor;
use crate::base::io_surface::device::Device as SystemDevice;
use crate::cgl::context::{CurrentContextGuard, NativeContext};
use crate::cgl::error::ToWindowingApiError;
Expand Down Expand Up @@ -164,7 +165,7 @@ impl Device {
return Err(Error::NoPixelFormatFound);
}

Ok(ContextDescriptor { cgl_pixel_format })
Ok(CglContextDescriptor { cgl_pixel_format }.into())
}
}

Expand All @@ -186,7 +187,7 @@ impl Device {
// Create the CGL context.
let mut cgl_context = ptr::null_mut();
let err = CGLCreateContext(
descriptor.cgl_pixel_format,
descriptor.cgl()?.cgl_pixel_format,
share_with.map_or(ptr::null_mut(), |ctx| ctx.cgl_context),
&mut cgl_context,
);
Expand Down Expand Up @@ -257,7 +258,7 @@ impl Device {
unsafe {
let mut cgl_pixel_format = CGLGetPixelFormat(context.cgl_context);
cgl_pixel_format = CGLRetainPixelFormat(cgl_pixel_format);
ContextDescriptor { cgl_pixel_format }
CglContextDescriptor { cgl_pixel_format }.into()
}
}

Expand Down Expand Up @@ -395,6 +396,9 @@ impl Device {
&self,
context_descriptor: &ContextDescriptor,
) -> ContextAttributes {
let context_descriptor = context_descriptor
.cgl()
.expect("Passed incompatible context descriptor");
unsafe {
let alpha_size = get_pixel_format_attribute(context_descriptor, kCGLPFAAlphaSize);
let depth_size = get_pixel_format_attribute(context_descriptor, kCGLPFADepthSize);
Expand Down Expand Up @@ -423,7 +427,7 @@ impl Device {
}

unsafe fn get_pixel_format_attribute(
context_descriptor: &ContextDescriptor,
context_descriptor: &CglContextDescriptor,
attribute: CGLPixelFormatAttribute,
) -> i32 {
let mut value = 0;
Expand Down
52 changes: 52 additions & 0 deletions src/context_descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* 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/. */

#[cfg(macos_platform)]
use crate::cgl::context::CglContextDescriptor;
use crate::macros::enum_conversion;
#[cfg(all(windows_platform, not(feature = "sm-no-wgl")))]
use crate::wgl::context::WglContextDescriptor;
#[cfg(any(android_platform, angle, free_unix, ohos_platform))]
use crate::EglContextDescriptor;

/// Information needed to create a context. Some APIs call this a "config" or a "pixel format".
///
/// These are local to a device.
#[derive(Clone)]
pub enum ContextDescriptor {
/// A [`ContextDescriptor`] for CGL platforms.
#[cfg(macos_platform)]
Cgl(CglContextDescriptor),
/// A [`ContextDescriptor`] for EGL platforms.
#[cfg(any(android_platform, angle, free_unix, ohos_platform))]
Egl(EglContextDescriptor),
/// A [`ContextDescriptor`] for WGL platforms.
#[cfg(all(windows_platform, not(feature = "sm-no-wgl")))]
Wgl(WglContextDescriptor),
}

#[cfg(macos_platform)]
enum_conversion!(
ContextDescriptor,
Cgl,
CglContextDescriptor,
cgl,
IncompatibleContextDescriptor
);
#[cfg(any(android_platform, angle, free_unix, ohos_platform))]
enum_conversion!(
ContextDescriptor,
Egl,
EglContextDescriptor,
egl,
IncompatibleContextDescriptor
);
#[cfg(all(windows_platform, not(feature = "sm-no-wgl")))]
enum_conversion!(
ContextDescriptor,
Wgl,
WglContextDescriptor,
wgl,
IncompatibleContextDescriptor
);
Loading
Loading