src/java.desktop/windows/native/common/awt/systemscale/systemScale.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 36907 jdk/src/java.desktop/windows/native/common/awt/systemscale/systemScale.cpp@c3d8383e3efb
child 48373 f9152f462cbc
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36907
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     1
/*
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     2
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     4
*
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     5
* This code is free software; you can redistribute it and/or modify it
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     6
* under the terms of the GNU General Public License version 2 only, as
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     7
* published by the Free Software Foundation.
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     8
*
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
     9
* This code is distributed in the hope that it will be useful, but WITHOUT
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    11
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    12
* version 2 for more details (a copy is included in the LICENSE file that
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    13
* accompanied this code).
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    14
*
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    15
* You should have received a copy of the GNU General Public License version
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    16
* 2 along with this work; if not, write to the Free Software Foundation,
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    18
*
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    20
* or visit www.oracle.com if you need additional information or have any
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    21
* questions.
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    22
*/
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    23
#include "systemScale.h"
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    24
#include <d2d1.h>
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    25
#pragma comment(lib, "d2d1")
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    26
#include <jdk_util.h>
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    27
#ifndef MDT_EFFECTIVE_DPI
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    28
#define MDT_EFFECTIVE_DPI 0
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    29
#endif
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    30
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    31
void GetScreenDpi(HMONITOR hmon, float *dpiX, float *dpiY)
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    32
{
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    33
    unsigned x = 0;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    34
    unsigned y = 0;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    35
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    36
    // for debug purposes
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    37
    static float scale = -2.0f;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    38
    if (scale == -2) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    39
        scale = -1;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    40
        char *uiScale = getenv("J2D_UISCALE");
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    41
        if (uiScale != NULL) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    42
            scale = (float)strtod(uiScale, NULL);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    43
            if (errno == ERANGE || scale <= 0) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    44
                scale = -1;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    45
            }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    46
        }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    47
    }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    48
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    49
    if (scale > 0) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    50
        *dpiX = *dpiY = scale;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    51
        return;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    52
    }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    53
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    54
    typedef HRESULT(WINAPI GetDpiForMonitorFunc)(HMONITOR, int, UINT*, UINT*);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    55
    static HMODULE hLibSHCoreDll = NULL;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    56
    static GetDpiForMonitorFunc *lpGetDpiForMonitor = NULL;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    57
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    58
    if (hLibSHCoreDll == NULL) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    59
        hLibSHCoreDll = JDK_LoadSystemLibrary("shcore.dll");
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    60
        if (hLibSHCoreDll != NULL) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    61
            lpGetDpiForMonitor = (GetDpiForMonitorFunc*)GetProcAddress(
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    62
                hLibSHCoreDll, "GetDpiForMonitor");
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    63
        }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    64
    }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    65
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    66
    if (lpGetDpiForMonitor != NULL) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    67
        HRESULT hResult = lpGetDpiForMonitor(hmon,
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    68
                                             MDT_EFFECTIVE_DPI, &x, &y);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    69
        if (hResult == S_OK) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    70
            *dpiX = static_cast<float>(x);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    71
            *dpiY = static_cast<float>(y);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    72
        }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    73
    } else {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    74
        ID2D1Factory* m_pDirect2dFactory;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    75
        HRESULT res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    76
                                        &m_pDirect2dFactory);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    77
        if (res == S_OK) {
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    78
            m_pDirect2dFactory->GetDesktopDpi(dpiX, dpiY);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    79
            m_pDirect2dFactory->Release();
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    80
        }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    81
    }
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    82
    return;
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    83
}
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    84
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    85
HMONITOR WINAPI getPrimaryMonitor()
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    86
{
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    87
    const POINT point = { 0, 0 };
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    88
    return MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY);
c3d8383e3efb 8145173: HiDPI splash screen support on Windows
rchamyal
parents:
diff changeset
    89
}