1 /*
   2  * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <string.h>
  28 #include <sys/time.h>
  29 #include <sys/utsname.h>
  30 #include <sys/types.h>
  31 #include <errno.h>
  32 #include <dlfcn.h>
  33 #include "jni.h"
  34 #include <jni_util.h>
  35 #include "jvm_md.h"
  36 #include "awt_Mlib.h"
  37 #include "java_awt_image_BufferedImage.h"
  38 
  39 static void start_timer(int numsec);
  40 static void stop_timer(int numsec, int ntimes);
  41 
  42 /*
  43  * This is called by awt_ImagingLib.initLib() to figure out if we
  44  * can use the VIS version of medialib
  45  */
  46 mlib_status awt_getImagingLib(JNIEnv *env, mlibFnS_t *sMlibFns,
  47                               mlibSysFnS_t *sMlibSysFns) {
  48     int status;
  49     jstring jstr = NULL;
  50     mlibFnS_t *mptr;
  51     void *(*vPtr)();
  52     int (*intPtr)();
  53     mlib_status (*fPtr)();
  54     int i;
  55     void *handle = NULL;
  56     mlibSysFnS_t tempSysFns;
  57     static int s_timeIt = 0;
  58     static int s_verbose = 1;
  59     mlib_status ret = MLIB_SUCCESS;
  60     struct utsname name;
  61 
  62     /*
  63      * Find out the machine name. If it is an SUN ultra, we
  64      * can use the vis library
  65      */
  66     if ((uname(&name) >= 0) && (getenv("NO_VIS") == NULL) &&
  67         (strncmp(name.machine, "sun4u" , 5) == 0) ||
  68         ((strncmp(name.machine, "sun4v" , 5) == 0) &&
  69          (getenv("USE_VIS_ON_SUN4V") != NULL)))
  70     {
  71         handle = dlopen(JNI_LIB_NAME("mlib_image_v"), RTLD_LAZY);
  72     }
  73 
  74     if (handle == NULL) {
  75         handle = dlopen(JNI_LIB_NAME("mlib_image"), RTLD_LAZY);
  76     }
  77 
  78     if (handle == NULL) {
  79         if (s_timeIt || s_verbose) {
  80             printf ("error in dlopen: %s", dlerror());
  81         }
  82         return MLIB_FAILURE;
  83     }
  84 
  85     /* So, if we are here, then either vis or generic version of
  86      * medialib library was sucessfuly loaded.
  87      * Let's try to initialize handlers...
  88      */
  89     if ((tempSysFns.createFP = (MlibCreateFP_t)dlsym(handle,
  90                                        "j2d_mlib_ImageCreate")) == NULL) {
  91         if (s_timeIt) {
  92             printf ("error in dlsym: %s", dlerror());
  93         }
  94         ret = MLIB_FAILURE;
  95     }
  96 
  97     if (ret == MLIB_SUCCESS) {
  98         if ((tempSysFns.createStructFP = (MlibCreateStructFP_t)dlsym(handle,
  99                                           "j2d_mlib_ImageCreateStruct")) == NULL) {
 100             if (s_timeIt) {
 101                 printf ("error in dlsym: %s", dlerror());
 102             }
 103             ret = MLIB_FAILURE;
 104         }
 105     }
 106 
 107     if (ret == MLIB_SUCCESS) {
 108         if ((tempSysFns.deleteImageFP = (MlibDeleteFP_t)dlsym(handle,
 109                                                  "j2d_mlib_ImageDelete")) == NULL) {
 110             if (s_timeIt) {
 111                 printf ("error in dlsym: %s", dlerror());
 112             }
 113             ret = MLIB_FAILURE;
 114         }
 115     }
 116 
 117     /* Set the system functions */
 118     if (ret == MLIB_SUCCESS) {
 119         *sMlibSysFns = tempSysFns;
 120     }
 121 
 122     /* Loop through all of the fns and load them from the next library */
 123     mptr = sMlibFns;
 124     i = 0;
 125     while ((ret == MLIB_SUCCESS) && (mptr[i].fname != NULL)) {
 126         fPtr = (mlib_status (*)())dlsym(handle, mptr[i].fname);
 127         if (fPtr != NULL) {
 128             mptr[i].fptr = fPtr;
 129         } else {
 130             ret = MLIB_FAILURE;
 131         }
 132         i++;
 133     }
 134     if (ret != MLIB_SUCCESS) {
 135         dlclose(handle);
 136     }
 137     return ret;
 138 }
 139 
 140 mlib_start_timer awt_setMlibStartTimer() {
 141     return start_timer;
 142 }
 143 
 144 mlib_stop_timer awt_setMlibStopTimer() {
 145     return stop_timer;
 146 }
 147 
 148 void awt_getBIColorOrder(int type, int *colorOrder) {
 149     switch(type) {
 150     case java_awt_image_BufferedImage_TYPE_INT_ARGB:
 151     case java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE:
 152         colorOrder[0] = 1;
 153         colorOrder[1] = 2;
 154         colorOrder[2] = 3;
 155         colorOrder[3] = 0;
 156         break;
 157     case java_awt_image_BufferedImage_TYPE_INT_BGR:
 158         colorOrder[0] = 2;
 159         colorOrder[1] = 1;
 160         colorOrder[2] = 0;
 161         break;
 162     case java_awt_image_BufferedImage_TYPE_4BYTE_ABGR:
 163     case java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE:
 164         colorOrder[0] = 3;
 165         colorOrder[1] = 2;
 166         colorOrder[2] = 1;
 167         colorOrder[3] = 0;
 168         break;
 169     case java_awt_image_BufferedImage_TYPE_3BYTE_BGR:
 170         colorOrder[0] = 2;
 171         colorOrder[1] = 1;
 172         colorOrder[2] = 0;
 173         break;
 174     case java_awt_image_BufferedImage_TYPE_INT_RGB:
 175         colorOrder[0] = 1;
 176         colorOrder[1] = 2;
 177         colorOrder[2] = 3;
 178         break;
 179     case java_awt_image_BufferedImage_TYPE_USHORT_565_RGB:
 180     case java_awt_image_BufferedImage_TYPE_USHORT_555_RGB:
 181         colorOrder[0] = 0;
 182         colorOrder[1] = 1;
 183         colorOrder[2] = 2;
 184         break;
 185     case java_awt_image_BufferedImage_TYPE_BYTE_GRAY:
 186     case java_awt_image_BufferedImage_TYPE_USHORT_GRAY:
 187     case java_awt_image_BufferedImage_TYPE_BYTE_BINARY:
 188     case java_awt_image_BufferedImage_TYPE_BYTE_INDEXED:
 189         colorOrder[0] = 0;
 190         break;
 191     }
 192 }
 193 
 194 /***************************************************************************
 195  *                          Static Functions                               *
 196  ***************************************************************************/
 197 
 198 static void start_timer(int numsec)
 199 {
 200     struct itimerval interval;
 201 
 202     interval.it_interval.tv_sec = numsec;
 203     interval.it_interval.tv_usec = 0;
 204     interval.it_value.tv_sec = numsec;
 205     interval.it_value.tv_usec = 0;
 206     setitimer(ITIMER_REAL, &interval, 0);
 207 }
 208 
 209 
 210 static void stop_timer(int numsec, int ntimes)
 211 {
 212     struct itimerval interval;
 213     double sec;
 214 
 215     getitimer(ITIMER_REAL, &interval);
 216     sec = (((double) (numsec - 1)) - (double) interval.it_value.tv_sec) +
 217             (1000000.0 - interval.it_value.tv_usec)/1000000.0;
 218     sec = sec/((double) ntimes);
 219     printf("%f msec per update\n", sec * 1000.0);
 220     interval.it_interval.tv_sec = 0;
 221     interval.it_interval.tv_usec = 0;
 222     interval.it_value.tv_sec = 0;
 223     interval.it_value.tv_usec = 0;
 224     setitimer(ITIMER_PROF, &interval, 0);
 225 }