Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m	(revision 54026)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m	(revision 54466)
@@ -219,4 +219,6 @@
 #endif
 
+/* Whether we control NSView automatic content zooming on Retina/HiDPI displays. */
+//#define VBOX_WITH_CONFIGURABLE_HIDPI_SCALING    1
 
 #ifdef IN_VMSVGA3D
@@ -890,4 +892,8 @@
 - (NSSize)size;
 - (void)updateViewportCS;
+#ifdef VBOX_WITH_CONFIGURABLE_HIDPI_SCALING
+- (NSRect)safeConvertRectToBacking:(NSRect *)pRect;
+- (CGFloat)safeGetBackingScaleFactor;
+#endif
 - (NSRect)safeConvertToScreen:(NSRect *)pRect;
 - (void)vboxReshapePerform;
@@ -1423,4 +1429,7 @@
     
     self = [super initWithFrame:frame];
+#ifdef VBOX_WITH_CONFIGURABLE_HIDPI_SCALING
+    [self performSelector:@selector(setWantsBestResolutionOpenGLSurface:) withObject: (id)YES];
+#endif
 
     COCOA_LOG_FLOW(("%s: returns self=%p\n", __PRETTY_FUNCTION__, (void *)self));
@@ -1722,4 +1731,100 @@
     COCOA_LOG_FLOW(("%s: returns\n", __PRETTY_FUNCTION__));
 }
+
+#ifdef VBOX_WITH_CONFIGURABLE_HIDPI_SCALING
+- (NSRect)safeConvertRectToBacking:(NSRect *)pRect
+{
+    NSRect resultingRect = NSZeroRect;
+
+    NSWindow *pWindow = [m_pParentView window];
+    if (pWindow)
+    {
+        if ([pWindow respondsToSelector:@selector(convertRectToBacking:)])
+        {
+            NSMethodSignature *pSignature = [pWindow methodSignatureForSelector:@selector(convertRectToBacking:)];
+            if (pSignature)
+            {
+                NSInvocation *pInvocation = [NSInvocation invocationWithMethodSignature:pSignature];
+                if (pInvocation)
+                {
+                    [pInvocation setSelector:@selector(convertRectToBacking:)];
+                    [pInvocation setTarget:pWindow];
+                    [pInvocation setArgument:pRect atIndex:2];
+                    [pInvocation invoke];
+                    [pInvocation getReturnValue:&resultingRect];
+
+                    DEBUG_MSG(("safeConvertRectToBacking: convert [X, Y, WxH]: [%d, %d, %dx%d] -> [%d, %d, %dx%d]\n",
+                        (int)pRect       ->origin.x, (int)pRect       ->origin.y, (int)pRect       ->size.width, (int)pRect       ->size.width,
+                        (int)resultingRect.origin.x, (int)resultingRect.origin.y, (int)resultingRect.size.width, (int)resultingRect.size.width));
+
+                    return resultingRect;
+                }
+            }
+        }
+    }
+    else
+        /* Should never happen. */
+        DEBUG_WARN(("safeConvertRectToBacking: parent widget has no window.\n"));
+
+    resultingRect = *pRect;
+
+    DEBUG_MSG(("safeConvertRectToBacking (reurn as is): convert [X, Y, WxH]: [%d, %d, %dx%d] -> [%d, %d, %dx%d]\n",
+        (int)pRect       ->origin.x, (int)pRect       ->origin.y, (int)pRect       ->size.width, (int)pRect       ->size.width,
+        (int)resultingRect.origin.x, (int)resultingRect.origin.y, (int)resultingRect.size.width, (int)resultingRect.size.width));
+
+    return resultingRect;
+}
+
+
+- (CGFloat)safeGetBackingScaleFactor
+{
+    /* Assume its default value. */
+    CGFloat backingScaleFactor = 1.;
+
+    NSWindow *pWindow = [m_pParentView window];
+    if (pWindow)
+    {
+        NSScreen *pScreen = [pWindow screen];
+        if (pScreen)
+        {
+            if ([pScreen respondsToSelector:@selector(backingScaleFactor)])
+            {
+                NSMethodSignature *pSignature = [pScreen methodSignatureForSelector:@selector(backingScaleFactor)];
+                if (pSignature)
+                {
+                    NSInvocation *pInvocation = [NSInvocation invocationWithMethodSignature:pSignature];
+                    if (pInvocation)
+                    {
+                        [pInvocation setSelector:@selector(backingScaleFactor)];
+                        [pInvocation setTarget:pScreen];
+                        [pInvocation invoke];
+                        [pInvocation getReturnValue:&backingScaleFactor];
+
+                        DEBUG_MSG(("safeGetBackingScaleFactor: %d\n", (int)backingScaleFactor));
+
+                        return backingScaleFactor;
+                    }
+                    else
+                        DEBUG_WARN(("safeGetBackingScaleFactor: unable to create invocation for backingScaleFactor method signature.\n"));
+                }
+                else
+                    DEBUG_WARN(("safeGetBackingScaleFactor: unable to create method signature for backingScaleFactor selector.\n"));
+            }
+            else
+                DEBUG_WARN(("safeGetBackingScaleFactor: NSScreen does not respond to backingScaleFactor selector.\n"));
+        }
+        else
+            /* Should never happen. */
+            DEBUG_WARN(("safeGetBackingScaleFactor: parent window has no screen.\n"));
+    }
+    else
+        /* Should never happen. */
+        DEBUG_WARN(("safeGetBackingScaleFactor: parent widget has no window.\n"));
+
+    return backingScaleFactor;
+}
+
+#endif
+
 
 - (NSRect)safeConvertToScreen:(NSRect *)pRect
@@ -2259,5 +2364,16 @@
         
     CrVrScrCompositorConstIterInit(pCompositor, &CIter);
-        
+
+    float backingStretchFactor = 1.;
+#ifdef VBOX_WITH_CONFIGURABLE_HIDPI_SCALING
+    /* Adjust viewport according to current NSView's backing store parameters. */
+    NSRect regularBounds = [self bounds];
+    NSRect backingBounds = [self safeConvertRectToBacking:&regularBounds];
+    glViewport(0, 0, backingBounds.size.width, backingBounds.size.height);
+
+    /* Update strech factor in order to satisfy current NSView's backing store parameters. */
+    backingStretchFactor = [self safeGetBackingScaleFactor];
+#endif
+
     glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
     glDrawBuffer(GL_BACK);
@@ -2300,5 +2416,5 @@
                     VBoxRectTranslate(&DstRect, -RestrictDstRect.xLeft, -RestrictDstRect.yTop);
 
-                    vboxNSRectToRectUnstretched(&m_RootRect, &RestrictSrcRect, xStretch, yStretch);
+                    vboxNSRectToRectUnstretched(&m_RootRect, &RestrictSrcRect, xStretch / backingStretchFactor, yStretch / backingStretchFactor);
                     VBoxRectTranslate(&RestrictSrcRect, 
                                       -CrVrScrCompositorEntryRectGet(pEntry)->xLeft, 
