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 HEADLESS
  27 
  28 #include <stdlib.h>
  29 #include <string.h>
  30 
  31 #include "sun_java2d_SunGraphics2D.h"
  32 
  33 #include "jlong.h"
  34 #include "jni_util.h"
  35 #include "MTLContext.h"
  36 #include "MTLRenderQueue.h"
  37 #include "MTLSurfaceDataBase.h"
  38 #include "GraphicsPrimitiveMgr.h"
  39 #include "Region.h"
  40 
  41 #include "jvm.h"
  42 
  43 extern jboolean MTLSD_InitMTLWindow(JNIEnv *env, MTLSDOps *mtlsdo);
  44 extern MTLContext *MTLSD_MakeMTLContextCurrent(JNIEnv *env,
  45                                                MTLSDOps *srcOps,
  46                                                MTLSDOps *dstOps);
  47 
  48 /**
  49  * This table contains the standard blending rules (or Porter-Duff compositing
  50  * factors) used in glBlendFunc(), indexed by the rule constants from the
  51  * AlphaComposite class.
  52  */
  53 MTLBlendRule MTStdBlendRules[] = {
  54 };
  55 
  56 /** Evaluates to "front" or "back", depending on the value of buf. */
  57 //#define MTLC_ACTIVE_BUFFER_NAME(buf) \
  58 //    (buf == GL_FRONT || buf == GL_COLOR_ATTACHMENT0_EXT) ? "front" : "back"
  59 
  60 /**
  61  * Initializes the viewport and projection matrix, effectively positioning
  62  * the origin at the top-left corner of the surface.  This allows Java 2D
  63  * coordinates to be passed directly to OpenGL, which is typically based on
  64  * a bottom-right coordinate system.  This method also sets the appropriate
  65  * read and draw buffers.
  66  */
  67 static void
  68 MTLContext_SetViewport(BMTLSDOps *srcOps,BMTLSDOps *dstOps)
  69 {
  70     //TODO
  71     J2dTraceNotImplPrimitive("MTLContext_SetViewport");
  72     jint width = dstOps->width;
  73     jint height = dstOps->height;
  74 }
  75 
  76 /**
  77  * Initializes the alpha channel of the current surface so that it contains
  78  * fully opaque alpha values.
  79  */
  80 static void
  81 MTLContext_InitAlphaChannel()
  82 {
  83     //TODO
  84     J2dTraceNotImplPrimitive("MTLContext_InitAlphaChannel");
  85     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_InitAlphaChannel");
  86 }
  87 
  88 /**
  89  * Fetches the MTLContext associated with the given destination surface,
  90  * makes the context current for those surfaces, updates the destination
  91  * viewport, and then returns a pointer to the MTLContext.
  92  */
  93 MTLContext *
  94 MTLContext_SetSurfaces(JNIEnv *env, jlong pSrc, jlong pDst)
  95 {
  96     BMTLSDOps *srcOps = (BMTLSDOps *)jlong_to_ptr(pSrc);
  97     BMTLSDOps *dstOps = (BMTLSDOps *)jlong_to_ptr(pDst);
  98     MTLContext *mtlc = NULL;
  99 
 100     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_SetSurfaces");
 101 
 102     if (srcOps == NULL || dstOps == NULL) {
 103         J2dRlsTraceLn(J2D_TRACE_ERROR,
 104                       "MTLContext_SetSurfaces: ops are null");
 105         return NULL;
 106     }
 107 
 108     J2dTraceLn2(J2D_TRACE_VERBOSE, "  srctype=%d dsttype=%d",
 109                 srcOps->drawableType, dstOps->drawableType);
 110 
 111     if (dstOps->drawableType == MTLSD_TEXTURE) {
 112         J2dRlsTraceLn(J2D_TRACE_ERROR,
 113                       "MTLContext_SetSurfaces: texture cannot be used as destination");
 114         return NULL;
 115     }
 116 
 117     if (dstOps->drawableType == MTLSD_UNDEFINED) {
 118         // initialize the surface as an OGLSD_WINDOW
 119         if (!MTLSD_InitMTLWindow(env, dstOps)) {
 120             J2dRlsTraceLn(J2D_TRACE_ERROR,
 121                           "MTLContext_SetSurfaces: could not init OGL window");
 122             return NULL;
 123         }
 124     }
 125 
 126     // make the context current
 127     mtlc = MTLSD_MakeMTLContextCurrent(env, srcOps, dstOps);
 128     if (mtlc == NULL) {
 129         J2dRlsTraceLn(J2D_TRACE_ERROR,
 130                       "MTLContext_SetSurfaces: could not make context current");
 131         return NULL;
 132     }
 133 
 134     // update the viewport
 135     MTLContext_SetViewport(srcOps, dstOps);
 136 
 137     // perform additional one-time initialization, if necessary
 138     if (dstOps->needsInit) {
 139         if (dstOps->isOpaque) {
 140             // in this case we are treating the destination as opaque, but
 141             // to do so, first we need to ensure that the alpha channel
 142             // is filled with fully opaque values (see 6319663)
 143             MTLContext_InitAlphaChannel();
 144         }
 145         dstOps->needsInit = JNI_FALSE;
 146     }
 147 
 148     return mtlc;
 149 }
 150 
 151 /**
 152  * Resets the current clip state (disables both scissor and depth tests).
 153  */
 154 void
 155 MTLContext_ResetClip(MTLContext *mtlc)
 156 {
 157     //TODO
 158     J2dTraceNotImplPrimitive("MTLContext_ResetClip");
 159     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_ResetClip");
 160 }
 161 
 162 /**
 163  * Sets the Metal scissor bounds to the provided rectangular clip bounds.
 164  */
 165 void
 166 MTLContext_SetRectClip(MTLContext *mtlc, BMTLSDOps *dstOps,
 167                        jint x1, jint y1, jint x2, jint y2)
 168 {
 169     //TODO
 170     J2dTraceNotImplPrimitive("MTLContext_SetRectClip");
 171     jint width = x2 - x1;
 172     jint height = y2 - y1;
 173 
 174     J2dTraceLn4(J2D_TRACE_INFO,
 175                 "MTLContext_SetRectClip: x=%d y=%d w=%d h=%d",
 176                 x1, y1, width, height);
 177 }
 178 
 179 /**
 180  * Sets up a complex (shape) clip using the OpenGL depth buffer.  This
 181  * method prepares the depth buffer so that the clip Region spans can
 182  * be "rendered" into it.  The depth buffer is first cleared, then the
 183  * depth func is setup so that when we render the clip spans,
 184  * nothing is rendered into the color buffer, but for each pixel that would
 185  * be rendered, a non-zero value is placed into that location in the depth
 186  * buffer.  With depth test enabled, pixels will only be rendered into the
 187  * color buffer if the corresponding value at that (x,y) location in the
 188  * depth buffer differs from the incoming depth value.
 189  */
 190 void
 191 MTLContext_BeginShapeClip(MTLContext *mtlc)
 192 {
 193     //TODO
 194     J2dTraceNotImplPrimitive("MTLContext_BeginShapeClip");
 195     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_BeginShapeClip");
 196 }
 197 
 198 /**
 199  * Finishes setting up the shape clip by resetting the depth func
 200  * so that future rendering operations will once again be written into the
 201  * color buffer (while respecting the clip set up in the depth buffer).
 202  */
 203 void
 204 MTLContext_EndShapeClip(MTLContext *mtlc, BMTLSDOps *dstOps)
 205 {
 206     //TODO
 207     J2dTraceNotImplPrimitive("MTLContext_EndShapeClip");
 208     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_EndShapeClip");
 209 }
 210 
 211 /**
 212  * Initializes the OpenGL state responsible for applying extra alpha.  This
 213  * step is only necessary for any operation that uses glDrawPixels() or
 214  * glCopyPixels() with a non-1.0f extra alpha value.  Since the source is
 215  * always premultiplied, we apply the extra alpha value to both alpha and
 216  * color components using GL_*_SCALE.
 217  */
 218 void
 219 MTLContext_SetExtraAlpha(jfloat ea)
 220 {
 221     //TODO
 222     J2dTraceNotImplPrimitive("MTLContext_SetExtraAlpha");
 223     J2dTraceLn1(J2D_TRACE_INFO, "MTLContext_SetExtraAlpha: ea=%f", ea);
 224 }
 225 
 226 /**
 227  * Resets all OpenGL compositing state (disables blending and logic
 228  * operations).
 229  */
 230 void
 231 MTLContext_ResetComposite(MTLContext *mtlc)
 232 {
 233     //TODO
 234     J2dTraceNotImplPrimitive("MTLContext_ResetComposite");
 235     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_ResetComposite");
 236 }
 237 
 238 /**
 239  * Initializes the OpenGL blending state.  XOR mode is disabled and the
 240  * appropriate blend functions are setup based on the AlphaComposite rule
 241  * constant.
 242  */
 243 void
 244 MTLContext_SetAlphaComposite(MTLContext *mtlc,
 245                              jint rule, jfloat extraAlpha, jint flags)
 246 {
 247     //TODO
 248     J2dTraceNotImplPrimitive("MTLContext_SetAlphaComposite");
 249     J2dTraceLn1(J2D_TRACE_INFO,
 250                 "MTLContext_SetAlphaComposite: flags=%d", flags);
 251 }
 252 
 253 /**
 254  * Initializes the OpenGL logic op state to XOR mode.  Blending is disabled
 255  * before enabling logic op mode.  The XOR pixel value will be applied
 256  * later in the MTLContext_SetColor() method.
 257  */
 258 void
 259 MTLContext_SetXorComposite(MTLContext *mtlc, jint xorPixel)
 260 {
 261     //TODO
 262     J2dTraceNotImplPrimitive("MTLContext_SetXorComposite");
 263     J2dTraceLn1(J2D_TRACE_INFO,
 264                 "MTLContext_SetXorComposite: xorPixel=%08x", xorPixel);
 265 
 266 }
 267 
 268 /**
 269  * Resets the OpenGL transform state back to the identity matrix.
 270  */
 271 void
 272 MTLContext_ResetTransform(MTLContext *mtlc)
 273 {
 274     //TODO
 275     J2dTraceNotImplPrimitive("MTLContext_ResetTransform");
 276     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_ResetTransform");
 277 }
 278 
 279 /**
 280  * Initializes the OpenGL transform state by setting the modelview transform
 281  * using the given matrix parameters.
 282  *
 283  * REMIND: it may be worthwhile to add serial id to AffineTransform, so we
 284  *         could do a quick check to see if the xform has changed since
 285  *         last time... a simple object compare won't suffice...
 286  */
 287 void
 288 MTLContext_SetTransform(MTLContext *mtlc,
 289                         jdouble m00, jdouble m10,
 290                         jdouble m01, jdouble m11,
 291                         jdouble m02, jdouble m12)
 292 {
 293     //TODO
 294     J2dTraceNotImplPrimitive("MTLContext_SetTransform");
 295     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_SetTransform");
 296 }
 297 
 298 /**
 299  * Creates a 2D texture of the given format and dimensions and returns the
 300  * texture object identifier.  This method is typically used to create a
 301  * temporary texture for intermediate work, such as in the
 302  * MTLContext_InitBlitTileTexture() method below.
 303  */
 304 jint
 305 MTLContext_CreateBlitTexture(jint internalFormat, jint pixelFormat,
 306                              jint width, jint height)
 307 {
 308     //TODO
 309     J2dTraceNotImplPrimitive("MTLContext_CreateBlitTexture");
 310     return 0;
 311 }
 312 
 313 /**
 314  * Initializes a small texture tile for use with tiled blit operations (see
 315  * MTLBlitLoops.c and MTLMaskBlit.c for usage examples).  The texture ID for
 316  * the tile is stored in the given MTLContext.  The tile is initially filled
 317  * with garbage values, but the tile is updated as needed (via
 318  * glTexSubImage2D()) with real RGBA values used in tiled blit situations.
 319  * The internal format for the texture is GL_RGBA8, which should be sufficient
 320  * for storing system memory surfaces of any known format (see PixelFormats
 321  * for a list of compatible surface formats).
 322  */
 323 jboolean
 324 MTLContext_InitBlitTileTexture(MTLContext *mtlc)
 325 {
 326     //TODO
 327     J2dTraceNotImplPrimitive("MTLContext_InitBlitTileTexture");
 328     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_InitBlitTileTexture");
 329 
 330     return JNI_TRUE;
 331 }
 332 
 333 /**
 334  * Destroys the OpenGL resources associated with the given MTLContext.
 335  * It is required that the native context associated with the MTLContext
 336  * be made current prior to calling this method.
 337  */
 338 void
 339 MTLContext_DestroyContextResources(MTLContext *mtlc)
 340 {
 341     //TODO
 342     J2dTraceNotImplPrimitive("MTLContext_DestroyContextResources");
 343     J2dTraceLn(J2D_TRACE_INFO, "MTLContext_DestroyContextResources");
 344 
 345     if (mtlc->xformMatrix != NULL) {
 346         free(mtlc->xformMatrix);
 347     }
 348 
 349     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 350     [mtlc->mtlDevice release];
 351     mtlc->mtlDevice = nil;
 352 
 353     [pool drain];
 354     //if (mtlc->blitTextureID != 0) {
 355     //}
 356 }
 357 
 358 /*
 359  * Class:     sun_java2d_metal_MTLContext
 360  * Method:    getMTLIdString
 361  * Signature: ()Ljava/lang/String;
 362  */
 363 JNIEXPORT jstring JNICALL Java_sun_java2d_metal_MTLContext_getMTLIdString
 364   (JNIEnv *env, jclass mtlcc)
 365 {
 366     char *vendor, *renderer, *version;
 367     char *pAdapterId;
 368     jobject ret = NULL;
 369     int len;
 370 
 371 
 372     return NULL;
 373 }
 374 
 375 #endif /* !HEADLESS */