From ce16bd3df98c0a2ac7b0490a595b2606929f9cae Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 4 Jun 2020 17:50:23 +0100 Subject: [PATCH] macOS: Use CGContextGetUserSpaceToDeviceSpaceTransform to get backing scale factor of graphics context to avoid unnecessary upscaling on retina displays on macOS 10.14+ --- .../native/juce_mac_CoreGraphicsContext.h | 3 +-- .../native/juce_mac_CoreGraphicsContext.mm | 11 +++++------ .../native/juce_ios_UIViewComponentPeer.mm | 2 +- .../native/juce_mac_NSViewComponentPeer.mm | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.h b/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.h index 89cc241d62..6038d4fc1d 100644 --- a/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.h +++ b/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.h @@ -31,7 +31,7 @@ namespace juce class CoreGraphicsContext : public LowLevelGraphicsContext { public: - CoreGraphicsContext (CGContextRef context, float flipHeight, float targetScale); + CoreGraphicsContext (CGContextRef context, float flipHeight); ~CoreGraphicsContext() override; //============================================================================== @@ -77,7 +77,6 @@ public: private: CGContextRef context; const CGFloat flipHeight; - float targetScale; CGColorSpaceRef rgbColourSpace, greyColourSpace; mutable Rectangle lastClipRect; mutable bool lastClipRectIsValid = false; diff --git a/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm b/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm index 6e9349a53f..3c2c9d33d4 100644 --- a/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm +++ b/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm @@ -70,7 +70,7 @@ public: { freeCachedImageRef(); sendDataChangeMessage(); - return std::make_unique (context, height, 1.0f); + return std::make_unique (context, height); } void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override @@ -195,10 +195,9 @@ ImagePixelData::Ptr NativeImageType::create (Image::PixelFormat format, int widt } //============================================================================== -CoreGraphicsContext::CoreGraphicsContext (CGContextRef c, float h, float scale) +CoreGraphicsContext::CoreGraphicsContext (CGContextRef c, float h) : context (c), flipHeight (h), - targetScale (scale), state (new SavedState()) { CGContextRetain (context); @@ -246,14 +245,14 @@ void CoreGraphicsContext::addTransform (const AffineTransform& transform) lastClipRectIsValid = false; jassert (getPhysicalPixelScaleFactor() > 0.0f); - jassert (getPhysicalPixelScaleFactor() > 0.0f); } float CoreGraphicsContext::getPhysicalPixelScaleFactor() { - auto t = CGContextGetCTM (context); + auto t = CGContextGetUserSpaceToDeviceSpaceTransform (context); + auto determinant = (t.a * t.d) - (t.c * t.b); - return targetScale * (float) (juce_hypot (t.a, t.c) + juce_hypot (t.b, t.d)) / 2.0f; + return (float) std::sqrt (std::abs (determinant)); } bool CoreGraphicsContext::clipToRectangle (const Rectangle& r) diff --git a/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm index 86b5616aeb..f0e43a9159 100644 --- a/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm @@ -1084,7 +1084,7 @@ void UIViewComponentPeer::drawRect (CGRect r) // NB the CTM on iOS already includes a factor for the display scale, so // we'll tell the context that the scale is 1.0 to avoid it using it twice - CoreGraphicsContext g (cg, getComponent().getHeight(), 1.0f); + CoreGraphicsContext g (cg, getComponent().getHeight()); insideDrawRect = true; handlePaint (g); diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm index c1860b8a02..b1ce12e3c0 100644 --- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm @@ -881,7 +881,7 @@ public: #if USE_COREGRAPHICS_RENDERING if (usingCoreGraphics) { - CoreGraphicsContext context (cg, (float) [view frame].size.height, displayScale); + CoreGraphicsContext context (cg, (float) [view frame].size.height); invokePaint (context); } else