Browse Source

macOS: Use CGContextGetUserSpaceToDeviceSpaceTransform to get backing scale factor of graphics context to avoid unnecessary upscaling on retina displays on macOS 10.14+

tags/2021-05-28
ed 5 years ago
parent
commit
7b17d42039
4 changed files with 8 additions and 10 deletions
  1. +1
    -2
      modules/juce_graphics/native/juce_mac_CoreGraphicsContext.h
  2. +5
    -6
      modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm
  3. +1
    -1
      modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm
  4. +1
    -1
      modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm

+ 1
- 2
modules/juce_graphics/native/juce_mac_CoreGraphicsContext.h View File

@@ -23,7 +23,7 @@ namespace juce
class CoreGraphicsContext : public LowLevelGraphicsContext class CoreGraphicsContext : public LowLevelGraphicsContext
{ {
public: public:
CoreGraphicsContext (CGContextRef context, float flipHeight, float targetScale);
CoreGraphicsContext (CGContextRef context, float flipHeight);
~CoreGraphicsContext() override; ~CoreGraphicsContext() override;
//============================================================================== //==============================================================================
@@ -69,7 +69,6 @@ public:
private: private:
CGContextRef context; CGContextRef context;
const CGFloat flipHeight; const CGFloat flipHeight;
float targetScale;
CGColorSpaceRef rgbColourSpace, greyColourSpace; CGColorSpaceRef rgbColourSpace, greyColourSpace;
mutable Rectangle<int> lastClipRect; mutable Rectangle<int> lastClipRect;
mutable bool lastClipRectIsValid = false; mutable bool lastClipRectIsValid = false;


+ 5
- 6
modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm View File

@@ -62,7 +62,7 @@ public:
{ {
freeCachedImageRef(); freeCachedImageRef();
sendDataChangeMessage(); sendDataChangeMessage();
return std::make_unique<CoreGraphicsContext> (context, height, 1.0f);
return std::make_unique<CoreGraphicsContext> (context, height);
} }
void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override
@@ -187,10 +187,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), : context (c),
flipHeight (h), flipHeight (h),
targetScale (scale),
state (new SavedState()) state (new SavedState())
{ {
CGContextRetain (context); CGContextRetain (context);
@@ -238,14 +237,14 @@ void CoreGraphicsContext::addTransform (const AffineTransform& transform)
lastClipRectIsValid = false; lastClipRectIsValid = false;
jassert (getPhysicalPixelScaleFactor() > 0.0f); jassert (getPhysicalPixelScaleFactor() > 0.0f);
jassert (getPhysicalPixelScaleFactor() > 0.0f);
} }
float CoreGraphicsContext::getPhysicalPixelScaleFactor() 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<int>& r) bool CoreGraphicsContext::clipToRectangle (const Rectangle<int>& r)


+ 1
- 1
modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm View File

@@ -1087,7 +1087,7 @@ void UIViewComponentPeer::drawRect (CGRect r)
// NB the CTM on iOS already includes a factor for the display scale, so // 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 // 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; insideDrawRect = true;
handlePaint (g); handlePaint (g);


+ 1
- 1
modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm View File

@@ -873,7 +873,7 @@ public:
#if USE_COREGRAPHICS_RENDERING #if USE_COREGRAPHICS_RENDERING
if (usingCoreGraphics) if (usingCoreGraphics)
{ {
CoreGraphicsContext context (cg, (float) [view frame].size.height, displayScale);
CoreGraphicsContext context (cg, (float) [view frame].size.height);
invokePaint (context); invokePaint (context);
} }
else else


Loading…
Cancel
Save