-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcg.rs
More file actions
417 lines (368 loc) · 16 KB
/
cg.rs
File metadata and controls
417 lines (368 loc) · 16 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Softbuffer implementation using CoreGraphics.
use crate::error::InitError;
use crate::{backend_interface::*, AlphaMode};
use crate::{util, Pixel, Rect, SoftBufferError};
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, Bool};
use objc2::{define_class, msg_send, AllocAnyThread, DefinedClass, MainThreadMarker, Message};
use objc2_core_foundation::{CFRetained, CGPoint};
use objc2_core_graphics::{
CGBitmapInfo, CGColorRenderingIntent, CGColorSpace, CGDataProvider, CGImage, CGImageAlphaInfo,
CGImageByteOrderInfo, CGImageComponentInfo, CGImagePixelFormatInfo,
};
use objc2_foundation::{
ns_string, NSDictionary, NSKeyValueChangeKey, NSKeyValueChangeNewKey,
NSKeyValueObservingOptions, NSNumber, NSObject, NSObjectNSKeyValueObserverRegistration,
NSString, NSValue,
};
use objc2_quartz_core::{kCAGravityTopLeft, CALayer, CATransaction};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle};
use std::ffi::c_void;
use std::marker::PhantomData;
use std::mem::size_of;
use std::num::NonZeroU32;
use std::ops::Deref;
use std::ptr::{self, slice_from_raw_parts_mut, NonNull};
define_class!(
#[unsafe(super(NSObject))]
#[name = "SoftbufferObserver"]
#[ivars = SendCALayer]
#[derive(Debug)]
struct Observer;
/// NSKeyValueObserving
impl Observer {
#[unsafe(method(observeValueForKeyPath:ofObject:change:context:))]
fn observe_value(
&self,
key_path: Option<&NSString>,
_object: Option<&AnyObject>,
change: Option<&NSDictionary<NSKeyValueChangeKey, AnyObject>>,
_context: *mut c_void,
) {
self.update(key_path, change);
}
}
);
impl Observer {
fn new(layer: &CALayer) -> Retained<Self> {
let this = Self::alloc().set_ivars(SendCALayer(layer.retain()));
unsafe { msg_send![super(this), init] }
}
fn update(
&self,
key_path: Option<&NSString>,
change: Option<&NSDictionary<NSKeyValueChangeKey, AnyObject>>,
) {
let layer = self.ivars();
let change =
change.expect("requested a change dictionary in `addObserver`, but none was provided");
let new = change
.objectForKey(unsafe { NSKeyValueChangeNewKey })
.expect("requested change dictionary did not contain `NSKeyValueChangeNewKey`");
// NOTE: Setting these values usually causes a quarter second animation to occur, which is
// undesirable.
//
// However, since we're setting them inside an observer, there already is a transaction
// ongoing, and as such we don't need to wrap this in a `CATransaction` ourselves.
if key_path == Some(ns_string!("contentsScale")) {
let new = new.downcast::<NSNumber>().unwrap();
let scale_factor = new.as_cgfloat();
// Set the scale factor of the layer to match the root layer when it changes (e.g. if
// moved to a different monitor, or monitor settings changed).
layer.setContentsScale(scale_factor);
} else if key_path == Some(ns_string!("bounds")) {
let new = new.downcast::<NSValue>().unwrap();
let bounds = new.get_rect().expect("new bounds value was not CGRect");
// Set `bounds` and `position` so that the new layer is inside the superlayer.
//
// This differs from just setting the `bounds`, as it also takes into account any
// translation that the superlayer may have that we'd want to preserve.
layer.setFrame(bounds);
} else {
panic!("unknown observed keypath {key_path:?}");
}
}
}
#[derive(Debug)]
pub struct CGImpl<D, W> {
/// Our layer.
layer: SendCALayer,
/// The layer that our layer was created from.
///
/// Can also be retrieved from `layer.superlayer()`.
root_layer: SendCALayer,
observer: Retained<Observer>,
color_space: CFRetained<CGColorSpace>,
/// The width of the underlying buffer.
width: usize,
/// The height of the underlying buffer.
height: usize,
window_handle: W,
_display: PhantomData<D>,
}
impl<D, W> Drop for CGImpl<D, W> {
fn drop(&mut self) {
// SAFETY: Registered in `new`, must be removed before the observer is deallocated.
unsafe {
self.root_layer
.removeObserver_forKeyPath(&self.observer, ns_string!("contentsScale"));
self.root_layer
.removeObserver_forKeyPath(&self.observer, ns_string!("bounds"));
}
}
}
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<D, W> {
type Context = D;
type Buffer<'surface>
= BufferImpl<'surface>
where
Self: 'surface;
fn new(window_src: W, _display: &D) -> Result<Self, InitError<W>> {
// `NSView`/`UIView` can only be accessed from the main thread.
let _mtm = MainThreadMarker::new().ok_or(SoftBufferError::PlatformError(
Some("can only access Core Graphics handles from the main thread".to_string()),
None,
))?;
let root_layer = match window_src.window_handle()?.as_raw() {
RawWindowHandle::AppKit(handle) => {
// SAFETY: The pointer came from `WindowHandle`, which ensures that the
// `AppKitWindowHandle` contains a valid pointer to an `NSView`.
//
// We use `NSObject` here to avoid importing `objc2-app-kit`.
let view: &NSObject = unsafe { handle.ns_view.cast().as_ref() };
// Force the view to become layer backed
let _: () = unsafe { msg_send![view, setWantsLayer: Bool::YES] };
// SAFETY: `-[NSView layer]` returns an optional `CALayer`
let layer: Option<Retained<CALayer>> = unsafe { msg_send![view, layer] };
layer.expect("failed making the view layer-backed")
}
RawWindowHandle::UiKit(handle) => {
// SAFETY: The pointer came from `WindowHandle`, which ensures that the
// `UiKitWindowHandle` contains a valid pointer to an `UIView`.
//
// We use `NSObject` here to avoid importing `objc2-ui-kit`.
let view: &NSObject = unsafe { handle.ui_view.cast().as_ref() };
// SAFETY: `-[UIView layer]` returns `CALayer`
let layer: Retained<CALayer> = unsafe { msg_send![view, layer] };
layer
}
_ => return Err(InitError::Unsupported(window_src)),
};
// Add a sublayer, to avoid interfering with the root layer, since setting the contents of
// e.g. a view-controlled layer is brittle.
let layer = CALayer::new();
root_layer.addSublayer(&layer);
// Set the anchor point and geometry. Softbuffer's uses a coordinate system with the origin
// in the top-left corner.
//
// NOTE: This doesn't really matter unless we start modifying the `position` of our layer
// ourselves, but it's nice to have in place.
layer.setAnchorPoint(CGPoint::new(0.0, 0.0));
layer.setGeometryFlipped(true);
// Do not use auto-resizing mask.
//
// This is done to work around a bug in macOS 14 and above, where views using auto layout
// may end up setting fractional values as the bounds, and that in turn doesn't propagate
// properly through the auto-resizing mask and with contents gravity.
//
// Instead, we keep the bounds of the layer in sync with the root layer using an observer,
// see below.
//
// layer.setAutoresizingMask(kCALayerHeightSizable | kCALayerWidthSizable);
let observer = Observer::new(&layer);
// Observe changes to the root layer's bounds and scale factor, and apply them to our layer.
//
// The previous implementation updated the scale factor inside `resize`, but this works
// poorly with transactions, and is generally inefficient. Instead, we update the scale
// factor only when needed because the super layer's scale factor changed.
//
// Note that inherent in this is an explicit design decision: We control the `bounds` and
// `contentsScale` of the layer directly, and instead let the `resize` call that the user
// controls only be the size of the underlying buffer.
//
// SAFETY: Observer deregistered in `Drop` before the observer object is deallocated.
unsafe {
root_layer.addObserver_forKeyPath_options_context(
&observer,
ns_string!("contentsScale"),
NSKeyValueObservingOptions::New | NSKeyValueObservingOptions::Initial,
ptr::null_mut(),
);
root_layer.addObserver_forKeyPath_options_context(
&observer,
ns_string!("bounds"),
NSKeyValueObservingOptions::New | NSKeyValueObservingOptions::Initial,
ptr::null_mut(),
);
}
// Set the content so that it is placed in the top-left corner if it does not have the same
// size as the surface itself.
//
// TODO(madsmtm): Consider changing this to `kCAGravityResize` to stretch the content if
// resized to something that doesn't fit, see #177.
layer.setContentsGravity(unsafe { kCAGravityTopLeft });
// Default alpha mode is opaque.
layer.setOpaque(true);
// Initialize color space here, to reduce work later on.
let color_space = CGColorSpace::new_device_rgb().unwrap();
// Grab initial width and height from the layer (whose properties have just been initialized
// by the observer using `NSKeyValueObservingOptionInitial`).
let size = layer.bounds().size;
let scale_factor = layer.contentsScale();
let width = (size.width * scale_factor) as usize;
let height = (size.height * scale_factor) as usize;
Ok(Self {
layer: SendCALayer(layer),
root_layer: SendCALayer(root_layer),
observer,
color_space,
width,
height,
_display: PhantomData,
window_handle: window_src,
})
}
#[inline]
fn window(&self) -> &W {
&self.window_handle
}
#[inline]
fn supports_alpha_mode(&self, _alpha_mode: AlphaMode) -> bool {
true
}
fn configure(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
alpha_mode: AlphaMode,
) -> Result<(), SoftBufferError> {
let opaque = matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored);
self.layer.setOpaque(opaque);
// TODO: Set opaque-ness on root layer too? Is that our responsibility, or Winit's?
// self.root_layer.setOpaque(opaque);
self.width = width.get() as usize;
self.height = height.get() as usize;
Ok(())
}
fn next_buffer(&mut self, alpha_mode: AlphaMode) -> Result<BufferImpl<'_>, SoftBufferError> {
let buffer_size = util::byte_stride(self.width as u32) as usize * self.height / 4;
Ok(BufferImpl {
buffer: util::PixelBuffer(vec![Pixel::INIT; buffer_size]),
width: self.width,
height: self.height,
color_space: &self.color_space,
alpha_info: match (alpha_mode, cfg!(target_endian = "little")) {
(AlphaMode::Opaque | AlphaMode::Ignored, true) => CGImageAlphaInfo::NoneSkipFirst,
(AlphaMode::Opaque | AlphaMode::Ignored, false) => CGImageAlphaInfo::NoneSkipLast,
(AlphaMode::Premultiplied, true) => CGImageAlphaInfo::PremultipliedFirst,
(AlphaMode::Premultiplied, false) => CGImageAlphaInfo::PremultipliedLast,
(AlphaMode::Postmultiplied, true) => CGImageAlphaInfo::First,
(AlphaMode::Postmultiplied, false) => CGImageAlphaInfo::Last,
},
layer: &mut self.layer,
})
}
}
#[derive(Debug)]
pub struct BufferImpl<'surface> {
width: usize,
height: usize,
color_space: &'surface CGColorSpace,
buffer: util::PixelBuffer,
alpha_info: CGImageAlphaInfo,
layer: &'surface mut SendCALayer,
}
impl BufferInterface for BufferImpl<'_> {
fn byte_stride(&self) -> NonZeroU32 {
NonZeroU32::new(util::byte_stride(self.width as u32)).unwrap()
}
fn width(&self) -> NonZeroU32 {
NonZeroU32::new(self.width as u32).unwrap()
}
fn height(&self) -> NonZeroU32 {
NonZeroU32::new(self.height as u32).unwrap()
}
#[inline]
fn pixels_mut(&mut self) -> &mut [Pixel] {
&mut self.buffer
}
fn age(&self) -> u8 {
0
}
fn present_with_damage(self, _damage: &[Rect]) -> Result<(), SoftBufferError> {
unsafe extern "C-unwind" fn release(
_info: *mut c_void,
data: NonNull<c_void>,
size: usize,
) {
let data = data.cast::<Pixel>();
let slice = slice_from_raw_parts_mut(data.as_ptr(), size / size_of::<Pixel>());
// SAFETY: This is the same slice that we passed to `Box::into_raw` below.
drop(unsafe { Box::from_raw(slice) })
}
let data_provider = {
let len = self.buffer.len() * size_of::<Pixel>();
let buffer: *mut [Pixel] = Box::into_raw(self.buffer.0.into_boxed_slice());
// Convert slice pointer to thin pointer.
let data_ptr = buffer.cast::<c_void>();
// SAFETY: The data pointer and length are valid.
// The info pointer can safely be NULL, we don't use it in the `release` callback.
unsafe {
CGDataProvider::with_data(ptr::null_mut(), data_ptr, len, Some(release)).unwrap()
}
};
// `CGBitmapInfo` consists of a combination of `CGImageAlphaInfo`, `CGImageComponentInfo`
// `CGImageByteOrderInfo` and `CGImagePixelFormatInfo` (see e.g. `CGBitmapInfoMake`).
//
// TODO: Use `CGBitmapInfo::new` once the next version of objc2-core-graphics is released.
let bitmap_info = CGBitmapInfo(
self.alpha_info.0
| CGImageComponentInfo::Integer.0
| CGImageByteOrderInfo::Order32Host.0
| CGImagePixelFormatInfo::Packed.0,
);
let image = unsafe {
CGImage::new(
self.width,
self.height,
8,
32,
util::byte_stride(self.width as u32) as usize,
Some(self.color_space),
bitmap_info,
Some(&data_provider),
ptr::null(),
false,
CGColorRenderingIntent::RenderingIntentDefault,
)
}
.unwrap();
// The CALayer has a default action associated with a change in the layer contents, causing
// a quarter second fade transition to happen every time a new buffer is applied. This can
// be avoided by wrapping the operation in a transaction and disabling all actions.
CATransaction::begin();
CATransaction::setDisableActions(true);
// SAFETY: The contents is `CGImage`, which is a valid class for `contents`.
unsafe { self.layer.setContents(Some(image.as_ref())) };
CATransaction::commit();
Ok(())
}
}
#[derive(Debug)]
struct SendCALayer(Retained<CALayer>);
// SAFETY: CALayer is dubiously thread safe, like most things in Core Animation.
// But since we make sure to do our changes within a CATransaction, it is
// _probably_ fine for us to use CALayer from different threads.
//
// See also:
// https://developer.apple.com/documentation/quartzcore/catransaction/1448267-lock?language=objc
// https://stackoverflow.com/questions/76250226/how-to-render-content-of-calayer-on-a-background-thread
unsafe impl Send for SendCALayer {}
// SAFETY: Same as above.
unsafe impl Sync for SendCALayer {}
impl Deref for SendCALayer {
type Target = CALayer;
fn deref(&self) -> &Self::Target {
&self.0
}
}