< 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     static float scale = -2.0f;
 665     if (scale == -2) {
 666         scale = -1;
 667         char *uiScale = getenv("J2D_UISCALE");
 668         if (uiScale != NULL) {
 669             scale = (float)strtod(uiScale, NULL);
 670             if (errno == ERANGE || scale <= 0) {
 671                 scale = -1;
 672             }
 673         }
 674     }
 675 
 676     if (scale > 0) {
 677         SetScale(scale, scale);
 678         return;
 679     }
 680 
 681     typedef HRESULT(WINAPI GetDpiForMonitorFunc)(HMONITOR, int, UINT*, UINT*);
 682     static HMODULE hLibSHCoreDll = NULL;
 683     static GetDpiForMonitorFunc *lpGetDpiForMonitor = NULL;
 684 
 685     if (hLibSHCoreDll == NULL) {
 686         hLibSHCoreDll = JDK_LoadSystemLibrary("shcore.dll");
 687         if (hLibSHCoreDll != NULL) {
 688             lpGetDpiForMonitor = (GetDpiForMonitorFunc*)GetProcAddress(
 689                 hLibSHCoreDll, "GetDpiForMonitor");
 690         }
 691     }
 692 
 693     if (lpGetDpiForMonitor != NULL) {
 694         HRESULT hResult = lpGetDpiForMonitor(GetMonitor(),
 695                                              MDT_Effective_DPI, &x, &y);
 696         if (hResult == S_OK) {
 697             dpiX = static_cast<float>(x);
 698             dpiY = static_cast<float>(y);
 699         }
 700     } else {
 701         ID2D1Factory* m_pDirect2dFactory;
 702         HRESULT res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
 703                                         &m_pDirect2dFactory);
 704         if (res == S_OK) {
 705             m_pDirect2dFactory->GetDesktopDpi(&dpiX, &dpiY);
 706             m_pDirect2dFactory->Release();
 707         }
 708     }
 709 
 710     if (dpiX > 0 && dpiY > 0) {
 711         SetScale(dpiX / 96, dpiY / 96);
 712     }
 713 }
 714 
 715 float AwtWin32GraphicsDevice::GetScaleX()
 716 {
 717     return scaleX;
 718 }
 719 
 720 float AwtWin32GraphicsDevice::GetScaleY()
 721 {
 722     return scaleY;
 723 }
 724 
 725 /**
 726  * Disables offscreen acceleration for this device.  This
 727  * sets a flag in the java object that is used to determine
 728  * whether offscreen surfaces can be created on the device.
 729  */
 730 void AwtWin32GraphicsDevice::DisableOffscreenAcceleration()
 731 {
 732     // REMIND: noop for now
 733 }
 734 
 735 /**
 736  * Invalidates the GraphicsDevice object associated with this
 737  * device by disabling offscreen acceleration and calling
 738  * invalidate(defIndex) on the java object.
 739  */
 740 void AwtWin32GraphicsDevice::Invalidate(JNIEnv *env)
 741 {
 742     int defIndex = AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
 743     DisableOffscreenAcceleration();
 744     jobject javaDevice = GetJavaDevice();
 745     if (!JNU_IsNull(env, javaDevice)) {


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