< prev index next >

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

Print this page
rev 60071 : 8211999: Window positioning bugs due to overlapping GraphicsDevice bounds (Windows/HiDPI)
Reviewed-by: XXX


  60 jfieldID    AwtWin32GraphicsDevice::indexCMcacheID;
  61 jmethodID   AwtWin32GraphicsDevice::paletteChangedMID;
  62 BOOL        AwtWin32GraphicsDevice::primaryPalettized;
  63 int         AwtWin32GraphicsDevice::primaryIndex = 0;
  64 
  65 
  66 /**
  67  * Construct this device.  Store the screen (index into the devices
  68  * array of this object), the array (used in static references via
  69  * particular device indices), the monitor/pMonitorInfo (which other
  70  * classes will inquire of this device), the bits per pixel of this
  71  * device, and information on whether the primary device is palettized.
  72  */
  73 AwtWin32GraphicsDevice::AwtWin32GraphicsDevice(int screen,
  74                                                HMONITOR mhnd, Devices *arr)
  75 {
  76     this->screen  = screen;
  77     this->devicesArray = arr;
  78     this->scaleX = 1;
  79     this->scaleY = 1;

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


 616  */
 617 void AwtWin32GraphicsDevice::SetJavaDevice(JNIEnv *env, jobject objPtr)
 618 {
 619     javaDevice = env->NewWeakGlobalRef(objPtr);
 620 }
 621 
 622 /**
 623  * Sets horizontal and vertical scale factors
 624  */
 625 void AwtWin32GraphicsDevice::SetScale(float sx, float sy)
 626 {
 627     scaleX = sx;
 628     scaleY = sy;
 629 }
 630 
 631 int AwtWin32GraphicsDevice::ScaleUpX(int x)
 632 {
 633     return ClipRound(x * scaleX);
 634 }
 635 






 636 int AwtWin32GraphicsDevice::ScaleUpY(int y)
 637 {
 638     return ClipRound(y * scaleY);
 639 }
 640 






 641 int AwtWin32GraphicsDevice::ScaleDownX(int x)
 642 {
 643     return ClipRound(x / scaleX);
 644 }
 645 






 646 int AwtWin32GraphicsDevice::ScaleDownY(int y)
 647 {
 648     return ClipRound(y / scaleY);
 649 }
 650 






 651 int AwtWin32GraphicsDevice::ClipRound(double value)
 652 {
 653     value -= 0.5;
 654     if (value < INT_MIN)
 655     {
 656         return INT_MIN;
 657     }
 658 
 659     if (value > INT_MAX)
 660     {
 661         return INT_MAX;
 662     }
 663 
 664     return (int)ceil(value);
 665 }
 666 
 667 void AwtWin32GraphicsDevice::InitDesktopScales()
 668 {
 669     float dpiX = -1.0f;
 670     float dpiY = -1.0f;
 671     GetScreenDpi(GetMonitor(), &dpiX, &dpiY);
 672     if (dpiX > 0 && dpiY > 0) {
 673         SetScale(dpiX / 96, dpiY / 96);
 674     }

 675 }
 676 
 677 float AwtWin32GraphicsDevice::GetScaleX()
 678 {
 679     return scaleX;
 680 }
 681 
 682 float AwtWin32GraphicsDevice::GetScaleY()
 683 {
 684     return scaleY;
 685 }
 686 
 687 /**
 688  * Disables offscreen acceleration for this device.  This
 689  * sets a flag in the java object that is used to determine
 690  * whether offscreen surfaces can be created on the device.
 691  */
 692 void AwtWin32GraphicsDevice::DisableOffscreenAcceleration()
 693 {
 694     // REMIND: noop for now
 695 }
 696 





 697 /**
 698  * Invalidates the GraphicsDevice object associated with this
 699  * device by disabling offscreen acceleration and calling
 700  * invalidate(defIndex) on the java object.
 701  */
 702 void AwtWin32GraphicsDevice::Invalidate(JNIEnv *env)
 703 {
 704     int defIndex = AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
 705     DisableOffscreenAcceleration();
 706     jobject javaDevice = GetJavaDevice();
 707     if (!JNU_IsNull(env, javaDevice)) {
 708         JNU_CallMethodByName(env, NULL, javaDevice, "invalidate",
 709                              "(I)V", defIndex);
 710     }
 711 }
 712 
 713 /**
 714  * Static deviceIndex-based methods
 715  *
 716  * The following methods take a deviceIndex for the list of devices


 737  * This function updates the data in the MONITORINFOEX structure pointed to by
 738  * pMonitorInfo for all monitors on the system.  Added for 4654713.
 739  */
 740 void AwtWin32GraphicsDevice::ResetAllMonitorInfo()
 741 {
 742     //IE in some circumstances generates WM_SETTINGCHANGE message on appearance
 743     //and thus triggers this method
 744     //but we may not have the devices list initialized yet.
 745     if (!Devices::GetInstance()){
 746         return;
 747     }
 748     Devices::InstanceAccess devices;
 749     int devicesNum = devices->GetNumDevices();
 750     for (int deviceIndex = 0; deviceIndex < devicesNum; deviceIndex++) {
 751         HMONITOR monitor = devices->GetDevice(deviceIndex)->GetMonitor();
 752         ::GetMonitorInfo(monitor,
 753                          devices->GetDevice(deviceIndex)->pMonitorInfo);
 754     }
 755 }
 756 















 757 void AwtWin32GraphicsDevice::DisableOffscreenAccelerationForDevice(
 758     HMONITOR hMonitor)
 759 {
 760     Devices::InstanceAccess devices;
 761     if (hMonitor == NULL) {
 762         devices->GetDevice(0)->DisableOffscreenAcceleration();
 763     } else {
 764         int devicesNum = devices->GetNumDevices();
 765         for (int i = 0; i < devicesNum; ++i) {
 766             if (devices->GetDevice(i)->GetMonitor() == hMonitor) {
 767                 devices->GetDevice(i)->DisableOffscreenAcceleration();
 768             }
 769         }
 770     }
 771 }
 772 
 773 HMONITOR AwtWin32GraphicsDevice::GetMonitor(int deviceIndex)
 774 {
 775     Devices::InstanceAccess devices;
 776     return devices->GetDevice(deviceIndex)->GetMonitor();


1376     Java_sun_awt_Win32GraphicsDevice_initDevice
1377     (JNIEnv *env, jobject thisPtr, jint screen)
1378 {
1379     Devices::InstanceAccess devices;
1380     devices->GetDevice(screen)->SetJavaDevice(env, thisPtr);
1381 }
1382 
1383 /*
1384  * Class:     sun_awt_Win32GraphicsDevice
1385  * Method:    setNativeScale
1386  * Signature: (I,F,F)V
1387  */
1388 JNIEXPORT void JNICALL
1389     Java_sun_awt_Win32GraphicsDevice_setNativeScale
1390     (JNIEnv *env, jobject thisPtr, jint screen, jfloat scaleX, jfloat scaleY)
1391 {
1392     Devices::InstanceAccess devices;
1393     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1394 
1395     if (device != NULL ) {

1396         device->SetScale(scaleX, scaleY);
1397     }
1398 }
1399 
1400 /*
1401  * Class:     sun_awt_Win32GraphicsDevice
1402  * Method:    getNativeScaleX
1403  * Signature: (I)F
1404  */
1405 JNIEXPORT jfloat JNICALL
1406     Java_sun_awt_Win32GraphicsDevice_getNativeScaleX
1407     (JNIEnv *env, jobject thisPtr, jint screen)
1408 {
1409     Devices::InstanceAccess devices;
1410     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1411     return (device == NULL) ? 1 : device->GetScaleX();
1412 }
1413 
1414 /*
1415  * Class:     sun_awt_Win32GraphicsDevice




  60 jfieldID    AwtWin32GraphicsDevice::indexCMcacheID;
  61 jmethodID   AwtWin32GraphicsDevice::paletteChangedMID;
  62 BOOL        AwtWin32GraphicsDevice::primaryPalettized;
  63 int         AwtWin32GraphicsDevice::primaryIndex = 0;
  64 
  65 
  66 /**
  67  * Construct this device.  Store the screen (index into the devices
  68  * array of this object), the array (used in static references via
  69  * particular device indices), the monitor/pMonitorInfo (which other
  70  * classes will inquire of this device), the bits per pixel of this
  71  * device, and information on whether the primary device is palettized.
  72  */
  73 AwtWin32GraphicsDevice::AwtWin32GraphicsDevice(int screen,
  74                                                HMONITOR mhnd, Devices *arr)
  75 {
  76     this->screen  = screen;
  77     this->devicesArray = arr;
  78     this->scaleX = 1;
  79     this->scaleY = 1;
  80     disableScaleAutoRefresh = FALSE;
  81     javaDevice = NULL;
  82     colorData = new ImgColorData;
  83     colorData->grayscale = GS_NOTGRAY;
  84     palette = NULL;
  85     cData = NULL;
  86     gpBitmapInfo = NULL;
  87     monitor = mhnd;
  88     pMonitorInfo = new MONITORINFOEX;
  89     pMonitorInfo->cbSize = sizeof(MONITORINFOEX);
  90     ::GetMonitorInfo(monitor, pMonitorInfo);
  91 
  92     // Set primary device info: other devices will need to know
  93     // whether the primary is palettized during the initialization
  94     // process
  95     HDC hDC = this->GetDC();
  96     colorData->bitsperpixel = ::GetDeviceCaps(hDC, BITSPIXEL);
  97     this->ReleaseDC(hDC);
  98     if (MONITORINFOF_PRIMARY & pMonitorInfo->dwFlags) {
  99         primaryIndex = screen;
 100         if (colorData->bitsperpixel > 8) {


 617  */
 618 void AwtWin32GraphicsDevice::SetJavaDevice(JNIEnv *env, jobject objPtr)
 619 {
 620     javaDevice = env->NewWeakGlobalRef(objPtr);
 621 }
 622 
 623 /**
 624  * Sets horizontal and vertical scale factors
 625  */
 626 void AwtWin32GraphicsDevice::SetScale(float sx, float sy)
 627 {
 628     scaleX = sx;
 629     scaleY = sy;
 630 }
 631 
 632 int AwtWin32GraphicsDevice::ScaleUpX(int x)
 633 {
 634     return ClipRound(x * scaleX);
 635 }
 636 
 637 int AwtWin32GraphicsDevice::ScaleUpAbsX(int x)
 638 {
 639     LONG screen = pMonitorInfo->rcMonitor.left;
 640     return screen + ClipRound((x - screen) * scaleX);
 641 }
 642 
 643 int AwtWin32GraphicsDevice::ScaleUpY(int y)
 644 {
 645     return ClipRound(y * scaleY);
 646 }
 647 
 648 int AwtWin32GraphicsDevice::ScaleUpAbsY(int y)
 649 {
 650     LONG screen = pMonitorInfo->rcMonitor.top;
 651     return screen + ClipRound((y - screen) * scaleY);
 652 }
 653 
 654 int AwtWin32GraphicsDevice::ScaleDownX(int x)
 655 {
 656     return ClipRound(x / scaleX);
 657 }
 658 
 659 int AwtWin32GraphicsDevice::ScaleDownAbsX(int x)
 660 {
 661     LONG screen = pMonitorInfo->rcMonitor.left;
 662     return screen + ClipRound((x - screen) / scaleX);
 663 }
 664 
 665 int AwtWin32GraphicsDevice::ScaleDownY(int y)
 666 {
 667     return ClipRound(y / scaleY);
 668 }
 669 
 670 int AwtWin32GraphicsDevice::ScaleDownAbsY(int y)
 671 {
 672     LONG screen = pMonitorInfo->rcMonitor.top;
 673     return screen + ClipRound((y - screen) / scaleY);
 674 }
 675 
 676 int AwtWin32GraphicsDevice::ClipRound(double value)
 677 {
 678     value -= 0.5;
 679     if (value < INT_MIN)
 680     {
 681         return INT_MIN;
 682     }
 683 
 684     if (value > INT_MAX)
 685     {
 686         return INT_MAX;
 687     }
 688 
 689     return (int)ceil(value);
 690 }
 691 
 692 void AwtWin32GraphicsDevice::InitDesktopScales() {
 693     if (!disableScaleAutoRefresh) {
 694         float dpiX = -1.0f;
 695         float dpiY = -1.0f;
 696         GetScreenDpi(GetMonitor(), &dpiX, &dpiY);
 697         if (dpiX > 0 && dpiY > 0) {
 698             SetScale(dpiX / 96, dpiY / 96);
 699         }
 700     }
 701 }
 702 
 703 float AwtWin32GraphicsDevice::GetScaleX()
 704 {
 705     return scaleX;
 706 }
 707 
 708 float AwtWin32GraphicsDevice::GetScaleY()
 709 {
 710     return scaleY;
 711 }
 712 
 713 /**
 714  * Disables offscreen acceleration for this device.  This
 715  * sets a flag in the java object that is used to determine
 716  * whether offscreen surfaces can be created on the device.
 717  */
 718 void AwtWin32GraphicsDevice::DisableOffscreenAcceleration()
 719 {
 720     // REMIND: noop for now
 721 }
 722 
 723 void AwtWin32GraphicsDevice::DisableScaleAutoRefresh()
 724 {
 725     disableScaleAutoRefresh = TRUE;
 726 }
 727 
 728 /**
 729  * Invalidates the GraphicsDevice object associated with this
 730  * device by disabling offscreen acceleration and calling
 731  * invalidate(defIndex) on the java object.
 732  */
 733 void AwtWin32GraphicsDevice::Invalidate(JNIEnv *env)
 734 {
 735     int defIndex = AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
 736     DisableOffscreenAcceleration();
 737     jobject javaDevice = GetJavaDevice();
 738     if (!JNU_IsNull(env, javaDevice)) {
 739         JNU_CallMethodByName(env, NULL, javaDevice, "invalidate",
 740                              "(I)V", defIndex);
 741     }
 742 }
 743 
 744 /**
 745  * Static deviceIndex-based methods
 746  *
 747  * The following methods take a deviceIndex for the list of devices


 768  * This function updates the data in the MONITORINFOEX structure pointed to by
 769  * pMonitorInfo for all monitors on the system.  Added for 4654713.
 770  */
 771 void AwtWin32GraphicsDevice::ResetAllMonitorInfo()
 772 {
 773     //IE in some circumstances generates WM_SETTINGCHANGE message on appearance
 774     //and thus triggers this method
 775     //but we may not have the devices list initialized yet.
 776     if (!Devices::GetInstance()){
 777         return;
 778     }
 779     Devices::InstanceAccess devices;
 780     int devicesNum = devices->GetNumDevices();
 781     for (int deviceIndex = 0; deviceIndex < devicesNum; deviceIndex++) {
 782         HMONITOR monitor = devices->GetDevice(deviceIndex)->GetMonitor();
 783         ::GetMonitorInfo(monitor,
 784                          devices->GetDevice(deviceIndex)->pMonitorInfo);
 785     }
 786 }
 787 
 788 /**
 789  * This function updates the scale factor for all monitors on the system.
 790  */
 791 void AwtWin32GraphicsDevice::ResetAllDesktopScales()
 792 {
 793     if (!Devices::GetInstance()){
 794         return;
 795     }
 796     Devices::InstanceAccess devices;
 797     int devicesNum = devices->GetNumDevices();
 798     for (int deviceIndex = 0; deviceIndex < devicesNum; deviceIndex++) {
 799         devices->GetDevice(deviceIndex)->InitDesktopScales();
 800     }
 801 }
 802 
 803 void AwtWin32GraphicsDevice::DisableOffscreenAccelerationForDevice(
 804     HMONITOR hMonitor)
 805 {
 806     Devices::InstanceAccess devices;
 807     if (hMonitor == NULL) {
 808         devices->GetDevice(0)->DisableOffscreenAcceleration();
 809     } else {
 810         int devicesNum = devices->GetNumDevices();
 811         for (int i = 0; i < devicesNum; ++i) {
 812             if (devices->GetDevice(i)->GetMonitor() == hMonitor) {
 813                 devices->GetDevice(i)->DisableOffscreenAcceleration();
 814             }
 815         }
 816     }
 817 }
 818 
 819 HMONITOR AwtWin32GraphicsDevice::GetMonitor(int deviceIndex)
 820 {
 821     Devices::InstanceAccess devices;
 822     return devices->GetDevice(deviceIndex)->GetMonitor();


1422     Java_sun_awt_Win32GraphicsDevice_initDevice
1423     (JNIEnv *env, jobject thisPtr, jint screen)
1424 {
1425     Devices::InstanceAccess devices;
1426     devices->GetDevice(screen)->SetJavaDevice(env, thisPtr);
1427 }
1428 
1429 /*
1430  * Class:     sun_awt_Win32GraphicsDevice
1431  * Method:    setNativeScale
1432  * Signature: (I,F,F)V
1433  */
1434 JNIEXPORT void JNICALL
1435     Java_sun_awt_Win32GraphicsDevice_setNativeScale
1436     (JNIEnv *env, jobject thisPtr, jint screen, jfloat scaleX, jfloat scaleY)
1437 {
1438     Devices::InstanceAccess devices;
1439     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1440 
1441     if (device != NULL ) {
1442         device->DisableScaleAutoRefresh();
1443         device->SetScale(scaleX, scaleY);
1444     }
1445 }
1446 
1447 /*
1448  * Class:     sun_awt_Win32GraphicsDevice
1449  * Method:    getNativeScaleX
1450  * Signature: (I)F
1451  */
1452 JNIEXPORT jfloat JNICALL
1453     Java_sun_awt_Win32GraphicsDevice_getNativeScaleX
1454     (JNIEnv *env, jobject thisPtr, jint screen)
1455 {
1456     Devices::InstanceAccess devices;
1457     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
1458     return (device == NULL) ? 1 : device->GetScaleX();
1459 }
1460 
1461 /*
1462  * Class:     sun_awt_Win32GraphicsDevice


< prev index next >