OpenGL DPI scaling support (#11883)

Scale opengl width/height based on primary screen DPI scaling.
This commit is contained in:
jonjenssen 2024-11-15 12:12:59 +01:00 committed by GitHub
parent f6109cab2b
commit dbea0e2593
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 57 additions and 39 deletions

View File

@ -78,13 +78,6 @@ int main( int argc, char* argv[] )
} }
#endif #endif
#ifdef WIN32
// Temporary workaround for incorrect scaling of opengl viewport on windows with dpi scaling != 100%
// Reverts scaling behaviour to Qt5 style
// Should be replaced by proper scaling of x and y coordinates in caf::Viewer
qputenv( "QT_ENABLE_HIGHDPI_SCALING", "0" );
#endif
// The Qt::AA_ShareOpenGLContexts setting is needed when we have multiple viz widgets in flight // The Qt::AA_ShareOpenGLContexts setting is needed when we have multiple viz widgets in flight
// and we have a setup where these widgets belong to different top-level windows, or end up // and we have a setup where these widgets belong to different top-level windows, or end up
// belonging to different top-level windows through re-parenting. // belonging to different top-level windows through re-parenting.

View File

@ -61,8 +61,11 @@ bool RiuComparisonViewMover::eventFilter( QObject* watched, QEvent* event )
if ( m_dragState == LEFT_EDGE ) if ( m_dragState == LEFT_EDGE )
{ {
const int viewWidth = m_viewer->width();
const int viewHeight = m_viewer->height();
QPointF mousePos = mEv->localPos(); QPointF mousePos = mEv->localPos();
QPointF normMousePos = { mousePos.x() / m_viewer->width(), mousePos.y() / m_viewer->height() }; QPointF normMousePos = { mousePos.x() / viewWidth, mousePos.y() / viewHeight };
cvf::Rectf orgCompViewWindow = m_viewer->comparisonViewVisibleNormalizedRect(); cvf::Rectf orgCompViewWindow = m_viewer->comparisonViewVisibleNormalizedRect();
float minx = normMousePos.x(); float minx = normMousePos.x();
@ -97,8 +100,9 @@ void RiuComparisonViewMover::paintMoverHandles( QPainter* painter )
const int handleThickness = 7; const int handleThickness = 7;
cvf::Rectf normalizedComparisonRect = m_viewer->comparisonViewVisibleNormalizedRect(); cvf::Rectf normalizedComparisonRect = m_viewer->comparisonViewVisibleNormalizedRect();
int viewerWidth = m_viewer->width(); const auto ratio = m_viewer->displayScalingRatio();
int viewerHeight = m_viewer->height(); int viewerWidth = (int)( ratio * m_viewer->width() );
int viewerHeight = (int)( ratio * m_viewer->height() );
int leftEdgePos = viewerWidth * normalizedComparisonRect.min().x(); int leftEdgePos = viewerWidth * normalizedComparisonRect.min().x();
int width = viewerWidth * normalizedComparisonRect.width(); int width = viewerWidth * normalizedComparisonRect.width();

View File

@ -401,6 +401,11 @@ void RiuViewer::paintOverlayItems( QPainter* painter )
// and when they are shared between views the positions are overwritten. // and when they are shared between views the positions are overwritten.
updateLegendLayout(); updateLegendLayout();
// adjust for possible DPI scaling
auto ratio = displayScalingRatio();
const auto trueWidth = (int)( ratio * this->width() );
const auto trueHeight = (int)( ratio * this->height() );
int columnWidth = 200; int columnWidth = 200;
int edgeAxisFrameBorderWidth = m_showWindowEdgeAxes ? m_windowEdgeAxisOverlay->frameBorderWidth() : 0; int edgeAxisFrameBorderWidth = m_showWindowEdgeAxes ? m_windowEdgeAxisOverlay->frameBorderWidth() : 0;
@ -415,7 +420,7 @@ void RiuViewer::paintOverlayItems( QPainter* painter )
if ( m_showInfoText ) columnWidth = std::max( columnWidth, m_infoLabel->sizeHint().width() ); if ( m_showInfoText ) columnWidth = std::max( columnWidth, m_infoLabel->sizeHint().width() );
int columnPos = width() - columnWidth - margin - edgeAxisFrameBorderWidth; int columnPos = trueWidth - columnWidth - margin - edgeAxisFrameBorderWidth;
if ( isComparisonViewActive() ) if ( isComparisonViewActive() )
{ {
@ -424,9 +429,8 @@ void RiuViewer::paintOverlayItems( QPainter* painter )
{ {
columnWidth = 200; columnWidth = 200;
// int sliderPos = width() * comparisonViewVisibleNormalizedRect().min().x(); int sliderPos = 0.5 * trueWidth;
int sliderPos = 0.5 * width(); int compViewItemsXPos = sliderPos + 0.5 * ( trueWidth - sliderPos ) - 0.5 * columnWidth;
int compViewItemsXPos = sliderPos + 0.5 * ( width() - sliderPos ) - 0.5 * columnWidth;
columnPos = 0.5 * sliderPos - 0.5 * columnWidth; columnPos = 0.5 * sliderPos - 0.5 * columnWidth;
if ( m_showInfoText ) if ( m_showInfoText )
@ -520,13 +524,13 @@ void RiuViewer::paintOverlayItems( QPainter* painter )
{ {
m_histogramWidget->resize( columnWidth, 40 ); m_histogramWidget->resize( columnWidth, 40 );
m_histogramWidget->render( painter, QPoint( columnPos, yPos ) ); m_histogramWidget->render( painter, QPoint( columnPos, yPos ) );
// yPos += m_histogramWidget->height() + margin;
} }
if ( m_showVersionInfo ) // Version Label if ( m_showVersionInfo ) // Version Label
{ {
QSize size( m_versionInfoLabel->sizeHint().width(), m_versionInfoLabel->sizeHint().height() ); QSize size( m_versionInfoLabel->sizeHint().width(), m_versionInfoLabel->sizeHint().height() );
QPoint pos( width() - size.width() - margin - edgeAxisFrameBorderWidth, height() - size.height() - margin - edgeAxisFrameBorderHeight ); QPoint pos( trueWidth - size.width() - margin - edgeAxisFrameBorderWidth,
trueHeight - size.height() - margin - edgeAxisFrameBorderHeight );
m_versionInfoLabel->resize( size.width(), size.height() ); m_versionInfoLabel->resize( size.width(), size.height() );
m_versionInfoLabel->render( painter, pos ); m_versionInfoLabel->render( painter, pos );
} }
@ -550,8 +554,9 @@ void RiuViewer::paintOverlayItems( QPainter* painter )
cvf::Vec3d screenCoords; cvf::Vec3d screenCoords;
if ( mainCamera()->project( displayCoord, &screenCoords ) ) if ( mainCamera()->project( displayCoord, &screenCoords ) )
{ {
int translatedMousePosY = height() - screenCoords.y(); int translatedMousePosX = (int)( ratio * screenCoords.x() );
QPoint centerPos( screenCoords.x(), translatedMousePosY ); int translatedMousePosY = (int)( ratio * ( height() - screenCoords.y() ) );
QPoint centerPos( translatedMousePosX, translatedMousePosY );
// Draw a cross hair marker // Draw a cross hair marker
int markerHalfLength = 6; int markerHalfLength = 6;
@ -926,8 +931,9 @@ void RiuViewer::updateLegendLayout()
auto scaleLegendSize = m_scaleLegend->renderSize(); auto scaleLegendSize = m_scaleLegend->renderSize();
auto otherItemsHeight = m_versionInfoLabel->sizeHint().height(); auto otherItemsHeight = m_versionInfoLabel->sizeHint().height();
const int xPos = width() - (int)scaleLegendSize.x() - margin - edgeAxisBorderWidth; const auto ratio = displayScalingRatio();
const int yPos = margin + edgeAxisBorderHeight + margin + otherItemsHeight; const int xPos = (int)( ratio * width() ) - (int)scaleLegendSize.x() - margin - edgeAxisBorderWidth;
const int yPos = margin + edgeAxisBorderHeight + margin + otherItemsHeight;
m_scaleLegend->setLayoutFixedPosition( { xPos, yPos } ); m_scaleLegend->setLayoutFixedPosition( { xPos, yPos } );
} }
@ -1058,14 +1064,6 @@ void RiuViewer::optimizeClippingPlanes()
caf::Viewer::optimizeClippingPlanes(); caf::Viewer::optimizeClippingPlanes();
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuViewer::resizeGL( int width, int height )
{
caf::Viewer::resizeGL( width, height );
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -141,7 +141,6 @@ public slots:
protected: protected:
void optimizeClippingPlanes() override; void optimizeClippingPlanes() override;
void resizeGL( int width, int height ) override;
void mouseMoveEvent( QMouseEvent* e ) override; void mouseMoveEvent( QMouseEvent* e ) override;
void enterEvent( QEnterEvent* e ) override; void enterEvent( QEnterEvent* e ) override;
void leaveEvent( QEvent* ) override; void leaveEvent( QEvent* ) override;

View File

@ -69,6 +69,7 @@
#include "cvfqtPerformanceInfoHud.h" #include "cvfqtPerformanceInfoHud.h"
#include "cvfqtUtils.h" #include "cvfqtUtils.h"
#include <QApplication>
#include <QDebug> #include <QDebug>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QInputEvent> #include <QInputEvent>
@ -184,6 +185,14 @@ caf::Viewer::~Viewer()
if ( m_layoutWidget ) m_layoutWidget->deleteLater(); if ( m_layoutWidget ) m_layoutWidget->deleteLater();
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double caf::Viewer::displayScalingRatio() const
{
return qApp->primaryScreen()->devicePixelRatio();
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -325,7 +334,9 @@ void caf::Viewer::setComparisonViewVisibleNormalizedRect( const cvf::Rectf& visi
{ {
m_comparisonWindowNormalizedRect = visibleRect; m_comparisonWindowNormalizedRect = visibleRect;
updateCamera( width(), height() ); const auto ratio = displayScalingRatio();
updateCamera( (int)( ratio * width() ), (int)( ratio * height() ) );
update(); update();
} }
@ -639,8 +650,9 @@ cvf::ref<cvf::RayIntersectSpec> caf::Viewer::rayIntersectSpecFromWindowCoordinat
cvf::ref<cvf::RayIntersectSpec> cvf::ref<cvf::RayIntersectSpec>
caf::Viewer::rayIntersectSpecFromWindowCoordinates( int winPosX, int winPosY, bool isForComparisonView ) caf::Viewer::rayIntersectSpecFromWindowCoordinates( int winPosX, int winPosY, bool isForComparisonView )
{ {
int translatedMousePosX = winPosX; const auto ratio = displayScalingRatio();
int translatedMousePosY = height() - winPosY; int translatedMousePosX = (int)( ratio * winPosX );
int translatedMousePosY = (int)( ratio * ( height() - winPosY ) );
cvf::Rendering* renderingToInvestigate = isForComparisonView ? m_comparisonMainRendering.p() : m_mainRendering.p(); cvf::Rendering* renderingToInvestigate = isForComparisonView ? m_comparisonMainRendering.p() : m_mainRendering.p();
@ -711,6 +723,10 @@ bool caf::Viewer::isMousePosWithinComparisonView( int winPosX, int winPosY )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void caf::Viewer::resizeGL( int width, int height ) void caf::Viewer::resizeGL( int width, int height )
{ {
auto ratio = displayScalingRatio();
width = (int)( ratio * width );
height = (int)( ratio * height );
if ( width < 1 || height < 1 ) return; if ( width < 1 || height < 1 ) return;
if ( m_offscreenFbo.notNull() ) if ( m_offscreenFbo.notNull() )
@ -755,6 +771,11 @@ void caf::Viewer::paintGL()
CVF_CHECK_OGL( myOglContext.p() ); CVF_CHECK_OGL( myOglContext.p() );
CVF_ASSERT( myOglContext->isContextValid() ); CVF_ASSERT( myOglContext->isContextValid() );
auto ratio = displayScalingRatio();
auto thisSize = this->size();
thisSize.setHeight( (int)( ratio * thisSize.height() ) );
thisSize.setWidth( (int)( ratio * thisSize.width() ) );
QPainter painter( this ); QPainter painter( this );
if ( m_renderingSequence.isNull() || !canRender() ) if ( m_renderingSequence.isNull() || !canRender() )
@ -769,9 +790,9 @@ void caf::Viewer::paintGL()
if ( m_isOverlayPaintingEnabled || m_showPerfInfoHud ) if ( m_isOverlayPaintingEnabled || m_showPerfInfoHud )
{ {
// Set up image to draw to, and painter // Set up image to draw to, and painter
if ( m_overlayPaintingQImage.size() != this->size() ) if ( m_overlayPaintingQImage.size() != thisSize )
{ {
m_overlayPaintingQImage = QImage( this->size(), QImage::Format_ARGB32 ); m_overlayPaintingQImage = QImage( thisSize, QImage::Format_ARGB32 );
} }
m_overlayPaintingQImage.fill( Qt::transparent ); m_overlayPaintingQImage.fill( Qt::transparent );
@ -798,16 +819,16 @@ void caf::Viewer::paintGL()
// Convert the QImage into the cvf::TextureImage, // Convert the QImage into the cvf::TextureImage,
// handling vertical mirroring and (possible) byteswapping // handling vertical mirroring and (possible) byteswapping
if ( ( (int)m_overlayTextureImage->height() ) != this->height() || if ( ( (int)m_overlayTextureImage->height() ) != thisSize.height() ||
( (int)m_overlayTextureImage->width() != this->width() ) ) ( (int)m_overlayTextureImage->width() != thisSize.width() ) )
{ {
m_overlayTextureImage->allocate( this->width(), this->height() ); m_overlayTextureImage->allocate( thisSize.width(), thisSize.height() );
} }
cvfqt::Utils::toTextureImage( m_overlayPaintingQImage, m_overlayTextureImage.p() ); cvfqt::Utils::toTextureImage( m_overlayPaintingQImage, m_overlayTextureImage.p() );
m_overlayImage->setImage( m_overlayTextureImage.p() ); m_overlayImage->setImage( m_overlayTextureImage.p() );
m_overlayImage->setPixelSize( cvf::Vec2ui( this->width(), this->height() ) ); m_overlayImage->setPixelSize( cvf::Vec2ui( thisSize.width(), thisSize.height() ) );
} }
painter.beginNativePainting(); painter.beginNativePainting();

View File

@ -181,6 +181,9 @@ public:
void setCurrentComparisonFrame( int frameIndex ); void setCurrentComparisonFrame( int frameIndex );
void setComparisonViewToFollowAnimation( bool isToFollow ); void setComparisonViewToFollowAnimation( bool isToFollow );
// display scaling ratio (should be 1 if DPI scaling is not enabled)
double displayScalingRatio() const;
public slots: public slots:
virtual void slotSetCurrentFrame( int frameIndex ); virtual void slotSetCurrentFrame( int frameIndex );
virtual void slotEndAnimation(); virtual void slotEndAnimation();