diff --git a/.gitignore b/.gitignore index 0ec9e7f9..284beb57 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/build* +/build*/ +/zig-cache/ +/zig-out/ diff --git a/build.zig b/build.zig new file mode 100644 index 00000000..cbdef7b2 --- /dev/null +++ b/build.zig @@ -0,0 +1,59 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.addStaticLibrary(.{ + .name = "drm", + .target = target, + .optimize = optimize, + }); + lib.root_module.c_std = .C11; + lib.addCSourceFiles(.{ + .files = &.{ + "xf86drm.c", + "xf86drmHash.c", + "xf86drmRandom.c", + "xf86drmSL.c", + "xf86drmMode.c", + }, + }); + lib.addIncludePath(.{ .path = "include/drm" }); + lib.linkLibC(); + lib.defineCMacro("HAVE_OPEN_MEMSTREAM", "1"); + // TODO: Figure out if where this varies + lib.defineCMacro("MAJOR_IN_SYSMACROS", "1"); + // lib.defineCMacro("MAJOR_IN_MKDEV", "1"); + + lib.installHeader("libsync.h", "libsync.h"); + lib.installHeader("xf86drm.h", "xf86drm.h"); + lib.installHeader("xf86drmMode.h", "xf86drmMode.h"); + + lib.installHeadersDirectoryOptions(.{ + .source_dir = .{ .path = "include/drm" }, + .install_dir = .header, + .install_subdir = "libdrm", + }); + + b.installArtifact(lib); + + // const lib_unit_tests = b.addTest(.{ + // .root_source_file = .{ .path = "src/root.zig" }, + // .target = target, + // .optimize = optimize, + // }); + + // const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + + // const exe_unit_tests = b.addTest(.{ + // .root_source_file = .{ .path = "src/main.zig" }, + // .target = target, + // .optimize = optimize, + // }); + + // const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + // const test_step = b.step("test", "Run unit tests"); + // test_step.dependOn(&run_lib_unit_tests.step); + // test_step.dependOn(&run_exe_unit_tests.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 00000000..dc90bd7a --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,19 @@ +.{ + .name = "drm", + .version = "2.4.120", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + .dependencies = .{ }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ "" }, +} diff --git a/gen_table_fourcc.zig b/gen_table_fourcc.zig new file mode 100644 index 00000000..ac90d7b8 --- /dev/null +++ b/gen_table_fourcc.zig @@ -0,0 +1,113 @@ +const std = @import("std"); + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + const argv = try std.process.argsAlloc(gpa.allocator()); + defer std.process.argsFree(gpa.allocator(), argv); + + const filename = argv[1]; + + const contents = try std.fs.cwd().readFileAlloc(gpa.allocator(), filename, 50 * 1024 * 1024); + defer gpa.allocator().free(contents); + + const stdout = std.io.getStdOut(); + const out = stdout.writer(); + + try out.writeAll( + \\/* AUTOMATICALLY GENERATED by gen_table_fourcc.py. You should modify + \\ that script instead of adding here entries manually! */ + \\static const struct drmFormatModifierInfo drm_format_modifier_table[] = { + \\ { DRM_MODIFIER_INVALID(NONE, INVALID) }, + \\ { DRM_MODIFIER_LINEAR(NONE, LINEAR) }, + \\ + ); + var line_iter = std.mem.splitScalar(u8, contents, '\n'); + check_each_line: while (line_iter.next()) |line| { + errdefer std.log.debug("line = \"{}\"", .{std.zig.fmtEscapes(line)}); + if (matchStart(line, "#define I915_FORMAT_MOD_")) |after| { + const modifier_end = std.mem.indexOfAny(u8, after, "\t ") orelse return error.Invalid; + const modifier = after[0..modifier_end]; + for (modifier) |character| { + if (character != '_' and !std.ascii.isAlphanumeric(character)) { + continue :check_each_line; + } + } + try out.print( + \\ {{ DRM_MODIFIER_INTEL({[modifier]s}, {[modifier]s}) }}, + \\ + , .{ + .modifier = modifier, + }); + } else if (matchStart(line, "#define DRM_FORMAT_MOD_")) |after| { + const end_of_identifier = std.mem.indexOfAny(u8, after, "\t ") orelse return error.Invalid; + const identifier = after[0..end_of_identifier]; + + const end_of_vendor = std.mem.indexOfScalar(u8, identifier, '_') orelse continue; + const vendor_str = identifier[0..end_of_vendor]; + const modifier = identifier[end_of_vendor + 1 ..]; + + for (modifier) |character| { + if (character != '_' and !std.ascii.isAlphanumeric(character)) { + continue :check_each_line; + } + } + + const Vendor = enum { ARM, SAMSUNG, QCOM, VIVANTE, NVIDIA, BROADCOM, ALLWINNER }; + + const vendor = std.meta.stringToEnum(Vendor, vendor_str) orelse continue; + + if (vendor == .ARM) { + if (std.mem.eql(u8, modifier, "TYPE_AFBC")) continue; + if (std.mem.eql(u8, modifier, "TYPE_MISC")) continue; + if (std.mem.eql(u8, modifier, "TYPE_AFRC")) continue; + } + + try out.print( + \\ {{ DRM_MODIFIER({[vendor]s}, {[modifier]s}, {[modifier]s}) }}, + \\ + , .{ + .vendor = @tagName(vendor), + .modifier = modifier, + }); + } + } + try out.writeAll( + \\}; + \\static const struct drmFormatModifierVendorInfo drm_format_modifier_vendor_table[] = { + \\ + ); + + line_iter.reset(); + check_each_line: while (line_iter.next()) |line| { + if (matchStart(line, "#define DRM_FORMAT_MOD_VENDOR_")) |after| { + const modifier_end = std.mem.indexOfAny(u8, after, "\t ") orelse return error.Invalid; + const modifier = after[0..modifier_end]; + + for (modifier) |character| { + if (character != '_' and !std.ascii.isAlphanumeric(character)) { + continue :check_each_line; + } + } + + try out.print( + \\ {{ DRM_FORMAT_MOD_VENDOR_{[modifier]s}, "{[modifier]s}" }}, + \\ + , .{ + .modifier = modifier, + }); + } + } + try out.writeAll( + \\}; + \\ + ); +} + +fn matchStart(haystack: []const u8, needle: []const u8) ?[]const u8 { + if (std.mem.startsWith(u8, haystack, needle)) { + return haystack[needle.len..]; + } + return null; +} diff --git a/generated_static_table_fourcc.h b/generated_static_table_fourcc.h new file mode 100644 index 00000000..468b84e2 --- /dev/null +++ b/generated_static_table_fourcc.h @@ -0,0 +1,58 @@ +/* AUTOMATICALLY GENERATED by gen_table_fourcc.py. You should modify + that script instead of adding here entries manually! */ +static const struct drmFormatModifierInfo drm_format_modifier_table[] = { + { DRM_MODIFIER_INVALID(NONE, INVALID) }, + { DRM_MODIFIER_LINEAR(NONE, LINEAR) }, + { DRM_MODIFIER_INTEL(X_TILED, X_TILED) }, + { DRM_MODIFIER_INTEL(Y_TILED, Y_TILED) }, + { DRM_MODIFIER_INTEL(Yf_TILED, Yf_TILED) }, + { DRM_MODIFIER_INTEL(Y_TILED_CCS, Y_TILED_CCS) }, + { DRM_MODIFIER_INTEL(Yf_TILED_CCS, Yf_TILED_CCS) }, + { DRM_MODIFIER_INTEL(Y_TILED_GEN12_RC_CCS, Y_TILED_GEN12_RC_CCS) }, + { DRM_MODIFIER_INTEL(Y_TILED_GEN12_MC_CCS, Y_TILED_GEN12_MC_CCS) }, + { DRM_MODIFIER_INTEL(Y_TILED_GEN12_RC_CCS_CC, Y_TILED_GEN12_RC_CCS_CC) }, + { DRM_MODIFIER_INTEL(4_TILED, 4_TILED) }, + { DRM_MODIFIER_INTEL(4_TILED_DG2_RC_CCS, 4_TILED_DG2_RC_CCS) }, + { DRM_MODIFIER_INTEL(4_TILED_DG2_MC_CCS, 4_TILED_DG2_MC_CCS) }, + { DRM_MODIFIER_INTEL(4_TILED_DG2_RC_CCS_CC, 4_TILED_DG2_RC_CCS_CC) }, + { DRM_MODIFIER_INTEL(4_TILED_MTL_RC_CCS, 4_TILED_MTL_RC_CCS) }, + { DRM_MODIFIER_INTEL(4_TILED_MTL_MC_CCS, 4_TILED_MTL_MC_CCS) }, + { DRM_MODIFIER_INTEL(4_TILED_MTL_RC_CCS_CC, 4_TILED_MTL_RC_CCS_CC) }, + { DRM_MODIFIER(SAMSUNG, 64_32_TILE, 64_32_TILE) }, + { DRM_MODIFIER(SAMSUNG, 16_16_TILE, 16_16_TILE) }, + { DRM_MODIFIER(QCOM, COMPRESSED, COMPRESSED) }, + { DRM_MODIFIER(QCOM, TILED3, TILED3) }, + { DRM_MODIFIER(QCOM, TILED2, TILED2) }, + { DRM_MODIFIER(VIVANTE, TILED, TILED) }, + { DRM_MODIFIER(VIVANTE, SUPER_TILED, SUPER_TILED) }, + { DRM_MODIFIER(VIVANTE, SPLIT_TILED, SPLIT_TILED) }, + { DRM_MODIFIER(VIVANTE, SPLIT_SUPER_TILED, SPLIT_SUPER_TILED) }, + { DRM_MODIFIER(NVIDIA, TEGRA_TILED, TEGRA_TILED) }, + { DRM_MODIFIER(NVIDIA, 16BX2_BLOCK_ONE_GOB, 16BX2_BLOCK_ONE_GOB) }, + { DRM_MODIFIER(NVIDIA, 16BX2_BLOCK_TWO_GOB, 16BX2_BLOCK_TWO_GOB) }, + { DRM_MODIFIER(NVIDIA, 16BX2_BLOCK_FOUR_GOB, 16BX2_BLOCK_FOUR_GOB) }, + { DRM_MODIFIER(NVIDIA, 16BX2_BLOCK_EIGHT_GOB, 16BX2_BLOCK_EIGHT_GOB) }, + { DRM_MODIFIER(NVIDIA, 16BX2_BLOCK_SIXTEEN_GOB, 16BX2_BLOCK_SIXTEEN_GOB) }, + { DRM_MODIFIER(NVIDIA, 16BX2_BLOCK_THIRTYTWO_GOB, 16BX2_BLOCK_THIRTYTWO_GOB) }, + { DRM_MODIFIER(BROADCOM, VC4_T_TILED, VC4_T_TILED) }, + { DRM_MODIFIER(BROADCOM, SAND32, SAND32) }, + { DRM_MODIFIER(BROADCOM, SAND64, SAND64) }, + { DRM_MODIFIER(BROADCOM, SAND128, SAND128) }, + { DRM_MODIFIER(BROADCOM, SAND256, SAND256) }, + { DRM_MODIFIER(BROADCOM, UIF, UIF) }, + { DRM_MODIFIER(ARM, 16X16_BLOCK_U_INTERLEAVED, 16X16_BLOCK_U_INTERLEAVED) }, + { DRM_MODIFIER(ALLWINNER, TILED, TILED) }, +}; +static const struct drmFormatModifierVendorInfo drm_format_modifier_vendor_table[] = { + { DRM_FORMAT_MOD_VENDOR_NONE, "NONE" }, + { DRM_FORMAT_MOD_VENDOR_INTEL, "INTEL" }, + { DRM_FORMAT_MOD_VENDOR_AMD, "AMD" }, + { DRM_FORMAT_MOD_VENDOR_NVIDIA, "NVIDIA" }, + { DRM_FORMAT_MOD_VENDOR_SAMSUNG, "SAMSUNG" }, + { DRM_FORMAT_MOD_VENDOR_QCOM, "QCOM" }, + { DRM_FORMAT_MOD_VENDOR_VIVANTE, "VIVANTE" }, + { DRM_FORMAT_MOD_VENDOR_BROADCOM, "BROADCOM" }, + { DRM_FORMAT_MOD_VENDOR_ARM, "ARM" }, + { DRM_FORMAT_MOD_VENDOR_ALLWINNER, "ALLWINNER" }, + { DRM_FORMAT_MOD_VENDOR_AMLOGIC, "AMLOGIC" }, +};