Skip to content

Commit 3c14075

Browse files
committed
Ensure bytes per row value meets alignment requirements for macOS surfaces.
1 parent ace981c commit 3c14075

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/platform/macos/system/surface.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use cocoa::quartzcore::{transaction, CALayer, CATransform3D};
1515
use core_foundation::base::TCFType;
1616
use core_foundation::dictionary::CFDictionary;
1717
use core_foundation::number::CFNumber;
18-
use core_foundation::string::CFString;
18+
use core_foundation::string::{CFString, CFStringRef};
1919
use core_graphics::geometry::{CGRect, CGSize, CG_ZERO_POINT};
2020
use euclid::default::Size2D;
2121
use io_surface::{self, kIOSurfaceBytesPerElement, kIOSurfaceBytesPerRow, IOSurface, IOSurfaceRef};
@@ -29,6 +29,11 @@ use std::slice;
2929
use std::sync::{Arc, Condvar, Mutex};
3030
use std::thread;
3131

32+
#[link(name = "IOSurface", kind = "framework")]
33+
extern "C" {
34+
fn IOSurfaceAlignProperty(property: CFStringRef, value: usize) -> usize;
35+
}
36+
3237
const BYTES_PER_PIXEL: i32 = 4;
3338

3439
/// Represents a hardware buffer of pixels that can be rendered to via the CPU or GPU and either
@@ -319,6 +324,7 @@ impl Device {
319324
};
320325

321326
unsafe {
327+
let bytes_per_row = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, (size.width * BYTES_PER_PIXEL) as usize) as i32;
322328
let properties = CFDictionary::from_CFType_pairs(&[
323329
(
324330
CFString::wrap_under_get_rule(kIOSurfaceWidth),
@@ -334,7 +340,7 @@ impl Device {
334340
),
335341
(
336342
CFString::wrap_under_get_rule(kIOSurfaceBytesPerRow),
337-
CFNumber::from(size.width * BYTES_PER_PIXEL).as_CFType(),
343+
CFNumber::from(bytes_per_row).as_CFType(),
338344
),
339345
(
340346
CFString::wrap_under_get_rule(kIOSurfacePixelFormat),

0 commit comments

Comments
 (0)