1 /*
   2  * Copyright (c) 2019, 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 MTLSurfaceDataBase_h_Included
  27 #define MTLSurfaceDataBase_h_Included
  28 
  29 #include "java_awt_image_AffineTransformOp.h"
  30 #include "sun_java2d_metal_MTLSurfaceData.h"
  31 #include "sun_java2d_pipe_hw_AccelSurface.h"
  32 
  33 #include "SurfaceData.h"
  34 #include "Trace.h"
  35 #include "MTLFuncs.h"
  36 
  37 typedef struct _BMTLSDOps BMTLSDOps;
  38 
  39 /**
  40  * The MTLPixelFormat structure contains all the information OpenGL needs to
  41  * know when copying from or into a particular system memory image buffer (via
  42  * glDrawPixels(), glReadPixels, glTexSubImage2D(), etc).
  43  *
  44  *     GLenum format;
  45  * The pixel format parameter used in glDrawPixels() and other similar calls.
  46  * Indicates the component ordering for each pixel (e.g. GL_BGRA).
  47  *
  48  *     GLenum type;
  49  * The pixel data type parameter used in glDrawPixels() and other similar
  50  * calls.  Indicates the data type for an entire pixel or for each component
  51  * in a pixel (e.g. GL_UNSIGNED_BYTE with GL_BGR means a pixel consists of
  52  * 3 unsigned byte components, blue first, then green, then red;
  53  * GL_UNSIGNED_INT_8_8_8_8_REV with GL_BGRA means a pixel consists of 1
  54  * unsigned integer comprised of four byte components, alpha first, then red,
  55  * then green, then blue).
  56  *
  57  *     jint alignment;
  58  * The byte alignment parameter used in glPixelStorei(GL_UNPACK_ALIGNMENT).  A
  59  * value of 4 indicates that each pixel starts on a 4-byte aligned region in
  60  * memory, and so on.  This alignment parameter helps OpenGL speed up pixel
  61  * transfer operations by transferring memory in aligned blocks.
  62  *
  63  *     jboolean hasAlpha;
  64  * If true, indicates that this pixel format contains an alpha component.
  65  *
  66  *     jboolean isPremult;
  67  * If true, indicates that this pixel format contains color components that
  68  * have been pre-multiplied by their corresponding alpha component.
  69  */
  70 typedef struct {
  71     //GLenum   format;
  72     //GLenum   type;
  73     jint format;
  74     jint type;
  75     jint     alignment;
  76     jboolean hasAlpha;
  77     jboolean isPremult;
  78 } MTPixelFormat;
  79 
  80 /**
  81  * The MTLSDOps structure describes a native OpenGL surface and contains all
  82  * information pertaining to the native surface.  Some information about
  83  * the more important/different fields:
  84  *
  85  *     void *privOps;
  86  * Pointer to native-specific (GLX, WGL, etc.) SurfaceData info, such as the
  87  * native Drawable handle and GraphicsConfig data.
  88  *
  89  *     jint drawableType;
  90  * The surface type; can be any one of the surface type constants defined
  91  * below (MTLSD_WINDOW, MTLSD_TEXTURE, etc).
  92  *
  93  *     GLenum activeBuffer;
  94  * Can be either GL_FRONT if this is the front buffer surface of an onscreen
  95  * window or a pbuffer surface, or GL_BACK if this is the backbuffer surface
  96  * of an onscreen window.
  97  *
  98  *     jboolean isOpaque;
  99  * If true, the surface should be treated as being fully opaque.  If
 100  * the underlying surface (e.g. pbuffer) has an alpha channel and isOpaque
 101  * is true, then we should take appropriate action (i.e. call glColorMask()
 102  * to disable writes into the alpha channel) to ensure that the surface
 103  * remains fully opaque.
 104  *
 105  *     jboolean needsInit;
 106  * If true, the surface requires some one-time initialization, which should
 107  * be performed after a context has been made current to the surface for
 108  * the first time.
 109  *
 110  *     jint x/yOffset
 111  * The offset in pixels of the OpenGL viewport origin from the lower-left
 112  * corner of the heavyweight drawable.  For example, a top-level frame on
 113  * Windows XP has lower-left insets of (4,4).  The OpenGL viewport origin
 114  * would typically begin at the lower-left corner of the client region (inside
 115  * the frame decorations), but AWT/Swing will take the insets into account
 116  * when rendering into that window.  So in order to account for this, we
 117  * need to adjust the OpenGL viewport origin by an x/yOffset of (-4,-4).  On
 118  * X11, top-level frames typically don't have this insets issue, so their
 119  * x/yOffset would be (0,0) (the same applies to pbuffers).
 120  *
 121  *     jint width/height;
 122  * The cached surface bounds.  For offscreen surface types (MTLSD_FBOBJECT,
 123  * MTLSD_TEXTURE, etc.) these values must remain constant.  Onscreen window
 124  * surfaces (MTLSD_WINDOW, MTLSD_FLIP_BACKBUFFER, etc.) may have their
 125  * bounds changed in response to a programmatic or user-initiated event, so
 126  * these values represent the last known dimensions.  To determine the true
 127  * current bounds of this surface, query the native Drawable through the
 128  * privOps field.
 129  *
 130  *     GLuint textureID;
 131  * The texture object handle, as generated by glGenTextures().  If this value
 132  * is zero, the texture has not yet been initialized.
 133  *
 134  *     jint textureWidth/Height;
 135  * The actual bounds of the texture object for this surface.  If the
 136  * GL_ARB_texture_non_power_of_two extension is not present, the dimensions
 137  * of an OpenGL texture object must be a power-of-two (e.g. 64x32 or 128x512).
 138  * The texture image that we care about has dimensions specified by the width
 139  * and height fields in this MTLSDOps structure.  For example, if the image
 140  * to be stored in the texture has dimensions 115x47, the actual OpenGL
 141  * texture we allocate will have dimensions 128x64 to meet the pow2
 142  * restriction.  The image bounds within the texture can be accessed using
 143  * floating point texture coordinates in the range [0.0,1.0].
 144  *
 145  *     GLenum textureTarget;
 146  * The texture target of the texture object for this surface.  If this
 147  * surface is not backed by a texture, this value is set to zero.  Otherwise,
 148  * this value is GL_TEXTURE_RECTANGLE_ARB when the GL_ARB_texture_rectangle
 149  * extension is in use; if not, it is set to GL_TEXTURE_2D.
 150  *
 151  *     GLint textureFilter;
 152  * The current filter state for this texture object (can be either GL_NEAREST
 153  * or GL_LINEAR).  We cache this value here and check it before updating
 154  * the filter state to avoid redundant calls to glTexParameteri() when the
 155  * filter state remains constant (see the MTLSD_UPDATE_TEXTURE_FILTER()
 156  * macro below).
 157  *
 158  *     GLuint fbobjectID, depthID;
 159  * The object handles for the framebuffer object and depth renderbuffer
 160  * associated with this surface.  These fields are only used when
 161  * drawableType is MTLSD_FBOBJECT, otherwise they are zero.
 162  */
 163 struct _BMTLSDOps {
 164     SurfaceDataOps               sdOps;
 165     void                         *privOps;
 166     jint                         drawableType;
 167     jint                       activeBuffer;
 168     jboolean                     isOpaque;
 169     jboolean                     needsInit;
 170     jint                         xOffset;
 171     jint                         yOffset;
 172     jint                         width;
 173     jint                         height;
 174    /* GLuint */ jint                      textureID;
 175     jint                         textureWidth;
 176     jint                         textureHeight;
 177    /* GLenum */ jint                      textureTarget;
 178    /* GLint  */ jint                      textureFilter;
 179    /* GLuint */ jint                      fbobjectID;
 180    /* GLuint  */ jint                     depthID;
 181 };
 182 
 183 /**
 184  * The following convenience macros are used when rendering rectangles (either
 185  * a single rectangle, or a whole series of them).  To render a single
 186  * rectangle, simply invoke the GLRECT() macro.  To render a whole series of
 187  * rectangles, such as spans in a complex shape, first invoke GLRECT_BEGIN(),
 188  * then invoke the appropriate inner loop macro (either XYXY or XYWH) for
 189  * each rectangle, and finally invoke GLRECT_END() to notify OpenGL that the
 190  * vertex list is complete.  Care should be taken to avoid calling OpenGL
 191  * commands (besides GLRECT_BODY_*()) inside the BEGIN/END pair.
 192  */
 193 
 194 #define GLRECT_BEGIN j2d_glBegin(GL_QUADS)
 195 
 196 #define GLRECT_BODY_XYXY(x1, y1, x2, y2) \
 197     do { \
 198         j2d_glVertex2i(x1, y1); \
 199         j2d_glVertex2i(x2, y1); \
 200         j2d_glVertex2i(x2, y2); \
 201         j2d_glVertex2i(x1, y2); \
 202     } while (0)
 203 
 204 #define GLRECT_BODY_XYWH(x, y, w, h) \
 205     GLRECT_BODY_XYXY(x, y, (x) + (w), (y) + (h))
 206 
 207 #define GLRECT_END j2d_glEnd()
 208 
 209 #define GLRECT(x, y, w, h) \
 210     do { \
 211         GLRECT_BEGIN; \
 212         GLRECT_BODY_XYWH(x, y, w, h); \
 213         GLRECT_END; \
 214     } while (0)
 215 
 216 /**
 217  * These are shorthand names for the surface type constants defined in
 218  * MTLSurfaceData.java.
 219  */
 220 #define MTLSD_UNDEFINED       sun_java2d_pipe_hw_AccelSurface_UNDEFINED
 221 #define MTLSD_WINDOW          sun_java2d_pipe_hw_AccelSurface_WINDOW
 222 #define MTLSD_TEXTURE         sun_java2d_pipe_hw_AccelSurface_TEXTURE
 223 #define MTLSD_FLIP_BACKBUFFER sun_java2d_pipe_hw_AccelSurface_FLIP_BACKBUFFER
 224 #define MTLSD_FBOBJECT        sun_java2d_pipe_hw_AccelSurface_RT_TEXTURE
 225 
 226 /**
 227  * These are shorthand names for the filtering method constants used by
 228  * image transform methods.
 229  */
 230 #define MTLSD_XFORM_DEFAULT 0
 231 #define MTLSD_XFORM_NEAREST_NEIGHBOR \
 232     java_awt_image_AffineTransformOp_TYPE_NEAREST_NEIGHBOR
 233 #define MTLSD_XFORM_BILINEAR \
 234     java_awt_image_AffineTransformOp_TYPE_BILINEAR
 235 
 236 /**
 237  * Helper macros that update the current texture filter state only when
 238  * it needs to be changed, which helps reduce overhead for small texturing
 239  * operations.  The filter state is set on a per-texture (not per-context)
 240  * basis; for example, it is possible for one texture to be using GL_NEAREST
 241  * while another texture uses GL_LINEAR under the same context.
 242  */
 243 #define MTLSD_INIT_TEXTURE_FILTER(mtlSDOps, filter)                          \
 244     do {                                                                     \
 245         j2d_glTexParameteri((mtlSDOps)->textureTarget,                       \
 246                             GL_TEXTURE_MAG_FILTER, (filter));                \
 247         j2d_glTexParameteri((mtlSDOps)->textureTarget,                       \
 248                             GL_TEXTURE_MIN_FILTER, (filter));                \
 249         (mtlSDOps)->textureFilter = (filter);                                \
 250     } while (0)
 251 
 252 #define MTLSD_UPDATE_TEXTURE_FILTER(mtlSDOps, filter)    \
 253     do {                                                 \
 254         if ((mtlSDOps)->textureFilter != (filter)) {     \
 255             MTLSD_INIT_TEXTURE_FILTER(mtlSDOps, filter); \
 256         }                                                \
 257     } while (0)
 258 
 259 /**
 260  * Convenience macros for setting the texture wrap mode for a given target.
 261  * The texture wrap mode should be reset to our default value of
 262  * GL_CLAMP_TO_EDGE by calling MTLSD_RESET_TEXTURE_WRAP() when a texture
 263  * is first created.  If another mode is needed (e.g. GL_REPEAT in the case
 264  * of TexturePaint acceleration), one can call the MTLSD_UPDATE_TEXTURE_WRAP()
 265  * macro to easily set up the new wrap mode.  However, it is important to
 266  * restore the wrap mode back to its default value (by calling the
 267  * MTLSD_RESET_TEXTURE_WRAP() macro) when the operation is finished.
 268  */
 269 #define MTLSD_UPDATE_TEXTURE_WRAP(target, wrap)                   \
 270     do {                                                          \
 271         j2d_glTexParameteri((target), GL_TEXTURE_WRAP_S, (wrap)); \
 272         j2d_glTexParameteri((target), GL_TEXTURE_WRAP_T, (wrap)); \
 273     } while (0)
 274 
 275 #define MTLSD_RESET_TEXTURE_WRAP(target) \
 276     MTLSD_UPDATE_TEXTURE_WRAP(target, GL_CLAMP_TO_EDGE)
 277 
 278 /**
 279  * Exported methods.
 280  */
 281 jint MTLSD_Lock(JNIEnv *env,
 282                 SurfaceDataOps *ops, SurfaceDataRasInfo *pRasInfo,
 283                 jint lockflags);
 284 void MTLSD_GetRasInfo(JNIEnv *env,
 285                       SurfaceDataOps *ops, SurfaceDataRasInfo *pRasInfo);
 286 void MTLSD_Unlock(JNIEnv *env,
 287                   SurfaceDataOps *ops, SurfaceDataRasInfo *pRasInfo);
 288 void MTLSD_Dispose(JNIEnv *env, SurfaceDataOps *ops);
 289 void MTLSD_Delete(JNIEnv *env, BMTLSDOps *mtlsdo);
 290 jint MTLSD_NextPowerOfTwo(jint val, jint max);
 291 
 292 /*jboolean MTLSD_InitFBObject(GLuint *fbobjectID, GLuint *depthID,
 293                             GLuint textureID, GLenum textureTarget,
 294                             jint textureWidth, jint textureHeight);*/
 295 
 296 #endif /* MTLSurfaceDataBase_h_Included */