Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CGraphicsEnv.m
38829 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import <JavaNativeFoundation/JavaNativeFoundation.h>2627#import "jni_util.h"28#import "ThreadUtilities.h"29#import "LWCToolkit.h"30#import "AWT_debug.h"313233/*34* Class: sun_awt_CGraphicsEnvironment35* Method: initCocoa36* Signature: ()V37*/38JNIEXPORT void JNICALL39Java_sun_awt_CGraphicsEnvironment_initCocoa40(JNIEnv *env, jclass self)41{42JNF_COCOA_ENTER(env);4344// Inform Cocoa that we're multi-threaded.45// Creating a short-lived NSThread is the recommended way of doing so.46[NSThread detachNewThreadSelector:@selector(self) toTarget:[NSObject class] withObject:nil];4748JNF_COCOA_EXIT(env);49}5051#define MAX_DISPLAYS 645253/*54* Class: sun_awt_CGraphicsEnvironment55* Method: getDisplayIDs56* Signature: ()[I57*/58JNIEXPORT jintArray JNICALL59Java_sun_awt_CGraphicsEnvironment_getDisplayIDs60(JNIEnv *env, jclass class)61{62jintArray ret = NULL;6364JNF_COCOA_ENTER(env);6566/* Get the count */67CGDisplayCount displayCount;68if (CGGetOnlineDisplayList(MAX_DISPLAYS, NULL, &displayCount) != kCGErrorSuccess) {69[JNFException raise:env70as:kInternalError71reason:"CGGetOnlineDisplayList() failed to get display count"];72return NULL;73}7475/* Allocate an array and get the size list of display Ids */76CGDirectDisplayID displays[MAX_DISPLAYS];77if (CGGetOnlineDisplayList(displayCount, displays, &displayCount) != kCGErrorSuccess) {78[JNFException raise:env79as:kInternalError80reason:"CGGetOnlineDisplayList() failed to get display list"];81return NULL;82}8384CGDisplayCount i;85CGDisplayCount displayActiveCount = 0; //Active and sleeping.86for (i = 0; i < displayCount; ++i) {87if (CGDisplayMirrorsDisplay(displays[i]) == kCGNullDirectDisplay) {88++displayActiveCount;89} else {90displays[i] = kCGNullDirectDisplay;91}92}9394/* Allocate a java array for display identifiers */95ret = JNFNewIntArray(env, displayActiveCount);9697/* Initialize and return the backing int array */98assert(sizeof(jint) >= sizeof(CGDirectDisplayID));99jint *elems = (*env)->GetIntArrayElements(env, ret, 0);100CHECK_NULL_RETURN(elems, NULL);101102/* Filter out the mirrored displays */103for (i = 0; i < displayCount; ++i) {104if (displays[i] != kCGNullDirectDisplay) {105elems[--displayActiveCount] = displays[i];106}107}108109(*env)->ReleaseIntArrayElements(env, ret, elems, 0);110111JNF_COCOA_EXIT(env);112113return ret;114}115116/*117* Class: sun_awt_CGraphicsEnvironment118* Method: getMainDisplayID119* Signature: ()I120*/121JNIEXPORT jint JNICALL122Java_sun_awt_CGraphicsEnvironment_getMainDisplayID123(JNIEnv *env, jclass class)124{125return CGMainDisplayID();126}127128/*129* Post the display reconfiguration event.130*/131static void displaycb_handle132(CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo)133{134if (flags == kCGDisplayBeginConfigurationFlag) return;135136[ThreadUtilities performOnMainThreadWaiting:NO block:^() {137138JNFPerformEnvBlock(JNFThreadDetachImmediately, ^(JNIEnv *env) {139JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)userInfo;140141jobject graphicsEnv = [wrapper jObjectWithEnv:env];142if (graphicsEnv == NULL) return; // ref already GC'd143static JNF_CLASS_CACHE(jc_CGraphicsEnvironment, "sun/awt/CGraphicsEnvironment");144static JNF_MEMBER_CACHE(jm_displayReconfiguration,145jc_CGraphicsEnvironment, "_displayReconfiguration","(IZ)V");146JNFCallVoidMethod(env, graphicsEnv, jm_displayReconfiguration,147(jint) display, (jboolean) flags & kCGDisplayRemoveFlag);148(*env)->DeleteLocalRef(env, graphicsEnv);149});150}];151}152153/*154* Class: sun_awt_CGraphicsEnvironment155* Method: registerDisplayReconfiguration156* Signature: ()J157*/158JNIEXPORT jlong JNICALL159Java_sun_awt_CGraphicsEnvironment_registerDisplayReconfiguration160(JNIEnv *env, jobject this)161{162jlong ret = 0L;163164JNF_COCOA_ENTER(env);165166JNFWeakJObjectWrapper *wrapper = [[JNFWeakJObjectWrapper wrapperWithJObject:this withEnv:env] retain];167168/* Register the callback */169if (CGDisplayRegisterReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {170[JNFException raise:env171as:kInternalError172reason:"CGDisplayRegisterReconfigurationCallback() failed"];173return 0L;174}175176ret = ptr_to_jlong(wrapper);177178JNF_COCOA_EXIT(env);179180return ret;181}182183/*184* Class: sun_awt_CGraphicsEnvironment185* Method: deregisterDisplayReconfiguration186* Signature: (J)V187*/188JNIEXPORT void JNICALL189Java_sun_awt_CGraphicsEnvironment_deregisterDisplayReconfiguration190(JNIEnv *env, jobject this, jlong p)191{192JNF_COCOA_ENTER(env);193194JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)jlong_to_ptr(p);195if (!wrapper) return;196197/* Remove the registration */198if (CGDisplayRemoveReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {199[JNFException raise:env200as:kInternalError201reason:"CGDisplayRemoveReconfigurationCallback() failed, leaking the callback context!"];202return;203}204205[wrapper setJObject:NULL withEnv:env]; // more efficient to pre-clear206[wrapper release];207208JNF_COCOA_EXIT(env);209}210211212