lots of progress running a surface but still crashes

This commit is contained in:
Mitchell Hashimoto
2023-02-17 15:49:19 -08:00
parent 55b05b22bb
commit ff9af8a07b
12 changed files with 156 additions and 61 deletions

View File

@@ -387,8 +387,9 @@ pub const CAPI = struct {
app: *App,
opts: *const apprt.runtime.Window.Options,
) !*Window {
_ = opts;
const w = try app.newWindow(.{});
const w = try app.newWindow(.{
.runtime = opts.*,
});
return w;
}

View File

@@ -8,6 +8,7 @@ const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const objc = @import("objc");
const apprt = @import("../apprt.zig");
const CoreApp = @import("../App.zig");
const CoreWindow = @import("../Window.zig");
@@ -45,15 +46,25 @@ pub const App = struct {
};
pub const Window = struct {
nsview: objc.Object,
scale_factor: f64,
pub const Options = extern struct {
id: usize = 0,
/// The pointer to the backing NSView for the surface.
nsview: *anyopaque = undefined,
/// The scale factor of the screen.
scale_factor: f64 = 1,
};
pub fn init(app: *const CoreApp, core_win: *CoreWindow, opts: Options) !Window {
_ = app;
_ = core_win;
_ = opts;
return .{};
return .{
.nsview = objc.Object.fromId(opts.nsview),
.scale_factor = opts.scale_factor,
};
}
pub fn deinit(self: *Window) void {
@@ -67,7 +78,10 @@ pub const Window = struct {
pub fn getSize(self: *const Window) !apprt.WindowSize {
_ = self;
return apprt.WindowSize{ .width = 1, .height = 1 };
// Initially our window will have a zero size. Until we can determine
// the size of the window, we just send down this value.
return apprt.WindowSize{ .width = 800, .height = 600 };
}
pub fn setSizeLimits(self: *Window, min: apprt.WindowSize, max_: ?apprt.WindowSize) !void {

View File

@@ -305,22 +305,42 @@ pub fn deinit(self: *Metal) void {
/// This is called just prior to spinning up the renderer thread for
/// final main thread setup requirements.
pub fn finalizeWindowInit(self: *const Metal, win: apprt.runtime.Window) !void {
// Set our window backing layer to be our swapchain
const nswindow = switch (apprt.runtime) {
apprt.glfw => objc.Object.fromId(glfwNative.getCocoaWindow(win.window).?),
apprt.embedded => @panic("TODO"),
const Info = struct {
view: objc.Object,
scaleFactor: f64,
};
// Get the view and scale factor for our surface.
const info: Info = switch (apprt.runtime) {
apprt.glfw => info: {
// Everything in glfw is window-oriented so we grab the backing
// window, then derive everything from that.
const nswindow = objc.Object.fromId(glfwNative.getCocoaWindow(win.window).?);
const contentView = objc.Object.fromId(nswindow.getProperty(?*anyopaque, "contentView").?);
const scaleFactor = nswindow.getProperty(macos.graphics.c.CGFloat, "backingScaleFactor");
break :info .{
.view = contentView,
.scaleFactor = scaleFactor,
};
},
apprt.embedded => .{
.view = win.nsview,
.scaleFactor = win.scale_factor,
},
else => @compileError("unsupported apprt for metal"),
};
const contentView = objc.Object.fromId(nswindow.getProperty(?*anyopaque, "contentView").?);
contentView.setProperty("layer", self.swapchain.value);
contentView.setProperty("wantsLayer", true);
// Make our view layer-backed with our Metal layer
info.view.setProperty("layer", self.swapchain.value);
info.view.setProperty("wantsLayer", true);
// Ensure that our metal layer has a content scale set to match the
// scale factor of the window. This avoids magnification issues leading
// to blurry rendering.
const layer = contentView.getProperty(objc.Object, "layer");
const scaleFactor = nswindow.getProperty(macos.graphics.c.CGFloat, "backingScaleFactor");
layer.setProperty("contentsScale", scaleFactor);
const layer = info.view.getProperty(objc.Object, "layer");
layer.setProperty("contentsScale", info.scaleFactor);
}
/// This is called if this renderer runs DevMode.

View File

@@ -106,10 +106,10 @@ pub const Padding = struct {
const padding_bot = space_bot - padding_top;
return .{
.top = padding_top,
.bottom = padding_bot,
.right = padding_right,
.left = padding_left,
.top = @max(0, padding_top),
.bottom = @max(0, padding_bot),
.right = @max(0, padding_right),
.left = @max(0, padding_left),
};
}
@@ -124,6 +124,17 @@ pub const Padding = struct {
}
};
test "Padding balanced on zero" {
// On some systems, our screen can be zero-sized for a bit, and we
// don't want to end up with negative padding.
const testing = std.testing;
const grid: GridSize = .{ .columns = 100, .rows = 37 };
const cell: CellSize = .{ .width = 10, .height = 20 };
const screen: ScreenSize = .{ .width = 0, .height = 0 };
const padding = Padding.balanced(screen, grid, cell);
try testing.expectEqual(padding, .{});
}
test "GridSize update exact" {
const testing = std.testing;