< prev index next >

src/java.desktop/windows/native/libawt/windows/awt_Win32GraphicsDevice.cpp

Print this page




  32  * Instead, anyone wishing to reference a device in the array (e.g.,
  33  * the current default device or a device for a given hWnd) must
  34  * call one of the static methods of this class with the index of
  35  * the device in question.  Those methods will then lock the devices
  36  * array and forward the request to the current device at that
  37  * array index.
  38  */
  39 
  40 #include <awt.h>
  41 #include <sun_awt_Win32GraphicsDevice.h>
  42 #include "awt_Canvas.h"
  43 #include "awt_Win32GraphicsDevice.h"
  44 #include "awt_Window.h"
  45 #include "java_awt_Transparency.h"
  46 #include "java_awt_color_ColorSpace.h"
  47 #include "sun_awt_Win32GraphicsDevice.h"
  48 #include "java_awt_image_DataBuffer.h"
  49 #include "dither.h"
  50 #include "img_util_md.h"
  51 #include "Devices.h"






  52 
  53 uns_ordered_dither_array img_oda_alpha;
  54 
  55 jclass      AwtWin32GraphicsDevice::indexCMClass;
  56 jclass      AwtWin32GraphicsDevice::wToolkitClass;
  57 jfieldID    AwtWin32GraphicsDevice::dynamicColorModelID;
  58 jfieldID    AwtWin32GraphicsDevice::indexCMrgbID;
  59 jfieldID    AwtWin32GraphicsDevice::indexCMcacheID;
  60 jmethodID   AwtWin32GraphicsDevice::paletteChangedMID;
  61 BOOL        AwtWin32GraphicsDevice::primaryPalettized;
  62 int         AwtWin32GraphicsDevice::primaryIndex = 0;
  63 
  64 
  65 /**
  66  * Construct this device.  Store the screen (index into the devices
  67  * array of this object), the array (used in static references via
  68  * particular device indices), the monitor/pMonitorInfo (which other
  69  * classes will inquire of this device), the bits per pixel of this
  70  * device, and information on whether the primary device is palettized.
  71  */
  72 AwtWin32GraphicsDevice::AwtWin32GraphicsDevice(int screen,
  73                                                HMONITOR mhnd, Devices *arr)
  74 {
  75     this->screen  = screen;
  76     this->devicesArray = arr;


  77     javaDevice = NULL;
  78     colorData = new ImgColorData;
  79     colorData->grayscale = GS_NOTGRAY;
  80     palette = NULL;
  81     cData = NULL;
  82     gpBitmapInfo = NULL;
  83     monitor = mhnd;
  84     pMonitorInfo = new MONITORINFOEX;
  85     pMonitorInfo->cbSize = sizeof(MONITORINFOEX);
  86     ::GetMonitorInfo(monitor, pMonitorInfo);
  87 
  88     // Set primary device info: other devices will need to know
  89     // whether the primary is palettized during the initialization
  90     // process
  91     HDC hDC = this->GetDC();
  92     colorData->bitsperpixel = ::GetDeviceCaps(hDC, BITSPIXEL);
  93     this->ReleaseDC(hDC);
  94     if (MONITORINFOF_PRIMARY & pMonitorInfo->dwFlags) {
  95         primaryIndex = screen;
  96         if (colorData->bitsperpixel > 8) {


 600  * Object referring to this device is releasing that reference.
 601  * This allows the array holding all devices to be released (once
 602  * all references to the array have gone away).
 603  */
 604 void AwtWin32GraphicsDevice::Release()
 605 {
 606     devicesArray->Release();
 607 }
 608 
 609 /**
 610  * Links this native object with its java Win32GraphicsDevice.
 611  * Need this link because the colorModel of the java device
 612  * may be updated from native code.
 613  */
 614 void AwtWin32GraphicsDevice::SetJavaDevice(JNIEnv *env, jobject objPtr)
 615 {
 616     javaDevice = env->NewWeakGlobalRef(objPtr);
 617 }
 618 
 619 /**




























































































 620  * Disables offscreen acceleration for this device.  This
 621  * sets a flag in the java object that is used to determine
 622  * whether offscreen surfaces can be created on the device.
 623  */
 624 void AwtWin32GraphicsDevice::DisableOffscreenAcceleration()
 625 {
 626     // REMIND: noop for now
 627 }
 628 
 629 /**
 630  * Invalidates the GraphicsDevice object associated with this
 631  * device by disabling offscreen acceleration and calling
 632  * invalidate(defIndex) on the java object.
 633  */
 634 void AwtWin32GraphicsDevice::Invalidate(JNIEnv *env)
 635 {
 636     int defIndex = AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
 637     DisableOffscreenAcceleration();
 638     jobject javaDevice = GetJavaDevice();
 639     if (!JNU_IsNull(env, javaDevice)) {


1287 JNIEXPORT jobject JNICALL
1288     Java_sun_awt_Win32GraphicsDevice_makeColorModel
1289     (JNIEnv *env, jobject thisPtr, jint screen, jboolean dynamic)
1290 {
1291     Devices::InstanceAccess devices;
1292     return devices->GetDevice(screen)->GetColorModel(env, dynamic);
1293 }
1294 
1295 /*
1296  * Class:     sun_awt_Win32GraphicsDevice
1297  * Method:    initDevice
1298  * Signature: (I)V
1299  */
1300 JNIEXPORT void JNICALL
1301     Java_sun_awt_Win32GraphicsDevice_initDevice
1302     (JNIEnv *env, jobject thisPtr, jint screen)
1303 {
1304     Devices::InstanceAccess devices;
1305     devices->GetDevice(screen)->SetJavaDevice(env, thisPtr);
1306 }
































































  32  * Instead, anyone wishing to reference a device in the array (e.g.,
  33  * the current default device or a device for a given hWnd) must
  34  * call one of the static methods of this class with the index of
  35  * the device in question.  Those methods will then lock the devices
  36  * array and forward the request to the current device at that
  37  * array index.
  38  */
  39 
  40 #include <awt.h>
  41 #include <sun_awt_Win32GraphicsDevice.h>
  42 #include "awt_Canvas.h"
  43 #include "awt_Win32GraphicsDevice.h"
  44 #include "awt_Window.h"
  45 #include "java_awt_Transparency.h"
  46 #include "java_awt_color_ColorSpace.h"
  47 #include "sun_awt_Win32GraphicsDevice.h"
  48 #include "java_awt_image_DataBuffer.h"
  49 #include "dither.h"
  50 #include "img_util_md.h"
  51 #include "Devices.h"
  52 #include <d2d1.h>
  53 #pragma comment(lib, "d2d1")
  54 
  55 #ifndef MDT_Effective_DPI 
  56 #define MDT_Effective_DPI 0
  57 #endif
  58 
  59 uns_ordered_dither_array img_oda_alpha;
  60 
  61 jclass      AwtWin32GraphicsDevice::indexCMClass;
  62 jclass      AwtWin32GraphicsDevice::wToolkitClass;
  63 jfieldID    AwtWin32GraphicsDevice::dynamicColorModelID;
  64 jfieldID    AwtWin32GraphicsDevice::indexCMrgbID;
  65 jfieldID    AwtWin32GraphicsDevice::indexCMcacheID;
  66 jmethodID   AwtWin32GraphicsDevice::paletteChangedMID;
  67 BOOL        AwtWin32GraphicsDevice::primaryPalettized;
  68 int         AwtWin32GraphicsDevice::primaryIndex = 0;
  69 
  70 
  71 /**
  72  * Construct this device.  Store the screen (index into the devices
  73  * array of this object), the array (used in static references via
  74  * particular device indices), the monitor/pMonitorInfo (which other
  75  * classes will inquire of this device), the bits per pixel of this
  76  * device, and information on whether the primary device is palettized.
  77  */
  78 AwtWin32GraphicsDevice::AwtWin32GraphicsDevice(int screen,
  79                                                HMONITOR mhnd, Devices *arr)
  80 {
  81     this->screen  = screen;
  82     this->devicesArray = arr;
  83     this->scaleX = 1;
  84     this->scaleY = 1;
  85     javaDevice = NULL;
  86     colorData = new ImgColorData;
  87     colorData->grayscale = GS_NOTGRAY;
  88     palette = NULL;
  89     cData = NULL;
  90     gpBitmapInfo = NULL;
  91     monitor = mhnd;
  92     pMonitorInfo = new MONITORINFOEX;
  93     pMonitorInfo->cbSize = sizeof(MONITORINFOEX);
  94     ::GetMonitorInfo(monitor, pMonitorInfo);
  95 
  96     // Set primary device info: other devices will need to know
  97     // whether the primary is palettized during the initialization
  98     // process
  99     HDC hDC = this->GetDC();
 100     colorData->bitsperpixel = ::GetDeviceCaps(hDC, BITSPIXEL);
 101     this->ReleaseDC(hDC);
 102     if (MONITORINFOF_PRIMARY & pMonitorInfo->dwFlags) {
 103         primaryIndex = screen;
 104         if (colorData->bitsperpixel > 8) {


 608  * Object referring to this device is releasing that reference.
 609  * This allows the array holding all devices to be released (once
 610  * all references to the array have gone away).
 611  */
 612 void AwtWin32GraphicsDevice::Release()
 613 {
 614     devicesArray->Release();
 615 }
 616 
 617 /**
 618  * Links this native object with its java Win32GraphicsDevice.
 619  * Need this link because the colorModel of the java device
 620  * may be updated from native code.
 621  */
 622 void AwtWin32GraphicsDevice::SetJavaDevice(JNIEnv *env, jobject objPtr)
 623 {
 624     javaDevice = env->NewWeakGlobalRef(objPtr);
 625 }
 626 
 627 /**
 628  * Sets horizontal and vertical scale factors
 629  */
 630 void AwtWin32GraphicsDevice::SetScale(float sx, float sy)
 631 {
 632     scaleX = sx;
 633     scaleY = sy;
 634 }
 635 
 636 int AwtWin32GraphicsDevice::ScaleUpX(int x)
 637 {
 638     return (int)ceil(x * scaleX);
 639 }
 640 
 641 int AwtWin32GraphicsDevice::ScaleUpY(int y)
 642 {
 643     return (int)ceil(y * scaleY);
 644 }
 645 
 646 int AwtWin32GraphicsDevice::ScaleDownX(int x)
 647 {
 648     return (int)ceil(x / scaleX);
 649 }
 650 
 651 int AwtWin32GraphicsDevice::ScaleDownY(int y)
 652 {
 653         return (int)ceil(y / scaleY);
 654 }
 655 
 656 void AwtWin32GraphicsDevice::InitDesktopScales()
 657 {
 658     unsigned x = 0;
 659     unsigned y = 0;
 660     float dpiX = -1.0f;
 661     float dpiY = -1.0f;
 662 
 663     // for debug purposes
 664     char *uiScale = getenv("J2D_WIN_UISCALE");
 665     float scale = -1.0f;
 666 
 667     if (uiScale != NULL) {
 668         scale = (float)strtod(uiScale, NULL);
 669         if (errno != ERANGE && scale > 0) {
 670             SetScale(scale, scale);
 671             return;
 672         }
 673     }
 674 
 675     typedef HRESULT(WINAPI GetDpiForMonitorFunc)(HMONITOR, int, UINT*, UINT*);
 676     static HMODULE hLibSHCoreDll = NULL;
 677     static GetDpiForMonitorFunc *lpGetDpiForMonitor = NULL;
 678 
 679     if (hLibSHCoreDll == NULL) {
 680         hLibSHCoreDll = JDK_LoadSystemLibrary("shcore.dll");
 681         if (hLibSHCoreDll != NULL) {
 682             lpGetDpiForMonitor = (GetDpiForMonitorFunc*)GetProcAddress(
 683                 hLibSHCoreDll, "GetDpiForMonitor");
 684         }
 685     }
 686 
 687     if (lpGetDpiForMonitor != NULL) {
 688         HRESULT hResult = lpGetDpiForMonitor(GetMonitor(),
 689                                              MDT_Effective_DPI, &x, &y);
 690         if (hResult == S_OK) {
 691             dpiX = static_cast<float>(x);
 692             dpiY = static_cast<float>(y);
 693         }
 694     } else {
 695         ID2D1Factory* m_pDirect2dFactory;
 696         HRESULT res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
 697                                         &m_pDirect2dFactory);
 698         if (res == S_OK) {
 699             m_pDirect2dFactory->GetDesktopDpi(&dpiX, &dpiY);
 700             m_pDirect2dFactory->Release();
 701         }
 702     }
 703 
 704     if (dpiX > 0 && dpiY > 0) {
 705         SetScale(dpiX / 96, dpiY / 96);
 706     }
 707 }
 708 
 709 float AwtWin32GraphicsDevice::GetScaleX()
 710 {
 711     return scaleX;
 712 }
 713 
 714 float AwtWin32GraphicsDevice::GetScaleY()
 715 {
 716     return scaleY;
 717 }
 718 
 719 /**
 720  * Disables offscreen acceleration for this device.  This
 721  * sets a flag in the java object that is used to determine
 722  * whether offscreen surfaces can be created on the device.
 723  */
 724 void AwtWin32GraphicsDevice::DisableOffscreenAcceleration()
 725 {
 726     // REMIND: noop for now
 727 }
 728 
 729 /**
 730  * Invalidates the GraphicsDevice object associated with this
 731  * device by disabling offscreen acceleration and calling
 732  * invalidate(defIndex) on the java object.
 733  */
 734 void AwtWin32GraphicsDevice::Invalidate(JNIEnv *env)
 735 {
 736     int defIndex = AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
 737     DisableOffscreenAcceleration();
 738     jobject javaDevice = GetJavaDevice();
 739     if (!JNU_IsNull(env, javaDevice)) {


1387 JNIEXPORT jobject JNICALL
1388     Java_sun_awt_Win32GraphicsDevice_makeColorModel
1389     (JNIEnv *env, jobject thisPtr, jint screen, jboolean dynamic)
1390 {
1391     Devices::InstanceAccess devices;
1392     return devices->GetDevice(screen)->GetColorModel(env, dynamic);
1393 }
1394 
1395 /*
1396  * Class:     sun_awt_Win32GraphicsDevice
1397  * Method:    initDevice
1398  * Signature: (I)V
1399  */
1400 JNIEXPORT void JNICALL
1401     Java_sun_awt_Win32GraphicsDevice_initDevice
1402     (JNIEnv *env, jobject thisPtr, jint screen)
1403 {
1404     Devices::InstanceAccess devices;
1405     devices->GetDevice(screen)->SetJavaDevice(env, thisPtr);
1406 }
1407 
1408 /*
1409  * Class:     sun_awt_Win32GraphicsDevice
1410  * Method:    setNativeScale
1411  * Signature: (I,F,F)V
1412  */
1413 JNIEXPORT void JNICALL
1414     Java_sun_awt_Win32GraphicsDevice_setNativeScale
1415     (JNIEnv *env, jobject thisPtr, jint screen, jfloat scaleX, jfloat scaleY)
1416 {
1417     Devices::InstanceAccess devices;
1418     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1419 
1420     if (device != NULL ) {
1421         device->SetScale(scaleX, scaleY);
1422     }
1423 }
1424 
1425 /*
1426  * Class:     sun_awt_Win32GraphicsDevice
1427  * Method:    getNativeScaleX
1428  * Signature: (I)F
1429  */
1430 JNIEXPORT jfloat JNICALL
1431     Java_sun_awt_Win32GraphicsDevice_getNativeScaleX
1432     (JNIEnv *env, jobject thisPtr, jint screen)
1433 {
1434     Devices::InstanceAccess devices;
1435     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1436     return (device == NULL) ? 1 : device->GetScaleX();
1437 }
1438 
1439 /*
1440  * Class:     sun_awt_Win32GraphicsDevice
1441  * Method:    getNativeScaleY
1442  * Signature: (I)F
1443  */
1444 JNIEXPORT jfloat JNICALL
1445     Java_sun_awt_Win32GraphicsDevice_getNativeScaleY
1446     (JNIEnv *env, jobject thisPtr, jint screen)
1447 {
1448     Devices::InstanceAccess devices;
1449     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1450     return (device == NULL) ? 1 : device->GetScaleY();
1451 }
1452 
1453 /*
1454 * Class:     sun_awt_Win32GraphicsDevice
1455 * Method:    initNativeScale
1456 * Signature: (I)V;
1457 */
1458 JNIEXPORT void JNICALL
1459 Java_sun_awt_Win32GraphicsDevice_initNativeScale
1460 (JNIEnv *env, jobject thisPtr, jint screen)
1461 {
1462     Devices::InstanceAccess devices;
1463     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1464 
1465     if (device != NULL) {
1466         device->InitDesktopScales();
1467     }
1468 }
< prev index next >