# DPI/screen scaling when using OpenGL

**URL:** <https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828>\
**Category:** Android\
**Created:** [May 24, 2016, 4:47pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828 "2016-05-24T16:47:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [May 24, 2016, 4:47pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/1 "2016-05-24T16:47:44Z")

</div>

I’m porting one of our apps to Android - it already runs fine on iOS and desktop formats.

The app runs only in landscape and is hardcoded to 1024 x 768. I’m using setGlobalScaleFactor to account for different screen DPIs.

If I don’t use OpenGL, then the app scales perfectly to the screen but the graphics are sluggish.

If I attach an OpenGLContext to my MainComponent then the app appears squashed vertically but the touch sense acts as if the app has been rendered correctly, i.e. tapping slightly below where the button appears gets registered as a button press. The graphics are very responsive though 😉

Is there something I need to do to get the OpenGL version to render correctly?

---

<div class="post-metadata">

**Author:** ![trickyflemming](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@trickyflemming](https://forum.juce.com/u/trickyflemming)\
**Post date:** [May 24, 2016, 9:55pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/2 "2016-05-24T21:55:05Z")

</div>

This may be related to a bug that I reported:

> [@OpenGLContext, OS X, Plugins, and Scaling](https://forum.juce.com/t/openglcontext-os-x-plugins-and-scaling/17528):
>
> JUCE 4.2, latest develop: We’re having a hell of a time adding interface scaling to our plug-ins. We’ve been using the SoundRadix fork for a while, but ran into this bug. We’ve switched to the main develop branch to try to troubleshoot this. So, we have a PluginEditor. We also have an OpenGLContext (continuous repaint enabled) that is attached to the editor. To do interface scaling, we’re resizing the window and applying an affine transform to scale the component. setSize(getPluginMainWidth()…

My best recommendation is using the SoundRadix branch, which has fixes for OpenGLContext scaling.

One thing we had to do as well: Don’t attach OpenGLContext to the top level component. Create a Component to hold your OpenGL Component, and attach the context to the OpenGL Component only. Between the SoundRadix fixes and that, we were able to fix our OpenGL issues.

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [May 24, 2016, 10:10pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/3 "2016-05-24T22:10:42Z")

</div>

Thanks for your reply - that’s the exact problem!

Is this only an issue for Juce 4.2? If so I can roll back to an earlier version.

---

<div class="post-metadata">

**Author:** ![trickyflemming](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@trickyflemming](https://forum.juce.com/u/trickyflemming)\
**Post date:** [May 24, 2016, 10:15pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/4 "2016-05-24T22:15:35Z")

</div>

Unfortunately, no. It appears that this problem has been happening for a while. The SR branch closely mirrors the main JUCE branch and doesn’t require additional changes to your codebase (i.e. no bonus virtual functions that need defined anywhere)

It can be found at [https://github.com/soundradix/JUCE/commits/master](https://github.com/soundradix/JUCE/commits/master)

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [May 24, 2016, 10:18pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/5 "2016-05-24T22:18:26Z")

</div>

Excellent, thanks! I’ll give that a try tomorrow and report back.

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [May 25, 2016, 10:45am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/6 "2016-05-25T10:45:02Z")

</div>

Unfortunately this doesn’t seem to fix the problem on Android.

Using the SR fork, I have tried various combinations of attaching the OpenGLContext to parent and child components, but the scaling is always wrong.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [May 25, 2016, 1:39pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/7 "2016-05-25T13:39:45Z")

</div>

I’m trying to fix this but I am (once more) struggling to get the Android Studio debugger working. However, I think I’ve narrowed down the problem pretty far already.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [May 26, 2016, 9:13am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/8 "2016-05-26T09:13:02Z")

</div>

Sorry this seems like a hard nut to crack. It’s actually a bug which occurs on all platforms except OS X. If fixed a bunch of other bugs along the way but it still not working properly. Jules will have a go at this today.

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [May 26, 2016, 9:30am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/9 "2016-05-26T09:30:15Z")

</div>

Thanks for the update Fabian. I did notice that it works fine on OS X, as I did some testing yesterday.

Fingers crossed for a solution 🙂

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [June 3, 2016, 10:26am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/10 "2016-06-03T10:26:39Z")

</div>

Has there been any progress on this bug?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 3, 2016, 11:12am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/11 "2016-06-03T11:12:21Z")

</div>

Sorry, Fabian’s away this week and I’ve been super-busy… Not forgotten though, we’ll nail it soon!

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [June 20, 2016, 2:42pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/12 "2016-06-20T14:42:37Z")

</div>

Any progress to report on this issue?

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [June 20, 2016, 5:25pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/13 "2016-06-20T17:25:28Z")

</div>

We’re still working on this issue. It requires some re-thinking of a few functions in ComponentPeer which will ultimately break backward compatibility with code directly using the ComponentPeer and global scaling factors. We need to think about how to do the changes without breaking too much.

Sorry this bug turned out to be much more involved than expected.

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [June 21, 2016, 9:50am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/14 "2016-06-21T09:50:43Z")

</div>

Hi Fabian,

Thanks for the update. Are there any possible workarounds that we can try here?

Cheers,  
Mark

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [June 22, 2016, 8:45am UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/15 "2016-06-22T08:45:18Z")

</div>

Well don’t use setGlobalScaleFactor and instead edit the scaling factor code [here](https://github.com/julianstorer/JUCE/blob/a33abe657ea2a9af823117551bfc80920c89753e/modules/juce_gui_basics/native/juce_android_Windowing.cpp#L827). Sorry about the unclean workaround.

---

<div class="post-metadata">

**Author:** ![MarkC\_SSL](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/markc_ssl/32/1080_2.png) [@MarkC\_SSL](https://forum.juce.com/u/MarkC_SSL)\
**Post date:** [June 23, 2016, 12:45pm UTC](https://forum.juce.com/t/dpi-screen-scaling-when-using-opengl/17828/16 "2016-06-23T12:45:40Z")

</div>

Hi Fabian,

Thanks for the tip - I’ve now got the screen scaling correctly when using OpenGL.

One question, why is the ratio calculation in findDisplays `d.dpi/150?` Should it not be `d.dpi/160`? According to the Android docs, 1 density independent pixel corresponds to a physical pixel at 160 DPI. I’m targeting the Samsung Galaxy Tab A which has a DPI of 160, and it does not render correctly unless the ratio is 1:1.  
With `d.dpi/150` the GUI is rendered too large for the screen.
