Skip to content

Commit

Permalink
Ensure bytes per row value meets alignment requirements for macOS sur…
Browse files Browse the repository at this point in the history
…faces (#307)

* Ensure bytes per row value meets alignment requirements for macOS surfaces.

* Formatting.

* Publish 0.9.8.
  • Loading branch information
jdm authored Aug 19, 2024
1 parent ace981c commit 3a82fef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "surfman"
license = "MIT OR Apache-2.0 OR MPL-2.0"
edition = "2018"
version = "0.9.6"
version = "0.9.7"
authors = [
"Patrick Walton <pcwalton@mimiga.net>",
"Emilio Cobos Álvarez <emilio@crisal.io>",
Expand Down
13 changes: 11 additions & 2 deletions src/platform/macos/system/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cocoa::quartzcore::{transaction, CALayer, CATransform3D};
use core_foundation::base::TCFType;
use core_foundation::dictionary::CFDictionary;
use core_foundation::number::CFNumber;
use core_foundation::string::CFString;
use core_foundation::string::{CFString, CFStringRef};
use core_graphics::geometry::{CGRect, CGSize, CG_ZERO_POINT};
use euclid::default::Size2D;
use io_surface::{self, kIOSurfaceBytesPerElement, kIOSurfaceBytesPerRow, IOSurface, IOSurfaceRef};
Expand All @@ -29,6 +29,11 @@ use std::slice;
use std::sync::{Arc, Condvar, Mutex};
use std::thread;

#[link(name = "IOSurface", kind = "framework")]
extern "C" {
fn IOSurfaceAlignProperty(property: CFStringRef, value: usize) -> usize;
}

const BYTES_PER_PIXEL: i32 = 4;

/// Represents a hardware buffer of pixels that can be rendered to via the CPU or GPU and either
Expand Down Expand Up @@ -319,6 +324,10 @@ impl Device {
};

unsafe {
let bytes_per_row = IOSurfaceAlignProperty(
kIOSurfaceBytesPerRow,
(size.width * BYTES_PER_PIXEL) as usize,
) as i32;
let properties = CFDictionary::from_CFType_pairs(&[
(
CFString::wrap_under_get_rule(kIOSurfaceWidth),
Expand All @@ -334,7 +343,7 @@ impl Device {
),
(
CFString::wrap_under_get_rule(kIOSurfaceBytesPerRow),
CFNumber::from(size.width * BYTES_PER_PIXEL).as_CFType(),
CFNumber::from(bytes_per_row).as_CFType(),
),
(
CFString::wrap_under_get_rule(kIOSurfacePixelFormat),
Expand Down

0 comments on commit 3a82fef

Please sign in to comment.