< prev index next >
src/java.desktop/macosx/native/libawt_lwawt/awt/CGraphicsDevice.m
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 23,35 ****
--- 23,43 ----
* questions.
*/
#import "LWCToolkit.h"
#import "ThreadUtilities.h"
+ #include "GeomUtilities.h"
#import <JavaNativeFoundation/JavaNativeFoundation.h>
+ /**
+ * Some default values for invalid CoreGraphics display ID.
+ */
+ #define DEFAULT_DEVICE_WIDTH 1024
+ #define DEFAULT_DEVICE_HEIGHT 768
+ #define DEFAULT_DEVICE_DPI 72
+
/*
* Convert the mode string to the more convinient bits per pixel value
*/
static int getBPPFromModeString(CFStringRef mode)
{
*** 54,103 ****
static BOOL isValidDisplayMode(CGDisplayModeRef mode){
return (1 < CGDisplayModeGetWidth(mode) && 1 < CGDisplayModeGetHeight(mode));
}
static CFMutableArrayRef getAllValidDisplayModes(jint displayID){
CFArrayRef allModes = CGDisplayCopyAllDisplayModes(displayID, NULL);
!
CFIndex numModes = CFArrayGetCount(allModes);
! CFMutableArrayRef validModes = CFArrayCreateMutable(kCFAllocatorDefault, numModes + 1, &kCFTypeArrayCallBacks);
CFIndex n;
for (n=0; n < numModes; n++) {
CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
if (cRef != NULL && isValidDisplayMode(cRef)) {
CFArrayAppendValue(validModes, cRef);
}
}
CFRelease(allModes);
CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
!
BOOL containsCurrentMode = NO;
numModes = CFArrayGetCount(validModes);
for (n=0; n < numModes; n++) {
if(CFArrayGetValueAtIndex(validModes, n) == currentMode){
containsCurrentMode = YES;
break;
}
}
-
if (!containsCurrentMode) {
CFArrayAppendValue(validModes, currentMode);
}
CGDisplayModeRelease(currentMode);
return validModes;
}
/*
* Find the best possible match in the list of display modes that we can switch to based on
* the provided parameters.
*/
static CGDisplayModeRef getBestModeForParameters(CFArrayRef allModes, int w, int h, int bpp, int refrate) {
CGDisplayModeRef bestGuess = NULL;
! CFIndex numModes = CFArrayGetCount(allModes), n;
int thisBpp = 0;
for(n = 0; n < numModes; n++ ) {
CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
if(cRef == NULL) {
continue;
--- 62,115 ----
static BOOL isValidDisplayMode(CGDisplayModeRef mode){
return (1 < CGDisplayModeGetWidth(mode) && 1 < CGDisplayModeGetHeight(mode));
}
static CFMutableArrayRef getAllValidDisplayModes(jint displayID){
+ // CGDisplayCopyAllDisplayModes can return NULL if displayID is invalid
CFArrayRef allModes = CGDisplayCopyAllDisplayModes(displayID, NULL);
! CFMutableArrayRef validModes = nil;
! if (allModes) {
CFIndex numModes = CFArrayGetCount(allModes);
! validModes = CFArrayCreateMutable(kCFAllocatorDefault, numModes + 1, &kCFTypeArrayCallBacks);
CFIndex n;
for (n=0; n < numModes; n++) {
CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
if (cRef != NULL && isValidDisplayMode(cRef)) {
CFArrayAppendValue(validModes, cRef);
}
}
CFRelease(allModes);
+ // CGDisplayCopyDisplayMode can return NULL if displayID is invalid
CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
! if (currentMode) {
BOOL containsCurrentMode = NO;
numModes = CFArrayGetCount(validModes);
for (n=0; n < numModes; n++) {
if(CFArrayGetValueAtIndex(validModes, n) == currentMode){
containsCurrentMode = YES;
break;
}
}
if (!containsCurrentMode) {
CFArrayAppendValue(validModes, currentMode);
}
CGDisplayModeRelease(currentMode);
+ }
+ }
return validModes;
}
/*
* Find the best possible match in the list of display modes that we can switch to based on
* the provided parameters.
*/
static CGDisplayModeRef getBestModeForParameters(CFArrayRef allModes, int w, int h, int bpp, int refrate) {
CGDisplayModeRef bestGuess = NULL;
! CFIndex numModes = allModes ? CFArrayGetCount(allModes) : 0, n;
int thisBpp = 0;
for(n = 0; n < numModes; n++ ) {
CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
if(cRef == NULL) {
continue;
*** 127,148 ****
}
return bestGuess;
}
/*
! * Create a new java.awt.DisplayMode instance based on provided CGDisplayModeRef
*/
! static jobject createJavaDisplayMode(CGDisplayModeRef mode, JNIEnv *env, jint displayID) {
jobject ret = NULL;
! jint h, w, bpp, refrate;
JNF_COCOA_ENTER(env);
CFStringRef currentBPP = CGDisplayModeCopyPixelEncoding(mode);
bpp = getBPPFromModeString(currentBPP);
refrate = CGDisplayModeGetRefreshRate(mode);
h = CGDisplayModeGetHeight(mode);
w = CGDisplayModeGetWidth(mode);
CFRelease(currentBPP);
static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
static JNF_CTOR_CACHE(jc_DisplayMode_ctor, jc_DisplayMode, "(IIII)V");
ret = JNFNewObject(env, jc_DisplayMode_ctor, w, h, bpp, refrate);
JNF_COCOA_EXIT(env);
return ret;
--- 139,163 ----
}
return bestGuess;
}
/*
! * Create a new java.awt.DisplayMode instance based on provided
! * CGDisplayModeRef, if CGDisplayModeRef is NULL, then some stub is returned.
*/
! static jobject createJavaDisplayMode(CGDisplayModeRef mode, JNIEnv *env) {
jobject ret = NULL;
! jint h = DEFAULT_DEVICE_HEIGHT, w = DEFAULT_DEVICE_WIDTH, bpp = 0, refrate = 0;
JNF_COCOA_ENTER(env);
+ if (mode) {
CFStringRef currentBPP = CGDisplayModeCopyPixelEncoding(mode);
bpp = getBPPFromModeString(currentBPP);
refrate = CGDisplayModeGetRefreshRate(mode);
h = CGDisplayModeGetHeight(mode);
w = CGDisplayModeGetWidth(mode);
CFRelease(currentBPP);
+ }
static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
static JNF_CTOR_CACHE(jc_DisplayMode_ctor, jc_DisplayMode, "(IIII)V");
ret = JNFNewObject(env, jc_DisplayMode_ctor, w, h, bpp, refrate);
JNF_COCOA_EXIT(env);
return ret;
*** 161,171 ****
// CGDisplayScreenSize can return 0 if displayID is invalid
CGSize size = CGDisplayScreenSize(displayID);
CGRect rect = CGDisplayBounds(displayID);
// 1 inch == 25.4 mm
jfloat inches = size.width / 25.4f;
! return inches > 0 ? rect.size.width / inches : 72;
}
/*
* Class: sun_awt_CGraphicsDevice
* Method: nativeGetYResolution
--- 176,186 ----
// CGDisplayScreenSize can return 0 if displayID is invalid
CGSize size = CGDisplayScreenSize(displayID);
CGRect rect = CGDisplayBounds(displayID);
// 1 inch == 25.4 mm
jfloat inches = size.width / 25.4f;
! return inches > 0 ? rect.size.width / inches : DEFAULT_DEVICE_DPI;
}
/*
* Class: sun_awt_CGraphicsDevice
* Method: nativeGetYResolution
*** 178,188 ****
// CGDisplayScreenSize can return 0 if displayID is invalid
CGSize size = CGDisplayScreenSize(displayID);
CGRect rect = CGDisplayBounds(displayID);
// 1 inch == 25.4 mm
jfloat inches = size.height / 25.4f;
! return inches > 0 ? rect.size.height / inches : 72;
}
/*
* Class: sun_awt_CGraphicsDevice
* Method: nativeGetScreenInsets
--- 193,222 ----
// CGDisplayScreenSize can return 0 if displayID is invalid
CGSize size = CGDisplayScreenSize(displayID);
CGRect rect = CGDisplayBounds(displayID);
// 1 inch == 25.4 mm
jfloat inches = size.height / 25.4f;
! return inches > 0 ? rect.size.height / inches : DEFAULT_DEVICE_DPI;
! }
!
! /*
! * Class: sun_awt_CGraphicsDevice
! * Method: nativeGetBounds
! * Signature: (I)Ljava/awt/Rectangle;
! */
! JNIEXPORT jobject JNICALL
! Java_sun_awt_CGraphicsDevice_nativeGetBounds
! (JNIEnv *env, jclass class, jint displayID)
! {
! CGRect rect = CGDisplayBounds(displayID);
! if (rect.size.width == 0) {
! rect.size.width = DEFAULT_DEVICE_WIDTH;
! }
! if (rect.size.height == 0) {
! rect.size.height = DEFAULT_DEVICE_HEIGHT;
! }
! return CGToJavaRect(env, rect);
}
/*
* Class: sun_awt_CGraphicsDevice
* Method: nativeGetScreenInsets
*** 267,278 ****
JNIEXPORT jobject JNICALL
Java_sun_awt_CGraphicsDevice_nativeGetDisplayMode
(JNIEnv *env, jclass class, jint displayID)
{
jobject ret = NULL;
CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
! ret = createJavaDisplayMode(currentMode, env, displayID);
CGDisplayModeRelease(currentMode);
return ret;
}
/*
--- 301,313 ----
JNIEXPORT jobject JNICALL
Java_sun_awt_CGraphicsDevice_nativeGetDisplayMode
(JNIEnv *env, jclass class, jint displayID)
{
jobject ret = NULL;
+ // CGDisplayCopyDisplayMode can return NULL if displayID is invalid
CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
! ret = createJavaDisplayMode(currentMode, env);
CGDisplayModeRelease(currentMode);
return ret;
}
/*
*** 286,296 ****
{
jobjectArray jreturnArray = NULL;
JNF_COCOA_ENTER(env);
CFArrayRef allModes = getAllValidDisplayModes(displayID);
! CFIndex numModes = CFArrayGetCount(allModes);
static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
jreturnArray = JNFNewObjectArray(env, &jc_DisplayMode, (jsize) numModes);
if (!jreturnArray) {
NSLog(@"CGraphicsDevice can't create java array of DisplayMode objects");
--- 321,331 ----
{
jobjectArray jreturnArray = NULL;
JNF_COCOA_ENTER(env);
CFArrayRef allModes = getAllValidDisplayModes(displayID);
! CFIndex numModes = allModes ? CFArrayGetCount(allModes): 0;
static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
jreturnArray = JNFNewObjectArray(env, &jc_DisplayMode, (jsize) numModes);
if (!jreturnArray) {
NSLog(@"CGraphicsDevice can't create java array of DisplayMode objects");
*** 299,319 ****
CFIndex n;
for (n=0; n < numModes; n++) {
CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
if (cRef != NULL) {
! jobject oneMode = createJavaDisplayMode(cRef, env, displayID);
(*env)->SetObjectArrayElement(env, jreturnArray, n, oneMode);
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
continue;
}
(*env)->DeleteLocalRef(env, oneMode);
}
}
CFRelease(allModes);
JNF_COCOA_EXIT(env);
return jreturnArray;
}
--- 334,356 ----
CFIndex n;
for (n=0; n < numModes; n++) {
CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
if (cRef != NULL) {
! jobject oneMode = createJavaDisplayMode(cRef, env);
(*env)->SetObjectArrayElement(env, jreturnArray, n, oneMode);
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
continue;
}
(*env)->DeleteLocalRef(env, oneMode);
}
}
+ if (allModes) {
CFRelease(allModes);
+ }
JNF_COCOA_EXIT(env);
return jreturnArray;
}
< prev index next >