Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/awt/awt_DrawingSurface.c
32287 views
/*1* Copyright (c) 1996, 2014, 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/*26#ifdef HEADLESS27#error This file should not be included in headless library28#endif29*/3031#ifdef __ANDROID__32# include "awt.h"33#endif34#include "awt_p.h"35#include "java_awt_Component.h"3637#include "awt_Component.h"3839#include <jni.h>40#include <jni_util.h>41#include <jawt_md.h>4243#include "awt_GraphicsEnv.h"444546// FIXME awt_TopLevel.c not found47#ifndef __ANDROID__48extern struct ComponentIDs componentIDs;4950extern jfieldID windowID;51extern jfieldID targetID;52extern jfieldID graphicsConfigID;53extern jfieldID drawStateID;54extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;55#else56struct ComponentIDs componentIDs;5758jfieldID windowID;59jfieldID targetID;60jfieldID graphicsConfigID;61jfieldID drawStateID;62struct X11GraphicsConfigIDs x11GraphicsConfigIDs;63#endif6465/*66* Lock the surface of the target component for native rendering.67* When finished drawing, the surface must be unlocked with68* Unlock(). This function returns a bitmask with one or more of the69* following values:70*71* JAWT_LOCK_ERROR - When an error has occurred and the surface could not72* be locked.73*74* JAWT_LOCK_CLIP_CHANGED - When the clip region has changed.75*76* JAWT_LOCK_BOUNDS_CHANGED - When the bounds of the surface have changed.77*78* JAWT_LOCK_SURFACE_CHANGED - When the surface itself has changed79*/80JNIEXPORT jint JNICALL awt_DrawingSurface_Lock(JAWT_DrawingSurface* ds)81{82JNIEnv* env;83jobject target, peer;84jclass componentClass;85jint drawState;8687if (ds == NULL) {88#ifdef DEBUG89fprintf(stderr, "Drawing Surface is NULL\n");90#endif91return (jint)JAWT_LOCK_ERROR;92}93env = ds->env;94target = ds->target;9596/* Make sure the target is a java.awt.Component */97componentClass = (*env)->FindClass(env, "java/awt/Component");98CHECK_NULL_RETURN(componentClass, (jint)JAWT_LOCK_ERROR);99100if (!(*env)->IsInstanceOf(env, target, componentClass)) {101#ifdef DEBUG102fprintf(stderr, "Target is not a component\n");103#endif104return (jint)JAWT_LOCK_ERROR;105}106107if (!awtLockInited) {108return (jint)JAWT_LOCK_ERROR;109}110AWT_LOCK();111112/* Get the peer of the target component */113peer = (*env)->GetObjectField(env, target, componentIDs.peer);114if (JNU_IsNull(env, peer)) {115#ifdef DEBUG116fprintf(stderr, "Component peer is NULL\n");117#endif118AWT_FLUSH_UNLOCK();119return (jint)JAWT_LOCK_ERROR;120}121122drawState = (*env)->GetIntField(env, peer, drawStateID);123(*env)->SetIntField(env, peer, drawStateID, 0);124return drawState;125}126127JNIEXPORT int32_t JNICALL128awt_GetColor(JAWT_DrawingSurface* ds, int32_t r, int32_t g, int32_t b)129{130JNIEnv* env;131jobject target, peer;132jclass componentClass;133AwtGraphicsConfigDataPtr adata;134int32_t result;135jobject gc_object;136if (ds == NULL) {137#ifdef DEBUG138fprintf(stderr, "Drawing Surface is NULL\n");139#endif140return (int32_t) 0;141}142143env = ds->env;144target = ds->target;145146/* Make sure the target is a java.awt.Component */147componentClass = (*env)->FindClass(env, "java/awt/Component");148CHECK_NULL_RETURN(componentClass, (int32_t) 0);149150if (!(*env)->IsInstanceOf(env, target, componentClass)) {151#ifdef DEBUG152fprintf(stderr, "DrawingSurface target must be a component\n");153#endif154return (int32_t) 0;155}156157if (!awtLockInited) {158return (int32_t) 0;159}160161AWT_LOCK();162163/* Get the peer of the target component */164peer = (*env)->GetObjectField(env, target, componentIDs.peer);165if (JNU_IsNull(env, peer)) {166#ifdef DEBUG167fprintf(stderr, "Component peer is NULL\n");168#endif169AWT_UNLOCK();170return (int32_t) 0;171}172/* GraphicsConfiguration object of MComponentPeer */173gc_object = (*env)->GetObjectField(env, peer, graphicsConfigID);174175if (gc_object != NULL) {176adata = (AwtGraphicsConfigDataPtr)177JNU_GetLongFieldAsPtr(env, gc_object,178x11GraphicsConfigIDs.aData);179} else {180#ifndef __ANDROID__181adata = getDefaultConfig(DefaultScreen(awt_display));182#else183adata = getDefaultConfig(0);184#endif185}186187result = adata->AwtColorMatch(r, g, b, adata);188AWT_UNLOCK();189return result;190}191192/*193* Get the drawing surface info.194* The value returned may be cached, but the values may change if195* additional calls to Lock() or Unlock() are made.196* Lock() must be called before this can return a valid value.197* Returns NULL if an error has occurred.198* When finished with the returned value, FreeDrawingSurfaceInfo must be199* called.200*/201JNIEXPORT JAWT_DrawingSurfaceInfo* JNICALL202awt_DrawingSurface_GetDrawingSurfaceInfo(JAWT_DrawingSurface* ds)203{204JNIEnv* env;205jobject target, peer;206jclass componentClass;207JAWT_X11DrawingSurfaceInfo* px;208JAWT_DrawingSurfaceInfo* p;209XWindowAttributes attrs;210211if (ds == NULL) {212#ifdef DEBUG213fprintf(stderr, "Drawing Surface is NULL\n");214#endif215return NULL;216}217218env = ds->env;219target = ds->target;220221/* Make sure the target is a java.awt.Component */222componentClass = (*env)->FindClass(env, "java/awt/Component");223CHECK_NULL_RETURN(componentClass, NULL);224225if (!(*env)->IsInstanceOf(env, target, componentClass)) {226#ifdef DEBUG227fprintf(stderr, "DrawingSurface target must be a component\n");228#endif229return NULL;230}231232if (!awtLockInited) {233return NULL;234}235236AWT_LOCK();237238/* Get the peer of the target component */239peer = (*env)->GetObjectField(env, target, componentIDs.peer);240if (JNU_IsNull(env, peer)) {241#ifdef DEBUG242fprintf(stderr, "Component peer is NULL\n");243#endif244AWT_UNLOCK();245return NULL;246}247248AWT_UNLOCK();249250/* Allocate platform-specific data */251px = (JAWT_X11DrawingSurfaceInfo*)252malloc(sizeof(JAWT_X11DrawingSurfaceInfo));253254/* Set drawable and display */255px->drawable = (*env)->GetLongField(env, peer, windowID);256/*257#ifdef __ANDROID__258Display fake_awt_display;259awt_display = &fake_awt_display;260awt_display->proto_major_version = 11;261awt_display->proto_minor_version = 7;262awt_display->vendor = "Android Xlib";263#endif264*/265px->display = awt_display;266267/* Get window attributes to set other values */268#if !defined(__ANDROID__) && !defined(HEADLESS)269XGetWindowAttributes(awt_display, (Window)(px->drawable), &attrs);270271px->visualID = XVisualIDFromVisual(attrs.visual);272#else273px->visualID = TrueColor;274attrs.colormap = 1; // FIXME!275attrs.depth = 24;276#endif277278/* Set the other values */279px->colormapID = attrs.colormap;280px->depth = attrs.depth;281px->GetAWTColor = awt_GetColor;282283/* Allocate and initialize platform-independent data */284p = (JAWT_DrawingSurfaceInfo*)malloc(sizeof(JAWT_DrawingSurfaceInfo));285p->platformInfo = px;286p->ds = ds;287p->bounds.x = (*env)->GetIntField(env, target, componentIDs.x);288p->bounds.y = (*env)->GetIntField(env, target, componentIDs.y);289p->bounds.width = (*env)->GetIntField(env, target, componentIDs.width);290p->bounds.height = (*env)->GetIntField(env, target, componentIDs.height);291p->clipSize = 1;292p->clip = &(p->bounds);293294/* Return our new structure */295return p;296}297298/*299* Free the drawing surface info.300*/301JNIEXPORT void JNICALL302awt_DrawingSurface_FreeDrawingSurfaceInfo(JAWT_DrawingSurfaceInfo* dsi)303{304if (dsi == NULL ) {305#ifdef DEBUG306fprintf(stderr, "Drawing Surface Info is NULL\n");307#endif308return;309}310free(dsi->platformInfo);311free(dsi);312}313314/*315* Unlock the drawing surface of the target component for native rendering.316*/317JNIEXPORT void JNICALL awt_DrawingSurface_Unlock(JAWT_DrawingSurface* ds)318{319JNIEnv* env;320if (ds == NULL) {321#ifdef DEBUG322fprintf(stderr, "Drawing Surface is NULL\n");323#endif324return;325}326env = ds->env;327AWT_FLUSH_UNLOCK();328}329330JNIEXPORT JAWT_DrawingSurface* JNICALL331awt_GetDrawingSurface(JNIEnv* env, jobject target)332{333jclass componentClass;334JAWT_DrawingSurface* p;335336/* Make sure the target component is a java.awt.Component */337componentClass = (*env)->FindClass(env, "java/awt/Component");338CHECK_NULL_RETURN(componentClass, NULL);339340if (!(*env)->IsInstanceOf(env, target, componentClass)) {341#ifdef DEBUG342fprintf(stderr,343"GetDrawingSurface target must be a java.awt.Component\n");344#endif345return NULL;346}347348p = (JAWT_DrawingSurface*)malloc(sizeof(JAWT_DrawingSurface));349p->env = env;350p->target = (*env)->NewGlobalRef(env, target);351p->Lock = awt_DrawingSurface_Lock;352p->GetDrawingSurfaceInfo = awt_DrawingSurface_GetDrawingSurfaceInfo;353p->FreeDrawingSurfaceInfo = awt_DrawingSurface_FreeDrawingSurfaceInfo;354p->Unlock = awt_DrawingSurface_Unlock;355return p;356}357358JNIEXPORT void JNICALL359awt_FreeDrawingSurface(JAWT_DrawingSurface* ds)360{361JNIEnv* env;362363if (ds == NULL ) {364#ifdef DEBUG365fprintf(stderr, "Drawing Surface is NULL\n");366#endif367return;368}369env = ds->env;370(*env)->DeleteGlobalRef(env, ds->target);371free(ds);372}373374JNIEXPORT void JNICALL375awt_Lock(JNIEnv* env)376{377if (awtLockInited) {378AWT_LOCK();379}380}381382JNIEXPORT void JNICALL383awt_Unlock(JNIEnv* env)384{385if (awtLockInited) {386AWT_FLUSH_UNLOCK();387}388}389390JNIEXPORT jobject JNICALL391awt_GetComponent(JNIEnv* env, void* platformInfo)392{393Window window = (Window)platformInfo;394jobject peer = NULL;395jobject target = NULL;396397AWT_LOCK();398399if (window != None) {400peer = JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XToolkit",401"windowToXWindow", "(J)Lsun/awt/X11/XBaseWindow;", (jlong)window).l;402if ((*env)->ExceptionCheck(env)) {403AWT_UNLOCK();404return (jobject)NULL;405}406}407if ((peer != NULL) &&408(JNU_IsInstanceOfByName(env, peer, "sun/awt/X11/XWindow") == 1)) {409target = (*env)->GetObjectField(env, peer, targetID);410}411412if (target == NULL) {413(*env)->ExceptionClear(env);414JNU_ThrowNullPointerException(env, "NullPointerException");415AWT_UNLOCK();416return (jobject)NULL;417}418419AWT_UNLOCK();420421return target;422}423424425