1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-24 01:54:22 +00:00

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

This commit is contained in:
ed 2020-06-04 17:50:23 +01:00
parent b883284a1f
commit ce16bd3df9
4 changed files with 8 additions and 10 deletions

View file

@ -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<int> lastClipRect;
mutable bool lastClipRectIsValid = false;

View file

@ -70,7 +70,7 @@ public:
{
freeCachedImageRef();
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
@ -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<int>& r)

View file

@ -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);

View file

@ -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