1 /*
   2  * Copyright (c) 1999, 2015, 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 #ifndef _JAVASOFT_JAWT_H_
  27 #define _JAVASOFT_JAWT_H_
  28 
  29 #include "jni.h"
  30 #include "jni_util.h"
  31 
  32 #ifdef __cplusplus
  33 extern "C" {
  34 #endif
  35 
  36 /*
  37  * AWT native interface (new in JDK 1.3)
  38  *
  39  * The AWT native interface allows a native C or C++ application a means
  40  * by which to access native structures in AWT.  This is to facilitate moving
  41  * legacy C and C++ applications to Java and to target the needs of the
  42  * community who, at present, wish to do their own native rendering to canvases
  43  * for performance reasons.  Standard extensions such as Java3D also require a
  44  * means to access the underlying native data structures of AWT.
  45  *
  46  * There may be future extensions to this API depending on demand.
  47  *
  48  * A VM does not have to implement this API in order to pass the JCK.
  49  * It is recommended, however, that this API is implemented on VMs that support
  50  * standard extensions, such as Java3D.
  51  *
  52  * Since this is a native API, any program which uses it cannot be considered
  53  * 100% pure java.
  54  */
  55 
  56 /*
  57  * AWT Native Drawing Surface (JAWT_DrawingSurface).
  58  *
  59  * For each platform, there is a native drawing surface structure.  This
  60  * platform-specific structure can be found in jawt_md.h.  It is recommended
  61  * that additional platforms follow the same model.  It is also recommended
  62  * that VMs on Win32 and Solaris support the existing structures in jawt_md.h.
  63  *
  64  *******************
  65  * EXAMPLE OF USAGE:
  66  *******************
  67  *
  68  * In Win32, a programmer wishes to access the HWND of a canvas to perform
  69  * native rendering into it.  The programmer has declared the paint() method
  70  * for their canvas subclass to be native:
  71  *
  72  *
  73  * MyCanvas.java:
  74  *
  75  * import java.awt.*;
  76  *
  77  * public class MyCanvas extends Canvas {
  78  *
  79  *     static {
  80  *         System.loadLibrary("mylib");
  81  *     }
  82  *
  83  *     public native void paint(Graphics g);
  84  * }
  85  *
  86  *
  87  * myfile.c:
  88  *
  89  * #include "jawt_md.h"
  90  * #include <assert.h>
  91  *
  92  * JNIEXPORT void JNICALL
  93  * Java_MyCanvas_paint(JNIEnv* env, jobject canvas, jobject graphics)
  94  * {
  95  *     JAWT awt;
  96  *     JAWT_DrawingSurface* ds;
  97  *     JAWT_DrawingSurfaceInfo* dsi;
  98  *     JAWT_Win32DrawingSurfaceInfo* dsi_win;
  99  *     jboolean result;
 100  *     jint lock;
 101  *
 102  *     // Get the AWT
 103  *     awt.version = JAWT_VERSION_1_3;
 104  *     result = JAWT_GetAWT(env, &awt);
 105  *     assert(result != JNI_FALSE);
 106  *
 107  *     // Get the drawing surface
 108  *     ds = awt.GetDrawingSurface(env, canvas);
 109  *     assert(ds != NULL);
 110  *
 111  *     // Lock the drawing surface
 112  *     lock = ds->Lock(ds);
 113  *     assert((lock & JAWT_LOCK_ERROR) == 0);
 114  *
 115  *     // Get the drawing surface info
 116  *     dsi = ds->GetDrawingSurfaceInfo(ds);
 117  *
 118  *     // Get the platform-specific drawing info
 119  *     dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
 120  *
 121  *     //////////////////////////////
 122  *     // !!! DO PAINTING HERE !!! //
 123  *     //////////////////////////////
 124  *
 125  *     // Free the drawing surface info
 126  *     ds->FreeDrawingSurfaceInfo(dsi);
 127  *
 128  *     // Unlock the drawing surface
 129  *     ds->Unlock(ds);
 130  *
 131  *     // Free the drawing surface
 132  *     awt.FreeDrawingSurface(ds);
 133  * }
 134  *
 135  */
 136 
 137 /*
 138  * JAWT_Rectangle
 139  * Structure for a native rectangle.
 140  */
 141 typedef struct jawt_Rectangle {
 142     jint x;
 143     jint y;
 144     jint width;
 145     jint height;
 146 } JAWT_Rectangle;
 147 
 148 struct jawt_DrawingSurface;
 149 
 150 /*
 151  * JAWT_DrawingSurfaceInfo
 152  * Structure for containing the underlying drawing information of a component.
 153  */
 154 typedef struct jawt_DrawingSurfaceInfo {
 155     /*
 156      * Pointer to the platform-specific information.  This can be safely
 157      * cast to a JAWT_Win32DrawingSurfaceInfo on Windows or a
 158      * JAWT_X11DrawingSurfaceInfo on Solaris. On Mac OS X this is a
 159      * pointer to a NSObject that conforms to the JAWT_SurfaceLayers
 160      * protocol. See jawt_md.h for details.
 161      */
 162     void* platformInfo;
 163     /* Cached pointer to the underlying drawing surface */
 164     struct jawt_DrawingSurface* ds;
 165     /* Bounding rectangle of the drawing surface */
 166     JAWT_Rectangle bounds;
 167     /* Number of rectangles in the clip */
 168     jint clipSize;
 169     /* Clip rectangle array */
 170     JAWT_Rectangle* clip;
 171 } JAWT_DrawingSurfaceInfo;
 172 
 173 #define JAWT_LOCK_ERROR                 0x00000001
 174 #define JAWT_LOCK_CLIP_CHANGED          0x00000002
 175 #define JAWT_LOCK_BOUNDS_CHANGED        0x00000004
 176 #define JAWT_LOCK_SURFACE_CHANGED       0x00000008
 177 
 178 /*
 179  * JAWT_DrawingSurface
 180  * Structure for containing the underlying drawing information of a component.
 181  * All operations on a JAWT_DrawingSurface MUST be performed from the same
 182  * thread as the call to GetDrawingSurface.
 183  */
 184 typedef struct jawt_DrawingSurface {
 185     /*
 186      * Cached reference to the Java environment of the calling thread.
 187      * If Lock(), Unlock(), GetDrawingSurfaceInfo() or
 188      * FreeDrawingSurfaceInfo() are called from a different thread,
 189      * this data member should be set before calling those functions.
 190      */
 191     JNIEnv* env;
 192     /* Cached reference to the target object */
 193     jobject target;
 194     /*
 195      * Lock the surface of the target component for native rendering.
 196      * When finished drawing, the surface must be unlocked with
 197      * Unlock().  This function returns a bitmask with one or more of the
 198      * following values:
 199      *
 200      * JAWT_LOCK_ERROR - When an error has occurred and the surface could not
 201      * be locked.
 202      *
 203      * JAWT_LOCK_CLIP_CHANGED - When the clip region has changed.
 204      *
 205      * JAWT_LOCK_BOUNDS_CHANGED - When the bounds of the surface have changed.
 206      *
 207      * JAWT_LOCK_SURFACE_CHANGED - When the surface itself has changed
 208      */
 209     jint (JNICALL *Lock)
 210         (struct jawt_DrawingSurface* ds);
 211     /*
 212      * Get the drawing surface info.
 213      * The value returned may be cached, but the values may change if
 214      * additional calls to Lock() or Unlock() are made.
 215      * Lock() must be called before this can return a valid value.
 216      * Returns NULL if an error has occurred.
 217      * When finished with the returned value, FreeDrawingSurfaceInfo must be
 218      * called.
 219      */
 220     JAWT_DrawingSurfaceInfo* (JNICALL *GetDrawingSurfaceInfo)
 221         (struct jawt_DrawingSurface* ds);
 222     /*
 223      * Free the drawing surface info.
 224      */
 225     void (JNICALL *FreeDrawingSurfaceInfo)
 226         (JAWT_DrawingSurfaceInfo* dsi);
 227     /*
 228      * Unlock the drawing surface of the target component for native rendering.
 229      */
 230     void (JNICALL *Unlock)
 231         (struct jawt_DrawingSurface* ds);
 232 } JAWT_DrawingSurface;
 233 
 234 /*
 235  * JAWT
 236  * Structure for containing native AWT functions.
 237  */
 238 typedef struct jawt {
 239     /*
 240      * Version of this structure.  This must always be set before
 241      * calling JAWT_GetAWT()
 242      */
 243     jint version;
 244     /*
 245      * Return a drawing surface from a target jobject.  This value
 246      * may be cached.
 247      * Returns NULL if an error has occurred.
 248      * Target must be a java.awt.Component (should be a Canvas
 249      * or Window for native rendering).
 250      * FreeDrawingSurface() must be called when finished with the
 251      * returned JAWT_DrawingSurface.
 252      */
 253     JAWT_DrawingSurface* (JNICALL *GetDrawingSurface)
 254         (JNIEnv* env, jobject target);
 255     /*
 256      * Free the drawing surface allocated in GetDrawingSurface.
 257      */
 258     void (JNICALL *FreeDrawingSurface)
 259         (JAWT_DrawingSurface* ds);
 260     /*
 261      * Since 1.4
 262      * Locks the entire AWT for synchronization purposes
 263      */
 264     void (JNICALL *Lock)(JNIEnv* env);
 265     /*
 266      * Since 1.4
 267      * Unlocks the entire AWT for synchronization purposes
 268      */
 269     void (JNICALL *Unlock)(JNIEnv* env);
 270     /*
 271      * Since 1.4
 272      * Returns a reference to a java.awt.Component from a native
 273      * platform handle.  On Windows, this corresponds to an HWND;
 274      * on Solaris and Linux, this is a Drawable.  For other platforms,
 275      * see the appropriate machine-dependent header file for a description.
 276      * The reference returned by this function is a local
 277      * reference that is only valid in this environment.
 278      * This function returns a NULL reference if no component could be
 279      * found with matching platform information.
 280      */
 281     jobject (JNICALL *GetComponent)(JNIEnv* env, void* platformInfo);
 282 
 283 } JAWT;
 284 
 285 /*
 286  * Get the AWT native structure.  This function returns JNI_FALSE if
 287  * an error occurs.
 288  */
 289 _JNI_IMPORT_OR_EXPORT_
 290 jboolean JNICALL JAWT_GetAWT(JNIEnv* env, JAWT* awt);
 291 
 292 #define JAWT_VERSION_1_3 0x00010003
 293 #define JAWT_VERSION_1_4 0x00010004
 294 #define JAWT_VERSION_1_7 0x00010007
 295 
 296 #ifdef __cplusplus
 297 } /* extern "C" */
 298 #endif
 299 
 300 #endif /* !_JAVASOFT_JAWT_H_ */