This patch borrows some of the code from Chromium. It doesn't try to detect dual GPU situations and assumes that CGDisplayIOServicePort(kCGDirectMainDisplay) will do the correct thing. This assumption is supported by testing.