Skip to content

Commit

Permalink
Fixed scaling bug on a HiDPI display.
Browse files Browse the repository at this point in the history
  • Loading branch information
hidefuku committed Apr 11, 2017
1 parent fa4ac5b commit 0de1493
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/core/CameraInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace core
{

CameraInfo::CameraInfo()
: mScreenSize()
: mDevicePixelRatio(1.0)
, mScreenSize()
, mImageSize()
, mCenter()
, mLeftTopPos()
Expand All @@ -15,8 +16,10 @@ CameraInfo::CameraInfo()
{
}

void CameraInfo::reset(const QSize& aScreenSize, const QSize& aImageSize, const QPoint& aLeftTopPos)
void CameraInfo::reset(const QSize& aScreenSize, double aDpr,
const QSize& aImageSize, const QPoint& aLeftTopPos)
{
mDevicePixelRatio = aDpr;
mScreenSize = aScreenSize;
mImageSize = aImageSize;
mScale = 1.0f;
Expand All @@ -25,6 +28,11 @@ void CameraInfo::reset(const QSize& aScreenSize, const QSize& aImageSize, const
mCenter = mLeftTopPos + centerOffset();
}

void CameraInfo::setDevicePixelRatio(double aRatio)
{
mDevicePixelRatio = aRatio;
}

void CameraInfo::setLeftTopPos(const QVector2D& aPos)
{
mLeftTopPos = aPos;
Expand Down
7 changes: 6 additions & 1 deletion src/core/CameraInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class CameraInfo
public:
CameraInfo();

void reset(const QSize& aScreenSize, const QSize& aImageSize, const QPoint& aLeftTopPos);
void reset(const QSize& aScreenSize, double aDpr,
const QSize& aImageSize, const QPoint& aLeftTopPos);

void setDevicePixelRatio(double aRatio);
inline void setScreenWidth(int aWidth) { mScreenSize.setWidth(aWidth); }
inline void setScreenHeight(int aHeight) { mScreenSize.setHeight(aHeight); }
inline void setScreenSize(const QSize& aSize) { mScreenSize = aSize; }
Expand All @@ -28,9 +30,11 @@ class CameraInfo
void setScale(float aScale);
void setRotate(float aRadian);

inline double devicePixelRatio() const { return mDevicePixelRatio; }
inline int screenWidth() const { return mScreenSize.width(); }
inline int screenHeight() const { return mScreenSize.height(); }
inline QSize screenSize() const { return mScreenSize; }
inline QSize deviceScreenSize() const { return mScreenSize * mDevicePixelRatio; }
QVector2D screenCenter() const;

inline QSize imageSize() const { return mImageSize; }
Expand Down Expand Up @@ -69,6 +73,7 @@ class CameraInfo
return 0.5f * QVector2D(mImageSize.width(), mImageSize.height());
}

double mDevicePixelRatio;
QSize mScreenSize;
QSize mImageSize;
QVector2D mCenter;
Expand Down
4 changes: 2 additions & 2 deletions src/core/LayerNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void LayerNode::renderClipper(
shader.setAttributeArray("inTexCoord", mCurrentMesh->texCoords(), mCurrentMesh->vertexCount());

shader.setUniformValue("uViewMatrix", viewMatrix);
shader.setUniformValue("uScreenSize", QSizeF(aInfo.camera.screenSize()));
shader.setUniformValue("uScreenSize", QSizeF(aInfo.camera.deviceScreenSize()));
shader.setUniformValue("uImageSize", QSizeF(textureSize));
shader.setUniformValue("uTexCoordOffset", texCoordOffset);
shader.setUniformValue("uColor", color);
Expand Down Expand Up @@ -388,7 +388,7 @@ void LayerNode::renderShape(
shader.setAttributeArray("inTexCoord", mCurrentMesh->texCoords(), mCurrentMesh->vertexCount());

shader.setUniformValue("uViewMatrix", viewMatrix);
shader.setUniformValue("uScreenSize", QSizeF(aInfo.camera.screenSize()));
shader.setUniformValue("uScreenSize", QSizeF(aInfo.camera.deviceScreenSize()));
shader.setUniformValue("uImageSize", QSizeF(textureSize));
shader.setUniformValue("uTexCoordOffset", texCoordOffset);
shader.setUniformValue("uColor", color);
Expand Down
2 changes: 1 addition & 1 deletion src/ctrl/Exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ bool Exporter::update()

// render
core::RenderInfo renderInfo;
renderInfo.camera.reset(originSize, originSize, QPoint());
renderInfo.camera.reset(originSize, 1.0, originSize, QPoint());
renderInfo.time = timeInfo;
renderInfo.framebuffer = mFramebuffers.front()->handle();
renderInfo.dest = mFramebuffers.front()->texture();
Expand Down
1 change: 1 addition & 0 deletions src/gui/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ int entryPoint(int argc, char *argv[])
// create qt application
QApplication app(argc, argv);
XC_DEBUG_REPORT() << "exe path =" << app.applicationFilePath();
app.setAttribute(Qt::AA_UseHighDpiPixmaps);

// application path
#if defined(Q_OS_MAC)
Expand Down
29 changes: 19 additions & 10 deletions src/gui/MainDisplayWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <functional>
#include <QMouseEvent>
#include <QOpenGLFunctions>
#include <QGuiApplication>
#include "XC.h"
#include "util/Finally.h"
#include "gl/Util.h"
Expand Down Expand Up @@ -40,6 +41,7 @@ MainDisplayWidget::MainDisplayWidget(ViaPoint& aViaPoint, QWidget* aParent)
, mCanvasMover()
, mMovingCanvasByTool(false)
, mMovingCanvasByKey(false)
, mDevicePixelRatio(1.0)
{
#ifdef USE_GL_CORE_PROFILE
// setup opengl format
Expand Down Expand Up @@ -134,6 +136,7 @@ void MainDisplayWidget::setProject(core::Project* aProject)
{
mProject = aProject->pointee();
mRenderInfo = &(static_cast<ProjectHook*>(mProject->hook())->renderInfo());
mRenderInfo->camera.setDevicePixelRatio(this->devicePixelRatioF());
mRenderInfo->camera.setScreenSize(this->size());
mRenderInfo->camera.setImageSize(mProject->attribute().imageSize());
mCanvasMover.setCamera(&(mRenderInfo->camera));
Expand Down Expand Up @@ -221,16 +224,18 @@ void MainDisplayWidget::initializeGL()
mDefaultVAO->bind(); // keep binding
#endif

mDevicePixelRatio = this->devicePixelRatioF();

// create framebuffer for display
mFramebuffer.reset(new QOpenGLFramebufferObject(this->size()));
mFramebuffer.reset(new QOpenGLFramebufferObject(deviceSize()));

// create clipping buffer
mClippingFrame.reset(new core::ClippingFrame());
mClippingFrame->resize(this->size());
mClippingFrame->resize(deviceSize());

// create texturizer for destination colors of the framebuffer
mDestinationTexturizer.reset(new core::DestinationTexturizer());
mDestinationTexturizer->resize(this->size());
mDestinationTexturizer->resize(deviceSize());

// create texture drawer for copying framebuffer to display
mTextureDrawer.reset(new gl::EasyTextureDrawer());
Expand Down Expand Up @@ -262,7 +267,7 @@ void MainDisplayWidget::paintGL()
}

// setup
gl::Util::setViewportAsActualPixels(this->size());
gl::Util::setViewportAsActualPixels(deviceSize());
gl::Util::clearColorBuffer(0.25f, 0.25f, 0.25f, 1.0f);
gl::Util::resetRenderState();
GL_CHECK_ERROR();
Expand Down Expand Up @@ -342,25 +347,29 @@ void MainDisplayWidget::paintEvent(QPaintEvent* aEvent)

void MainDisplayWidget::resizeGL(int w, int h)
{
const QSize newSize(w, h);
// currrent device pixel ratio (Attention that it is variable.)
mDevicePixelRatio = this->devicePixelRatioF();
const QSize devSize(mDevicePixelRatio * w, mDevicePixelRatio * h); // device pixel size
const QSize absSize(w, h); // abstract pixel size

if (mRenderInfo)
{
mRenderInfo->camera.setScreenSize(newSize);
mRenderInfo->camera.setDevicePixelRatio(mDevicePixelRatio);
mRenderInfo->camera.setScreenSize(absSize);
mCanvasMover.onScreenResized();
}
mFramebuffer.reset();
mFramebuffer.reset(new QOpenGLFramebufferObject(w, h));
mFramebuffer.reset(new QOpenGLFramebufferObject(devSize.width(), devSize.height()));

mClippingFrame.reset();
mClippingFrame.reset(new core::ClippingFrame());
mClippingFrame->resize(newSize);
mClippingFrame->resize(devSize);

mDestinationTexturizer->resize(newSize);
mDestinationTexturizer->resize(devSize);

if (mProjectTabBar)
{
mProjectTabBar->updateTabPosition(newSize);
mProjectTabBar->updateTabPosition(absSize);
}
GL_CHECK_ERROR();
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/MainDisplayWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class MainDisplayWidget : public QOpenGLWidget
virtual void tabletEvent(QTabletEvent* event);

void updateCursor();
QSize deviceSize() const { return this->size() * mDevicePixelRatio; }

ViaPoint& mViaPoint;
gl::DeviceInfo mGLDeviceInfo;
Expand All @@ -95,6 +96,7 @@ class MainDisplayWidget : public QOpenGLWidget
CanvasMover mCanvasMover;
bool mMovingCanvasByTool;
bool mMovingCanvasByKey;
double mDevicePixelRatio;
};

} // namespace gui
Expand Down

0 comments on commit 0de1493

Please sign in to comment.