Skip to content

Commit

Permalink
Add GPU detection for dynamic DDC response time
Browse files Browse the repository at this point in the history
  • Loading branch information
alin23 committed Dec 18, 2019
1 parent f56b16f commit 81b95cd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions EDIDReader/EDIDReader/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ func printDetails(_ id: CGDirectDisplayID) {
let sn = CGDisplaySerialNumber(id)
let model = CGDisplayModelNumber(id)
let vendor = CGDisplayVendorNumber(id)
let metalDevice = CGDirectDisplayCopyCurrentMetalDevice(id)

print(" S/N: \(sn)")
print(" Model Number: \(model))")
print(" Vendor: \(vendor)\n")
print(" GPU: \(metalDevice?.name ?? "")\n")

let idData = DDC.getDisplayIdentificationData(displayID: id)
let name = DDC.getDisplayName(for: id)
Expand Down
6 changes: 3 additions & 3 deletions Lunar/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15505"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -65,7 +65,7 @@
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Read Monitor Brightness Periodically" id="5DH-eD-imY">
<menuItem title="Read External Monitor Brightness Periodically" id="5DH-eD-imY">
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
Expand Down
4 changes: 2 additions & 2 deletions Lunar/DDC.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ bool DDCWrite(CGDirectDisplayID displayID, struct DDCWriteCommand* write, CFMuta
return result;
}

bool DDCRead(CGDirectDisplayID displayID, struct DDCReadCommand* read, CFMutableDictionaryRef displayUUIDByEDID)
bool DDCRead(CGDirectDisplayID displayID, struct DDCReadCommand* read, CFMutableDictionaryRef displayUUIDByEDID, long ddcMinReplyDelay)
{
IOI2CRequest request;
UInt8 reply_data[11] = {};
Expand All @@ -366,7 +366,7 @@ bool DDCRead(CGDirectDisplayID displayID, struct DDCReadCommand* read, CFMutable
request.sendTransactionType = kIOI2CSimpleTransactionType;
request.sendBuffer = (vm_address_t)&data[0];
request.sendBytes = 5;
request.minReplyDelay = 30 * kMillisecondScale;
request.minReplyDelay = ddcMinReplyDelay * kNanosecondScale;

data[0] = 0x51;
data[1] = 0x82;
Expand Down
2 changes: 1 addition & 1 deletion Lunar/DDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ struct EDID {

bool logToFile(char* format, ...);
bool DDCWrite(CGDirectDisplayID displayID, struct DDCWriteCommand *write, CFMutableDictionaryRef displayUUIDByEDID);
bool DDCRead(CGDirectDisplayID displayID, struct DDCReadCommand *read, CFMutableDictionaryRef displayUUIDByEDID);
bool DDCRead(CGDirectDisplayID displayID, struct DDCReadCommand *read, CFMutableDictionaryRef displayUUIDByEDID, long ddcMinReplyDelay);
bool EDIDTest(CGDirectDisplayID displayID, struct EDID *edid, uint8_t edidData[256], CFMutableDictionaryRef displayUUIDByEDID);
UInt32 SupportedTransactionType(void);
void setDebugMode(UInt8);
Expand Down
16 changes: 15 additions & 1 deletion Lunar/DDC/DDC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ let MAX_WRITE_DURATION_MS = 2000
let MAX_READ_FAULTS = 10
let MAX_WRITE_FAULTS = 20

let DDC_MIN_REPLY_DELAY_AMD = 30_000_000
let DDC_MIN_REPLY_DELAY_INTEL = 1
let DDC_MIN_REPLY_DELAY_NVIDIA = 1

struct DDCReadResult {
var controlID: ControlID
var maxValue: UInt8
Expand Down Expand Up @@ -256,8 +260,18 @@ class DDC {
let displayUUIDByEDIDCopy = displayUUIDByEDID
let nsDisplayUUIDByEDID = NSMutableDictionary(dictionary: displayUUIDByEDIDCopy)

let metalDevice = CGDirectDisplayCopyCurrentMetalDevice(displayID)
var ddcDelay = DDC_MIN_REPLY_DELAY_INTEL
if let gpu = metalDevice {
if gpu.name.lowercased().contains("amd") {
ddcDelay = DDC_MIN_REPLY_DELAY_AMD
} else if gpu.name.lowercased().contains("nvidia") {
ddcDelay = DDC_MIN_REPLY_DELAY_NVIDIA
}
}

let readStartedAt = DispatchTime.now()
DDCRead(displayID, &command, nsDisplayUUIDByEDID as CFMutableDictionary)
DDCRead(displayID, &command, nsDisplayUUIDByEDID as CFMutableDictionary, ddcDelay)
let readMs = (DispatchTime.now().rawValue - readStartedAt.rawValue) / 1_000_000
if readMs > MAX_READ_DURATION_MS {
log.debug("Reading \(controlID) took too long: \(readMs)ms", context: displayID)
Expand Down

0 comments on commit 81b95cd

Please sign in to comment.