Path: blob/master/src/java.desktop/unix/native/libawt/awt/awt_Mlib.c
41152 views
/*1* Copyright (c) 1998, 2020, 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#include <stdlib.h>26#include <string.h>27#include <sys/time.h>28#include <sys/utsname.h>29#include <sys/types.h>30#include <errno.h>31#include <dlfcn.h>32#include "jni.h"33#include <jni_util.h>34#include "jvm_md.h"35#include "awt_Mlib.h"36#include "java_awt_image_BufferedImage.h"3738#ifdef STATIC_BUILD39#include "mlib_image.h"40#endif4142static void start_timer(int numsec);43static void stop_timer(int numsec, int ntimes);4445#ifdef STATIC_BUILD46// Mapping functions to their names for runtime check47static mlibFnS_t sMlibFnsStatic[] = {48{j2d_mlib_ImageConvMxN, "j2d_mlib_ImageConvMxN"},49{j2d_mlib_ImageAffine, "j2d_mlib_ImageAffine"},50{j2d_mlib_ImageLookUp, "j2d_mlib_ImageLookUp"},51{j2d_mlib_ImageConvKernelConvert, "j2d_mlib_ImageConvKernelConvert"},52};5354mlib_status awt_getImagingLib(JNIEnv *env, mlibFnS_t *sMlibFns,55mlibSysFnS_t *sMlibSysFns) {56mlibFnS_t *mptr;57int i;58char *fName;59mlibSysFnS_t tempSysFns;60mlib_status ret = MLIB_SUCCESS;6162tempSysFns.createFP = j2d_mlib_ImageCreate;63tempSysFns.createStructFP = j2d_mlib_ImageCreateStruct;64tempSysFns.deleteImageFP = j2d_mlib_ImageDelete;65*sMlibSysFns = tempSysFns;6667mptr = sMlibFns;68i = 0;69while (mptr[i].fname != NULL) {70fName = mptr[i].fname;71if(strcmp(fName, sMlibFnsStatic[i].fname) == 0) {72mptr[i].fptr = sMlibFnsStatic[i].fptr;73} else {74ret = MLIB_FAILURE;75}76i++;77}7879return ret;80}81#else82/*83* This is called by awt_ImagingLib.initLib()84*/85mlib_status awt_getImagingLib(JNIEnv *env, mlibFnS_t *sMlibFns,86mlibSysFnS_t *sMlibSysFns) {87int status;88jstring jstr = NULL;89mlibFnS_t *mptr;90void *(*vPtr)();91int (*intPtr)();92mlib_status (*fPtr)();93int i;94void *handle = NULL;95mlibSysFnS_t tempSysFns;96static int s_timeIt = 0;97static int s_verbose = 1;98mlib_status ret = MLIB_SUCCESS;99struct utsname name;100101handle = dlopen(JNI_LIB_NAME("mlib_image"), RTLD_LAZY);102103if (handle == NULL) {104if (s_timeIt || s_verbose) {105printf ("error in dlopen: %s", dlerror());106}107return MLIB_FAILURE;108}109110/* So, if we are here, then either vis or generic version of111* medialib library was sucessfuly loaded.112* Let's try to initialize handlers...113*/114if ((tempSysFns.createFP = (MlibCreateFP_t)dlsym(handle,115"j2d_mlib_ImageCreate")) == NULL) {116if (s_timeIt) {117printf ("error in dlsym: %s", dlerror());118}119ret = MLIB_FAILURE;120}121122if (ret == MLIB_SUCCESS) {123if ((tempSysFns.createStructFP = (MlibCreateStructFP_t)dlsym(handle,124"j2d_mlib_ImageCreateStruct")) == NULL) {125if (s_timeIt) {126printf ("error in dlsym: %s", dlerror());127}128ret = MLIB_FAILURE;129}130}131132if (ret == MLIB_SUCCESS) {133if ((tempSysFns.deleteImageFP = (MlibDeleteFP_t)dlsym(handle,134"j2d_mlib_ImageDelete")) == NULL) {135if (s_timeIt) {136printf ("error in dlsym: %s", dlerror());137}138ret = MLIB_FAILURE;139}140}141142/* Set the system functions */143if (ret == MLIB_SUCCESS) {144*sMlibSysFns = tempSysFns;145}146147/* Loop through all of the fns and load them from the next library */148mptr = sMlibFns;149i = 0;150while ((ret == MLIB_SUCCESS) && (mptr[i].fname != NULL)) {151fPtr = (mlib_status (*)())dlsym(handle, mptr[i].fname);152if (fPtr != NULL) {153mptr[i].fptr = fPtr;154} else {155ret = MLIB_FAILURE;156}157i++;158}159if (ret != MLIB_SUCCESS) {160dlclose(handle);161}162return ret;163}164#endif165166mlib_start_timer awt_setMlibStartTimer() {167return start_timer;168}169170mlib_stop_timer awt_setMlibStopTimer() {171return stop_timer;172}173174/***************************************************************************175* Static Functions *176***************************************************************************/177178static void start_timer(int numsec)179{180struct itimerval interval;181182interval.it_interval.tv_sec = numsec;183interval.it_interval.tv_usec = 0;184interval.it_value.tv_sec = numsec;185interval.it_value.tv_usec = 0;186setitimer(ITIMER_REAL, &interval, 0);187}188189190static void stop_timer(int numsec, int ntimes)191{192struct itimerval interval;193double sec;194195getitimer(ITIMER_REAL, &interval);196sec = (((double) (numsec - 1)) - (double) interval.it_value.tv_sec) +197(1000000.0 - interval.it_value.tv_usec)/1000000.0;198sec = sec/((double) ntimes);199printf("%f msec per update\n", sec * 1000.0);200interval.it_interval.tv_sec = 0;201interval.it_interval.tv_usec = 0;202interval.it_value.tv_sec = 0;203interval.it_value.tv_usec = 0;204setitimer(ITIMER_PROF, &interval, 0);205}206207208