Bug 588664. Cache DPI in nsCocoaWindow. r=joshmoz,a=blocking

This commit is contained in:
Robert O'Callahan 2010-08-19 21:35:08 +12:00
parent 832394c514
commit 81e67c505a
3 changed files with 42 additions and 15 deletions

View File

@ -969,22 +969,11 @@ float
nsChildView::GetDPI()
{
NSWindow* window = [mView window];
NSScreen* screen = [window screen];
if (!screen)
return 96.0f;
if (window && [window isKindOfClass:[BaseWindow class]]) {
return [(BaseWindow*)window getDPI];
}
CGDirectDisplayID displayID =
[[[screen deviceDescription] objectForKey:@"NSScreenNumber"] intValue];
CGFloat heightMM = CGDisplayScreenSize(displayID).height;
size_t heightPx = CGDisplayPixelsHigh(displayID);
CGFloat scaleFactor = [window userSpaceScaleFactor];
// Currently we don't do our own scaling to take account
// of userSpaceScaleFactor, so every "pixel" we draw is actually
// userSpaceScaleFactor screen pixels. So divide the screen height
// by userSpaceScaleFactor to get the number of "device pixels"
// available.
return (heightPx / scaleFactor) / (heightMM / MM_PER_INCH_FLOAT);
return 96.0;
}
LayerManager*

View File

@ -76,6 +76,11 @@ typedef struct _nsCocoaWindowList {
// Shadow
BOOL mScheduledShadowInvalidation;
// DPI cache. Getting the physical screen size (CGDisplayScreenSize)
// is ridiculously slow, so we cache it in the toplevel window for all
// descendants to use.
float mDPI;
}
- (void)importState:(NSDictionary*)aState;
@ -87,6 +92,7 @@ typedef struct _nsCocoaWindowList {
- (void)deferredInvalidateShadow;
- (void)invalidateShadow;
- (float)getDPI;
@end

View File

@ -1937,6 +1937,31 @@ void nsCocoaWindow::SetPopupWindowLevel()
@end
static float
GetDPI(NSWindow* aWindow)
{
NSScreen* screen = [aWindow screen];
if (!screen)
return 96.0f;
CGDirectDisplayID displayID =
[[[screen deviceDescription] objectForKey:@"NSScreenNumber"] intValue];
CGFloat heightMM = ::CGDisplayScreenSize(displayID).height;
size_t heightPx = ::CGDisplayPixelsHigh(displayID);
CGFloat scaleFactor = [aWindow userSpaceScaleFactor];
if (scaleFactor < 0.01 || heightMM < 1 || heightPx < 1) {
// Something extremely bogus is going on
return 96.0f;
}
// Currently we don't do our own scaling to take account
// of userSpaceScaleFactor, so every "pixel" we draw is actually
// userSpaceScaleFactor screen pixels. So divide the screen height
// by userSpaceScaleFactor to get the number of "device pixels"
// available.
return (heightPx / scaleFactor) / (heightMM / MM_PER_INCH_FLOAT);
}
@implementation BaseWindow
- (id)initWithContentRect:(NSRect)aContentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)aBufferingType defer:(BOOL)aFlag
@ -1947,6 +1972,8 @@ void nsCocoaWindow::SetPopupWindowLevel()
mActiveTitlebarColor = nil;
mInactiveTitlebarColor = nil;
mScheduledShadowInvalidation = NO;
mDPI = GetDPI(self);
return self;
}
@ -2034,6 +2061,11 @@ static const NSString* kStateShowsToolbarButton = @"showsToolbarButton";
mScheduledShadowInvalidation = NO;
}
- (float)getDPI
{
return mDPI;
}
- (void) doCommandBySelector:(SEL)aSelector
{
// We override this so that it won't beep if it can't act.