From b20a1b56a08d59d44eb8f7d423fccf88375c2c73 Mon Sep 17 00:00:00 2001 From: "bungeman@google.com" Date: Fri, 28 Jun 2013 16:44:55 +0000 Subject: Minimal changes for SampleApp to eb HiDPI aware on Mac. R=caryclark@google.com Review URL: https://codereview.chromium.org/18055009 git-svn-id: http://skia.googlecode.com/svn/trunk@9807 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/views/mac/SampleApp-Info.plist | 2 + src/views/mac/SkNSView.mm | 78 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 4 deletions(-) (limited to 'src/views/mac') diff --git a/src/views/mac/SampleApp-Info.plist b/src/views/mac/SampleApp-Info.plist index 64c6d481ff..6b42c2a465 100644 --- a/src/views/mac/SampleApp-Info.plist +++ b/src/views/mac/SampleApp-Info.plist @@ -28,5 +28,7 @@ SampleApp NSPrincipalClass NSApplication + NSHighResolutionCapable + diff --git a/src/views/mac/SkNSView.mm b/src/views/mac/SkNSView.mm index 2d796e46b6..6a09fa65c1 100644 --- a/src/views/mac/SkNSView.mm +++ b/src/views/mac/SkNSView.mm @@ -13,6 +13,9 @@ SK_COMPILE_ASSERT(SK_SUPPORT_GPU, not_implemented_for_non_gpu_build); //#define FORCE_REDRAW +// Can be dropped when we no longer support 10.6. +#define RETINA_API_AVAILABLE (defined(MAC_OS_X_VERSION_10_7) && \ + MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7) @implementation SkNSView @synthesize fWind, fTitle, fOptionsDelegate, fGLContext; @@ -33,15 +36,26 @@ SK_COMPILE_ASSERT(SK_SUPPORT_GPU, not_implemented_for_non_gpu_build); } - (id)initWithDefaults { +#if RETINA_API_AVAILABLE + [self setWantsBestResolutionOpenGLSurface:YES]; +#endif fRedrawRequestPending = false; fWind = NULL; return self; } - (void)setUpWindow { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(backingPropertiesChanged:) + name:@"NSWindowDidChangeBackingPropertiesNotification" + object:[self window]]; if (NULL != fWind) { fWind->setVisibleP(true); - fWind->resize((int) self.frame.size.width, (int) self.frame.size.height, + NSSize size = self.frame.size; +#if RETINA_API_AVAILABLE + size = [self convertSizeToBacking:self.frame.size]; +#endif + fWind->resize((int) size.width, (int) size.height, SkBitmap::kARGB_8888_Config); } } @@ -54,13 +68,49 @@ SK_COMPILE_ASSERT(SK_SUPPORT_GPU, not_implemented_for_non_gpu_build); return YES; } +- (float)scaleFactor { + NSWindow *window = [self window]; +#if RETINA_API_AVAILABLE + if (window) { + return [window backingScaleFactor]; + } + return [[NSScreen mainScreen] backingScaleFactor]; +#else + if (window) { + return [window userSpaceScaleFactor]; + } + return [[NSScreen mainScreen] userSpaceScaleFactor]; +#endif +} + +- (void)backingPropertiesChanged:(NSNotification *)notification { + CGFloat oldBackingScaleFactor = [ + [notification.userInfo objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue + ]; + CGFloat newBackingScaleFactor = [self scaleFactor]; + if (oldBackingScaleFactor == newBackingScaleFactor) { + return; + } + + // TODO: need a better way to force a refresh (that works). + // [fGLContext update] does not appear to update if the point size has not changed, + // even if the backing size has changed. + [self setFrameSize:NSMakeSize(self.frame.size.width + 1, self.frame.size.height + 1)]; +} + - (void)resizeSkView:(NSSize)newSize { - if (NULL != fWind && (fWind->width() != newSize.width || fWind->height() != newSize.height)) { +#if RETINA_API_AVAILABLE + newSize = [self convertSizeToBacking:newSize]; +#endif + if (NULL != fWind && + (fWind->width() != newSize.width || + fWind->height() != newSize.height)) + { fWind->resize((int) newSize.width, (int) newSize.height); if (NULL != fGLContext) { glClear(GL_STENCIL_BUFFER_BIT); + [fGLContext update]; } - [fGLContext update]; } } @@ -219,6 +269,10 @@ static unsigned convertNSModifiersToSk(NSUInteger nsModi) { if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { NSPoint loc = [self convertPoint:p fromView:nil]; +#if RETINA_API_AVAILABLE + loc = [self convertPointToBacking:loc]; //y-up + loc.y = -loc.y; +#endif fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kDown_State, self, modi); } @@ -230,6 +284,10 @@ static unsigned convertNSModifiersToSk(NSUInteger nsModi) { if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { NSPoint loc = [self convertPoint:p fromView:nil]; +#if RETINA_API_AVAILABLE + loc = [self convertPointToBacking:loc]; //y-up + loc.y = -loc.y; +#endif fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kMoved_State, self, modi); } @@ -241,6 +299,10 @@ static unsigned convertNSModifiersToSk(NSUInteger nsModi) { if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { NSPoint loc = [self convertPoint:p fromView:nil]; +#if RETINA_API_AVAILABLE + loc = [self convertPointToBacking:loc]; //y-up + loc.y = -loc.y; +#endif fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kMoved_State, self, modi); } @@ -252,6 +314,10 @@ static unsigned convertNSModifiersToSk(NSUInteger nsModi) { if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { NSPoint loc = [self convertPoint:p fromView:nil]; +#if RETINA_API_AVAILABLE + loc = [self convertPointToBacking:loc]; //y-up + loc.y = -loc.y; +#endif fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kUp_State, self, modi); } @@ -329,7 +395,11 @@ CGLContextObj createGLContext(int msaaSampleCount) { CGLPixelFormatObj format = CGLGetPixelFormat((CGLContextObj)[fGLContext CGLContextObj]); CGLDescribePixelFormat(format, 0, kCGLPFASamples, &info->fSampleCount); CGLDescribePixelFormat(format, 0, kCGLPFAStencilSize, &info->fStencilBits); - glViewport(0, 0, (int) self.bounds.size.width, (int) self.bounds.size.width); + NSSize size = self.bounds.size; +#if RETINA_API_AVAILABLE + size = [self convertSizeToBacking:size]; +#endif + glViewport(0, 0, (int) size.width, (int) size.height); glClearColor(0, 0, 0, 0); glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); -- cgit v1.2.3